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