home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 09 / brief / bbe.c next >
Text File  |  1991-04-15  |  13KB  |  451 lines

  1. /****************************************************************************
  2.  
  3.  BBE.EXE : BRIEF Binary Editing Executable
  4.  
  5.            This programs main purpose and intent is to assist in editing
  6.            non-standard DOS files in BRIEF.  The converter is called
  7.            by the macros provided.  The BRIEF macros will attempt to
  8.            second guess the files origin and call the converter accordingly.
  9.  
  10.  AUTHOR  : Timothy Hanna
  11.  
  12.  DATE    : April 15, 1991
  13.  
  14.  
  15. Commands : ------------------------------------------------------------------
  16.  
  17.            BBE BH inFile outFile outFile
  18.            BBE HB inFile outFile 
  19.            BBE UD inFile outFile 
  20.            BBE DU inFile outFile 
  21.            BBE RC inFile outFile recordlength 
  22.            BBE RD inFile outFile recordlength 
  23.  
  24.            BH Convert binary (or text) to hex formatted file.           
  25.            HB Convert hex formatted file to binary (or text) file.      
  26.            UD Convert Unix style LF delimited lines to DOS CR LF lines. 
  27.            DU Convert DOS style CR LF delimited lines to Unix LF lines. 
  28.            RC Add CR LF to a data file for record delimiting at recordLength.           
  29.            RD Delete CR LF from a data file for data file at recordLength.
  30.  
  31.          : ------------------------------------------------------------------
  32.  
  33.  This program is hereby placed into the public domain.  No fee's other
  34.  than nominal copying fees may be charged.  
  35.  
  36. ****************************************************************************/
  37.  
  38.  
  39. #include <dos.h>
  40. #include <mem.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43.  
  44. #define ERROR_WNOP -1 /* ERROR_WrongNumberOfParameters */
  45. #define ERROR_IP   -2 /* ERROR_InvalidParameters       */
  46. #define ERROR_FNF  -3 /* ERROR_FileNotFound            */
  47.  
  48. /* ------------------------------------------------------------------------ 
  49.  
  50.  main()
  51.  
  52.  This function will parse out the command line and according to the values
  53.  call the appropriate function.  If it is determined that inappropriate
  54.  or insufficent values were passed then the function will return the
  55.  error value.
  56.  
  57.  ------------------------------------------------------------------------ */
  58.  
  59. int main(int argc, char *argv[])
  60. {
  61.  int result;
  62.  
  63.  FILE *inFile, *outFile1, *outFile2;
  64.  
  65.  if (argc<4)
  66.    return ERROR_WNOP;
  67.  
  68.  /* ------------------------------------------------------------------------
  69.  
  70.     Identify if selected conversion is BH, RC or RD.  These options
  71.     require four parameters.  Otherwise the check for HB, DU and UD
  72.     options.  Then open needed files and check that the files
  73.     have been opened.  Lastly call the selected conversion routine.
  74.  
  75.  ------------------------------------------------------------------------ */
  76.  
  77.  if (!strcmp(argv[1],"BH") || !strcmp(argv[1],"RC") || !strcmp(argv[1],"RD"))
  78.     {
  79.      if (argc<5)
  80.         return ERROR_WNOP;
  81.  
  82.      if (!strcmp(argv[1],"BH"))
  83.         {
  84.          inFile  = fopen(argv[2],"rb");
  85.          outFile1 = fopen(argv[3],"wb");
  86.          outFile2 = fopen(argv[4],"wb");
  87.         
  88.          if ((inFile==NULL) && (outFile1==NULL) && (outFile2==NULL))
  89.             {
  90.              if (inFile!=NULL)
  91.                 fclose(inFile);
  92.         
  93.              if (outFile1!=NULL)
  94.                 fclose(outFile1);
  95.         
  96.              if (outFile2!=NULL)
  97.                 fclose(outFile2);
  98.          
  99.              return ERROR_FNF;
  100.             }
  101.          
  102.          result = ConvertBinaryToHex(inFile,outFile1,outFile2);
  103.         
  104.          fclose(inFile);
  105.          fclose(outFile1);
  106.          fclose(outFile2);
  107.         }
  108.      else
  109.         {
  110.          inFile  = fopen(argv[2],"rb");
  111.          outFile1 = fopen(argv[3],"wb");
  112.  
  113.          if ((inFile==NULL) && (outFile1==NULL))
  114.             {
  115.              if (inFile!=NULL)
  116.                 fclose(inFile);
  117.         
  118.              if (outFile1!=NULL)
  119.                 fclose(outFile1);
  120.  
  121.              return ERROR_FNF;
  122.             }
  123.  
  124.          if (!strcmp(argv[1],"RC"))
  125.             result = RecordsCreate(inFile,outFile1,atoi(argv[4]));
  126.  
  127.          if (!strcmp(argv[1],"RD"))
  128.             result = RecordsDelete(inFile,outFile1,atoi(argv[4]));
  129.         }
  130.     }
  131.  else if (!strcmp(argv[1],"HB")  || !strcmp(argv[1],"UD") || !strcmp(argv[1],"DU"))
  132.     {
  133.      inFile  = fopen(argv[2],"rb");
  134.      outFile1 = fopen(argv[3],"wb");
  135.  
  136.      if ((inFile==NULL) && (outFile1==NULL))
  137.         {
  138.          if (inFile!=NULL)
  139.             fclose(inFile);
  140.     
  141.          if (outFile1!=NULL)
  142.             fclose(outFile1);
  143.     
  144.          return ERROR_FNF;
  145.         }
  146.  
  147.      if (!strcmp(argv[1],"HB"))
  148.         result = ConvertHexToBinary(inFile,outFile1);
  149.  
  150.      if (!strcmp(argv[1],"UD"))
  151.         result = ConvertUnixToDOS(inFile,outFile1);
  152.  
  153.      if (!strcmp(argv[1],"DU"))
  154.         result = ConvertDOSToUnix(inFile,outFile1);
  155.  
  156.      fclose(inFile);
  157.      fclose(outFile1);
  158.     }
  159.  else
  160.     return ERROR_IP;
  161.  
  162.  return result;
  163. }
  164.  
  165. /* ------------------------------------------------------------------------ 
  166.  
  167.  ConvertBinaryToHex()
  168.    
  169.    This function converts the infile character by character to destination
  170.    files of hexadecimal and ascii values.  Since BRIEF is incapable of
  171.    displaying the ASCII values of 0, 9 and 13 they are left as dots in the
  172.    buffer.
  173.  
  174.  ------------------------------------------------------------------------ */
  175.  
  176. int ConvertBinaryToHex(FILE *inFile, FILE *outFile1, FILE *outFile2)
  177. {
  178.  const valuesPerLine = 25;
  179.  const hexLineIncrement = 2;
  180.  
  181.  unsigned char buffer[1200];
  182.  
  183.  unsigned char hexBuffer[52];
  184.  unsigned char charBuffer[27];
  185.  
  186.  unsigned int numberOfBytes=1,index1,index2,index3,index4,charactersLeft;
  187.  
  188.  unsigned char decimalToHex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  189.  
  190.  hexBuffer[50] = '\r';
  191.  hexBuffer[51] = '\n';
  192.  
  193.  charBuffer[25] = '\r';
  194.  charBuffer[26] = '\n';
  195.  
  196.  while (numberOfBytes)
  197.     {
  198.      numberOfBytes = fread(buffer,1,1200,inFile);
  199.  
  200.      if (numberOfBytes)
  201.         {
  202.          for (index1=0;index1<numberOfBytes;index1+=valuesPerLine)
  203.             {
  204.              charactersLeft = (numberOfBytes-index1 < valuesPerLine) ? (numberOfBytes-index1) : valuesPerLine;
  205.  
  206.              memset(hexBuffer,' ',50); 
  207.              memset(charBuffer,'.',25);
  208.  
  209.              for (index2=0;index2<charactersLeft;index2++)
  210.                {
  211.                 index3 = index1+index2;
  212.                 index4 = index2*hexLineIncrement;
  213.  
  214.                 hexBuffer[index4]   = decimalToHex[buffer[index3]/16];
  215.                 hexBuffer[index4+1] = decimalToHex[buffer[index3]%16];
  216.  
  217.                 switch (buffer[index3])
  218.                   {
  219.                    case 0:
  220.                    case 9:
  221.                    case 13:
  222.                         break;
  223.  
  224.                    default:
  225.                         charBuffer[index2] = buffer[index3];
  226.  
  227.                   };
  228.                }
  229.  
  230.              if (charactersLeft<valuesPerLine)
  231.                 {
  232.                  numberOfBytes=0;
  233.                  for (index2=charactersLeft;index2<valuesPerLine;index2++)
  234.                     {
  235.                      hexBuffer[index2*hexLineIncrement]   = 'X';
  236.                      hexBuffer[index2*hexLineIncrement+1] = 'X';
  237.                     }
  238.                 }
  239.  
  240.              fwrite(hexBuffer,1,52,outFile1);
  241.  
  242.              fwrite(charBuffer,1,27,outFile2);
  243.  
  244.             }
  245.         }
  246.     }
  247.  
  248.    return 0;
  249. }
  250.  
  251. /* ------------------------------------------------------------------------ 
  252.  
  253.  ConvertHexToBinary()
  254.    
  255.    This function converts the infile based on expected two character
  256.    hexadecimal values.  Each record is expected to be 52 chracters
  257.    in length with the last two chraters per record being thrown away.
  258.    Every two chracters are converted to a single binary value and stored
  259.    into a binary buffer.  If the chracter 'X' is encountered as the
  260.    first of the two characters then that and the next character are ignored.
  261.  
  262.  ------------------------------------------------------------------------ */
  263.  
  264. int ConvertHexToBinary(FILE *inFile, FILE *outFile)
  265. {
  266.  unsigned char hexToDecimal[] = { 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,0,10,11,12,13,14,15}; 
  267.  
  268.  unsigned char buffer[52];
  269.  
  270.  unsigned char byteBuffer[2];
  271.  unsigned char binaryBuffer[25];
  272.  
  273.  unsigned int numberOfBytes=1,index1,index2,writeAmount;
  274.  
  275.  while (numberOfBytes)
  276.     {
  277.      numberOfBytes = fread(buffer,1,52,inFile);
  278.  
  279.      if (numberOfBytes==52)
  280.         {
  281.          writeAmount=25;
  282.  
  283.          for (index1=0;index1<25;index1++)
  284.             {
  285.              index2 = index1*2;
  286.  
  287.              if ((byteBuffer[0] = buffer[index2]) != 'X')
  288.                {
  289.                 byteBuffer[1] = buffer[index2+1];
  290.                 binaryBuffer[index1] = (hexToDecimal[byteBuffer[0]-48] * 16) + (hexToDecimal[byteBuffer[1]-48]);
  291.                }
  292.              else
  293.                 writeAmount--;
  294.             }
  295.  
  296.          fwrite(binaryBuffer,1,writeAmount,outFile);
  297.         }
  298.     }
  299.  
  300.  return 0;
  301. }
  302.  
  303. /* ------------------------------------------------------------------------ 
  304.  
  305.    ConvertUnixToDOS()
  306.  
  307.    This function will search the file for the occurance of the unix line
  308.    delimiter 0x0A (a line feed chracter).  Since BRIEF needs to have a
  309.    carraige return, one is inserted into the destination buffer prior to
  310.    the line feed chracter.
  311.  
  312.  ------------------------------------------------------------------------ */
  313.  
  314. int ConvertUnixToDOS(FILE *inFile, FILE *outFile)
  315. {
  316.  unsigned char inBuffer[4000];
  317.  unsigned char outBuffer[8000];
  318.  
  319.  unsigned int numberOfBytes=1,index1,index2;
  320.  
  321.  while (numberOfBytes)
  322.     {
  323.      numberOfBytes = fread(inBuffer,1,4000,inFile);
  324.  
  325.      if (numberOfBytes)
  326.         {
  327.          for (index1=0,index2=0;index1<numberOfBytes;index1++)
  328.             {
  329.              if (inBuffer[index1]==0x0A)
  330.                 outBuffer[index2++]=0x0D;
  331.  
  332.              outBuffer[index2++]=inBuffer[index1];
  333.             }
  334.  
  335.          fwrite(outBuffer,1,index2,outFile);
  336.         }
  337.     }
  338.  return 0;
  339. }
  340.  
  341. /* ------------------------------------------------------------------------ 
  342.  
  343.    ConvertDosToUnix()
  344.  
  345.    This function will search the file for the occurance of a carridge return
  346.    line feed character sequence.  If identified it will only write the
  347.    line feed character, and skip the carraidge return.
  348.  
  349.  ------------------------------------------------------------------------ */
  350.  
  351. int ConvertDOSToUnix(FILE *inFile, FILE *outFile)
  352. {
  353.  unsigned char inBuffer[4000];
  354.  unsigned char outBuffer[4000];
  355.  
  356.  unsigned int numberOfBytes=1,index1,index2;
  357.  
  358.  while (numberOfBytes)
  359.     {
  360.      numberOfBytes = fread(inBuffer,1,4000,inFile);
  361.  
  362.      if (numberOfBytes)
  363.         {
  364.          for (index1=0,index2=0;index1<numberOfBytes;index1++)
  365.             {
  366.              if (inBuffer[index1]==0x0D && inBuffer[index1+1]==0x0A)
  367.                  index1++;
  368.  
  369.              outBuffer[index2++]=inBuffer[index1];
  370.             }
  371.  
  372.          fwrite(outBuffer,1,index2,outFile);
  373.         }
  374.     }
  375.  return 0;
  376. }
  377.  
  378. /* ------------------------------------------------------------------------ 
  379.  
  380.    RecordsCreate()
  381.  
  382.    This function will, based on a selected value, add the carridge return
  383.    line feed sequence at every occurance of that number of characters.
  384.  
  385.  ------------------------------------------------------------------------ */
  386.  
  387. int RecordsCreate(FILE *inFile, FILE *outFile, int recordSize)
  388. {
  389.  unsigned char buffer[512];
  390.  
  391.  unsigned int numberOfBytes=1;
  392.  
  393.  if (recordSize>510)
  394.     return ERROR_IP;
  395.  
  396.  buffer[recordSize]   = '\r';
  397.  buffer[recordSize+1] = '\n';
  398.  
  399.  while (numberOfBytes)
  400.     {
  401.      numberOfBytes = fread(buffer,1,recordSize,inFile);
  402.  
  403.      if (numberOfBytes==recordSize)
  404.         fwrite(buffer,1,recordSize+2,outFile);
  405.      else
  406.         {
  407.          buffer[numberOfBytes] = '\r';
  408.          buffer[numberOfBytes+1] = '\n';
  409.  
  410.          fwrite(buffer,1,numberOfBytes+1,outFile);
  411.         }
  412.     }
  413.  return 0;
  414. }
  415.  
  416. /* ------------------------------------------------------------------------ 
  417.  
  418.    RecordsDelete()
  419.  
  420.    This function will, based on a selected value, removes two chracters
  421.    at every occurance of that number of characters.
  422.  
  423.  ------------------------------------------------------------------------ */
  424.  
  425. int RecordsDelete(FILE *inFile, FILE *outFile, int recordSize)
  426. {
  427.  unsigned char buffer[512];
  428.  unsigned int numberOfBytes=1;
  429.  
  430.  if (recordSize>510)
  431.     return ERROR_IP;
  432.  
  433.  while (numberOfBytes)
  434.     {
  435.      numberOfBytes = fread(buffer,1,recordSize+2,inFile);
  436.  
  437.      if (numberOfBytes==recordSize)
  438.         fwrite(buffer,1,recordSize,outFile);
  439.      else
  440.         fwrite(buffer,1,numberOfBytes-2,outFile);
  441.     }
  442.  return 0;
  443. }
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.