]> git.tdb.fi Git - libs/core.git/blob - examples/grep.cpp
Use string::size_type rather than unsigned to store string offsets
[libs/core.git] / examples / grep.cpp
1 #include <iostream>
2 #include <string>
3 #include <msp/core/getopt.h>
4 #include <msp/strings/regex.h>
5
6 using namespace std;
7 using namespace Msp;
8
9 int main(int argc, char **argv)
10 {
11         bool debug = false;
12         GetOpt getopt;
13         getopt.add_option('d', "debug", debug, GetOpt::NO_ARG);
14         getopt(argc, argv);
15
16         const vector<string> &args = getopt.get_args();
17
18         if(args.empty())
19         {
20                 cerr<<"Usage: "<<argv[0]<<" <regex>\n";
21                 return 1;
22         }
23
24         Regex regex(args[0]);
25         if(debug)
26                 cout<<regex.disassemble();
27         string line;
28         while(getline(cin, line))
29         {
30                 if(RegMatch match = regex.match(line))
31                         cout<<line<<'\n';
32         }
33
34         return 0;
35 }