#ifndef ARG_PARSER_H #define ARG_PARSER_H #include #include #include #include #include #include #include // for basic parsing // and for formatting error msgs #include // 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::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 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 > 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 T nextArg(); template T nextRaw(); // see if cur arg is a prefix of patt */ int prefix(const std::string& patt); template arg_parser& operator>>(T& val); std::string xcase(const std::string& arg); }; template struct myTraits {static const char* name;}; #define arg_parser_h #endif