File: main.cpp

package info (click to toggle)
boost 1.27.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,908 kB
  • ctags: 26,546
  • sloc: cpp: 122,225; ansic: 10,956; python: 4,412; sh: 855; yacc: 803; makefile: 257; perl: 165; lex: 90; csh: 6
file content (284 lines) | stat: -rw-r--r-- 6,798 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 *
 * Copyright (c) 1998-2000
 * Dr John Maddock
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Dr John Maddock makes no representations
 * about the suitability of this software for any purpose.  
 * It is provided "as is" without express or implied warranty.
 *
 */
 
 /*
  *   FILE     main.cpp
  *   VERSION  see <boost/version.hpp>
  */


#include <stdio.h>
#include <boost/regex.hpp>
#ifdef JM_OLD_IOSTREAM
#include <iostream.h>
#else
#include <iostream>
using std::cout;
using std::cin;
using std::cerr;
using std::endl;
#endif
#pragma hrdstop

#include <boost/regex/detail/fileiter.hpp>
#include "jgrep.h"

#ifndef JM_ALGO_INCLUDED
// HP and SGI STL's use <algo.h> instead
// this will have been pulled in by <jm_cfg.h>
// for std::distance
#include <algorithm>
#endif

allocator_type a;

re_type e(a);
//rei_type ei(a);

// flags for output:

bool use_case = true;
bool show_lines = false;
bool count_only = false;
bool files_only = false;
bool recurse = false;
bool regularexs = true;
bool words_only = false;
bool verbose = false;


void usage()
{
   cout <<
"jgrep version 0.95\n"
"usage: jgrep [-options] expression file [files...]\n"
"\n"
"options can be one of the following:\n"
"\n"
"-c   prints a count of the number of matching lines only\n"
"-d   recurses through subdirectories for matching files\n"
"-i   causes case to be ignored when matching\n"
"-l   lists the files which contain a match only\n"
"-n   displays the line numbers of matches\n"
"-r-  causes the expression to be interpreted as a literal string and not\n"
"     as a regular expression\n"
"-w   searches for matches that are whole words only\n"
"-z   verbose mode\n"
"\n"
"expression: a regular expression, or a literal string if -r- is specified\n"
"\n"
"files:  one or more files to search, the names can contain the wildcard\n"
"        characters ? and *\n" << endl;

}

void parse_switch(const char* flag)
{
   ++flag;
   while(*flag)
   {
      switch(*flag)
      {
         case '-':
            switch(*(flag-1))
            {
               case 'c':
                  count_only = false;
                  break;
               case 'd':
                  recurse = false;
                  break;
               case 'i':
                  use_case = false;
                  break;
               case 'l':
                  files_only = false;
                  break;
               case 'n':
                  show_lines = false;
                  break;
               case 'r':
                  regularexs = false;
                  break;
               case 'w':
                  words_only = false;
                  break;
               case 'z':
                  verbose = false;
                  break;
               default:
                  cout << "Undefined option -";
                  cout.put(*flag);
                  cout << endl;
            }
            // turn off prev character:
            break;
         case 'c':
            count_only = true;
            break;
         case 'd':
            recurse = true;
            break;
         case 'i':
            use_case = false;
            break;
         case 'l':
            files_only = true;
            break;
         case 'n':
            show_lines = true;
            break;
         case 'r':
            regularexs = true;
            break;
         case 'w':
            words_only = true;
            break;
         case 'z':
            verbose = true;
            break;
         case '?':
            usage();
            exit(0);
         case '+':
            break;
         default:
            cout << "Undefined option -";
            cout.put(*flag);
            cout << endl;
      }
      ++flag;
   }
}

void HandleFile(const char* wild)
{
   using namespace boost;
   jm_trace("Handling file " << wild);
   file_iterator end;
   file_iterator start(wild);

   if(recurse)
   {
      // go through sub directories:
      char buf[MAX_PATH];
      std::strcpy(buf, start.root());
      int rootlen = strlen(buf);
      if(*buf == 0)
      {
         std::strcpy(buf, ".");
         std::strcat(buf, directory_iterator::separator());
         std::strcat(buf, "*");
      }
      else
      {
         std::strcat(buf, directory_iterator::separator());
         std::strcat(buf, "*");
      }
      jm_trace("Enumerating directories: " << buf);
      directory_iterator dstart(buf);
      directory_iterator dend;

      // now get the file mask bit of "wild":
      const char* ptr = wild + rootlen;
      if(*ptr) ++ptr;
      jm_trace("File mask part is: " << ptr);

      while(dstart != dend)
      {
         std::sprintf(buf, "%s%s%s", dstart.path(), directory_iterator::separator(), ptr);
         HandleFile(buf);
         ++dstart;
      }
   }
   std::for_each(start, end, process_grep);
}

int done = 0;

void HandleArg(const char* arg)
{
   using namespace boost;
   jm_trace("Handling argument: " << arg);
   if(*arg == '-')
   {
      parse_switch(arg);
      return;
   }
   if(done == 0)
   {
      // parse regular expression
      if(regularexs)
      {
         if(words_only == 0)
         {
            e.set_expression(arg, use_case ? regbase::normal : regbase::normal | regbase::icase);
            //ei.set_expression(arg);
         }
         else
         {
            char* buf = new char[std::strlen(arg) + 8];
            std::sprintf(buf, "\\<%s\\>", arg);
            e.set_expression(buf, use_case ? regbase::normal : regbase::normal | regbase::icase);
            //ei.set_expression(buf);
            delete[] buf;
         }
      }
      else
      {
         // we need to convert text to literal:
         int len2 = std::strlen(arg);
         int len = len2 * 5 + 6;
         char buf[8];
         char* buf2 = new char[len];
         *buf2 = 0;
         if(words_only)
            std::strcpy(buf2, "\\<");
         for(int j = 0; j < len2; ++j)
         {
            std::sprintf(buf, "\\0%o", int(arg[j]));
            std::strcat(buf2, buf);
         }
         if(words_only)
            std::strcat(buf2, "\\>");
         e.set_expression(buf2, use_case ? regbase::normal : regbase::normal | regbase::icase);
         //ei.set_expression(buf2);
         delete[] buf2;
      }
      done = 1;
      return;
   }
   // if we get to here we have one or more file names to process:
   ++done;
   HandleFile(arg);
}

int main(int argc, char * argv[])
{
   for(int i = 1; i < argc; ++i)
      HandleArg(argv[i]);
   if(done < 2)
      usage();
   return 0;
}