aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorJulien Dessaux2019-08-01 18:04:48 +0200
committerJulien Dessaux2019-08-01 18:04:48 +0200
commit3f38170efd67a9869cb451642fd4cd0a1f2dfb5f (patch)
treefea8e0b5ddcb43780036cec8c33a1268feb36ac7 /common
parentInitial import (diff)
downloadrocket-cli-client-3f38170efd67a9869cb451642fd4cd0a1f2dfb5f.tar.gz
rocket-cli-client-3f38170efd67a9869cb451642fd4cd0a1f2dfb5f.tar.bz2
rocket-cli-client-3f38170efd67a9869cb451642fd4cd0a1f2dfb5f.zip
Added configuration file parsing
Diffstat (limited to '')
-rw-r--r--common/CMakeLists.txt1
-rw-r--r--common/config.c58
-rw-r--r--common/config.h.in8
3 files changed, 67 insertions, 0 deletions
diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt
index 8636758..aa5402a 100644
--- a/common/CMakeLists.txt
+++ b/common/CMakeLists.txt
@@ -1,3 +1,4 @@
file(GLOB_RECURSE SOURCES *.c)
+include_directories("${CMAKE_CURRENT_BINARY_DIR}")
ADD_LIBRARY(common STATIC ${SOURCES})
diff --git a/common/config.c b/common/config.c
new file mode 100644
index 0000000..3320d7c
--- /dev/null
+++ b/common/config.c
@@ -0,0 +1,58 @@
+#include <libconfig.h>
+#include <stdlib.h>
+
+#include "config.h"
+
+config_t * config = NULL;
+
+char // returns 0 if ok, greater than 0 otherwise
+config_load(const char *config_file)
+{
+ config = malloc(sizeof(config_t));
+ config_init(config);
+ config_set_tab_width(config, 4);
+ if (config_read_file(config, config_file) != CONFIG_TRUE) {
+ switch(config_error_type(config)) {
+ case CONFIG_ERR_NONE:
+ fprintf(stderr, "Configuration read error with none type reported... This shouldn't happen!\n");
+ break;
+ case CONFIG_ERR_FILE_IO:
+ fprintf(stderr, "Configuration I/O error, the most common cause is a file not found at %s\n", CONFIG_PATH);
+ break;
+ case CONFIG_ERR_PARSE:
+ fprintf(stderr, "Configuration parse error\n");
+ break;
+ }
+ fprintf(stderr, "Configuration read error occured at %s:%d %s\n", config_error_file(config), config_error_line(config), config_error_text(config));
+ return 1;
+ }
+ return 0;
+
+}
+
+const char * config_get_web_url(void)
+{
+ const char * url;
+ if (config_lookup_string(config, "web_url", &url) != CONFIG_TRUE) {
+ return DEFAULT_WEB_URL;
+ }
+ return url;
+}
+
+const char * config_get_wss_url(void)
+{
+ const char * url;
+ if (config_lookup_string(config, "wss_url", &url) != CONFIG_TRUE) {
+ return DEFAULT_WSS_URL;
+ }
+ return url;
+}
+
+void config_clean(void)
+{
+ if (config != NULL) {
+ config_destroy(config);
+ free(config);
+ config = NULL;
+ }
+}
diff --git a/common/config.h.in b/common/config.h.in
index 2f41d8d..9f27b70 100644
--- a/common/config.h.in
+++ b/common/config.h.in
@@ -6,4 +6,12 @@
#define VERSION "@PROJECT_VERSION@"
#define GIT_HASH "@GIT_HASH@"
+#define DEFAULT_WEB_URL "http://localhost:3000/"
+#define DEFAULT_WSS_URL "ws://localhost:3000/websocket"
+
+char config_load(const char *config_file);
+const char * config_get_web_url(void);
+const char * config_get_wss_url(void);
+void config_clean(void);
+
#endif