| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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> |
| 32 |
#include "gdal_priv.h" |
| 33 |
#include "base/typeconversion.h" |
| 34 |
|
| 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 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
} |
| 65 |
|
| 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 |
|
| 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 |
|
| 90 |
std::vector<std::string> getOptions() const{return m_argv;}; |
| 91 |
|
| 92 |
std::vector<std::string> getOptions() {return m_argv;}; |
| 93 |
|
| 94 |
void clearShortRegister(){ |
| 95 |
m_shortRegistered.clear(); |
| 96 |
} |
| 97 |
|
| 98 |
void clearLongRegister(){ |
| 99 |
m_longRegistered.clear(); |
| 100 |
} |
| 101 |
|
| 102 |
void clearRegister(){ |
| 103 |
clearRegister(); |
| 104 |
clearRegister(); |
| 105 |
} |
| 106 |
|
| 107 |
std::vector<std::string> getShortRegister() const {return(m_shortRegistered);}; |
| 108 |
|
| 109 |
std::vector<std::string> getShortRegister() {return(m_shortRegistered);}; |
| 110 |
|
| 111 |
std::vector<std::string> getLongRegister() const {return(m_longRegistered);}; |
| 112 |
|
| 113 |
std::vector<std::string> getLongRegister() {return(m_longRegistered);}; |
| 114 |
|
| 115 |
int badKeys(std::vector<std::string>& badKeys){ |
| 116 |
badKeys.clear(); |
| 117 |
std::locale loc; |
| 118 |
std::string helpStringShort="-h"; |
| 119 |
std::string helpStringLong="--help"; |
| 120 |
std::string helpStringDoxygen="--doxygen"; |
| 121 |
std::string helpStringDoxypar="--doxypar"; |
| 122 |
std::string versionString="--version"; |
| 123 |
std::string licenseString="--license"; |
| 124 |
for(int i = 1; i < m_argc; ++i ){ |
| 125 |
std::string currentArgument; |
| 126 |
std::string currentOption=m_argv[i]; |
| 127 |
|
| 128 |
|
| 129 |
bool isLongOption=(!currentOption.compare(0,2,"--")); |
| 130 |
bool isShortOption=(!currentOption.compare(0,1,"-")&&!std::isdigit(currentOption[1],loc)); |
| 131 |
bool currentRegistered=true; |
| 132 |
|
| 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){ |
| 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 |
| 184 |
continue; |
| 185 |
} |
| 186 |
return(badKeys.size()); |
| 187 |
} |
| 188 |
|
| 189 |
void registerShortOption(const std::string& shortName){ |
| 190 |
m_shortRegistered.push_back(shortName); |
| 191 |
} |
| 192 |
|
| 193 |
void registerLongOption(const std::string& longName){ |
| 194 |
m_longRegistered.push_back(longName); |
| 195 |
} |
| 196 |
|
| 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 |
|
| 204 |
void pushShortOption(const std::string &key) |
| 205 |
{ |
| 206 |
std::ostringstream os; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
os << "-" << key; |
| 212 |
m_argv.push_back(os.str().c_str()); |
| 213 |
++m_argc; |
| 214 |
}; |
| 215 |
|
| 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 |
|
| 224 |
void setShortOption(const std::string &key) |
| 225 |
{ |
| 226 |
clearOption(key); |
| 227 |
std::ostringstream os; |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
os << "-" << key; |
| 233 |
m_argv.push_back(os.str().c_str()); |
| 234 |
++m_argc; |
| 235 |
}; |
| 236 |
|
| 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 |
|
| 246 |
template<typename T> void setShortOption(const std::string &key, const T &value){ |
| 247 |
setShortOption(key,type2string<T>(value)); |
| 248 |
} |
| 249 |
|
| 250 |
template<typename T> void setLongOption(const std::string &key, const T &value){ |
| 251 |
setLongOption(key,type2string<T>(value)); |
| 252 |
} |
| 253 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 286 |
template<typename T> void pushShortOption(const std::string &key, T &value){ |
| 287 |
pushShortOption(key,type2string<T>(value)); |
| 288 |
} |
| 289 |
|
| 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 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
m_argv.push_back(value); |
| 305 |
++m_argc; |
| 306 |
}; |
| 307 |
|
| 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){ |
| 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 |
|
| 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 |
|
| 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 |