home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / bincon.zip / BINCON.C next >
Text File  |  1994-10-26  |  5KB  |  178 lines

  1.     /*=============================================================
  2.     =  (C)opyright 1994 Tim Triemstra and                         =
  3.     =  (C)opyright 1994 Empath Software, All Rights are Reserved  =
  4.     =                                                             =
  5.     =  The author can be contacted at:                            =
  6.     =  Internet: empath@umcc.umich.edu                            =
  7.     =============================================================*/
  8.  
  9. /*
  10.     This file is probably pretty ugly to alot of you "professional
  11.     programmers" (or anybody else actually.)  However, since I am
  12.     completely self-taught there are bound to be areas where I can
  13.     improve - just ask my professors.
  14.  
  15.     This code should, however, compile just fine for pretty much
  16.     any operating system.  I haven't tested it with a 16bit compile
  17.     but it should work (with somewhat limitted usefulness, look at
  18.     the comments for reasons why.)
  19.  
  20.     I included the source because there is probably a situation
  21.     that someone out there might like some more functionality.  I can't
  22.     really think of anything off the top of my head, though.
  23.  
  24.     If this code influences you to write an application along this
  25.     line, please release it as shareware/freeware.  The world needs
  26.     good programs and if we all have to spend our precious minutes
  27.     writing small tools like this then the real programs will take
  28.     alot longer.  The only thing I ask is that if you use it constantly
  29.     then distribute it.  After all, you're not the only wierd one out
  30.     there that may like    this program.
  31.  
  32.     You can mail me anything you think I may want/need (I always
  33.     accept money :) at the following addresses:
  34.  
  35.     Tim Triemstra
  36.     Empath Software
  37.     P.O.Box 40901
  38.     Redford, MI 48240
  39.  
  40.     I can be (and prefer to be) contacted at:
  41.     Internet:  empath@umcc.umich.edu
  42.     UUCPmail:  umcc!roofus!empath   (I think this still works :)
  43. */
  44.  
  45. #ifdef  __I86__
  46. #if     __I86__     > 2     /* Symantec uses I86 instead of __386__ */
  47. #define __386__     1       /* Make it like Watcom */
  48. #endif
  49. #endif
  50.  
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <dos.h>
  54. #include <io.h>
  55.  
  56. void main(int argc, char *argv[])
  57. {
  58.     FILE *f;
  59.     unsigned char   *raw;
  60.     unsigned int    length;
  61.     unsigned int    count;
  62.     short           count2;
  63.     int             start, end, temp;
  64.  
  65.     if(argc < 3)
  66.     {
  67.         #ifdef __386__              /* Make clear if in 32bit mode */
  68.             printf(">32bit version<\n");
  69.         #else
  70.             printf(">16bit version<\n");
  71.         #endif
  72.  
  73.         /* Instructions when no paramters are given */
  74.         printf("BINCON <file> <type D:H> <startByte> <endByte>\n");
  75.         printf("type D=Decimal, H=Hex, ");
  76.         printf("<start> defaults to 0, <end> defaults to the eof\n");
  77.         printf("IE: bincon bitmap.bmp D 54 822 >bitmap.dat\n");
  78.         printf("Would turn a 16x16 24bit Windows BMP into a raw data");
  79.         printf("file (3 bytes per pixel)\n");
  80.         exit(0);
  81.     }
  82.  
  83.     /* Take care of the file information */
  84.     f = fopen(argv[1],"rb");
  85.     if(!f)
  86.     {
  87.         printf("Could not open the requested file \"%s\"\n",argv[1]);
  88.         exit(255);
  89.     }
  90.  
  91.     /* Determine how much memory is needed */
  92.     /* +NOTE+   This program loads the entire file into memory
  93.                 before processing so 16bit compiles won't run very
  94.                 well.  This is done to save speed, the program can
  95.                 easily be modified to convert as it reads, having
  96.                 almost no memory overhead at all.  If you are still
  97.                 running a 16bit computer this is advised. */
  98.  
  99.     start = 0; end = filelength(fileno(f));
  100.     if(argc > 3)
  101.     {
  102.         start = atoi(argv[3]);
  103.     }
  104.     if(argc > 4)
  105.     {
  106.         temp = atoi(argv[4]);
  107.         if(temp < end) end = temp;
  108.     }
  109.     if(start < end) length = end - start + 1;
  110.     else
  111.     {
  112.         printf("** You cannot start beyond the end of the file\n");
  113.         exit(255);
  114.     }
  115.  
  116.     #ifdef __386__
  117.         raw = (unsigned char*)malloc(length);
  118.     #else
  119.         if(length > 64000)
  120.         {
  121.             printf("** 16bit version handles up to 64000 bytes only");
  122.             exit(255);
  123.         }
  124.         raw = farmalloc(length);
  125.     #endif
  126.  
  127.     fseek(f, start, SEEK_SET);
  128.     fread(raw,sizeof(char),length,f);
  129.     fclose(f);
  130.  
  131.     printf("\n{\n    ");
  132.  
  133.     /* This will simply output to the screen, good for >filename work */
  134.     if(argv[2][0] == 'd') argv[2][0] = 'D';
  135.     if(argv[2][0] == 'h') argv[2][0] = 'H';
  136.     count2 = -1;
  137.  
  138.     switch(argv[2][0])
  139.     {
  140.         /* Slightly different output formatting for Decimal and Hex */
  141.         /* Hexidecimal */
  142.         case 'H' :  for(count = 0; count < length - 1; count++)
  143.                     {
  144.                         if(++count2 != 0) printf(",");
  145.                         if(count2 == 3 || count2 == 6 ||
  146.                             count2 == 9) printf("  ");
  147.                         if(count2 > 11)
  148.                         {
  149.                             count2 = 0;
  150.                             printf("\n    ");
  151.                         }
  152.                         printf("%#4x",raw[count]);
  153.                     }
  154.                     break;
  155.         /* Decimal */
  156.         case 'D' :  for(count = 0; count < length - 1; count++)
  157.                     {
  158.                         if(++count2 != 0) printf(",");
  159.                         if(count2 == 3 || count2 == 6 ||
  160.                             count2 == 9) printf("  ");
  161.                         if(count2 > 11)
  162.                         {
  163.                             count2 = 0;
  164.                             printf("\n    ");
  165.                         }
  166.                         printf(" %3d",raw[count]);
  167.                     }
  168.                     break;
  169.         default  :  {
  170.                         printf("** You must specify (D)ec or (H)ex\n");
  171.                         exit(0);
  172.                     }
  173.     }
  174.     printf("\n};\n");       /* Closing brackets and we're done! */
  175.     exit(0);
  176. }
  177. 
  178.