gcc config
[prop.git] / lib-src / strings / bmgrep.cc
blobf9d9da4675df49f2d20475f66b160a72b2337e73
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
9 // your programs.
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
16 // code.
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
21 // Allen Leung
22 // 1994
23 //////////////////////////////////////////////////////////////////////////////
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <AD/strings/bm.h>
29 int grep(const char * fileName,FILE * file,BoyerMoore& pattern)
31 char buffer[BUFSIZ];
32 int notMatched = 1;
33 int line = 1;
34 while (fgets(buffer,BUFSIZ,file)) {
35 if (pattern.match(buffer) >= 0)
36 if (fileName)
37 printf("%s[%d]: %s", fileName, line, buffer);
38 else
39 printf("[%d]: %s", line, buffer);
40 line++;
42 return notMatched;
45 int main(int argc, char * argv[])
47 if (argc == 1) {
48 fprintf(stderr,"usage: %s pattern [files ...]\n",argv[0]);
49 return 0;
51 BoyerMoore pattern = argv[1];
52 int failure = 0;
53 if (argc == 2) {
54 failure += grep("",stdin,pattern);
55 } else {
56 for (int i = 2; i < argc; i++) {
57 FILE * file = fopen(argv[i],"r");
58 if (file == NULL) { perror(argv[i]); exit(1); }
59 failure += grep(argv[i],file,pattern);
60 fclose(file);
63 return failure;