home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 112.lha / Files / filefix.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  6KB  |  236 lines

  1.  
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #define MAXFSIZE 40 /* max chars in filename */
  5.   char infile[MAXFSIZE], outfile[MAXFSIZE]; /* global storage */
  6.   FILE *source, *destination;                         /* files */
  7.   char old[52],new[52],origold[52],orignew[52];
  8.   int verbose;
  9.   long nc,nl,nr;
  10. main(argc,argv)
  11. int argc;
  12. char *argv[];
  13. {
  14.     int c = 1;
  15.     char verbuffer[5];
  16.     verbose = 0;
  17.     if(argc == 6)
  18.       verbose = 1;
  19.     if(argc == 5 || argc == 6)  
  20.      {
  21.       strcpy(infile,argv[1]); 
  22.       strcpy(outfile,argv[2]); 
  23.       strcpy(origold,argv[3]); 
  24.       strcpy(orignew,argv[4]); 
  25.       if(!strcmp(infile,outfile))
  26.         exit(0);
  27.      } 
  28.     else  
  29.      {
  30.     if(argc != 1) 
  31.      {
  32.        printf("filefix v01 D. Wahl 3/3/88\n");
  33.        printf("General search and replace routine.\n");
  34.        printf("Replace any occurance of 1 - 40 chars\n");
  35.        printf("with 0 - 40 others. You may use a '[n]'\n");
  36.        printf("to force a newline, [aa] as hex string.\n");
  37.        printf("filefix [in_file out_file search_for replace_with] [verbose[y]]\n");
  38.        printf("Some common hex values:\n");
  39.        printf("[20] = space, [0a] = lf, [0d] = cr, [22] = \", [30] = *\n");
  40.        printf("[1b] = esc\n");
  41.      }  
  42.     do {
  43.          if (c == 0)
  44.      {  printf("      Input source and destination file names\n");
  45.         printf("           file names cannot be the same\n"); }
  46.        }
  47.     while ((c = get_file_names(&infile[0], &outfile[0] )) == 0);
  48.     c = 1;
  49.     do {
  50.          if (c == 0)
  51.            printf("Search and replace strings cannot match\n");
  52.        }
  53.     while ((c = get_match_strings(origold,orignew)) == 0);
  54.     printf("Verbose option (y/n) ? ");
  55.     fgets(verbuffer,3,stdin);
  56.     if(verbuffer[0] != 'y')
  57.        verbose = 0;
  58.     else   
  59.        verbose = 1;
  60.      } /* end else */  
  61.     open_files(&infile[0], &outfile[0] );
  62.     parse_string(origold,old);
  63.     parse_string(orignew,new);
  64.     process_file(old,new);
  65.     printf("Replaced \"%s\" in %s with \"%s\" in %s.\n",origold,infile,orignew,outfile);
  66.     printf("%ld replacements, %ld lines, %ld chars in file\n",nr,nl,nc);
  67.  
  68. }
  69.                    
  70. get_file_names(in, out)
  71.  
  72.     char *in, *out;
  73. {
  74.     char *a, *b;
  75.     int c;
  76.  /* input file */
  77.     printf("     Source = ");
  78.     fgets(in,40,stdin);
  79.     in[strlen(in)-1] = '\0'; /* remove newline from filename */
  80.     
  81.  
  82. /* output file  */
  83.     printf("Destination = ");
  84.     fgets(out,40,stdin);
  85.     out[strlen(out)-1] = '\0';
  86.     if(!strlen(in) || !strlen(out)) /* abort on NULL file name */
  87.       exit(0);
  88.    c = strcmp(in, out );  /* filenames must be different */
  89.      return(c);
  90. }
  91. get_match_strings(origold,orignew)
  92.  
  93.     char *origold, *orignew;
  94. {
  95.     char *a, *b;
  96.     int c;
  97.  /* input file */
  98.     printf(" string to replace = ");
  99.     fgets(origold,41,stdin);
  100.     origold[strlen(origold)-1] = '\0'; /* remove newline from filename */
  101.      
  102. /* output file  */
  103.     printf("replacement string = ");
  104.     fgets(orignew,41,stdin);
  105.     orignew[strlen(orignew)-1] = '\0';
  106.     if(!strlen(origold))
  107.       exit(0);    /* abort on NULL match string */
  108.    c = strcmp(origold,orignew);  /* strings must be different */
  109.      return(c);
  110. }
  111. parse_string(before,after)
  112. char *before;
  113. char *after;
  114.  
  115.  {
  116.   int c,i,val;
  117.  /* parse string,  */
  118.     c = i = 0;    
  119.     while(c <= strlen(before))
  120.      {
  121.       if(before[c] != '[')
  122.        {
  123.         after[i++] = before[c++];
  124.        }  
  125.       else 
  126.        {
  127.         c++;
  128.         if(before[c] == 'n' && before[c+1] == ']')
  129.          {
  130.           after[i++] = '\n';
  131.           c+=2;
  132.          } 
  133.         else if(before[c+2] == ']')
  134.          {
  135.           sscanf(&before[c],"%2x",&val);
  136.           after[i++] = val;
  137.           c+=3;
  138.          } 
  139.         else 
  140.           after[i++] = '[';
  141.         } 
  142.      } 
  143.   }
  144.  
  145.    
  146. open_files(in, out)
  147.  
  148.     char *in, *out;
  149. {
  150.     int ret = 1;
  151.  /* open files */
  152.  
  153.     if ((source = fopen(in, "r")) == NULL)
  154.        {    printf("open source file error\n");
  155.             exit(ret);
  156.        }
  157.     if ((destination = fopen(out, "w")) == NULL)
  158.        {    printf("open destination file error\n");
  159.             fclose(source);
  160.             exit(ret);
  161.        }
  162.  
  163. }
  164.  
  165.  
  166. process_file(old,new)
  167.     char *old, *new;
  168. {
  169.     char buffer[50],verbuffer[5];
  170.     int lenold,lennew;
  171.     register int i,c;
  172.     register int p = 0;
  173.     register int count = 0;
  174.     lenold = strlen(old);
  175.     lennew = strlen(new);
  176.     nl = nc = nr = 0;
  177.     /* byte by byte file transfer, it's slow but easy */
  178.        while(( i = getc(source)) != EOF)
  179.         {
  180.          nc++;
  181.          if(i == '\n')
  182.            nl++;
  183.          if(i == old[count])
  184.            {
  185.              buffer[count++] = i;  /* if we match, save char */
  186.              if(count == lenold)  /* we have a winner, write new */
  187.                { 
  188.                 p = 0; 
  189.                 if(verbose)
  190.                  {
  191.                   printf("Replace %s in line %ld [y/n] ? ",origold,nl);
  192.                   fgets(verbuffer,3,stdin);
  193.                   if(verbuffer[0] != 'y')
  194.                    {
  195.                     while(p != lenold)
  196.                      {
  197.                        putc(old[p],destination);
  198.                        p++;
  199.                      }  
  200.                     count = 0;
  201.                     continue;
  202.                    } 
  203.                   } 
  204.                 while(p != lennew)
  205.                  {
  206.                     putc(new[p],destination);
  207.                   p++;  
  208.                  }
  209.                 nr++; 
  210.                 count = 0; 
  211.                 continue;
  212.                } 
  213.              else    
  214.                continue;
  215.             }   
  216.           else if(count)  /* this char doesn't match but some did */     
  217.             {   
  218.               p = 0;
  219.               while(p < count)
  220.                {
  221.                 putc(old[p++],destination);
  222.                } 
  223.               putc((char)i,destination); 
  224.               count = 0;
  225.               continue;
  226.             }  
  227.            else           /* just a plain old mismatch */ 
  228.              {
  229.                  putc((char)i,destination);
  230.                  continue;    
  231.              }    
  232.         }
  233.      c = fclose(source);
  234.       c = fclose(destination);
  235. }
  236.