/********************************************************************** AppFactory.h: class for application functions Author(s): Pieter.Kempeneers@ec.europa.eu Copyright (C) 2016-2020 European Union (Joint Research Centre) This file is part of jiplib. jiplib is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. jiplib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with jiplib. If not, see . ***********************************************************************/ #ifndef _APPFACTORY_H_ #define _APPFACTORY_H_ #include #include #include #include #include #include #include #include //std::locale, std::isdigit #include "gdal_priv.h" #include "base/typeconversion.h" //#include "config.h" #include #include namespace app { #if JIPLIB_BUILD_WITH_PYTHON==1 #define MyProgressFunc(dfComplete,pszMessage,pProgressArg) {} #else #define MyProgressFunc(dfComplete,pszMessage,pProgressArg) pfnProgress(dfComplete,pszMessage,pProgressArg) #endif class AppFactory{ public: AppFactory(void) : m_argc(1), m_argv(std::vector(1,"appFactory")){} AppFactory(int argc, char* argv[]) : m_argc(1), m_argv(std::vector(1,"appFactory")){ setOptions(argc, argv); } AppFactory(Json::Value &jsonobj){json2app(jsonobj);}; AppFactory(const std::string &jsonAppString){ Json::Value root; std::istringstream sin(jsonAppString); sin >> root; json2app(root); // Json::Reader reader; // bool parsedSuccess = reader.parse(jsonAppString, root, false); // if( parsedSuccess ) // { // json2app(root); // } } ///copy constructor AppFactory(const AppFactory &theApp){ m_argc=theApp.getArgc(); m_argv=theApp.getArgv(); m_shortRegistered=theApp.getShortRegister(); m_longRegistered=theApp.getLongRegister(); } ///copy operator AppFactory &operator=(const AppFactory &theApp){ if( &theApp == this ) return *this; m_argc=theApp.getArgc(); m_argv=theApp.getArgv(); m_longRegistered=theApp.getLongRegister(); m_shortRegistered=theApp.getShortRegister(); return *this; } virtual ~AppFactory(void){}; bool empty() const {return(m_argv.empty());}; bool size() const {return(m_argv.size());}; void setOptions(int argc, const std::vector argv){ m_argc=argc; m_argv.clear(); m_argv=argv; } ///get all options and store in argv, return number of options const version std::vector getOptions() const{return m_argv;}; ///get all options and store in argv, return number of options std::vector getOptions() {return m_argv;}; ///clear short register void clearShortRegister(){ m_shortRegistered.clear(); } ///clear long register void clearLongRegister(){ m_longRegistered.clear(); } ///clear register void clearRegister(){ clearRegister(); clearRegister(); } ///get short register const version std::vector getShortRegister() const {return(m_shortRegistered);}; ///get short register std::vector getShortRegister() {return(m_shortRegistered);}; ///get long register const version std::vector getLongRegister() const {return(m_longRegistered);}; ///get long register std::vector getLongRegister() {return(m_longRegistered);}; ///check if app was called with not registered options int badKeys(std::vector& badKeys){ badKeys.clear(); std::locale loc; std::string helpStringShort="-h";//short option for help (hard coded) std::string helpStringLong="--help";//long option for help (hard coded) std::string helpStringDoxygen="--doxygen";//option to create table of options ready to use for doxygen std::string helpStringDoxypar="--doxypar";//option to create list of doxygen parameters for inline documentation std::string versionString="--version";//option to show current version std::string licenseString="--license";//option to show current version for(int i = 1; i < m_argc; ++i ){ std::string currentArgument; std::string currentOption=m_argv[i]; /* if(currentOption.compare(0,1,"-")) */ /* continue;//not an option */ bool isLongOption=(!currentOption.compare(0,2,"--")); bool isShortOption=(!currentOption.compare(0,1,"-")&&!std::isdigit(currentOption[1],loc)); bool currentRegistered=true; //bool specialOption=false;//return value, alert main program that hard coded option (help, version, license, doxygen) was invoked if(!helpStringShort.compare(currentOption)) continue; if(!helpStringLong.compare(currentOption)) continue; if(!helpStringDoxygen.compare(currentOption)) continue; if(!helpStringDoxypar.compare(currentOption)) continue; if(!versionString.compare(currentOption)) continue; if(!licenseString.compare(currentOption)) continue; if(isLongOption){ currentRegistered=false; for(unsigned int ireg=0;ireg void setShortOption(const std::string &key, const T &value){ setShortOption(key,type2string(value)); } //template function to set long option template void setLongOption(const std::string &key, const T &value){ setLongOption(key,type2string(value)); } ///set key value short option void setShortOption(const std::string &key, const std::string &value) { clearOption(key); std::ostringstream os; os << "-" << key; m_argv.push_back(os.str()); ++m_argc; m_argv.push_back(value); ++m_argc; }; ///set key value long option void setLongOption(const std::string &key, const std::string &value) { clearOption(key); std::ostringstream os; os << "--" << key; m_argv.push_back(os.str()); ++m_argc; m_argv.push_back(value); ++m_argc; }; ///push key value short option void pushShortOption(const std::string &key, const std::string &value) { std::ostringstream os; os << "-" << key; m_argv.push_back(os.str()); ++m_argc; m_argv.push_back(value); ++m_argc; }; ///push key value short option template version template void pushShortOption(const std::string &key, T &value){ pushShortOption(key,type2string(value)); } ///push key value long option void pushLongOption(const std::string &key, const std::string &value) { std::ostringstream os; os << "--" << key; m_argv.push_back(os.str()); ++m_argc; /* if(value.find(" ")!=std::string::npos){ */ /* m_argv.push_back("\"" + value + "\""); */ /* //from C++14: */ /* /\* std::stringstream ss; *\/ */ /* /\* ss << std::quoted(value); *\/ */ /* /\* m_argv.push_back(ss.str()); *\/ */ /* } */ /* else */ m_argv.push_back(value); ++m_argc; }; ///push key value long option template version template void pushLongOption(const std::string &key, T &value){ pushLongOption(key,type2string(value)); } void getHelp() {setLongOption("help");}; void clearOptions() {m_argc=1;m_argv.clear();m_argv.push_back("appFactory");}; void clearOption(const std::string &key) { std::vector::iterator opit=m_argv.begin(); while(opit!=m_argv.end()){ if(opit->find("-"+key)!=std::string::npos){ m_argv.erase(opit); --m_argc; if(opit!=m_argv.end()){ if(opit->find("-")==std::string::npos){//not a bool option m_argv.erase(opit); --m_argc; } } } else ++opit; } }; void showOptions() const { for(unsigned int iarg=1;iarg0)&&(i getArgv() const { return m_argv; } std::vector getArgv() { return m_argv; } Json::Value app2json(){ Json::Value jsonobj; jsonobj["argc"] = getArgc(); std::vector argv = getArgv(); Json::Value strarray; for(size_t i=0; i m_argv; std::vector m_longRegistered; std::vector m_shortRegistered; }; } #endif /* _APPFACTORY_H_ */