home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch16 / find.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-12  |  2.5 KB  |  79 lines

  1. /*
  2.     FIND.C
  3.  
  4.     Searches text stream for a string.
  5.  
  6.     Compile with:  C> cl find.c
  7.  
  8.     Usage is:  C> find "pattern" [<source] [>destination]
  9.  
  10.     Copyright (C) 1988 Ray Duncan
  11. */
  12.  
  13. #include <stdio.h>
  14.  
  15. #define TAB     '\x09'                  /* ASCII tab (^I) */
  16. #define BLANK   '\x20'                  /* ASCII space */
  17. #define TAB_WIDTH 8                     /* columns per tab stop */
  18. #define BUF_SIZE  256
  19.  
  20. static char input[BUF_SIZE];            /* input line buffer */
  21. static char output[BUF_SIZE];           /* output line buffer */
  22. static char pattern[BUF_SIZE];          /* search pattern buffer */
  23.  
  24. void writeline(int, char *);            /* function prototype */
  25.  
  26. main(int argc, char *argv[])
  27. {   
  28.     int line = 0;                       /* initialize line variable */
  29.  
  30.     if(argc < 2)                        /* search pattern supplied? */
  31.     {   
  32.         puts("find: missing pattern");  /* abort if no search pattern */
  33.         exit(1);
  34.     }
  35.  
  36.     strcpy(pattern,argv[1]);            /* save copy of search pattern */
  37.     strupr(pattern);                    /* fold it to uppercase */      
  38.  
  39.     while(gets(input) != NULL)          /* read a line from input */
  40.     {   
  41.         line++;                         /* count lines */
  42.  
  43.         strcpy(output, input);          /* save copy of input string */
  44.         strupr(input);                  /* fold input to uppercase */
  45.  
  46.         if(strstr(input, pattern))      /* if line contains pattern */
  47.             writeline(line, output);    /* write it to standard output */
  48.     }
  49.     exit(0);                            /* terminate at end of file */
  50. }
  51.  
  52. /*
  53.     WRITELINE: Write line number and text to standard output,
  54.     expanding any tab characters to stops defined by TAB_WIDTH.
  55. */
  56.  
  57. void writeline(int line, char *p)
  58. {   
  59.     int i = 0;                          /* index to input line */
  60.     int col = 0;                        /* output column counter */
  61.  
  62.     printf("\n%4d: ", line);            /* write line number */
  63.  
  64.     while(p[i] != NULL)                 /* while not end of line */
  65.     {   
  66.         if(p[i] == TAB)                 /* if tab, expand it */
  67.         {   
  68.             do putchar(BLANK);
  69.             while((++col % TAB_WIDTH) != 0);
  70.         }
  71.         else                            /* otherwise leave it alone */
  72.         {       
  73.             putchar(p[i]);              /* send character */
  74.             col++;                      /* count columns */
  75.         }
  76.         i++;                            /* advance through input */
  77.     }
  78. }
  79.