aboutsummaryrefslogtreecommitdiff
path: root/arg_parser.h
diff options
context:
space:
mode:
Diffstat (limited to 'arg_parser.h')
-rw-r--r--arg_parser.h95
1 files changed, 95 insertions, 0 deletions
diff --git a/arg_parser.h b/arg_parser.h
new file mode 100644
index 0000000..2ee69c6
--- /dev/null
+++ b/arg_parser.h
@@ -0,0 +1,95 @@
+#ifndef ARG_PARSER_H
+#define ARG_PARSER_H
+
+#include <iostream>
+#include <memory>
+#include <string>
+#include <list>
+#include <vector>
+#include <fstream>
+#include <sstream> // for basic parsing
+ // and for formatting error msgs
+#include <limits> // for ::infinity()
+
+// TODO: use "some_sstream >> some_int" instead of Atoi
+// since that allows more error checking
+
+#ifndef HAVE_INF
+double const Inf = std::numeric_limits<double>::infinity();
+#define HAVE_INF
+#endif
+
+std::string tolower(const std::string& arg);
+
+// This is the interfact that ordinary users use;
+// implementation in arg_parser.c is slightly tricky,
+// but users never see that.
+template<typename T> T Atox(std::string const &foo);
+
+// The base class:
+class baseGetter{
+public:
+ virtual std::string get()=0;
+ virtual int fail()=0;
+ virtual ~baseGetter(){
+//---- cerr << "dtor: baseGetter" << std::endl;
+ };
+ baseGetter(){
+//---- cerr << "basic ctor: baseGetter" << std::endl;
+ }
+};
+
+// A Getter that reads words from the command line:
+class cmdGetter : public baseGetter{
+public:
+ int argc;
+ char const * const * argv;
+ int wasgood;
+
+//ordinary constructor:
+ cmdGetter(int const _argc, char const * const * _argv);
+ virtual std::string get();
+ virtual int fail();
+};
+
+// A Getter that reads words from a file:
+class fileGetter : public baseGetter{
+public:
+ std::fstream file;
+
+//ordinary constructor:
+ fileGetter(const std::string fname);
+ virtual std::string get();
+ virtual int fail();
+ void dummy() {
+ if (Inf);
+ }
+};
+
+class arg_parser{
+public:
+ std::list <std::shared_ptr<baseGetter> > getsome;
+ std::string cur;
+ int fold_case;
+ std::string keyword;
+ int narg; // number of args picked up using >> after this keyword
+
+// Constructor, for reading words from command line:
+ arg_parser(int const _argc, char const * const * _argv);
+// Constructor, for reading words from file:
+ arg_parser(const std::string fname);
+ void push(const std::string fname);
+ int fail();
+ template<class T=std::string> T nextArg();
+ template<class T=std::string> T nextRaw();
+
+// see if cur arg is a prefix of patt */
+ int prefix(const std::string& patt);
+ template <typename T> arg_parser& operator>>(T& val);
+ std::string xcase(const std::string& arg);
+};
+
+template<typename T> struct myTraits {static const char* name;};
+
+#define arg_parser_h
+#endif