1 | /* The copyright in this software is being made available under the BSD |
---|
2 | * License, included below. This software may be subject to other third party |
---|
3 | * and contributor rights, including patent rights, and no such rights are |
---|
4 | * granted under this license. |
---|
5 | * |
---|
6 | * Copyright (c) 2010-2013, ITU/ISO/IEC |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions are met: |
---|
11 | * |
---|
12 | * * Redistributions of source code must retain the above copyright notice, |
---|
13 | * this list of conditions and the following disclaimer. |
---|
14 | * * Redistributions in binary form must reproduce the above copyright notice, |
---|
15 | * this list of conditions and the following disclaimer in the documentation |
---|
16 | * and/or other materials provided with the distribution. |
---|
17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
---|
18 | * be used to endorse or promote products derived from this software without |
---|
19 | * specific prior written permission. |
---|
20 | * |
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
---|
25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
31 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | */ |
---|
33 | #include <iostream> |
---|
34 | #include <sstream> |
---|
35 | #include <string> |
---|
36 | #include <list> |
---|
37 | #include <map> |
---|
38 | |
---|
39 | #if H_MV |
---|
40 | #include <vector> |
---|
41 | #include <errno.h> |
---|
42 | #include <cstring> |
---|
43 | |
---|
44 | #ifdef WIN32 |
---|
45 | #define strdup _strdup |
---|
46 | #endif |
---|
47 | #endif |
---|
48 | //! \ingroup TAppCommon |
---|
49 | //! \{ |
---|
50 | |
---|
51 | |
---|
52 | namespace df |
---|
53 | { |
---|
54 | namespace program_options_lite |
---|
55 | { |
---|
56 | struct Options; |
---|
57 | |
---|
58 | struct ParseFailure : public std::exception |
---|
59 | { |
---|
60 | ParseFailure(std::string arg0, std::string val0) throw() |
---|
61 | : arg(arg0), val(val0) |
---|
62 | {} |
---|
63 | |
---|
64 | ~ParseFailure() throw() {}; |
---|
65 | |
---|
66 | std::string arg; |
---|
67 | std::string val; |
---|
68 | |
---|
69 | const char* what() const throw() { return "Option Parse Failure"; } |
---|
70 | }; |
---|
71 | |
---|
72 | void doHelp(std::ostream& out, Options& opts, unsigned columns = 80); |
---|
73 | unsigned parseGNU(Options& opts, unsigned argc, const char* argv[]); |
---|
74 | unsigned parseSHORT(Options& opts, unsigned argc, const char* argv[]); |
---|
75 | std::list<const char*> scanArgv(Options& opts, unsigned argc, const char* argv[]); |
---|
76 | void scanLine(Options& opts, std::string& line); |
---|
77 | void scanFile(Options& opts, std::istream& in); |
---|
78 | void setDefaults(Options& opts); |
---|
79 | void parseConfigFile(Options& opts, const std::string& filename); |
---|
80 | bool storePair(Options& opts, const std::string& name, const std::string& value); |
---|
81 | |
---|
82 | /** OptionBase: Virtual base class for storing information relating to a |
---|
83 | * specific option This base class describes common elements. Type specific |
---|
84 | * information should be stored in a derived class. */ |
---|
85 | struct OptionBase |
---|
86 | { |
---|
87 | OptionBase(const std::string& name, const std::string& desc) |
---|
88 | : opt_string(name), opt_desc(desc) |
---|
89 | {}; |
---|
90 | |
---|
91 | virtual ~OptionBase() {} |
---|
92 | |
---|
93 | /* parse argument arg, to obtain a value for the option */ |
---|
94 | virtual void parse(const std::string& arg) = 0; |
---|
95 | /* set the argument to the default value */ |
---|
96 | virtual void setDefault() = 0; |
---|
97 | |
---|
98 | std::string opt_string; |
---|
99 | std::string opt_desc; |
---|
100 | }; |
---|
101 | |
---|
102 | /** Type specific option storage */ |
---|
103 | template<typename T> |
---|
104 | struct Option : public OptionBase |
---|
105 | { |
---|
106 | Option(const std::string& name, T& storage, T default_val, const std::string& desc) |
---|
107 | : OptionBase(name, desc), opt_storage(storage), opt_default_val(default_val) |
---|
108 | {} |
---|
109 | |
---|
110 | void parse(const std::string& arg); |
---|
111 | |
---|
112 | void setDefault() |
---|
113 | { |
---|
114 | opt_storage = opt_default_val; |
---|
115 | } |
---|
116 | |
---|
117 | T& opt_storage; |
---|
118 | T opt_default_val; |
---|
119 | }; |
---|
120 | |
---|
121 | /* Generic parsing */ |
---|
122 | template<typename T> |
---|
123 | inline void |
---|
124 | Option<T>::parse(const std::string& arg) |
---|
125 | { |
---|
126 | std::istringstream arg_ss (arg,std::istringstream::in); |
---|
127 | arg_ss.exceptions(std::ios::failbit); |
---|
128 | try |
---|
129 | { |
---|
130 | arg_ss >> opt_storage; |
---|
131 | } |
---|
132 | catch (...) |
---|
133 | { |
---|
134 | throw ParseFailure(opt_string, arg); |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | /* string parsing is specialized -- copy the whole string, not just the |
---|
139 | * first word */ |
---|
140 | template<> |
---|
141 | inline void |
---|
142 | Option<std::string>::parse(const std::string& arg) |
---|
143 | { |
---|
144 | opt_storage = arg; |
---|
145 | } |
---|
146 | |
---|
147 | #if H_MV |
---|
148 | template<> |
---|
149 | inline void |
---|
150 | Option<char*>::parse(const std::string& arg) |
---|
151 | { |
---|
152 | opt_storage = arg.empty() ? NULL : strdup(arg.c_str()) ; |
---|
153 | } |
---|
154 | |
---|
155 | template<> |
---|
156 | inline void |
---|
157 | Option< std::vector<char*> >::parse(const std::string& arg) |
---|
158 | { |
---|
159 | opt_storage.clear(); |
---|
160 | |
---|
161 | char* pcStart = (char*) arg.data(); |
---|
162 | char* pcEnd = strtok (pcStart," "); |
---|
163 | |
---|
164 | while (pcEnd != NULL) |
---|
165 | { |
---|
166 | size_t uiStringLength = pcEnd - pcStart; |
---|
167 | char* pcNewStr = (char*) malloc( uiStringLength + 1 ); |
---|
168 | strncpy( pcNewStr, pcStart, uiStringLength); |
---|
169 | pcNewStr[uiStringLength] = '\0'; |
---|
170 | pcStart = pcEnd+1; |
---|
171 | pcEnd = strtok (NULL, " ,.-"); |
---|
172 | opt_storage.push_back( pcNewStr ); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | template<> |
---|
178 | inline void |
---|
179 | Option< std::vector<double> >::parse(const std::string& arg) |
---|
180 | { |
---|
181 | char* pcNextStart = (char*) arg.data(); |
---|
182 | char* pcEnd = pcNextStart + arg.length(); |
---|
183 | |
---|
184 | char* pcOldStart = 0; |
---|
185 | |
---|
186 | size_t iIdx = 0; |
---|
187 | |
---|
188 | while (pcNextStart < pcEnd) |
---|
189 | { |
---|
190 | errno = 0; |
---|
191 | |
---|
192 | if ( iIdx < opt_storage.size() ) |
---|
193 | { |
---|
194 | opt_storage[iIdx] = strtod(pcNextStart, &pcNextStart); |
---|
195 | } |
---|
196 | else |
---|
197 | { |
---|
198 | opt_storage.push_back( strtod(pcNextStart, &pcNextStart)) ; |
---|
199 | } |
---|
200 | iIdx++; |
---|
201 | |
---|
202 | if ( errno == ERANGE || (pcNextStart == pcOldStart) ) |
---|
203 | { |
---|
204 | std::cerr << "Error Parsing Doubles: `" << arg << "'" << std::endl; |
---|
205 | exit(EXIT_FAILURE); |
---|
206 | }; |
---|
207 | while( (pcNextStart < pcEnd) && ( *pcNextStart == ' ' || *pcNextStart == '\t' || *pcNextStart == '\r' ) ) pcNextStart++; |
---|
208 | pcOldStart = pcNextStart; |
---|
209 | |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | template<> |
---|
214 | inline void |
---|
215 | Option< std::vector<int> >::parse(const std::string& arg) |
---|
216 | { |
---|
217 | opt_storage.clear(); |
---|
218 | |
---|
219 | |
---|
220 | char* pcNextStart = (char*) arg.data(); |
---|
221 | char* pcEnd = pcNextStart + arg.length(); |
---|
222 | |
---|
223 | char* pcOldStart = 0; |
---|
224 | |
---|
225 | size_t iIdx = 0; |
---|
226 | |
---|
227 | |
---|
228 | while (pcNextStart < pcEnd) |
---|
229 | { |
---|
230 | |
---|
231 | if ( iIdx < opt_storage.size() ) |
---|
232 | { |
---|
233 | opt_storage[iIdx] = (int) strtol(pcNextStart, &pcNextStart,10); |
---|
234 | } |
---|
235 | else |
---|
236 | { |
---|
237 | opt_storage.push_back( (int) strtol(pcNextStart, &pcNextStart,10)) ; |
---|
238 | } |
---|
239 | iIdx++; |
---|
240 | if ( errno == ERANGE || (pcNextStart == pcOldStart) ) |
---|
241 | { |
---|
242 | std::cerr << "Error Parsing Integers: `" << arg << "'" << std::endl; |
---|
243 | exit(EXIT_FAILURE); |
---|
244 | }; |
---|
245 | while( (pcNextStart < pcEnd) && ( *pcNextStart == ' ' || *pcNextStart == '\t' || *pcNextStart == '\r' ) ) pcNextStart++; |
---|
246 | pcOldStart = pcNextStart; |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | template<> |
---|
252 | inline void |
---|
253 | Option< std::vector<bool> >::parse(const std::string& arg) |
---|
254 | { |
---|
255 | char* pcNextStart = (char*) arg.data(); |
---|
256 | char* pcEnd = pcNextStart + arg.length(); |
---|
257 | |
---|
258 | char* pcOldStart = 0; |
---|
259 | |
---|
260 | size_t iIdx = 0; |
---|
261 | |
---|
262 | while (pcNextStart < pcEnd) |
---|
263 | { |
---|
264 | if ( iIdx < opt_storage.size() ) |
---|
265 | { |
---|
266 | opt_storage[iIdx] = (strtol(pcNextStart, &pcNextStart,10) != 0); |
---|
267 | } |
---|
268 | else |
---|
269 | { |
---|
270 | opt_storage.push_back(strtol(pcNextStart, &pcNextStart,10) != 0) ; |
---|
271 | } |
---|
272 | iIdx++; |
---|
273 | |
---|
274 | if ( errno == ERANGE || (pcNextStart == pcOldStart) ) |
---|
275 | { |
---|
276 | std::cerr << "Error Parsing Bools: `" << arg << "'" << std::endl; |
---|
277 | exit(EXIT_FAILURE); |
---|
278 | }; |
---|
279 | while( (pcNextStart < pcEnd) && ( *pcNextStart == ' ' || *pcNextStart == '\t' || *pcNextStart == '\r' ) ) pcNextStart++; |
---|
280 | pcOldStart = pcNextStart; |
---|
281 | } |
---|
282 | } |
---|
283 | #endif |
---|
284 | /** Option class for argument handling using a user provided function */ |
---|
285 | struct OptionFunc : public OptionBase |
---|
286 | { |
---|
287 | typedef void (Func)(Options&, const std::string&); |
---|
288 | |
---|
289 | OptionFunc(const std::string& name, Options& parent_, Func *func_, const std::string& desc) |
---|
290 | : OptionBase(name, desc), parent(parent_), func(func_) |
---|
291 | {} |
---|
292 | |
---|
293 | void parse(const std::string& arg) |
---|
294 | { |
---|
295 | func(parent, arg); |
---|
296 | } |
---|
297 | |
---|
298 | void setDefault() |
---|
299 | { |
---|
300 | return; |
---|
301 | } |
---|
302 | |
---|
303 | private: |
---|
304 | Options& parent; |
---|
305 | void (*func)(Options&, const std::string&); |
---|
306 | }; |
---|
307 | |
---|
308 | class OptionSpecific; |
---|
309 | struct Options |
---|
310 | { |
---|
311 | ~Options(); |
---|
312 | |
---|
313 | OptionSpecific addOptions(); |
---|
314 | |
---|
315 | struct Names |
---|
316 | { |
---|
317 | Names() : opt(0) {}; |
---|
318 | ~Names() |
---|
319 | { |
---|
320 | if (opt) |
---|
321 | delete opt; |
---|
322 | } |
---|
323 | std::list<std::string> opt_long; |
---|
324 | std::list<std::string> opt_short; |
---|
325 | OptionBase* opt; |
---|
326 | }; |
---|
327 | |
---|
328 | void addOption(OptionBase *opt); |
---|
329 | |
---|
330 | typedef std::list<Names*> NamesPtrList; |
---|
331 | NamesPtrList opt_list; |
---|
332 | |
---|
333 | typedef std::map<std::string, NamesPtrList> NamesMap; |
---|
334 | NamesMap opt_long_map; |
---|
335 | NamesMap opt_short_map; |
---|
336 | }; |
---|
337 | |
---|
338 | /* Class with templated overloaded operator(), for use by Options::addOptions() */ |
---|
339 | class OptionSpecific |
---|
340 | { |
---|
341 | public: |
---|
342 | OptionSpecific(Options& parent_) : parent(parent_) {} |
---|
343 | |
---|
344 | /** |
---|
345 | * Add option described by name to the parent Options list, |
---|
346 | * with storage for the option's value |
---|
347 | * with default_val as the default value |
---|
348 | * with desc as an optional help description |
---|
349 | */ |
---|
350 | template<typename T> |
---|
351 | OptionSpecific& |
---|
352 | operator()(const std::string& name, T& storage, T default_val, const std::string& desc = "") |
---|
353 | { |
---|
354 | parent.addOption(new Option<T>(name, storage, default_val, desc)); |
---|
355 | return *this; |
---|
356 | } |
---|
357 | |
---|
358 | #if H_MV |
---|
359 | template<typename T> |
---|
360 | OptionSpecific& |
---|
361 | operator()(const std::string& name, std::vector<T>& storage, T default_val, unsigned uiMaxNum, const std::string& desc = "" ) |
---|
362 | { |
---|
363 | std::string cNameBuffer; |
---|
364 | std::string cDescriptionBuffer; |
---|
365 | |
---|
366 | cNameBuffer .resize( name.size() + 10 ); |
---|
367 | cDescriptionBuffer.resize( desc.size() + 10 ); |
---|
368 | |
---|
369 | storage.resize(uiMaxNum); |
---|
370 | for ( unsigned int uiK = 0; uiK < uiMaxNum; uiK++ ) |
---|
371 | { |
---|
372 | // isn't there are sprintf function for string?? |
---|
373 | sprintf((char*) cNameBuffer.c_str() ,name.c_str(),uiK,uiK); |
---|
374 | sprintf((char*) cDescriptionBuffer.c_str(),desc.c_str(),uiK,uiK); |
---|
375 | |
---|
376 | parent.addOption(new Option<T>( cNameBuffer, (storage[uiK]), default_val, cDescriptionBuffer )); |
---|
377 | } |
---|
378 | |
---|
379 | return *this; |
---|
380 | } |
---|
381 | #endif |
---|
382 | /** |
---|
383 | * Add option described by name to the parent Options list, |
---|
384 | * with desc as an optional help description |
---|
385 | * instead of storing the value somewhere, a function of type |
---|
386 | * OptionFunc::Func is called. It is upto this function to correctly |
---|
387 | * handle evaluating the option's value. |
---|
388 | */ |
---|
389 | OptionSpecific& |
---|
390 | operator()(const std::string& name, OptionFunc::Func *func, const std::string& desc = "") |
---|
391 | { |
---|
392 | parent.addOption(new OptionFunc(name, parent, func, desc)); |
---|
393 | return *this; |
---|
394 | } |
---|
395 | private: |
---|
396 | Options& parent; |
---|
397 | }; |
---|
398 | |
---|
399 | }; /* namespace: program_options_lite */ |
---|
400 | }; /* namespace: df */ |
---|
401 | |
---|
402 | //! \} |
---|