Skip to content

Commit

Permalink
Parametre in command line for better handling per script
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno De Bel authored and Bruno De Bel committed Feb 8, 2018
1 parent 5797ed2 commit 431e95f
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 14 deletions.
8 changes: 8 additions & 0 deletions commonLib/commonLib.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@
RelativePath=".\dbconnection.cpp"
>
</File>
<File
RelativePath=".\paramMain.cpp"
>
</File>
<File
RelativePath=".\parserCheck.cpp"
>
Expand Down Expand Up @@ -191,6 +195,10 @@
RelativePath=".\dbconnection.h"
>
</File>
<File
RelativePath=".\paramMain.h"
>
</File>
<File
RelativePath=".\parserCheck.h"
>
Expand Down
103 changes: 103 additions & 0 deletions commonLib/paramMain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "paramMain.h"

#include<iostream>
#include<cstdio>
#include<ctype.h>
#include<cstdlib>


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;
}
20 changes: 20 additions & 0 deletions commonLib/paramMain.h
Original file line number Diff line number Diff line change
@@ -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
20 changes: 13 additions & 7 deletions loadInventory/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
#include "sqlloadinventory.h"
#include <paramMain.h>

namespace fs = boost::filesystem;

Expand Down Expand Up @@ -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<fs::path>());

std::stringstream configPath,sqlCreateTablePath;
configPath << CurrentPath << "/config.xml";
std::stringstream sqlCreateTablePath;
fs::path configPath = configFileName;


Parameters parameter;

configparser config(&parameter);
//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;
Expand All @@ -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 <<std::endl;

XMLPlatformUtils::Initialize ();

ret=start();
ret=start(std::string(ctx.configFile));

XMLPlatformUtils::Terminate ();
return ret;
Expand Down
8 changes: 8 additions & 0 deletions project/Project.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@
RelativePath=".\verifyFolder.cpp"
>
</File>
<File
RelativePath=".\verifyPages.cpp"
>
</File>
<Filter
Name="semanticchecks"
>
Expand Down Expand Up @@ -471,6 +475,10 @@
RelativePath=".\verifyIdentifierInMix.h"
>
</File>
<File
RelativePath=".\verifyPages.h"
>
</File>
<Filter
Name="semanticchecks"
>
Expand Down
21 changes: 14 additions & 7 deletions project/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
#include "boost/filesystem/path.hpp"
#include "boost/progress.hpp"
#include "boost/progress.hpp"
#include <paramMain.h>


#include "../common_files/precisiontimer.h"

Expand Down Expand Up @@ -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<fs::path>());

std::stringstream configPath,sqlCreateTablePath;
configPath << CurrentPath << "/config.xml";
std::stringstream sqlCreateTablePath;
fs::path configPath = configFileName;

ParameterMETS parameter;

configparser config(&parameter);
//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");
Expand Down Expand Up @@ -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;
}

0 comments on commit 431e95f

Please sign in to comment.