home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Programming / ini_library / Examples / src / C / Bin2H.c next >
C/C++ Source or Header  |  1999-03-26  |  3KB  |  153 lines

  1. /* Binary to C/C++ header file converter v1.0
  2. ** by Basty/Seasons (c) 1999. */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. #define ASCII 1
  9. #define BINARY 0
  10.  
  11. #define LINESIZE 16
  12.  
  13. #define CONV_OKAY 0
  14. #define CONV_ERROR_READ 1
  15. #define CONV_ERROR_WRITE 2
  16.  
  17. #define VERSION_MAJOR 1
  18. #define VERSION_MINOR 0
  19.  
  20. FILE *infile;
  21. FILE *outfile;
  22. unsigned long len;
  23.  
  24. int ConvertBinary()
  25. {
  26.   unsigned char Buffer[LINESIZE];
  27.   unsigned int rlen;
  28.   unsigned long rpos = 0;
  29.   int i;
  30.  
  31.   while (rlen = fread(Buffer,1,LINESIZE,infile))
  32.   {
  33.     if (ferror(infile)) return CONV_ERROR_READ;
  34.     fputs("\n\t",outfile);
  35.     for (i = 0; i<rlen; i++)
  36.     {
  37.       rpos++;
  38.       fprintf(outfile,"0x%02x",*(Buffer+i));
  39.       if ((rpos != len) || i != rlen-1)
  40.         fputs(",",outfile);
  41.       if (i != rlen-1) fputs(" ",outfile);
  42.     }
  43.     if (ferror(outfile)) return CONV_ERROR_WRITE;
  44.   }
  45.   return(0);
  46. }
  47.  
  48. int ConvertASCII()
  49. {
  50.   unsigned char Buffer[LINESIZE],cbyte;
  51.   unsigned int rlen;
  52.   unsigned long rpos = 0;
  53.   int i;
  54.  
  55.   while (rlen = fread(Buffer,1,LINESIZE,infile))
  56.   {
  57.     if (ferror(infile)) return CONV_ERROR_READ;
  58.     fputs("\n\t",outfile);
  59.     for (i = 0; i<rlen; i++)
  60.     {
  61.       rpos++;
  62.       cbyte = Buffer[i];
  63.       if ((cbyte > 31) && (cbyte < 128))
  64.         fprintf(outfile,"'%c'",cbyte);
  65.       else
  66.         fprintf(outfile,"0x%02x",cbyte);
  67.       if ((rpos != len) || i != rlen-1)
  68.         fputs(",",outfile);
  69.       if (i != rlen-1) fputs(" ",outfile);
  70.     }
  71.     if (ferror(outfile)) return CONV_ERROR_WRITE;
  72.   }
  73.   return(0);
  74. }
  75.  
  76. void main(int argc,char **argv)
  77. {
  78.   char Mode = BINARY;
  79.   int conv = 0;
  80.  
  81.   printf("Bin2H v%d.%02d (c) 1999 by Basty/Seasons (%s, %s).\n\n",\
  82. VERSION_MAJOR,VERSION_MINOR,__DATE__,__TIME__);
  83.  
  84.   if (argc != 3 && argc != 4) goto usage;
  85.   if (argc == 4)
  86.   {
  87.     if (stricmp(argv[3],"ASCII")) goto usage;
  88.     Mode = ASCII;
  89.   }
  90.   if (!(infile = fopen(argv[1],"rb"))) goto openerr;
  91.  
  92.   fseek(infile,0,SEEK_END);
  93.   len = ftell(infile);
  94.   fseek(infile,0,SEEK_SET);
  95.  
  96.   if (ferror(infile)) goto openerr;
  97.  
  98.   if (!(outfile = fopen(argv[2],"w"))) goto createrr;
  99.  
  100.     fprintf(outfile,"/* Converted with binary to C-include. \
  101. (c) 1999 by Basty/Seasons. */\n\n\
  102. unsigned char %s[%ld] =\n{",argv[1],len);
  103.  
  104.   switch (Mode)
  105.   {
  106.   case BINARY:
  107.     conv = ConvertBinary();
  108.   case ASCII:
  109.     conv = ConvertASCII();
  110.   }
  111.  
  112.   if (conv == CONV_OKAY) fputs("\n};\n",outfile);
  113.  
  114.   fclose(outfile);
  115.   fclose(infile);
  116.  
  117.   switch (conv)
  118.   {
  119.   case CONV_ERROR_READ:
  120.     goto readerr;
  121.   case CONV_ERROR_WRITE:
  122.     goto writerr;
  123.   }
  124.  
  125.   exit(0);
  126.  
  127.   usage:
  128.   printf("Invalid parameters !\n\nUsage is: %s <infile> <outfile> [ASCII]\n",argv[0]);
  129.  
  130.   exit(20);
  131.  
  132.   openerr:
  133.   printf("%s: Couldn't open %s.\nProgram terminated !\n",argv[0],argv[1]);
  134.  
  135.   exit(10);
  136.  
  137.   createrr:
  138.   fclose(infile);
  139.   printf("%s: Couldn't create %s.\nProgram terminated !\n",argv[0],argv[2]);
  140.  
  141.   exit(10);
  142.  
  143.   readerr:
  144.   printf("%s: Couldn't read in %s.\nProgram terminated !\n",argv[0],argv[1]);
  145.  
  146.   exit(10);
  147.  
  148.   writerr:
  149.   printf("%s: Couldn't write to %s.\nProgram terminated !\n",argv[0],argv[2]);
  150.  
  151.   exit(10);
  152. }
  153.