From: Rafał Długołęcki Date: Tue, 26 Jan 2016 22:32:24 +0000 (+0100) Subject: Separate class for handling configuration file X-Git-Tag: 3.4-b1~42 X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=fc17660a0464b22d77cf610fced6414351a596ed;p=vlp.git Separate class for handling configuration file --- diff --git a/Makefile.am b/Makefile.am index 46d0beb..bbfc2ba 100644 --- a/Makefile.am +++ b/Makefile.am @@ -70,7 +70,8 @@ bin_logker_SOURCES = \ src/kernel/options.cpp \ src/kernel/options.moc.cpp \ src/kernel/kill_interpreter_dialog.cpp \ - src/kernel/connect_dialog.cpp + src/kernel/connect_dialog.cpp \ + src/global/vlp/config.cpp bin_logker_CPPFLAGS = $(bin_logker_CFLAGS) bin_logker_LDADD = $(bin_logker_LIBS) -lconfig++ -lX11 bin_logkerdir = src/kernel @@ -79,7 +80,8 @@ bin_logker_HEADERS = \ src/kernel/lock.h \ src/kernel/options.h \ src/kernel/kill_interpreter_dialog.h \ - src/kernel/connect_dialog.h + src/kernel/connect_dialog.h \ + src/global/vlp/config.h src/kernel/kernel.moc.cpp: \ src/kernel/kernel.h \ src/kernel/ui/kernelwindow.h \ diff --git a/src/global/vlp/config.cpp b/src/global/vlp/config.cpp new file mode 100644 index 0000000..2191cd8 --- /dev/null +++ b/src/global/vlp/config.cpp @@ -0,0 +1,162 @@ +#include "vlp/config.h" + +#include + +namespace loglan { +namespace vlp { + +Config::Config() +{ + config_init(&cfg); + file = NULL; +} + +Config::~Config() +{ + config_destroy(&cfg); + if (file) { + fclose(file); + file = NULL; + } +} + +bool Config::load(const char * configFilePath) +{ + file = fopen(configFilePath, "rt"); + if (!file) { + fprintf(stderr, + "Error: Cannot load configuration file: %s!\n", + configFilePath + ); + return false; + } + + if (!config_read(&cfg, file)) { + fprintf(stderr, + "%s! In file %s, line %d\n", + config_error_text(&cfg), + config_error_file(&cfg), + config_error_line(&cfg)); + return false; + } + + return true; +} + +bool Config::save(const char * configFilePath) +{ + return false; +} + +int Config::getNodeNumber() +{ + config_setting_t *setting = config_lookup(&cfg, "node_number"); + + if (!setting) { + fprintf(stderr, + "%s! In file %s, '%s' was not found.\n", + "Warning", + config_error_file(&cfg), + "node_number" + ); + + throw; + } + + return config_setting_get_int(setting); +} +void Config::setNodeNumber(int nodeNumber) +{ +} + +std::vector Config::getHosts() +{ + config_setting_t *setting = config_lookup(&cfg, "host"); + std::vector hosts; + + if (!setting) { + fprintf(stderr, + "%s! In file %s, '%s' was not found.\n", + "Warning", + "configuration",//config_error_file(&cfg), + "host" + ); + + return hosts; + } + + switch(config_setting_type(setting)) { + /* TODO: Deprecated. Made for back compatibility. */ + case CONFIG_TYPE_STRING: { + std::string host(config_setting_get_string(setting)); + hosts.push_back(host); + break; + } + case CONFIG_TYPE_ARRAY: { + int size = config_setting_length(setting); + std::string host; + for (int i = 0; i < size; i++) { + host = config_setting_get_string_elem(setting, i); + hosts.push_back(host); + } + break; + } + default: + fprintf(stderr, + "%s! In file %s, bad entry type for %s will not be read.\n", + "Warning", + "configuration",//config_error_file(&cfg), + "host" + ); + } + + return hosts; +} + +ConnectionType Config::getConnectionType() +{ + config_setting_t *setting = config_lookup(&cfg, "type"); + + if (!setting) { + fprintf(stderr, + "%s! In file %s, '%s' was not found.\n", + "Warning", + "configuration",//config_error_file(&cfg), + "type" + ); + + throw; + } + + /* same as strcmp(..) == 0 */ + if (!strcmp(config_setting_get_string(setting), "explicit")) { + return EXPLICIT; + } + + return REGISTER; +} +void Config::setConnectionType(ConnectionType type) +{ +} + +const char * Config::getProgramDir() +{ + config_setting_t *setting = config_lookup(&cfg, "progdir"); + + if (!setting) { + fprintf(stderr, + "%s! In file %s, '%s' was not found.\n", + "Warning", + "configuration",//config_error_file(&cfg), + "progdir" + ); + } + + return config_setting_get_string(setting); +} +void Config::setProgramDir(const char * programDir) +{ +} + +} +} \ No newline at end of file diff --git a/src/global/vlp/config.h b/src/global/vlp/config.h new file mode 100644 index 0000000..e1bf59b --- /dev/null +++ b/src/global/vlp/config.h @@ -0,0 +1,58 @@ +#ifndef __VLP_CONFIG_H +#define __VLP_CONFIG_H + +/** + * + * @author Rafał Długołęcki + */ + + +#include +#include +#include +#include + +#include + +namespace loglan { +namespace vlp { + +enum ConnectionType { + EXPLICIT = 1, + REGISTER = 2 +}; + +/** + * Common config class used for handling configuration file I/O + */ +class Config +{ +private: + config_t cfg; + config_setting_t *setting; + + FILE * file; + +public: + Config(); + ~Config(); + + bool load(const char * configFilePath); + bool save(const char * configFilePath); + + int getNodeNumber(); + void setNodeNumber(int nodeNumber); + + std::vector getHosts(); + + ConnectionType getConnectionType(); + void setConnectionType(ConnectionType type); + + const char * getProgramDir(); + void setProgramDir(const char * programDir); +}; + +} +} + +#endif /* __VLP_CONFIG_H */ diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index d67aced..c420a7f 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -71,6 +71,8 @@ #include "kill_interpreter_dialog.h" #include "connect_dialog.h" +#include "vlp/config.h" + /* File resides in top directory (where are Makefiles)*/ #include "../../config.h" @@ -196,93 +198,19 @@ void QKernel::loadConfig(const QString & fname) */ void QKernel::loadConfig(const char * fname) { - config_t cfg; - config_setting_t *setting; - - /* Hack for checking if file exists without using external libs.*/ - - FILE * file = fopen(fname, "rt"); - if (!file) { - fprintf(stderr, "Error: Cannot load configuration file: %s!\n", - fname); - exit(3); - } - /* File exists, so file has been locked. Release it. */ - - config_init(&cfg); - - /* Read the file. If there is an error, report it and exit. */ - if (!config_read(&cfg, file)) { - fprintf(stderr, "%s! In file %s, line %d\n", - config_error_text(&cfg), - config_error_file(&cfg), - config_error_line(&cfg)); - config_destroy(&cfg); - fclose(file); - exit(3); - } + loglan::vlp::Config config; + if(config.load(fname)) { + NodeNumber = config.getNodeNumber(); + ConType = config.getConnectionType(); - setting = config_lookup(&cfg, "node_number"); - if (setting) { - NodeNumber = config_setting_get_int(setting); - } else { - fprintf(stderr, "%s! In file %s, '%s' was not found.\n", - "Warning", fname, "node_number"); - config_destroy(&cfg); - fclose(file); - exit(3); - } - setting = config_lookup(&cfg, "type"); - if (setting) { - /* same as strcmp(..) == 0 */ - if (!strcmp(config_setting_get_string(setting), "explicit")) { - ConType = 1; - } else { - ConType = 2; + std::vector hosts = config.getHosts(); + for (int i = 0; i < hosts.size(); i++) { + ConnectList.append(new ConnectEntry(hosts[i].c_str())); } - } else { - fprintf(stderr, "%s! In file %s, '%s' was not found.\n", - "Warning", fname, "type"); - } - setting = config_lookup(&cfg, "host"); - if (setting) { - switch(config_setting_type(setting)) { - /* TODO: Deprecated. Made for back compatibility. */ - case CONFIG_TYPE_STRING: - ConnectList.append(new ConnectEntry((char*) - config_setting_get_string(setting))); - break; - case CONFIG_TYPE_ARRAY: { - int size = config_setting_length(setting); - for (int i = 0; i < size; i++) { - ConnectList.append(new ConnectEntry((char*) - config_setting_get_string_elem(setting, - i))); - } - break; - } - default: - fprintf(stderr, "%s! In file %s, bad entry type for %s." - " Will not be read.\n", - "Error", fname, "host"); - } - } else { - fprintf(stderr, "%s! In file %s, '%s' was not found.\n", - "Warning", fname, "host"); + strncpy(progdir, config.getProgramDir(), 256); } - - setting = config_lookup(&cfg, "progdir"); - if (setting){ - strncpy(progdir, config_setting_get_string(setting), 256); - } else { - fprintf(stderr, "%s! In file %s, '%s' was not found.\n", - "Warning", fname, "progdir"); - } - - config_destroy(&cfg); - fclose(file); } /** diff --git a/src/kernel/kernel.h b/src/kernel/kernel.h index 18478a9..0de37a7 100644 --- a/src/kernel/kernel.h +++ b/src/kernel/kernel.h @@ -83,10 +83,14 @@ public: class ConnectEntry { public: char addr[256]; - + ConnectEntry(char *s) { strcpy(addr, s); }; + + ConnectEntry(const char *s) { + strcpy(addr, s); + }; };