rasdaman complete source
cmlparser.hh
Go to the documentation of this file.
1 /*
2 * This file is part of rasdaman community.
3 *
4 * Rasdaman community is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * Rasdaman community is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with rasdaman community. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
18 rasdaman GmbH.
19 *
20 * For more information please see <http://www.rasdaman.org>
21 * or contact Peter Baumann via <baumann@rasdaman.com>.
22 /
43 #ifndef AK_CMLPARSER_HH
44 #define AK_CMLPARSER_HH
45 
46 
47 
48 #include <list>
49 #include <string>
50 #include <exception>
51 #include <iostream>
52 
53 using std::string;
54 using std::list;
55 using std::ostream;
56 using std::cout;
57 
58 // Command Line Parser version
59 extern const char* CommandLineParserVersion;
60 
61 // specific errors thrown by the parser
62 class CmlException : public std::exception
63 {
64 public:
65  explicit CmlException(const string& whatString);
66  virtual ~CmlException() throw();
67  virtual const char* what() const throw();
68 
69 protected:
70  string problem;
71 };
72 
73 
74 class CommandLineParameter
75 {
76 public:
77 
78  static const char* defaultTitle;
79  static const char* descSep;
80  static const char* descTab;
81  static const char* descIndent;
82  static const char* descLineSep;
83  static const char descOpen;
84  static const char descClose;
85  static const char* descLeftDefault;
86  static const char* descRightDefault;
87 
88  virtual ~CommandLineParameter();
89 
90  //interface for Parser
91  void setDescription(const char*);
92 
93  bool doesMatch(char c);
94  bool doesMatch(const char* s);
95 
96  char getShortName() const;
97  const char* getLongName() const;
98 
99  virtual bool setPresent(char c) throw(CmlException) = 0;
100  virtual bool setPresent(const char* s) throw(CmlException) = 0;
101 
102  virtual bool needsValue() = 0;
103  virtual bool takeValue(const char* s) = 0;
104  virtual void popValue() = 0;
105 
106  void virtual reset();
107  const char *calledName();
108 
109  // has a (at least one) value been assigned?
110  virtual bool isPresent() = 0;
111 
112  virtual const char* getValueAsString() throw(CmlException) = 0;
113  virtual long getValueAsLong() throw(CmlException) = 0;
114  virtual double getValueAsDouble() throw(CmlException) = 0;
115 
116  virtual ostream& printStatus(ostream& = cout) = 0;
117  ostream& printHelp(ostream& = cout);
118 
119 protected:
120  CommandLineParameter(char newShortName, const char* newLongName, const char* newDefaultValue) throw(CmlException);
121  CommandLineParameter(char newShortName, const char* newLongName, long newDefaultValue) throw(CmlException);
122 
123 protected:
124 
125  char shortName;
126  char* longName;
127  bool present;
128  bool wasLongName;
129 
130  char* defaultValue;
131  char shNameString[2];
132 
133  char *descriptionText;
134  char *paramDescription;
135 };
136 
137 
138 class FlagParameter: public CommandLineParameter
139 {
140 public:
141  FlagParameter(char nShortName, const char*nLongName) throw(CmlException);
142 
143  bool setPresent(char c) throw(CmlException);
144  bool setPresent(const char* s) throw(CmlException);
145  bool isPresent();
146 
147  bool needsValue();
148  bool takeValue(const char* s);
149  void popValue();
150 
151  const char* getValueAsString() throw(CmlException);
152  long getValueAsLong() throw(CmlException);
153  double getValueAsDouble() throw(CmlException);
154 
155  ostream& printStatus(ostream& = cout);
156 };
157 
158 class StringParameter: public CommandLineParameter
159 {
160 private:
161  list<char*> value;
162 
163 public:
164  StringParameter(char nShortName, const char* nLongName, const char *newDefaultValue = NULL) throw(CmlException);
165  StringParameter(char nShortName, const char* nLongName, long newDefaultValue = 0L) throw(CmlException);
166  ~StringParameter();
167 
168  bool setPresent(char c) throw(CmlException);
169  bool setPresent(const char* s) throw(CmlException);
170  bool isPresent();
171 
172  bool needsValue();
173  bool takeValue(const char* s);
174  void popValue();
175 
176  const char* getValueAsString() throw(CmlException);
177  long getValueAsLong() throw(CmlException);
178  double getValueAsDouble() throw(CmlException);
179 
180  void reset();
181 
182  ostream& printStatus(ostream& = cout);
183 
184 };
185 
186 
187 class CommandLineParser
188 {
189 public:
190  static const char noShortName;
191  static const char* noLongName;
192  static const char* ShortSign;
193  static const char* LongSign;
194 
195 
196  static CommandLineParser& getInstance();
197 
198  ~CommandLineParser();
199 
200  /*
201  These functions take a parameter called description. This is a string used in printHelp
202  The format of this string has to be:
203  <name of parameter> line1\n\t\tline2...\n\t\tlineN
204  brackets<> and space after are mandatory if there is a parameter!
205  Otherwise no <>!
206  */
207  CommandLineParameter& addFlagParameter(char shortName, const char* longName, const char* description) throw(CmlException);
208  CommandLineParameter& addStringParameter(char shortName, const char* longName, const char* description, const char *newDefaultValue = NULL) throw(CmlException);
209  CommandLineParameter& addLongParameter(char shortName, const char* longName, const char* description, long newDefaultValue = 0L ) throw(CmlException);
210 
211  bool isPresent(char shortName) throw(CmlException);
212  bool isPresent(const char* longName) throw(CmlException);
213 
214  const char* getValueAsString(char shortName) throw(CmlException);
215  long getValueAsLong(char shortName) throw(CmlException);
216  double getValueAsDouble(char shortName) throw(CmlException);
217 
218  const char* getValueAsString(const char* longName) throw(CmlException);
219  long getValueAsLong(const char* longName) throw(CmlException);
220  double getValueAsDouble(const char* longName) throw(CmlException);
221 
222  void processCommandLine(int argc, char** argv) throw(CmlException);
223 
224  bool testProcessCommandLine(const char* test_cml);
225 
226  void printHelp();
227  void printStatus();
228 
229 private:
230  static CommandLineParser* myself;
231 
232  list<CommandLineParameter*> cmlParameter;
233 
234  CommandLineParameter *lastParameter;
235  bool nextTokenIsValue;
236 
237  CommandLineParser();
238 
239  CommandLineParameter& getParameter(char shortName) throw(CmlException);
240  CommandLineParameter& getParameter(const char* longName) throw(CmlException);
241 
242  void setValue(const char* value) throw(CmlException);
243 
244  void longNameParameter(const char* nextToken) throw(CmlException);
245 
246  void shortNameParameter(const char* nextToken) throw(CmlException);
247 };
248 
249 #endif