home *** CD-ROM | disk | FTP | other *** search
-
- /* slice.c 2/17/88 d.wahl, slice selected lines from text file */
- #include <ctype.h>
- #include <stdio.h>
- #define MAXFSIZE 40 /* max chars in filename */
- char infile[MAXFSIZE], outfile[MAXFSIZE]; /* global storage */
- FILE *source, *destination; /* files */
- char match[4][20];
- int mlines[4];
- int count;
- main(argc,argv)
- int argc;
- char *argv[];
- {
- if(argc < 5)
- {
- printf("USAGE: slice infile outfile match #lines [m #] [[m #]] [[[m #]]]");
- exit(0);
- }
- count = ((argc - 5) / 2) + 1;
- if(argc >= 5)
- {
- strcpy(match[0],argv[3]);
- mlines[0] = atoi(argv[4]);
- }
- if(argc >= 7)
- {
- strcpy(match[1],argv[5]);
- mlines[1] = atoi(argv[6]);
- }
- if(argc >= 9)
- {
- strcpy(match[2],argv[7]);
- mlines[2] = atoi(argv[8]);
- }
- if(argc == 11)
- {
- strcpy(match[3],argv[9]);
- mlines[3] = atoi(argv[10]);
- }
-
- open_files(argv[1],argv[2]);
-
- process_file();
-
-
- }
-
-
- open_files(in, out)
-
- char *in, *out;
- {
- int ret = 1;
- /* open files */
-
- if ((source = fopen(in, "r")) == NULL)
- { printf("open source file error\n");
- exit(ret);
- }
- if ((destination = fopen(out, "w")) == NULL)
- { printf("open destination file error\n");
- fclose(source);
- exit(ret);
- }
-
- }
-
-
- process_file()
-
- {
- char buffer[256],verbuffer[5];
- register int c,i;
- register int p = 0;
- /* read a line at a time */
- while(( i = fgets(buffer,255,source)) != NULL)
- {
- for(c = 0;c < count;c++)
- {
- if(matchstring(buffer,match[c]))
- {
- fputs(buffer,destination);
- for(p = 0;p < mlines[c];p++)
- {
- fgets(buffer,255,source);
- fputs(buffer,destination);
- }
- break;
- }
- }
- }
-
- c = fclose(source);
- c = fclose(destination);
- }
-
-
-
-
- /* ********************************************************************** */
- /* compare 2 strings, return 1 if strings match */
- matchstring(string,match)
- char *string;
- char *match;
-
- {
- int i,c,x;
- i = strlen(string);
- c = strlen(match);
- x = 0;
- while(x+c <= i)
- {
- if(!strncmp(&string[x],match,c))
- return(1);
- x++;
- }
- return(0);
- }
-
-