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-2014, 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 | //! \ingroup TAppCommon |
---|
40 | //! \{ |
---|
41 | |
---|
42 | |
---|
43 | namespace df |
---|
44 | { |
---|
45 | namespace program_options_lite |
---|
46 | { |
---|
47 | struct Options; |
---|
48 | |
---|
49 | struct ParseFailure : public std::exception |
---|
50 | { |
---|
51 | ParseFailure(std::string arg0, std::string val0) throw() |
---|
52 | : arg(arg0), val(val0) |
---|
53 | {} |
---|
54 | |
---|
55 | ~ParseFailure() throw() {}; |
---|
56 | |
---|
57 | std::string arg; |
---|
58 | std::string val; |
---|
59 | |
---|
60 | const char* what() const throw() { return "Option Parse Failure"; } |
---|
61 | }; |
---|
62 | |
---|
63 | void doHelp(std::ostream& out, Options& opts, unsigned columns = 80); |
---|
64 | unsigned parseGNU(Options& opts, unsigned argc, const char* argv[]); |
---|
65 | unsigned parseSHORT(Options& opts, unsigned argc, const char* argv[]); |
---|
66 | std::list<const char*> scanArgv(Options& opts, unsigned argc, const char* argv[]); |
---|
67 | void scanLine(Options& opts, std::string& line); |
---|
68 | void scanFile(Options& opts, std::istream& in); |
---|
69 | void setDefaults(Options& opts); |
---|
70 | void parseConfigFile(Options& opts, const std::string& filename); |
---|
71 | bool storePair(Options& opts, const std::string& name, const std::string& value); |
---|
72 | |
---|
73 | /** OptionBase: Virtual base class for storing information relating to a |
---|
74 | * specific option This base class describes common elements. Type specific |
---|
75 | * information should be stored in a derived class. */ |
---|
76 | struct OptionBase |
---|
77 | { |
---|
78 | OptionBase(const std::string& name, const std::string& desc) |
---|
79 | : opt_string(name), opt_desc(desc) |
---|
80 | {}; |
---|
81 | |
---|
82 | virtual ~OptionBase() {} |
---|
83 | |
---|
84 | /* parse argument arg, to obtain a value for the option */ |
---|
85 | virtual void parse(const std::string& arg) = 0; |
---|
86 | /* set the argument to the default value */ |
---|
87 | virtual void setDefault() = 0; |
---|
88 | |
---|
89 | std::string opt_string; |
---|
90 | std::string opt_desc; |
---|
91 | }; |
---|
92 | |
---|
93 | /** Type specific option storage */ |
---|
94 | template<typename T> |
---|
95 | struct Option : public OptionBase |
---|
96 | { |
---|
97 | Option(const std::string& name, T& storage, T default_val, const std::string& desc) |
---|
98 | : OptionBase(name, desc), opt_storage(storage), opt_default_val(default_val) |
---|
99 | {} |
---|
100 | |
---|
101 | void parse(const std::string& arg); |
---|
102 | |
---|
103 | void setDefault() |
---|
104 | { |
---|
105 | opt_storage = opt_default_val; |
---|
106 | } |
---|
107 | |
---|
108 | T& opt_storage; |
---|
109 | T opt_default_val; |
---|
110 | }; |
---|
111 | |
---|
112 | /* Generic parsing */ |
---|
113 | template<typename T> |
---|
114 | inline void |
---|
115 | Option<T>::parse(const std::string& arg) |
---|
116 | { |
---|
117 | std::istringstream arg_ss (arg,std::istringstream::in); |
---|
118 | arg_ss.exceptions(std::ios::failbit); |
---|
119 | try |
---|
120 | { |
---|
121 | arg_ss >> opt_storage; |
---|
122 | } |
---|
123 | catch (...) |
---|
124 | { |
---|
125 | throw ParseFailure(opt_string, arg); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | /* string parsing is specialized -- copy the whole string, not just the |
---|
130 | * first word */ |
---|
131 | template<> |
---|
132 | inline void |
---|
133 | Option<std::string>::parse(const std::string& arg) |
---|
134 | { |
---|
135 | opt_storage = arg; |
---|
136 | } |
---|
137 | |
---|
138 | /** Option class for argument handling using a user provided function */ |
---|
139 | struct OptionFunc : public OptionBase |
---|
140 | { |
---|
141 | typedef void (Func)(Options&, const std::string&); |
---|
142 | |
---|
143 | OptionFunc(const std::string& name, Options& parent_, Func *func_, const std::string& desc) |
---|
144 | : OptionBase(name, desc), parent(parent_), func(func_) |
---|
145 | {} |
---|
146 | |
---|
147 | void parse(const std::string& arg) |
---|
148 | { |
---|
149 | func(parent, arg); |
---|
150 | } |
---|
151 | |
---|
152 | void setDefault() |
---|
153 | { |
---|
154 | return; |
---|
155 | } |
---|
156 | |
---|
157 | private: |
---|
158 | Options& parent; |
---|
159 | void (*func)(Options&, const std::string&); |
---|
160 | }; |
---|
161 | |
---|
162 | class OptionSpecific; |
---|
163 | struct Options |
---|
164 | { |
---|
165 | ~Options(); |
---|
166 | |
---|
167 | OptionSpecific addOptions(); |
---|
168 | |
---|
169 | struct Names |
---|
170 | { |
---|
171 | Names() : opt(0) {}; |
---|
172 | ~Names() |
---|
173 | { |
---|
174 | if (opt) |
---|
175 | delete opt; |
---|
176 | } |
---|
177 | std::list<std::string> opt_long; |
---|
178 | std::list<std::string> opt_short; |
---|
179 | OptionBase* opt; |
---|
180 | }; |
---|
181 | |
---|
182 | void addOption(OptionBase *opt); |
---|
183 | |
---|
184 | typedef std::list<Names*> NamesPtrList; |
---|
185 | NamesPtrList opt_list; |
---|
186 | |
---|
187 | typedef std::map<std::string, NamesPtrList> NamesMap; |
---|
188 | NamesMap opt_long_map; |
---|
189 | NamesMap opt_short_map; |
---|
190 | }; |
---|
191 | |
---|
192 | /* Class with templated overloaded operator(), for use by Options::addOptions() */ |
---|
193 | class OptionSpecific |
---|
194 | { |
---|
195 | public: |
---|
196 | OptionSpecific(Options& parent_) : parent(parent_) {} |
---|
197 | |
---|
198 | /** |
---|
199 | * Add option described by name to the parent Options list, |
---|
200 | * with storage for the option's value |
---|
201 | * with default_val as the default value |
---|
202 | * with desc as an optional help description |
---|
203 | */ |
---|
204 | template<typename T> |
---|
205 | OptionSpecific& |
---|
206 | operator()(const std::string& name, T& storage, T default_val, const std::string& desc = "") |
---|
207 | { |
---|
208 | parent.addOption(new Option<T>(name, storage, default_val, desc)); |
---|
209 | return *this; |
---|
210 | } |
---|
211 | #if 1 //SVC_EXTENSION |
---|
212 | template<typename T> |
---|
213 | OptionSpecific& |
---|
214 | operator()(const std::string& name, T* storage, T default_val, unsigned uiMaxNum, const std::string& desc = "" ) |
---|
215 | { |
---|
216 | std::string cNameBuffer; |
---|
217 | std::string cDescriptionBuffer; |
---|
218 | |
---|
219 | cNameBuffer .resize( name.size() + 10 ); |
---|
220 | cDescriptionBuffer.resize( desc.size() + 10 ); |
---|
221 | |
---|
222 | for ( unsigned int uiK = 0; uiK < uiMaxNum; uiK++ ) |
---|
223 | { |
---|
224 | // isn't there are sprintf function for string?? |
---|
225 | sprintf((char*) cNameBuffer.c_str() ,name.c_str(),uiK,uiK); |
---|
226 | sprintf((char*) cDescriptionBuffer.c_str(),desc.c_str(),uiK,uiK); |
---|
227 | |
---|
228 | size_t pos = cNameBuffer.find_first_of('\0'); |
---|
229 | if(pos != std::string::npos) |
---|
230 | cNameBuffer.resize(pos); |
---|
231 | |
---|
232 | parent.addOption(new Option<T>( cNameBuffer, (storage[uiK]), default_val, cDescriptionBuffer )); |
---|
233 | } |
---|
234 | |
---|
235 | return *this; |
---|
236 | } |
---|
237 | |
---|
238 | template<typename T> |
---|
239 | OptionSpecific& |
---|
240 | operator()(const std::string& name, T** storage, T default_val, unsigned uiMaxNum, const std::string& desc = "" ) |
---|
241 | { |
---|
242 | std::string cNameBuffer; |
---|
243 | std::string cDescriptionBuffer; |
---|
244 | |
---|
245 | cNameBuffer .resize( name.size() + 10 ); |
---|
246 | cDescriptionBuffer.resize( desc.size() + 10 ); |
---|
247 | |
---|
248 | for ( unsigned int uiK = 0; uiK < uiMaxNum; uiK++ ) |
---|
249 | { |
---|
250 | // isn't there are sprintf function for string?? |
---|
251 | sprintf((char*) cNameBuffer.c_str() ,name.c_str(),uiK,uiK); |
---|
252 | sprintf((char*) cDescriptionBuffer.c_str(),desc.c_str(),uiK,uiK); |
---|
253 | |
---|
254 | size_t pos = cNameBuffer.find_first_of('\0'); |
---|
255 | if(pos != std::string::npos) |
---|
256 | cNameBuffer.resize(pos); |
---|
257 | |
---|
258 | parent.addOption(new Option<T>( cNameBuffer, *(storage[uiK]), default_val, cDescriptionBuffer )); |
---|
259 | } |
---|
260 | |
---|
261 | return *this; |
---|
262 | } |
---|
263 | #endif |
---|
264 | /** |
---|
265 | * Add option described by name to the parent Options list, |
---|
266 | * with desc as an optional help description |
---|
267 | * instead of storing the value somewhere, a function of type |
---|
268 | * OptionFunc::Func is called. It is upto this function to correctly |
---|
269 | * handle evaluating the option's value. |
---|
270 | */ |
---|
271 | OptionSpecific& |
---|
272 | operator()(const std::string& name, OptionFunc::Func *func, const std::string& desc = "") |
---|
273 | { |
---|
274 | parent.addOption(new OptionFunc(name, parent, func, desc)); |
---|
275 | return *this; |
---|
276 | } |
---|
277 | private: |
---|
278 | Options& parent; |
---|
279 | }; |
---|
280 | |
---|
281 | }; /* namespace: program_options_lite */ |
---|
282 | }; /* namespace: df */ |
---|
283 | |
---|
284 | //! \} |
---|