home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / REGEX011.TAZ / REGEX011 / regex-0.11 / test / fileregex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-15  |  1.6 KB  |  78 lines

  1. #include <sys/types.h>
  2. #include <stdio.h>
  3. #include "regex.h"
  4.  
  5. #define BYTEWIDTH 8
  6.  
  7. /* Sorry, but this is just a test file.  */
  8. #define LINE_MAX 500
  9.  
  10. int
  11. main (argc, argv)
  12.     int argc;
  13.     char *argv[];
  14. {
  15.   FILE *f;
  16.   char *filename;
  17.   char pat[500]; /* Sorry for that maximum size, too.  */
  18.   char line[LINE_MAX];
  19.   struct re_pattern_buffer buf;
  20.   char fastmap[(1 << BYTEWIDTH)];
  21.   const char *compile_ret;
  22.   unsigned lineno = 1;
  23.   unsigned nfound = 0;
  24.   
  25.   /* Actually, it might be useful to allow the data file to be standard
  26.      input, and to specify the pattern on the command line.  */
  27.   if (argc != 2)
  28.     {
  29.       fprintf (stderr, "Usage: %s <filename>.\n", argv[0]);
  30.       exit (1);
  31.     }
  32.  
  33.   filename = argv[1];
  34.   f = fopen (filename, "r");
  35.   if (f == NULL)
  36.     perror (filename);
  37.  
  38.   buf.allocated = 0;
  39.   buf.buffer = NULL;
  40.   buf.fastmap = fastmap;
  41.  
  42.   printf ("Pattern = ", pat);
  43.   gets (pat);
  44.   
  45.   if (feof (stdin))
  46.     {
  47.       putchar ('\n');
  48.       exit (0);
  49.     }
  50.  
  51.   compile_ret = re_compile_pattern (pat, strlen (pat), &buf);
  52.   if (compile_ret != NULL)
  53.     {
  54.       fprintf (stderr, "%s: %s\n", pat, compile_ret);
  55.       exit (1);
  56.     }
  57.  
  58.   while (fgets (line, LINE_MAX, f) != NULL)
  59.     {
  60.       struct re_registers regs;
  61.       size_t len = strlen (line);
  62.       int search_ret
  63.         = re_search_2 (&buf, NULL, 0, line, len, 0, len, ®s, len);
  64.       
  65.       if (search_ret == -2)
  66.         {
  67.           fprintf (stderr, "%s:%d: re_search failed.\n", filename, lineno);
  68.           exit (1);
  69.         }
  70.       
  71.       nfound += search_ret != -1;
  72.       lineno++;
  73.     }
  74.  
  75.   printf ("Matches found: %u (out of %u lines).\n", nfound, lineno - 1);
  76.   return 0;
  77. }
  78.