home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Source / exehead / bin2h.c < prev    next >
C/C++ Source or Header  |  2002-08-02  |  1KB  |  61 lines

  1. /* Generates a .h file from a binary file.
  2. ** v1.2 - 3/8/01
  3. ** Copyright (C) 1996-2001 Justin Frankel
  4. ** Public domain.
  5. */
  6. #include <stdio.h>
  7.  
  8. int main(int argc, char *argv[]) {
  9.   int length;
  10.   FILE *in, *out;
  11.   char *outfilename;
  12.   char *token;
  13.   int total_bytes=0;
  14.  
  15.   if (argc != 4) { 
  16.     fprintf(stderr,"Usage: bin2h file.dat outfile.h token_name\n");
  17.     return 1;
  18.   }
  19.  
  20.   in = fopen(argv[1],"rb");
  21.  
  22.   if (!in) {
  23.     fprintf(stderr,"Error: file not found\n");
  24.     return 1;
  25.   }
  26.   out = fopen(argv[2],"wt");
  27.  
  28.   if (!out) {
  29.     fclose(in);
  30.     fprintf(stderr,"Error: could not open output file\n");
  31.     return 1;
  32.   }
  33.   fseek(in,0,SEEK_END);
  34.   length=ftell(in);
  35.   fseek(in,0,SEEK_SET);
  36.  
  37.   outfilename = argv[2];
  38.   token = argv[3];
  39.   fprintf(out,"unsigned char %s[%d] = {  \n",token,length);
  40.   for (;;) {
  41.     static int firsttime;
  42.     static int linecount;
  43.     int c;
  44.     if (++linecount > 10) {
  45.       linecount = 0;
  46.       fprintf(out,",\n  ");
  47.     }
  48.     else if (firsttime) fprintf(out,", ");
  49.     firsttime = 1;
  50.     c = fgetc(in);
  51.     if (feof(in)) break;
  52.     total_bytes++;
  53.     fprintf(out,"%i",c);
  54.   }
  55.   fprintf(out,"};\n",token);
  56.   fclose(in);
  57.   fclose(out);
  58.   fprintf(stderr,"%s -> %s (%d bytes)\n\n",argv[1],token,total_bytes);
  59.   return 0;
  60. }
  61.