From 431e95fa2d715550c873aa368f9b6129b94a4593 Mon Sep 17 00:00:00 2001 From: Bruno De Bel Date: Thu, 8 Feb 2018 14:20:01 +0100 Subject: [PATCH] Parametre in command line for better handling per script --- commonLib/commonLib.vcproj | 8 +++ commonLib/paramMain.cpp | 103 +++++++++++++++++++++++++++++++++++++ commonLib/paramMain.h | 20 +++++++ loadInventory/main.cpp | 20 ++++--- project/Project.vcproj | 8 +++ project/main.cpp | 21 +++++--- 6 files changed, 166 insertions(+), 14 deletions(-) create mode 100644 commonLib/paramMain.cpp create mode 100644 commonLib/paramMain.h diff --git a/commonLib/commonLib.vcproj b/commonLib/commonLib.vcproj index 33c063f..23003bf 100644 --- a/commonLib/commonLib.vcproj +++ b/commonLib/commonLib.vcproj @@ -161,6 +161,10 @@ RelativePath=".\dbconnection.cpp" > + + @@ -191,6 +195,10 @@ RelativePath=".\dbconnection.h" > + + diff --git a/commonLib/paramMain.cpp b/commonLib/paramMain.cpp new file mode 100644 index 0000000..e041f6c --- /dev/null +++ b/commonLib/paramMain.cpp @@ -0,0 +1,103 @@ +#include "paramMain.h" + +#include +#include +#include +#include + + +contextParam::contextParam(){ + option_index = 1; + configFile="config.xml"; +}; + + +// TODO: link with gnu solution for getopt +// +int contextParam::getopt(int argc, char** argv,char* option){ + + if (option_index >= argc) return -1; + + char* tmp = argv[option_index]; + + char ret; + + if ( tmp[0] == '-' ){ // START OPTION + + ret = optopt = tmp[1]; + + if ( optopt == 0 ) { + ret='?'; + goto end; + } + + bool found = false; + for ( char* tmpOption = option; *tmpOption !=0 ; tmpOption++){ + if (*tmpOption == optopt ) {// Found + found = true; + tmpOption++; + if ( *tmpOption == ':' ){ + + if (tmp[2] == 0 ){ + option_index ++; + if (option_index >= argc){ + ret='?'; + goto end; + } + optarg = argv[option_index]; + }else{ + + optarg = tmp+2; + } + + } + } + + } + + if (!found){ + ret='?'; + goto end; + } + + + } +end: + option_index++; + return ret; + +} + +int contextParam::analyse(int argc, char** argv){ + + int c; + + + if (argc == 0){ + configFile = "config.xml"; + return 0; + } + + while ((c = getopt (argc, argv, "c:")) != -1) + switch (c) + { + case 'c': + configFile = strdup(optarg); + break; + case '?': + if (optopt == 'c') + fprintf (stderr, "Option -%c requires an argument.\n", optopt); + else if (isprint (optopt)) + fprintf (stderr, "Unknown option `-%c'.\n", optopt); + else + fprintf (stderr, + "Unknown option character `\\x%x'.\n", + optopt); + fprintf (stderr, "usage: %s -c config.xml\n", argv[0]); + return -1; + default: + fprintf (stderr, "usage: %s -c config.xml\n", argv[0]); + return -1; + } + return 0; +} diff --git a/commonLib/paramMain.h b/commonLib/paramMain.h new file mode 100644 index 0000000..a821882 --- /dev/null +++ b/commonLib/paramMain.h @@ -0,0 +1,20 @@ +#ifndef _PARAMMAIN_H +#define _PARAMMAIN_H + +// Manage the parameter in line + +class contextParam{ + +private: + int option_index; + char optopt; + char* optarg; + int getopt(int argc, char** argv,char* option); + +public: + contextParam(); + char* configFile; + int analyse(int argc, char** argv); +}; + +#endif \ No newline at end of file diff --git a/loadInventory/main.cpp b/loadInventory/main.cpp index 8741b36..2f7e469 100644 --- a/loadInventory/main.cpp +++ b/loadInventory/main.cpp @@ -9,6 +9,7 @@ #include "boost/filesystem/operations.hpp" #include "boost/filesystem/path.hpp" #include "sqlloadinventory.h" +#include namespace fs = boost::filesystem; @@ -43,21 +44,22 @@ int loadInventory (const char * fileName,SQLLoadInventory& db ){ return 0; } -int start(){ +int start(std::string& configFileName){ //get current path of the folder fs::path CurrentPath( fs::initial_path()); - std::stringstream configPath,sqlCreateTablePath; - configPath << CurrentPath << "/config.xml"; + std::stringstream sqlCreateTablePath; + fs::path configPath = configFileName; + Parameters parameter; configparser config(¶meter); //Parse the config file - std::cerr << "Config file :" << configPath.str().c_str() << std::endl; + std::cerr << "Config file :" << configPath.string() << std::endl; - if (config.parse(configPath.str().c_str()) !=0 ) + if (config.parse(configPath.string().c_str()) !=0 ) { std::cerr << "Unable to parse :" << configPath << std::endl; return 1; @@ -78,11 +80,15 @@ int start(){ return -1; } -int main () { +int main (int argc, char** argv) { int ret; + contextParam ctx; + if (ctx.analyse(argc,argv)) return -1; + //std::cout<< "configFile:" << ctx.configFile < + + @@ -471,6 +475,10 @@ RelativePath=".\verifyIdentifierInMix.h" > + + diff --git a/project/main.cpp b/project/main.cpp index e42ebf9..2202c61 100644 --- a/project/main.cpp +++ b/project/main.cpp @@ -54,6 +54,8 @@ #include "boost/filesystem/path.hpp" #include "boost/progress.hpp" #include "boost/progress.hpp" +#include + #include "../common_files/precisiontimer.h" @@ -140,24 +142,24 @@ void printItemLogical(Item* itemSet,PhysicalLogicalAltoFilter& filter, std::stri -int start() +int start(std::string &configFileName) { srand(time(NULL)); //get current path of the folder fs::path CurrentPath( fs::initial_path()); - std::stringstream configPath,sqlCreateTablePath; - configPath << CurrentPath << "/config.xml"; + std::stringstream sqlCreateTablePath; + fs::path configPath = configFileName; ParameterMETS parameter; configparser config(¶meter); //Parse the config file - std::cerr << "Config file :" << configPath.str().c_str() << std::endl; + std::cerr << "Config file :" << configPath.string() << std::endl; - if (config.parse(configPath.str().c_str()) !=0 ) + if (config.parse(configPath.string().c_str()) !=0 ) { errorHandler hLog; hLog.setlogFilePath("commonLog.log"); @@ -550,9 +552,14 @@ int start() int main(int argc, char** argv){ + int ret; + contextParam ctx; + if (ctx.analyse(argc,argv)) return -1; + XMLPlatformUtils::Initialize (); - start (); - + ret=start(std::string(ctx.configFile)); + XMLPlatformUtils::Terminate (); + return ret; } \ No newline at end of file