home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / GR.ZIP / GRR.C < prev    next >
Text File  |  1989-02-08  |  3KB  |  118 lines

  1. #include <stdio.h>
  2.  
  3. #include <io.h>
  4. #include <string.h>
  5. #include <cxlstr.h>
  6.  
  7. /*
  8.    Global Replace Reverse - Reads a data file of token definitions and then 
  9.               applies them to images read from an input file and writes the 
  10.               converted text to an output file.  GRR uses the same token 
  11.               definitions as GR, but reverses them.
  12.  
  13.                 Call is:   GRR  inputfile outputfile tokenfile
  14.  
  15.                     All three fields must be supplied or GRR errors.
  16.  
  17.               The format of the images in the token file is:
  18.  
  19.                     token=replacement-string
  20.  
  21.          !!!!!NOTE!!!!!  GRR replaces occurrences of "replacement-string" 
  22.                          with "token", just the REVERSE of GR.
  23.  
  24.                          Further, where GR takes the tokens from first to 
  25.                          last; GRR processes them against each line from 
  26.                          last to first!
  27. */
  28.  
  29. void file_error(char *fn)
  30. {
  31.     printf("***ERROR*** Unable to open %s File - GRR terminates.\n",fn);
  32.     exit(99);
  33. }
  34.  
  35. main(int argc, char *argv[])
  36. {
  37.     FILE *infile, *outfile, *tokfile;
  38.  
  39.     char token[100][80];
  40.     char *rplstr[100];
  41.     char buffer[132];
  42.     char temp[132];
  43.     char *where, *nxtline, *tokloc, *nlchar;
  44.  
  45.     int numtokens, numlines, i, j;
  46.  
  47.  
  48. printf("Global Replace REVERSE 1R1A      2/10/89\n");
  49.  
  50. /*  First check command line    */
  51.  
  52.     if (argc < 4)
  53.     {
  54.         printf("***ERROR*** Not all files specified.  GRR terminates.\n");
  55.         exit(99);
  56.     }
  57.  
  58.  
  59. /*  Read in tokens and set up pointers  */
  60.  
  61.     tokfile = fopen(argv[3], "r");
  62.     if (!tokfile) file_error("Token");
  63.  
  64.     numtokens=0;                       /* Initialize token count */
  65.  
  66.     while (nxtline = fgets(token[numtokens],80,tokfile))
  67.     {
  68.         if (where = strchr(token[numtokens], '='))
  69.         {
  70.             *where = '\0';
  71.             rplstr[numtokens] = where+1;
  72.             if (nlchar = strchr(rplstr[numtokens],'\n'))
  73.                           *nlchar = '\0';
  74.             numtokens +=1;
  75.         }
  76.         else
  77.             rplstr[numtokens] = NULL;
  78.  
  79.     }
  80.     fclose(tokfile);
  81.  
  82. /*  Now open input and output files */
  83.  
  84.     infile = fopen(argv[1],"r");
  85.     if (!infile) file_error("Input");
  86.  
  87.     outfile = fopen(argv[2],"w");
  88.     if (!outfile) file_error("Output");
  89.  
  90. /*  Now read in each image, convert it, and write it out  */
  91.  
  92.     numlines = 0;
  93.  
  94.     while (where = fgets(buffer, 132, infile))
  95.     {
  96.  
  97.         numlines +=1;
  98.         for (i=numtokens-1; i>=0; i--)
  99.         {
  100.             while (tokloc = strstr(buffer,rplstr[i]))
  101.             {
  102.                 *tokloc = '\0';
  103.                 strcpy(temp,buffer);
  104.                 strcat(temp,token[i]);
  105.                 strcat(temp,(char *)(tokloc+strlen(rplstr[i])));
  106.                 strcpy(buffer,temp);
  107.             }
  108.         }
  109.         fputs(buffer,outfile);
  110.     }
  111.  
  112.     fclose(infile);
  113.     fclose(outfile);
  114.     printf("Conversion completed ... %d lines\n",numlines);
  115.     exit(0);
  116. }
  117.  
  118.