From: Rafał Długołęcki Date: Wed, 10 Jul 2013 14:01:28 +0000 (+0200) Subject: Experimental usage. X-Git-Url: https://git.dlugolecki.net.pl/?p=vlp.git;a=commitdiff_plain;h=refs%2Fheads%2Fvlp-12-exp Experimental usage. --- diff --git a/src/global/AppConfiguration.cpp b/src/global/AppConfiguration.cpp index b29b76e..6b718ce 100644 --- a/src/global/AppConfiguration.cpp +++ b/src/global/AppConfiguration.cpp @@ -1,9 +1,54 @@ +#include +#include "genint1.h" #include "AppConfiguration.h" -void AppConfiguration::error(config_t *cfg) +AppConfiguration::AppConfiguration(const char * fname) { + config_init(&cfg); + + /* Read the file. If there is an error, report it and exit. */ + if(!config_read_file(&cfg, fname)) + { + error(); + config_destroy(&cfg); + exit(3);/* from original code. */ + } +} +int AppConfiguration::getInt(const char * path) +{ + int value = 0; + if (config_lookup_int(&cfg, path, &value) == CONFIG_FALSE) + { + fprintf(stderr, "Warning: %s was not found, or bad type requested\n", path); + } + return value; +} + +const char * AppConfiguration::getString(const char * path) +{ + const char * value; + if (config_lookup_string(&cfg, path, &value) == CONFIG_FALSE) + { + fprintf(stderr, "Warning: %s was not found, or bad type requested\n", path); + } + return value; +} + +int AppConfiguration::error() +{ + if (config_error_type(&cfg) == CONFIG_ERR_NONE) + { + return FALSE; + } + fprintf(stderr, "%s: In file %s, line %d\n", - config_error_text(cfg), - config_error_file(cfg), - config_error_line(cfg)); + config_error_text(&cfg), + config_error_file(&cfg), + config_error_line(&cfg)); + return TRUE; +} + +void AppConfiguration::release() +{ + config_destroy(&cfg); } diff --git a/src/global/AppConfiguration.h b/src/global/AppConfiguration.h index 23e2eb0..f1679cd 100644 --- a/src/global/AppConfiguration.h +++ b/src/global/AppConfiguration.h @@ -5,6 +5,11 @@ */ class AppConfiguration { protected: + config_t cfg; public: - static void error(config_t *cfg); + AppConfiguration(const char * fname); + int getInt(const char * path); + const char * getString(const char * path); + int error(); + void release(); }; diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index 7e83ebc..df3df49 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -294,11 +294,6 @@ void QKernel::n_impl() void QKernel::LoadConfig(char * fname) { - config_t cfg; - const char *str; - - config_init(&cfg); - /* Hack for checking if file exists without using external libs.*/ FILE * file = fopen(fname, "r"); if (!file) { @@ -308,53 +303,30 @@ void QKernel::LoadConfig(char * fname) /* File exists, so file has been locked. Release it. */ fclose(file); - /* Read the file. If there is an error, report it and exit. */ - if(!config_read_file(&cfg, fname)) - { - AppConfiguration::error(&cfg); - config_destroy(&cfg); - exit(3);/* from original code. */ - } - - if(!config_lookup_int(&cfg, "node_number", &NodeNumber)) - { - AppConfiguration::error(&cfg); - config_destroy(&cfg); - exit(3); - } + AppConfiguration config(fname); - if(config_lookup_string(&cfg, "type", &str)){ - ConType = (strcmp(str, "explicit") == 0) ? 1 : 2; - } - else { - AppConfiguration::error(&cfg); + NodeNumber = config.getInt("node_number"); + if (config.error() == TRUE) { + config.release(); + exit(3); } + ConType = (strcmp("explicit", config.getString("type")) == 0) ? 1 : 2; + config.error(); - if(config_lookup_string(&cfg, "host", &str)) { - char host[255]; - strcpy(host, str);//FIXME: buffer overflow - ConnectList.append(new ConnectEntry(host)); - } - else { - AppConfiguration::error(&cfg); + char host[255]; + strcpy(host, config.getString("host"));//FIXME: buffer overflow + if (config.error() == FALSE) { + ConnectList.append(new ConnectEntry(config.getString("host"))); } - if(config_lookup_string(&cfg, "progdir", &str)){ - strcpy(progdir, str);//FIXME: buffer overflow - } - else { - AppConfiguration::error(&cfg); - } + strcpy(progdir, config.getString("progdir"));//FIXME: buffer overflow + config.error(); - if(config_lookup_string(&cfg, "homedir", &str)){ - strcpy(HomeDir, str);//FIXME: buffer overflow - } - else { - AppConfiguration::error(&cfg); - } + strcpy(HomeDir, config.getString("homedir"));//FIXME: buffer overflow + config.error(); - config_destroy(&cfg); + config.release(); } /* +++++++++++++++++++++++++++++++++++++++++++++++ */ diff --git a/src/net/lognet.cpp b/src/net/lognet.cpp index fce3aee..9657142 100644 --- a/src/net/lognet.cpp +++ b/src/net/lognet.cpp @@ -186,12 +186,8 @@ NETMOD::NETMOD(char *kernel_name) void NETMOD::load_config(char *fname) { - config_t cfg; - const char *str; int on,k=0; NETlink *pomlink; - - config_init(&cfg); /* Hack for checking if file exists without using external libs.*/ @@ -204,33 +200,22 @@ void NETMOD::load_config(char *fname) /* File exists, so file has been locked. Release it. */ fclose(file); - /* Read the file. If there is an error, report it and exit. */ - if(!config_read_file(&cfg, fname)) - { - AppConfiguration::error(&cfg); - config_destroy(&cfg); - exit(3);/* from original code. */ - } + AppConfiguration config(fname); - if(config_lookup_int(&cfg, "node_number", &MyNode)) - { - if (MyNode==-1) { - write_at_console("Node number must be specified"); - config_destroy(&cfg); - exit(1); - }; - } - else - { - AppConfiguration::error(&cfg); - config_destroy(&cfg); + MyNode = config.getInt("node_number"); + if (config.error() == TRUE) { + write_at_console("Node number must be specified"); + config.release(); exit(1); } - if(config_lookup_string(&cfg, "host", &str)) { + char host[255]; + strcpy(host, config.getString("host")); + if(config.error() == FALSE) + { k++; pomlink = new NETlink; - strcpy(pomlink->addr, str); + strcpy(pomlink->addr, host); pomlink->connected = FALSE; pomlink->sock = socket(AF_INET, SOCK_STREAM, 0); fcntl(pomlink->sock, F_SETFL,O_NONBLOCK | fcntl(pomlink->sock,F_GETFL,0)); @@ -239,11 +224,7 @@ void NETMOD::load_config(char *fname) Links.append(pomlink); to_connect++; } - else { - AppConfiguration::error(&cfg); - } - - config_destroy(&cfg); + config.release(); if (k==0) all_connected=TRUE; }