home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / strings / gr / gri.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-08  |  2.6 KB  |  111 lines

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