From 944205758c2286c4036fb5a88df3a17dd2ada26a Mon Sep 17 00:00:00 2001 From: blastmaster Date: Fri, 22 Nov 2013 00:02:23 +0100 Subject: [PATCH] re-awesome config parsing sickness --- Makefile.am | 2 +- src/sicmain.cc | 111 +++++++++++++++++++++++++++---------------------- 2 files changed, 62 insertions(+), 51 deletions(-) diff --git a/Makefile.am b/Makefile.am index 20b8d09..bd3e241 100644 --- a/Makefile.am +++ b/Makefile.am @@ -8,7 +8,7 @@ bin_PROGRAMS=siccc siccc_SOURCES = src/options.c src/sicmain.cc mongoose/mongoose.c siccc_CFLAGS = -siccc_CXXFLAGS = ${AM_CXXFLAGS} -ggdb -I./mongoose/ -Darschlecken +siccc_CXXFLAGS = ${AM_CXXFLAGS} -DDEBUG -ggdb -I./mongoose/ -Darschlecken siccc_LDFLAGS = -ldl -pthread options.c: options.ggo diff --git a/src/sicmain.cc b/src/sicmain.cc index 39eba78..5b1208f 100644 --- a/src/sicmain.cc +++ b/src/sicmain.cc @@ -62,9 +62,9 @@ class MongooseHandler{ struct mg_context *ctx; //const char *options[]; unsigned short listenport; - + // = {"listening_ports", "8080", NULL}; - + public: //default listenport:8080 @@ -78,10 +78,10 @@ class MongooseHandler{ {"listening_ports",portstring,nullptr}; this->ctx= mg_start(options, &event_handler,nullptr); } - + //no copy constructor: MongooseHandler( const MongooseHandler &) = delete; - + //destructor stops the server ~MongooseHandler(){ if (this->ctx){ @@ -91,67 +91,78 @@ class MongooseHandler{ }; - -int main(int argc, char **argv) +#ifdef DEBUG +void dump_args() { - int result = 0; - /* MongooseHandler m; */ - - struct cmd_parser_params *params; - - //initialize the parameters structure - params = cmd_parser_params_create(); - - //call the command line parser - if (cmd_parser(argc, argv, &args_info) != 0) { - result = 1; - cmd_parser_free(&args_info); - free(params); - } - - //override command line options, - //but do not initialize args_info, check for required options. - //NOTICE: we must NOT skip the 0 assignment to initialize, - //since its default value is 1 and override defaults to 0 - //while check_required is already set to its default value, 1 - params->initialize = 1; - params->override = 0; - - //call the config file parser - if (cmd_parser_config_file - (args_info.conffile_arg, &args_info, params) != 0) { - result = 1; - cmd_parser_free(&args_info); - free(params); - } - - - //debugcode: cout << "value of port: " << args_info.port_arg << std::endl; - cout << "value of daemonize: " << + cout << "value of daemonize: " << static_cast(args_info.daemonize_flag) << std::endl; - cout << "value of listen_given: "<< + cout << "value of listen_given: "<< args_info.listen_given << std::endl ; for (unsigned int i = 0; i < args_info.listen_given; i++) cout << "value of listen: " << args_info.listen_arg[i] <check_required = 0; + + if (cmd_parser_config_file("./siccc.conf", &args_info, params) != 0) { + cmd_parser_free(&args_info); + free(params); + return false; } + + params->initialize = 0; + params->override = 1; + params->check_required = 1; + + //call the command line parser + if (cmd_parser(argc, argv, &args_info) != 0) { + cmd_parser_free(&args_info); + free(params); + return false; + } + return true; +} + +int main(int argc, char **argv) +{ + + if ( !configfile_parsing_action(argc, argv) ) { + std::cerr << "ERROR ERROR BEEP" << std::endl; + exit(1); + } + +#ifdef DEBUG + dump_args(); +#endif + MongooseHandler m(args_info.port_arg); if (!args_info.daemonize_flag) { while(1) getchar(); } - return result; + return 0; }