mirror of
https://github.com/hmaxnl/netex.git
synced 2025-01-18 15:34:20 +01:00
Added getter for configuration.
This commit is contained in:
parent
1b66d5edab
commit
334df8f9f0
|
@ -1,3 +1,4 @@
|
||||||
|
#include <string.h>
|
||||||
#include <json-c/json_object.h>
|
#include <json-c/json_object.h>
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
@ -14,7 +15,7 @@ int config_set(CONFIGURATION* config, const char* key, const char* value)
|
||||||
WARN("Parameter 'config' is NULL!");
|
WARN("Parameter 'config' is NULL!");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
SPLIT_STR* splitted_key = splstr(key, '.');
|
SPLIT_STR* splitted_key = splstr(key, SEPARATOR_CHAR);
|
||||||
if (splitted_key == NULL || splitted_key->count == 0)
|
if (splitted_key == NULL || splitted_key->count == 0)
|
||||||
return -1;
|
return -1;
|
||||||
if (config->json == NULL)
|
if (config->json == NULL)
|
||||||
|
@ -52,9 +53,30 @@ int config_set(CONFIGURATION* config, const char* key, const char* value)
|
||||||
return 0;
|
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)
|
char* config_get_default(const char* key, const char* default_value)
|
||||||
|
|
|
@ -9,5 +9,6 @@ typedef struct config_container
|
||||||
} CONFIGURATION;
|
} CONFIGURATION;
|
||||||
|
|
||||||
int config_set(CONFIGURATION* config, const char* key, const char* value);
|
int config_set(CONFIGURATION* config, const char* key, const char* value);
|
||||||
|
char* config_get(const CONFIGURATION* config, const char* key);
|
||||||
|
|
||||||
#endif //CONFIG_H
|
#endif //CONFIG_H
|
||||||
|
|
|
@ -15,6 +15,9 @@ int main(int argc, char *argv[])
|
||||||
config_set(&configuration, "Server.Connection.db", "SQLite");
|
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));
|
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.Test", "Test value for server config");
|
||||||
config_set("Server.port", "6920");*/
|
config_set("Server.port", "6920");*/
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user