home *** CD-ROM | disk | FTP | other *** search
/ MegaDoom Adventures / PMWMEGADOOM.iso / doom / creators / deu52gcc / src / contrib / bcc2grx / chr / bin2s.c next >
C/C++ Source or Header  |  1993-06-03  |  2KB  |  77 lines

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. #define TRUE (1==1)
  7. #define FALSE (!TRUE)
  8.  
  9. void usage(char *err)
  10. {
  11.   puts("bin2s -- convert binary files into assembler source\n");
  12.   puts("Usage:\n");
  13.   puts("  bin2s <input> <global name> [<output>]");
  14.   if (err != NULL)
  15.     printf("\n\nError: %s\n");
  16.   exit(1);
  17. }
  18.  
  19. char *timestr(void)
  20. {
  21.   struct tm *lt;
  22.   time_t    t;
  23.  
  24.   t = time(NULL);
  25.   lt = localtime(&t);
  26.   return asctime(lt);
  27. }
  28.  
  29. long filesize(FILE *f)
  30. {
  31.   long posi, res;
  32.  
  33.   posi = ftell(f);
  34.   fseek(f, 0, SEEK_END);
  35.   res = ftell(f);
  36.   fseek(f, posi, SEEK_SET);
  37.   return res;
  38. }
  39.  
  40. void main(int argc, char *argv[])
  41. {
  42.   FILE *inp, *outp;
  43.   char name[1000];
  44.   long length, count;
  45.   unsigned char *buffer;
  46.  
  47.   if (argc < 3 || argc > 4) usage("Incorrect command line");
  48.  
  49.   strcpy(name,"binary_data_field");
  50.   inp = fopen( argv[1], "rb");
  51.   if (inp == NULL) usage("Couldn't opem input file");
  52.  
  53.   strcpy( name, argv[2]);
  54.   if (argc > 3) {
  55.     outp = fopen(argv[3], "w");
  56.     if (outp == NULL) usage("Couldn't open output file");
  57.   } else
  58.     outp = stdout;
  59.  
  60.   length = filesize(inp);
  61.   buffer = malloc( length);
  62.   if (buffer == 0) usage("Out of memory");
  63.   if (fread( buffer, length, 1, inp) != 1) usage("read error");
  64.   fclose(inp);
  65.  
  66.   fprintf(outp, ".globl _%s\n.text\n_%s:\n", name, name);
  67.   count = 0;
  68.   while (count != length) {
  69.     if (((count++)&15) == 0) fprintf(outp, "\n .byte %d", *(buffer++));
  70.             else fprintf(outp, ",%d", *(buffer++));
  71.   }
  72.   fprintf(outp, "\n");
  73.   fclose( outp);
  74. }
  75.  
  76.  
  77.