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
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 \
--- /dev/null
+#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
--- /dev/null
+#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 */
#include "kill_interpreter_dialog.h"
#include "connect_dialog.h"
+#include "vlp/config.h"
+
/* File resides in top directory (where are Makefiles)*/
#include "../../config.h"
*/
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);
}
/**
class ConnectEntry {
public:
char addr[256];
-
+
ConnectEntry(char *s) {
strcpy(addr, s);
};
+
+ ConnectEntry(const char *s) {
+ strcpy(addr, s);
+ };
};