Debian Codegraph @

src/apps/AppFactory.h

raw symbols SCIP
1 /**********************************************************************
2 AppFactory.h: class for application functions
3 Author(s): [email protected]
4 Copyright (C) 2016-2020 European Union (Joint Research Centre)
5
6 This file is part of jiplib.
7
8 jiplib is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 jiplib is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with jiplib. If not, see <https://www.gnu.org/licenses/>.
20 ***********************************************************************/
21 #ifndef _APPFACTORY_H_
22 #define _APPFACTORY_H_
23
24 #include <string>
25 #include <vector>
26 #include <list>
27 #include <iostream>
28 #include <iomanip>
29 #include <sstream>
30 #include <algorithm>
31 #include <locale> //std::locale, std::isdigit
32 #include "gdal_priv.h"
33 #include "base/typeconversion.h"
34 //#include "config.h"
35 #include <json/value.h>
36 #include <json/json.h>
37
38 namespace app
39 {
40 #if JIPLIB_BUILD_WITH_PYTHON==1
41 #define MyProgressFunc(dfComplete,pszMessage,pProgressArg) {}
42 #else
43 #define MyProgressFunc(dfComplete,pszMessage,pProgressArg) pfnProgress(dfComplete,pszMessage,pProgressArg)
44 #endif
45 class AppFactory{
46
47 public:
48 AppFactory(void) : m_argc(1), m_argv(std::vector<std::string>(1,"appFactory")){}
49 AppFactory(int argc, char* argv[]) : m_argc(1), m_argv(std::vector<std::string>(1,"appFactory")){
50 setOptions(argc, argv);
51 }
52 AppFactory(Json::Value &jsonobj){json2app(jsonobj);};
53 AppFactory(const std::string &jsonAppString){
54 Json::Value root;
55 std::istringstream sin(jsonAppString);
56 sin >> root;
57 json2app(root);
58 // Json::Reader reader;
59 // bool parsedSuccess = reader.parse(jsonAppString, root, false);
60 // if( parsedSuccess )
61 // {
62 // json2app(root);
63 // }
64 }
65 ///copy constructor
66 AppFactory(const AppFactory &theApp){
67 m_argc=theApp.getArgc();
68 m_argv=theApp.getArgv();
69 m_shortRegistered=theApp.getShortRegister();
70 m_longRegistered=theApp.getLongRegister();
71 }
72 ///copy operator
73 AppFactory &operator=(const AppFactory &theApp){
74 if( &theApp == this ) return *this;
75 m_argc=theApp.getArgc();
76 m_argv=theApp.getArgv();
77 m_longRegistered=theApp.getLongRegister();
78 m_shortRegistered=theApp.getShortRegister();
79 return *this;
80 }
81 virtual ~AppFactory(void){};
82 bool empty() const {return(m_argv.empty());};
83 bool size() const {return(m_argv.size());};
84 void setOptions(int argc, const std::vector<std::string> argv){
85 m_argc=argc;
86 m_argv.clear();
87 m_argv=argv;
88 }
89 ///get all options and store in argv, return number of options const version
90 std::vector<std::string> getOptions() const{return m_argv;};
91 ///get all options and store in argv, return number of options
92 std::vector<std::string> getOptions() {return m_argv;};
93 ///clear short register
94 void clearShortRegister(){
95 m_shortRegistered.clear();
96 }
97 ///clear long register
98 void clearLongRegister(){
99 m_longRegistered.clear();
100 }
101 ///clear register
102 void clearRegister(){
103 clearRegister();
104 clearRegister();
105 }
106 ///get short register const version
107 std::vector<std::string> getShortRegister() const {return(m_shortRegistered);};
108 ///get short register
109 std::vector<std::string> getShortRegister() {return(m_shortRegistered);};
110 ///get long register const version
111 std::vector<std::string> getLongRegister() const {return(m_longRegistered);};
112 ///get long register
113 std::vector<std::string> getLongRegister() {return(m_longRegistered);};
114 ///check if app was called with not registered options
115 int badKeys(std::vector<std::string>& badKeys){
116 badKeys.clear();
117 std::locale loc;
118 std::string helpStringShort="-h";//short option for help (hard coded)
119 std::string helpStringLong="--help";//long option for help (hard coded)
120 std::string helpStringDoxygen="--doxygen";//option to create table of options ready to use for doxygen
121 std::string helpStringDoxypar="--doxypar";//option to create list of doxygen parameters for inline documentation
122 std::string versionString="--version";//option to show current version
123 std::string licenseString="--license";//option to show current version
124 for(int i = 1; i < m_argc; ++i ){
125 std::string currentArgument;
126 std::string currentOption=m_argv[i];
127 /* if(currentOption.compare(0,1,"-")) */
128 /* continue;//not an option */
129 bool isLongOption=(!currentOption.compare(0,2,"--"));
130 bool isShortOption=(!currentOption.compare(0,1,"-")&&!std::isdigit(currentOption[1],loc));
131 bool currentRegistered=true;
132 //bool specialOption=false;//return value, alert main program that hard coded option (help, version, license, doxygen) was invoked
133 if(!helpStringShort.compare(currentOption))
134 continue;
135 if(!helpStringLong.compare(currentOption))
136 continue;
137 if(!helpStringDoxygen.compare(currentOption))
138 continue;
139 if(!helpStringDoxypar.compare(currentOption))
140 continue;
141 if(!versionString.compare(currentOption))
142 continue;
143 if(!licenseString.compare(currentOption))
144 continue;
145 if(isLongOption){
146 currentRegistered=false;
147 for(unsigned int ireg=0;ireg<m_longRegistered.size();++ireg){
148 std::string longOption=m_longRegistered[ireg];
149 longOption.insert(0,"--");
150 size_t foundEqual=currentOption.rfind("=");
151 if(foundEqual!=std::string::npos){
152 currentArgument=currentOption.substr(foundEqual+1);
153 currentOption=currentOption.substr(0,foundEqual);
154 }
155 if(longOption==currentOption){
156 currentRegistered=true;
157 break;
158 }
159 }
160 if(!currentRegistered){
161 badKeys.push_back(currentOption.substr(2));
162 }
163 }
164 else if(isShortOption){//short option
165 currentRegistered=false;
166 for(unsigned int ireg=0;ireg<m_shortRegistered.size();++ireg){
167 std::string shortOption=m_shortRegistered[ireg];
168 shortOption.insert(0,"-");
169 size_t foundEqual=currentOption.rfind("=");
170 if(foundEqual!=std::string::npos){
171 currentArgument=currentOption.substr(foundEqual+1);
172 currentOption=currentOption.substr(0,foundEqual);
173 }
174 if(shortOption==currentOption){
175 currentRegistered=true;
176 break;
177 }
178 }
179 if(!currentRegistered){
180 badKeys.push_back(currentOption.substr(1));
181 }
182 }
183 else//not an option
184 continue;
185 }
186 return(badKeys.size());
187 }
188 ///register short option
189 void registerShortOption(const std::string& shortName){
190 m_shortRegistered.push_back(shortName);
191 }
192 ///register long option
193 void registerLongOption(const std::string& longName){
194 m_longRegistered.push_back(longName);
195 }
196 ///set all options from argc and argv
197 void setOptions(int argc, char* argv[]){
198 m_argc=argc;
199 m_argv.clear();
200 for(int iarg=0;iarg<argc;++iarg)
201 m_argv.push_back(argv[iarg]);
202 }
203 ///push bool option (used as flag)
204 void pushShortOption(const std::string &key)
205 {
206 std::ostringstream os;
207 /* // os << "--" << key;; */
208 /* if(key=="help") */
209 /* os << "--" << key; */
210 /* else */
211 os << "-" << key;
212 m_argv.push_back(os.str().c_str());
213 ++m_argc;
214 };
215 ///push bool long option (used as flag)
216 void pushLongOption(const std::string &key)
217 {
218 std::ostringstream os;
219 os << "--" << key;
220 m_argv.push_back(os.str().c_str());
221 ++m_argc;
222 };
223 ///set short bool option (used as flag)
224 void setShortOption(const std::string &key)
225 {
226 clearOption(key);
227 std::ostringstream os;
228 /* // os << "--" << key;; */
229 /* if(key=="help") */
230 /* os << "--" << key; */
231 /* else */
232 os << "-" << key;
233 m_argv.push_back(os.str().c_str());
234 ++m_argc;
235 };
236 ///set long bool option (used as flag)
237 void setLongOption(const std::string &key)
238 {
239 clearOption(key);
240 std::ostringstream os;
241 os << "--" << key;;
242 m_argv.push_back(os.str().c_str());
243 ++m_argc;
244 };
245 //template function to set short option
246 template<typename T> void setShortOption(const std::string &key, const T &value){
247 setShortOption(key,type2string<T>(value));
248 }
249 //template function to set long option
250 template<typename T> void setLongOption(const std::string &key, const T &value){
251 setLongOption(key,type2string<T>(value));
252 }
253 ///set key value short option
254 void setShortOption(const std::string &key, const std::string &value)
255 {
256 clearOption(key);
257 std::ostringstream os;
258 os << "-" << key;
259 m_argv.push_back(os.str());
260 ++m_argc;
261 m_argv.push_back(value);
262 ++m_argc;
263 };
264 ///set key value long option
265 void setLongOption(const std::string &key, const std::string &value)
266 {
267 clearOption(key);
268 std::ostringstream os;
269 os << "--" << key;
270 m_argv.push_back(os.str());
271 ++m_argc;
272 m_argv.push_back(value);
273 ++m_argc;
274 };
275 ///push key value short option
276 void pushShortOption(const std::string &key, const std::string &value)
277 {
278 std::ostringstream os;
279 os << "-" << key;
280 m_argv.push_back(os.str());
281 ++m_argc;
282 m_argv.push_back(value);
283 ++m_argc;
284 };
285 ///push key value short option template version
286 template<typename T> void pushShortOption(const std::string &key, T &value){
287 pushShortOption(key,type2string<T>(value));
288 }
289 ///push key value long option
290 void pushLongOption(const std::string &key, const std::string &value)
291 {
292 std::ostringstream os;
293 os << "--" << key;
294 m_argv.push_back(os.str());
295 ++m_argc;
296 /* if(value.find(" ")!=std::string::npos){ */
297 /* m_argv.push_back("\"" + value + "\""); */
298 /* //from C++14: */
299 /* /\* std::stringstream ss; *\/ */
300 /* /\* ss << std::quoted(value); *\/ */
301 /* /\* m_argv.push_back(ss.str()); *\/ */
302 /* } */
303 /* else */
304 m_argv.push_back(value);
305 ++m_argc;
306 };
307 ///push key value long option template version
308 template<typename T> void pushLongOption(const std::string &key, T &value){
309 pushLongOption(key,type2string<T>(value));
310 }
311 void getHelp() {setLongOption("help");};
312 void clearOptions() {m_argc=1;m_argv.clear();m_argv.push_back("appFactory");};
313 void clearOption(const std::string &key)
314 {
315 std::vector<std::string>::iterator opit=m_argv.begin();
316 while(opit!=m_argv.end()){
317 if(opit->find("-"+key)!=std::string::npos){
318 m_argv.erase(opit);
319 --m_argc;
320 if(opit!=m_argv.end()){
321 if(opit->find("-")==std::string::npos){//not a bool option
322 m_argv.erase(opit);
323 --m_argc;
324 }
325 }
326 }
327 else
328 ++opit;
329 }
330 };
331 void showOptions() const
332 {
333 for(unsigned int iarg=1;iarg<m_argv.size();++iarg)
334 std::cout << m_argv[iarg] << " ";
335 std::cout << std::endl;
336 };
337 int getArgc() const {return m_argc;};
338 std::string getArgv(unsigned int i) const {
339 if((i>0)&&(i<m_argv.size()))
340 return m_argv[i];
341 else
342 throw(std::string("Error: invalid index"));
343 }
344 std::vector<std::string> getArgv() const {
345 return m_argv;
346 }
347 std::vector<std::string> getArgv() {
348 return m_argv;
349 }
350 Json::Value app2json(){
351 Json::Value jsonobj;
352 jsonobj["argc"] = getArgc();
353 std::vector<std::string> argv = getArgv();
354 Json::Value strarray;
355 for(size_t i=0; i<argv.size(); i++) strarray.append(argv[i]);
356 jsonobj["argv"] = strarray;
357 return jsonobj;
358 }
359 bool json2app(Json::Value &jsonobj){
360 /* clearOptions(); */
361 m_argc=0;
362 m_argv.clear();
363 m_argc=jsonobj["argc"].asInt();
364 const Json::Value strarray = jsonobj["argv"];
365 for(unsigned int index = 0; index<strarray.size(); ++index) m_argv.push_back(strarray[index].asString());
366 return true;
367 }
368 private:
369 //todo: create member attribute for pointer to memory buffer?
370 int m_argc;
371 std::vector<std::string> m_argv;
372 std::vector<std::string> m_longRegistered;
373 std::vector<std::string> m_shortRegistered;
374 };
375 }
376
377 #endif /* _APPFACTORY_H_ */