home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / www / lemur / binary / 2b_bin2c.lzx / Bin2C / Bin2C.c next >
Encoding:
C/C++ Source or Header  |  1980-01-01  |  2.0 KB  |  118 lines

  1. /*
  2.     $VER: Bin2C 0.5
  3.  
  4.     Bin2C by LeMUr/ Fire
  5.                  /& blabla
  6.  
  7.         This programm writes bin-data as C source-code.
  8.  
  9.         If You have only KickStart 1.3 then change all "Printf" (dos.library
  10.     v37++) to "printf" (linked from amiga.lib), but it'll make longer
  11.     program.
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17.  
  18. #include <proto/dos.h>
  19. #include <proto/exec.h>
  20.  
  21. #include <exec/memory.h>
  22.  
  23. // version
  24. const static char Version[]="\0$VER: Bin2C 0.5 (15.12.95) by LeMUr/Fire & blabla\0";
  25.  
  26. // memory and its size
  27. UBYTE *Bufor=NULL;
  28. ULONG Size=NULL;
  29.  
  30.  
  31. void out(int kod)
  32. // procedure frees memory and quits
  33. {
  34.     if(Bufor && Size)
  35.         FreeMem(Bufor, Size);
  36.  
  37.     exit(kod);
  38. } /* out() */
  39.  
  40.  
  41. int main(int argc, char *argv[])
  42. {
  43.     UBYTE znak[10];
  44.     ULONG i=0;
  45.  
  46.     BPTR file, lock;
  47.     struct FileInfoBlock __aligned fib;
  48.  
  49.     /* let's check arguments... */
  50.     if(argc!=3)
  51.     {
  52.         Printf("I need two file-names as argument!\n");
  53.         out(5);
  54.     }
  55.  
  56.     /* read file into memory */
  57.     if(!(lock=Lock(argv[1], ACCESS_READ)))
  58.     {
  59.         Printf("Unable to lock \"%s\"\n", argv[1]);
  60.         out(25);
  61.     }
  62.  
  63.     if(!Examine(lock, &fib))
  64.     {
  65.         Printf("Unable to examine \"%s\"\n", argv[1]);
  66.         UnLock(lock);
  67.         out(30);
  68.     }
  69.  
  70.     if(!(Size=fib.fib_Size))
  71.     {
  72.         Printf("File couldn't be 0 bytes long!\n");
  73.         UnLock(lock);
  74.         out(32);
  75.     }
  76.  
  77.     if(!(Bufor=AllocMem(Size, MEMF_CHIP)))
  78.     {
  79.         Printf("Unable to allocate memory!\n");
  80.         UnLock(lock);
  81.         out(35);
  82.     }
  83.  
  84.     if(!(file=Open(argv[1], MODE_OLDFILE)))
  85.     {
  86.         Printf("Unable to open file \"%s\"\n", argv[1]);
  87.         UnLock(lock);
  88.         out(40);
  89.     }
  90.  
  91.     Read(file, Bufor, Size);
  92.     Close(file);
  93.     UnLock(lock);
  94.  
  95.     /* saving source code */
  96.     if(!(file=Open(argv[2], MODE_NEWFILE)))
  97.     {
  98.         Printf("Unable to open file \"%s\"\n", argv[2]);
  99.         UnLock(lock);
  100.         out(40);
  101.     }
  102.  
  103.     Write(file, "UBYTE Data[]=\n{\n", 16);
  104.     /* "conversion" */
  105.     for(i=0; i<Size; i++)
  106.     {
  107.         sprintf(znak, "0x%X,\n", Bufor[i]);
  108.         if(strlen(znak)==5)
  109.             sprintf(znak, "0x0%X,\n", Bufor[i]);
  110.         Write(file, znak, strlen(znak));
  111.     }
  112.  
  113.     Write(file, "\n};\n", 4);
  114.     Close(file);
  115.  
  116.     out(0);
  117. } /* main() */
  118.