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

  1.  
  2. /* slice.c 2/17/88 d.wahl, slice selected lines from text file */
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #define MAXFSIZE 40 /* max chars in filename */
  6.   char infile[MAXFSIZE], outfile[MAXFSIZE]; /* global storage */
  7.   FILE *source, *destination;                         /* files */
  8.   char match[4][20];
  9.   int mlines[4];
  10.   int count;
  11. main(argc,argv)
  12. int argc;
  13. char *argv[];
  14. {
  15.     if(argc < 5)
  16.       {
  17.        printf("USAGE: slice infile outfile match #lines [m #] [[m #]] [[[m #]]]");
  18.        exit(0);
  19.      }
  20.     count = ((argc - 5) / 2) + 1;
  21.     if(argc >= 5)
  22.       {
  23.         strcpy(match[0],argv[3]);
  24.         mlines[0] = atoi(argv[4]);
  25.       }
  26.     if(argc >= 7)
  27.       {
  28.         strcpy(match[1],argv[5]);
  29.         mlines[1] = atoi(argv[6]);
  30.       }
  31.     if(argc >= 9)
  32.       {
  33.         strcpy(match[2],argv[7]);
  34.         mlines[2] = atoi(argv[8]);
  35.       }
  36.     if(argc == 11)
  37.       {
  38.         strcpy(match[3],argv[9]);
  39.         mlines[3] = atoi(argv[10]);
  40.       }
  41.  
  42.     open_files(argv[1],argv[2]);
  43.     
  44.     process_file();  
  45.   
  46.  
  47.   }
  48.  
  49.    
  50. open_files(in, out)
  51.  
  52.     char *in, *out;
  53. {
  54.     int ret = 1;
  55.  /* open files */
  56.  
  57.     if ((source = fopen(in, "r")) == NULL)
  58.        {    printf("open source file error\n");
  59.             exit(ret);
  60.        }
  61.     if ((destination = fopen(out, "w")) == NULL)
  62.        {    printf("open destination file error\n");
  63.             fclose(source);
  64.             exit(ret);
  65.        }
  66.  
  67. }
  68.  
  69.  
  70. process_file()
  71.   
  72. {
  73.     char buffer[256],verbuffer[5];
  74.     register int c,i;
  75.     register int p = 0;
  76.     /* read a line at a time */
  77.     while(( i = fgets(buffer,255,source)) != NULL)
  78.      {
  79.         for(c = 0;c < count;c++)     
  80.            {
  81.              if(matchstring(buffer,match[c]))
  82.                 {
  83.                   fputs(buffer,destination);
  84.                   for(p = 0;p < mlines[c];p++)
  85.                      {
  86.                         fgets(buffer,255,source);
  87.                         fputs(buffer,destination);
  88.                       }
  89.                   break;
  90.                 }
  91.            }
  92.       }
  93.   
  94.      c = fclose(source);
  95.       c = fclose(destination);
  96. }
  97.  
  98.  
  99.  
  100.      
  101. /* ********************************************************************** */  
  102.   /* compare 2 strings, return 1 if strings match */
  103.   matchstring(string,match)
  104.     char *string;
  105.     char *match;
  106.     
  107.   {  
  108.    int i,c,x;
  109.    i = strlen(string);
  110.    c = strlen(match);
  111.    x = 0;
  112.    while(x+c <= i)
  113.     {
  114.      if(!strncmp(&string[x],match,c))
  115.       return(1);
  116.      x++; 
  117.     } 
  118.    return(0); 
  119.   }
  120.  
  121.