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 | #include "../TLibCommon/TypeDef.h" |
---|
39 | |
---|
40 | #if H_MV |
---|
41 | #include <vector> |
---|
42 | #include <errno.h> |
---|
43 | #include <cstring> |
---|
44 | |
---|
45 | #ifdef WIN32 |
---|
46 | #define strdup _strdup |
---|
47 | #endif |
---|
48 | #endif |
---|
49 | //! \ingroup TAppCommon |
---|
50 | //! \{ |
---|
51 | |
---|
52 | |
---|
53 | namespace df |
---|
54 | { |
---|
55 | namespace program_options_lite |
---|
56 | { |
---|
57 | struct Options; |
---|
58 | |
---|
59 | struct ParseFailure : public std::exception |
---|
60 | { |
---|
61 | ParseFailure(std::string arg0, std::string val0) throw() |
---|
62 | : arg(arg0), val(val0) |
---|
63 | {} |
---|
64 | |
---|
65 | ~ParseFailure() throw() {}; |
---|
66 | |
---|
67 | std::string arg; |
---|
68 | std::string val; |
---|
69 | |
---|
70 | const char* what() const throw() { return "Option Parse Failure"; } |
---|
71 | }; |
---|
72 | |
---|
73 | void doHelp(std::ostream& out, Options& opts, unsigned columns = 80); |
---|
74 | unsigned parseGNU(Options& opts, unsigned argc, const char* argv[]); |
---|
75 | unsigned parseSHORT(Options& opts, unsigned argc, const char* argv[]); |
---|
76 | std::list<const char*> scanArgv(Options& opts, unsigned argc, const char* argv[]); |
---|
77 | void scanLine(Options& opts, std::string& line); |
---|
78 | void scanFile(Options& opts, std::istream& in); |
---|
79 | void setDefaults(Options& opts); |
---|
80 | void parseConfigFile(Options& opts, const std::string& filename); |
---|
81 | bool storePair(Options& opts, const std::string& name, const std::string& value); |
---|
82 | |
---|
83 | /** OptionBase: Virtual base class for storing information relating to a |
---|
84 | * specific option This base class describes common elements. Type specific |
---|
85 | * information should be stored in a derived class. */ |
---|
86 | struct OptionBase |
---|
87 | { |
---|
88 | #if H_MV |
---|
89 | OptionBase(const std::string& name, const std::string& desc, bool duplicate = false) |
---|
90 | : opt_string(name), opt_desc(desc), opt_duplicate(duplicate) |
---|
91 | #else |
---|
92 | OptionBase(const std::string& name, const std::string& desc) |
---|
93 | : opt_string(name), opt_desc(desc) |
---|
94 | #endif |
---|
95 | {}; |
---|
96 | |
---|
97 | virtual ~OptionBase() {} |
---|
98 | |
---|
99 | /* parse argument arg, to obtain a value for the option */ |
---|
100 | virtual void parse(const std::string& arg) = 0; |
---|
101 | /* set the argument to the default value */ |
---|
102 | virtual void setDefault() = 0; |
---|
103 | |
---|
104 | std::string opt_string; |
---|
105 | std::string opt_desc; |
---|
106 | #if H_MV |
---|
107 | bool opt_duplicate; |
---|
108 | #endif |
---|
109 | }; |
---|
110 | |
---|
111 | /** Type specific option storage */ |
---|
112 | template<typename T> |
---|
113 | struct Option : public OptionBase |
---|
114 | { |
---|
115 | #if H_MV |
---|
116 | Option(const std::string& name, T& storage, T default_val, const std::string& desc, bool duplicate = false) |
---|
117 | : OptionBase(name, desc, duplicate), opt_storage(storage), opt_default_val(default_val) |
---|
118 | #else |
---|
119 | Option(const std::string& name, T& storage, T default_val, const std::string& desc) |
---|
120 | : OptionBase(name, desc), opt_storage(storage), opt_default_val(default_val) |
---|
121 | #endif |
---|
122 | {} |
---|
123 | |
---|
124 | void parse(const std::string& arg); |
---|
125 | |
---|
126 | void setDefault() |
---|
127 | { |
---|
128 | opt_storage = opt_default_val; |
---|
129 | } |
---|
130 | |
---|
131 | T& opt_storage; |
---|
132 | T opt_default_val; |
---|
133 | }; |
---|
134 | |
---|
135 | /* Generic parsing */ |
---|
136 | template<typename T> |
---|
137 | inline void |
---|
138 | Option<T>::parse(const std::string& arg) |
---|
139 | { |
---|
140 | std::istringstream arg_ss (arg,std::istringstream::in); |
---|
141 | arg_ss.exceptions(std::ios::failbit); |
---|
142 | try |
---|
143 | { |
---|
144 | arg_ss >> opt_storage; |
---|
145 | } |
---|
146 | catch (...) |
---|
147 | { |
---|
148 | throw ParseFailure(opt_string, arg); |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | /* string parsing is specialized -- copy the whole string, not just the |
---|
153 | * first word */ |
---|
154 | template<> |
---|
155 | inline void |
---|
156 | Option<std::string>::parse(const std::string& arg) |
---|
157 | { |
---|
158 | opt_storage = arg; |
---|
159 | } |
---|
160 | |
---|
161 | #if H_MV |
---|
162 | template<> |
---|
163 | inline void |
---|
164 | Option<char*>::parse(const std::string& arg) |
---|
165 | { |
---|
166 | opt_storage = arg.empty() ? NULL : strdup(arg.c_str()) ; |
---|
167 | } |
---|
168 | |
---|
169 | template<> |
---|
170 | inline void |
---|
171 | Option< std::vector<char*> >::parse(const std::string& arg) |
---|
172 | { |
---|
173 | opt_storage.clear(); |
---|
174 | |
---|
175 | char* pcStart = (char*) arg.data(); |
---|
176 | char* pcEnd = strtok (pcStart," "); |
---|
177 | |
---|
178 | while (pcEnd != NULL) |
---|
179 | { |
---|
180 | size_t uiStringLength = pcEnd - pcStart; |
---|
181 | char* pcNewStr = (char*) malloc( uiStringLength + 1 ); |
---|
182 | strncpy( pcNewStr, pcStart, uiStringLength); |
---|
183 | pcNewStr[uiStringLength] = '\0'; |
---|
184 | pcStart = pcEnd+1; |
---|
185 | pcEnd = strtok (NULL, " ,.-"); |
---|
186 | opt_storage.push_back( pcNewStr ); |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | template<> |
---|
192 | inline void |
---|
193 | Option< std::vector<double> >::parse(const std::string& arg) |
---|
194 | { |
---|
195 | char* pcNextStart = (char*) arg.data(); |
---|
196 | char* pcEnd = pcNextStart + arg.length(); |
---|
197 | |
---|
198 | char* pcOldStart = 0; |
---|
199 | |
---|
200 | size_t iIdx = 0; |
---|
201 | |
---|
202 | while (pcNextStart < pcEnd) |
---|
203 | { |
---|
204 | errno = 0; |
---|
205 | |
---|
206 | if ( iIdx < opt_storage.size() ) |
---|
207 | { |
---|
208 | opt_storage[iIdx] = strtod(pcNextStart, &pcNextStart); |
---|
209 | } |
---|
210 | else |
---|
211 | { |
---|
212 | opt_storage.push_back( strtod(pcNextStart, &pcNextStart)) ; |
---|
213 | } |
---|
214 | iIdx++; |
---|
215 | |
---|
216 | if ( errno == ERANGE || (pcNextStart == pcOldStart) ) |
---|
217 | { |
---|
218 | std::cerr << "Error Parsing Doubles: `" << arg << "'" << std::endl; |
---|
219 | exit(EXIT_FAILURE); |
---|
220 | }; |
---|
221 | while( (pcNextStart < pcEnd) && ( *pcNextStart == ' ' || *pcNextStart == '\t' || *pcNextStart == '\r' ) ) pcNextStart++; |
---|
222 | pcOldStart = pcNextStart; |
---|
223 | |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | template<> |
---|
228 | inline void |
---|
229 | Option< std::vector<int> >::parse(const std::string& arg) |
---|
230 | { |
---|
231 | opt_storage.clear(); |
---|
232 | |
---|
233 | |
---|
234 | char* pcNextStart = (char*) arg.data(); |
---|
235 | char* pcEnd = pcNextStart + arg.length(); |
---|
236 | |
---|
237 | char* pcOldStart = 0; |
---|
238 | |
---|
239 | size_t iIdx = 0; |
---|
240 | |
---|
241 | |
---|
242 | while (pcNextStart < pcEnd) |
---|
243 | { |
---|
244 | |
---|
245 | if ( iIdx < opt_storage.size() ) |
---|
246 | { |
---|
247 | opt_storage[iIdx] = (int) strtol(pcNextStart, &pcNextStart,10); |
---|
248 | } |
---|
249 | else |
---|
250 | { |
---|
251 | opt_storage.push_back( (int) strtol(pcNextStart, &pcNextStart,10)) ; |
---|
252 | } |
---|
253 | iIdx++; |
---|
254 | if ( errno == ERANGE || (pcNextStart == pcOldStart) ) |
---|
255 | { |
---|
256 | std::cerr << "Error Parsing Integers: `" << arg << "'" << std::endl; |
---|
257 | exit(EXIT_FAILURE); |
---|
258 | }; |
---|
259 | while( (pcNextStart < pcEnd) && ( *pcNextStart == ' ' || *pcNextStart == '\t' || *pcNextStart == '\r' ) ) pcNextStart++; |
---|
260 | pcOldStart = pcNextStart; |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | |
---|
265 | template<> |
---|
266 | inline void |
---|
267 | Option< std::vector<bool> >::parse(const std::string& arg) |
---|
268 | { |
---|
269 | char* pcNextStart = (char*) arg.data(); |
---|
270 | char* pcEnd = pcNextStart + arg.length(); |
---|
271 | |
---|
272 | char* pcOldStart = 0; |
---|
273 | |
---|
274 | size_t iIdx = 0; |
---|
275 | |
---|
276 | while (pcNextStart < pcEnd) |
---|
277 | { |
---|
278 | if ( iIdx < opt_storage.size() ) |
---|
279 | { |
---|
280 | opt_storage[iIdx] = (strtol(pcNextStart, &pcNextStart,10) != 0); |
---|
281 | } |
---|
282 | else |
---|
283 | { |
---|
284 | opt_storage.push_back(strtol(pcNextStart, &pcNextStart,10) != 0) ; |
---|
285 | } |
---|
286 | iIdx++; |
---|
287 | |
---|
288 | if ( errno == ERANGE || (pcNextStart == pcOldStart) ) |
---|
289 | { |
---|
290 | std::cerr << "Error Parsing Bools: `" << arg << "'" << std::endl; |
---|
291 | exit(EXIT_FAILURE); |
---|
292 | }; |
---|
293 | while( (pcNextStart < pcEnd) && ( *pcNextStart == ' ' || *pcNextStart == '\t' || *pcNextStart == '\r' ) ) pcNextStart++; |
---|
294 | pcOldStart = pcNextStart; |
---|
295 | } |
---|
296 | } |
---|
297 | #endif |
---|
298 | /** Option class for argument handling using a user provided function */ |
---|
299 | struct OptionFunc : public OptionBase |
---|
300 | { |
---|
301 | typedef void (Func)(Options&, const std::string&); |
---|
302 | |
---|
303 | OptionFunc(const std::string& name, Options& parent_, Func *func_, const std::string& desc) |
---|
304 | : OptionBase(name, desc), parent(parent_), func(func_) |
---|
305 | {} |
---|
306 | |
---|
307 | void parse(const std::string& arg) |
---|
308 | { |
---|
309 | func(parent, arg); |
---|
310 | } |
---|
311 | |
---|
312 | void setDefault() |
---|
313 | { |
---|
314 | return; |
---|
315 | } |
---|
316 | |
---|
317 | private: |
---|
318 | Options& parent; |
---|
319 | void (*func)(Options&, const std::string&); |
---|
320 | }; |
---|
321 | |
---|
322 | class OptionSpecific; |
---|
323 | struct Options |
---|
324 | { |
---|
325 | ~Options(); |
---|
326 | |
---|
327 | OptionSpecific addOptions(); |
---|
328 | |
---|
329 | struct Names |
---|
330 | { |
---|
331 | Names() : opt(0) {}; |
---|
332 | ~Names() |
---|
333 | { |
---|
334 | if (opt) |
---|
335 | delete opt; |
---|
336 | } |
---|
337 | std::list<std::string> opt_long; |
---|
338 | std::list<std::string> opt_short; |
---|
339 | OptionBase* opt; |
---|
340 | }; |
---|
341 | |
---|
342 | void addOption(OptionBase *opt); |
---|
343 | |
---|
344 | typedef std::list<Names*> NamesPtrList; |
---|
345 | NamesPtrList opt_list; |
---|
346 | |
---|
347 | typedef std::map<std::string, NamesPtrList> NamesMap; |
---|
348 | NamesMap opt_long_map; |
---|
349 | NamesMap opt_short_map; |
---|
350 | }; |
---|
351 | |
---|
352 | /* Class with templated overloaded operator(), for use by Options::addOptions() */ |
---|
353 | class OptionSpecific |
---|
354 | { |
---|
355 | public: |
---|
356 | OptionSpecific(Options& parent_) : parent(parent_) {} |
---|
357 | |
---|
358 | /** |
---|
359 | * Add option described by name to the parent Options list, |
---|
360 | * with storage for the option's value |
---|
361 | * with default_val as the default value |
---|
362 | * with desc as an optional help description |
---|
363 | */ |
---|
364 | template<typename T> |
---|
365 | OptionSpecific& |
---|
366 | operator()(const std::string& name, T& storage, T default_val, const std::string& desc = "") |
---|
367 | { |
---|
368 | parent.addOption(new Option<T>(name, storage, default_val, desc)); |
---|
369 | return *this; |
---|
370 | } |
---|
371 | |
---|
372 | #if H_MV |
---|
373 | template<typename T> |
---|
374 | OptionSpecific& |
---|
375 | operator()(const std::string& name, std::vector<T>& storage, T default_val, unsigned uiMaxNum, const std::string& desc = "" ) |
---|
376 | { |
---|
377 | std::string cNameBuffer; |
---|
378 | std::string cDescBuffer; |
---|
379 | |
---|
380 | cNameBuffer .resize( name.size() + 10 ); |
---|
381 | cDescBuffer.resize( desc.size() + 10 ); |
---|
382 | |
---|
383 | storage.resize(uiMaxNum); |
---|
384 | for ( unsigned int uiK = 0; uiK < uiMaxNum; uiK++ ) |
---|
385 | { |
---|
386 | Bool duplicate = (uiK != 0); |
---|
387 | // isn't there are sprintf function for string?? |
---|
388 | sprintf((char*) cNameBuffer.c_str() ,name.c_str(),uiK,uiK); |
---|
389 | |
---|
390 | if ( !duplicate ) |
---|
391 | { |
---|
392 | sprintf((char*) cDescBuffer.c_str(),desc.c_str(),uiK,uiK); |
---|
393 | } |
---|
394 | |
---|
395 | cNameBuffer.resize( std::strlen(cNameBuffer.c_str()) ); |
---|
396 | cDescBuffer.resize( std::strlen(cDescBuffer.c_str()) ); |
---|
397 | |
---|
398 | |
---|
399 | parent.addOption(new Option<T>( cNameBuffer, (storage[uiK]), default_val, cDescBuffer, duplicate )); |
---|
400 | } |
---|
401 | |
---|
402 | return *this; |
---|
403 | } |
---|
404 | #endif |
---|
405 | /** |
---|
406 | * Add option described by name to the parent Options list, |
---|
407 | * with desc as an optional help description |
---|
408 | * instead of storing the value somewhere, a function of type |
---|
409 | * OptionFunc::Func is called. It is upto this function to correctly |
---|
410 | * handle evaluating the option's value. |
---|
411 | */ |
---|
412 | OptionSpecific& |
---|
413 | operator()(const std::string& name, OptionFunc::Func *func, const std::string& desc = "") |
---|
414 | { |
---|
415 | parent.addOption(new OptionFunc(name, parent, func, desc)); |
---|
416 | return *this; |
---|
417 | } |
---|
418 | private: |
---|
419 | Options& parent; |
---|
420 | }; |
---|
421 | |
---|
422 | }; /* namespace: program_options_lite */ |
---|
423 | }; /* namespace: df */ |
---|
424 | |
---|
425 | //! \} |
---|