Separate class for handling configuration file
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Tue, 26 Jan 2016 22:32:24 +0000 (23:32 +0100)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Tue, 26 Jan 2016 22:32:24 +0000 (23:32 +0100)
Makefile.am
src/global/vlp/config.cpp [new file with mode: 0644]
src/global/vlp/config.h [new file with mode: 0644]
src/kernel/kernel.cpp
src/kernel/kernel.h

index 46d0bebbbd966e958e21758f7e8cfa7db3e54d28..bbfc2ba8e22c4617662e47d470351fe90ab1db43 100644 (file)
@@ -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 (file)
index 0000000..2191cd8
--- /dev/null
@@ -0,0 +1,162 @@
+#include "vlp/config.h"
+
+#include <cstring>
+
+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<std::string> Config::getHosts()
+{
+       config_setting_t *setting = config_lookup(&cfg, "host");
+       std::vector<std::string> 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 (file)
index 0000000..e1bf59b
--- /dev/null
@@ -0,0 +1,58 @@
+#ifndef __VLP_CONFIG_H
+#define __VLP_CONFIG_H
+
+/**
+ * 
+ * @author Rafał Długołęcki
+ */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <vector>
+#include <string>
+
+#include <libconfig.h>
+
+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<std::string> getHosts();
+
+       ConnectionType getConnectionType();
+       void setConnectionType(ConnectionType type);
+
+       const char * getProgramDir();
+       void setProgramDir(const char * programDir);
+};
+
+}
+}
+
+#endif /* __VLP_CONFIG_H */
index d67acede310ea76508526958845534f70152c89c..c420a7f3a6f8914b215f4079b43e4e3ff7e550cb 100644 (file)
@@ -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<std::string> 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);
 }
 
 /**
index 18478a943966538c0ed110189b07694e16647d9d..0de37a747e68b6bb31d5787501b214d45668a9bd 100644 (file)
@@ -83,10 +83,14 @@ public:
 class ConnectEntry {
 public:
        char addr[256];
-       
+
        ConnectEntry(char *s) {
                strcpy(addr, s);
        };
+
+       ConnectEntry(const char *s) {
+               strcpy(addr, s);
+       };
 };