aboutsummaryrefslogtreecommitdiff
path: root/arg_parser.h
blob: 2ee69c63e31de1126ab7f4c0ea3f685fea7ccabd (plain)
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
#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