diff --git a/src/net/configuration.c b/src/net/configuration.c index b8a564c..29c8d12 100644 --- a/src/net/configuration.c +++ b/src/net/configuration.c @@ -1,3 +1,4 @@ +#include #include #include "config.h" @@ -14,7 +15,7 @@ int config_set(CONFIGURATION* config, const char* key, const char* value) WARN("Parameter 'config' is NULL!"); return -1; } - SPLIT_STR* splitted_key = splstr(key, '.'); + SPLIT_STR* splitted_key = splstr(key, SEPARATOR_CHAR); if (splitted_key == NULL || splitted_key->count == 0) return -1; if (config->json == NULL) @@ -52,9 +53,30 @@ int config_set(CONFIGURATION* config, const char* key, const char* value) return 0; } -char* config_get(const char* key) +char* config_get(const CONFIGURATION* config, const char* key) { - return "test"; + if (config == NULL || config->json == NULL) + return NULL; + SPLIT_STR* splitted = splstr(key, SEPARATOR_CHAR); + if (splitted == NULL || splitted->count == 0) + return NULL; + json_object* tmp_json = config->json; + char* found_value = NULL; + for (int i = 0; i != splitted->count; i++) + { + json_object* current_json_object = json_object_new_object(); + if (json_object_object_get_ex(tmp_json, splitted->delimited[i], ¤t_json_object)) + tmp_json = current_json_object; + else // No key found + { + strsplfree(splitted); + return NULL; + } + } + if (tmp_json != NULL) + found_value = strdup(json_object_get_string(tmp_json)); + strsplfree(splitted); + return found_value; } char* config_get_default(const char* key, const char* default_value) diff --git a/src/net/include/config.h b/src/net/include/config.h index e15ed24..70aaa88 100644 --- a/src/net/include/config.h +++ b/src/net/include/config.h @@ -9,5 +9,6 @@ typedef struct config_container } CONFIGURATION; int config_set(CONFIGURATION* config, const char* key, const char* value); +char* config_get(const CONFIGURATION* config, const char* key); #endif //CONFIG_H diff --git a/src/server/server.c b/src/server/server.c index 4533393..1c970f8 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -15,6 +15,9 @@ int main(int argc, char *argv[]) config_set(&configuration, "Server.Connection.db", "SQLite"); PRINT_LINE("JSON:\n%s", json_object_to_json_string_ext(configuration.json, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY)); + char* proj_name = config_get(&configuration,"Global.Project.Name"); + PRINT_LINE("Project name: %s", proj_name); + /*config_set("Server.Test", "Test value for server config"); config_set("Server.port", "6920");*/