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