home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Multimedia / xcd / xcd2file / xcd2file.cpp next >
C/C++ Source or Header  |  2002-07-21  |  2KB  |  76 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "../core/xcd_media_reader.h"
  4.  
  5. #define MAX_BUFFER 65536
  6. #define MAX_FILE_NAME   512
  7. #define DEFAULE_OUT_EXTENSION ".out"
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11.     XCD_Media_Reader* pXmr;
  12.     char outFileName[MAX_FILE_NAME];
  13.     FILE *outFile;
  14.     char buffer[MAX_BUFFER];
  15.     int bytesRead, bytesWritten, totalWritten=0, errorOccured=0;
  16.     
  17.     
  18.     if (argc<2 || argc>3){
  19.         printf ("Usage: xcd2file <XCD file> [<output file name>]\n");
  20.         printf ("Convertrs an XCD (or any mode 2 form 2) file to a normal file\n");
  21.         printf ("by stripping the raw data and [if possible] using backup file\nto correct the original file\n");
  22.         printf ("if <output file name> is not defined, the output file is '<XCD file>.out'\n");
  23.         return 1;
  24.     }
  25.   
  26.   pXmr = new XCD_Media_Reader (argv[1]);
  27.  
  28.   if (!pXmr->isOpenedOK()){
  29.     printf ("File '%s' could not be properly opened for some reason,\n", argv[1]);
  30.     printf ("Operation aborted.\n");
  31.     return 1;
  32.   }
  33.  
  34.   if (argc<3){
  35.     strcpy(outFileName, argv[1]);
  36.     strcat(outFileName, DEFAULE_OUT_EXTENSION);
  37.   }  else
  38.     strcpy(outFileName, argv[2]);
  39.     
  40.     printf ("outFileName='%s'\n",outFileName);
  41.   
  42.   if (!(outFile=fopen(outFileName,"wb"))){
  43.     printf ("Cannot create output file '%s',\n",outFileName);
  44.     printf ("Operation aborted.\n");
  45.     return 1;
  46.   }
  47.  
  48.   while ((bytesRead=(pXmr->readNext(buffer, MAX_BUFFER))) > 0){
  49.     totalWritten+=(bytesWritten=fwrite (buffer, 1, bytesRead, outFile));
  50.     //dprintf ("bytesRead=%d, bytesWritten=%d, totalWritten=%d\n",bytesRead, bytesWritten, totalWritten);
  51.     printf("#");
  52.  
  53.     if (totalWritten/(10*1024*1024) != (totalWritten-bytesWritten)/(10*1024*1024))
  54.         printf ("\n%dM\n",totalWritten/(1024*1024));
  55.  
  56.     if (bytesWritten!=bytesRead){
  57.         printf ("Error occured while writing to file,\nOperation Aborted.\n");
  58.         errorOccured=1;
  59.         break;
  60.     }
  61.   }printf("\n");
  62.   
  63.   if (fclose(outFile)){
  64.     printf ("File '%s' could not be closed properly,\nOperation aborted.\n", outFileName);
  65.   }
  66.   
  67.   printf ("%d bytes were written to '%s'.\n",totalWritten, outFileName);  
  68.   
  69.   if (!errorOccured)
  70.     printf ("Success\n");
  71.   else
  72.     printf ("Failed\n");  
  73.  
  74.   return errorOccured;
  75. }
  76.