Added getter for configuration.

This commit is contained in:
Max 2024-01-03 23:23:05 +01:00
parent 1b66d5edab
commit 334df8f9f0
3 changed files with 29 additions and 3 deletions

View File

@ -1,3 +1,4 @@
#include <string.h>
#include <json-c/json_object.h>
#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], &current_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)

View File

@ -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

View File

@ -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");*/