]> git.tdb.fi Git - libs/core.git/blobdiff - examples/grep.cpp
Put examples in their own directory
[libs/core.git] / examples / grep.cpp
diff --git a/examples/grep.cpp b/examples/grep.cpp
new file mode 100644 (file)
index 0000000..c87ee72
--- /dev/null
@@ -0,0 +1,35 @@
+#include <iostream>
+#include <string>
+#include <msp/core/getopt.h>
+#include <msp/strings/regex.h>
+
+using namespace std;
+using namespace Msp;
+
+int main(int argc, char **argv)
+{
+       bool debug = false;
+       GetOpt getopt;
+       getopt.add_option('d', "debug", debug, GetOpt::NO_ARG);
+       getopt(argc, argv);
+
+       const vector<string> &args = getopt.get_args();
+
+       if(args.empty())
+       {
+               cerr<<"Usage: "<<argv[0]<<" <regex>\n";
+               return 1;
+       }
+
+       Regex regex(args[0]);
+       if(debug)
+               cout<<regex.disassemble();
+       string line;
+       while(getline(cin, line))
+       {
+               if(RegMatch match = regex.match(line))
+                       cout<<line<<'\n';
+       }
+
+       return 0;
+}