37 #include "include/em_libconfig_config.h"
39 #define SETTING_NAME_LEN 64
40 #define SETTING_PATH_LEN 256
51 const char *impl_field =
"em_implementation";
52 const char *vers_field =
"config_file_version";
55 config_init(config_rt);
58 if (!config_read_string(config, config_builtin)) {
59 EM_PRINT(
"Failed to read default config: %s(%d): %s\n",
60 config_error_file(config), config_error_line(config),
61 config_error_text(config));
65 filename = getenv(
"EM_CONFIG_FILE");
69 EM_PRINT(
"EM CONFIG FILE: %s\n", filename);
71 if (!config_read_file(config_rt, filename)) {
72 EM_PRINT(
" ERROR: failed to read config file: %s(%d): %s\n\n",
73 config_error_file(config_rt),
74 config_error_line(config_rt),
75 config_error_text(config_rt));
80 if (!config_lookup_string(config, impl_field, &impl) ||
81 !config_lookup_string(config_rt, impl_field, &impl_rt)) {
82 EM_PRINT(
" ERROR: missing mandatory field: %s\n\n",
86 if (!config_lookup_string(config, vers_field, &vers) ||
87 !config_lookup_string(config_rt, vers_field, &vers_rt)) {
88 EM_PRINT(
" ERROR: missing mandatory field: %s\n\n",
92 if (strcmp(impl, impl_rt)) {
93 EM_PRINT(
" ERROR: EM implementation name mismatch:\n"
95 " Found: \"%s\"\n\n", impl, impl_rt);
98 if (strcmp(vers, vers_rt)) {
99 EM_PRINT(
" ERROR: config file version number mismatch:\n"
100 " Expected: \"%s\"\n"
101 " Found: \"%s\"\n\n", vers, vers_rt);
108 EM_PRINT(
"Config file failure\n");
109 config_destroy(config);
110 config_destroy(config_rt);
125 int ret_def = CONFIG_FALSE;
126 int ret_rt = CONFIG_FALSE;
133 return (ret_def == CONFIG_TRUE || ret_rt == CONFIG_TRUE) ? 1 : 0;
139 int ret_def = CONFIG_FALSE;
140 int ret_rt = CONFIG_FALSE;
141 long long value_ll = 0;
148 if (ret_def == CONFIG_TRUE || ret_rt == CONFIG_TRUE) {
149 *value = (int64_t)value_ll;
159 int ret_def = CONFIG_FALSE;
160 int ret_rt = CONFIG_FALSE;
169 if (ret_def == CONFIG_TRUE || ret_rt == CONFIG_TRUE) {
170 *value = cfg_value ? true :
false;
180 int ret_def = CONFIG_FALSE;
181 int ret_rt = CONFIG_FALSE;
188 return (ret_def == CONFIG_TRUE || ret_rt == CONFIG_TRUE) ? 1 : 0;
192 int value[],
int max_num)
194 const config_t *config;
195 const config_setting_t *setting;
199 for (
int j = 0; j < 2; j++) {
205 setting = config_lookup(config, path);
214 if (config_setting_is_array(setting) == CONFIG_FALSE)
217 num = config_setting_length(setting);
219 if (num <= 0 || num > max_num)
222 for (
int i = 0; i < num; i++)
223 value[i] = config_setting_get_int_elem(setting, i);
233 libconfig_setting_t **setting_default,
234 libconfig_setting_t **setting_runtime)
240 int em_libconfig_setting_lookup_int(
const libconfig_setting_t *setting,
241 const char *name,
int *value)
243 return config_setting_lookup_int(setting, name, value);
246 const libconfig_list_t *
247 em_libconfig_setting_get_list(
const libconfig_setting_t *setting,
const char *name)
249 const libconfig_list_t *list_setting;
251 list_setting = config_setting_get_member(setting, name);
253 if (list_setting && config_setting_is_list(list_setting))
259 int em_libconfig_list_length(
const libconfig_list_t *list)
261 return config_setting_length(list);
264 static uint32_t path_get_depth(
const char *path,
char delim)
266 const char *p = path;
283 static int setting_get_child(
const config_setting_t *parent,
const char *path,
284 const char *delim,
const uint32_t depth,
285 char *name, config_setting_t **child)
288 const char *member_name;
289 char path_cp[SETTING_PATH_LEN];
292 strncpy(path_cp, path, SETTING_PATH_LEN - 1);
293 path_cp[SETTING_PATH_LEN - 1] =
'\0';
296 member_name = strtok_r(path_cp, delim, &saveptr);
300 for (uint32_t i = 0; i < depth - 1; i++) {
301 *child = config_setting_get_member(parent, member_name);
307 member_name = strtok_r(NULL, delim, &saveptr);
314 strncpy(name, member_name, SETTING_NAME_LEN - 1);
315 name[SETTING_NAME_LEN - 1] =
'\0';
327 static int list_get_setting(
const libconfig_list_t *list,
int index,
328 const char *path,
char *name,
329 config_setting_t **setting)
332 config_setting_t *element;
335 element = config_setting_get_elem(list, index);
337 EM_LOG(EM_LOG_ERR,
"List element %d does not exist\n", index);
341 depth = path_get_depth(path, delim[0]);
344 strncpy(name, path, SETTING_NAME_LEN - 1);
345 name[SETTING_NAME_LEN - 1] =
'\0';
350 return setting_get_child(element, path, delim, depth, name, setting);
353 libconfig_group_t *em_libconfig_list_lookup_group(
const libconfig_list_t *list,
354 int index,
const char *path)
356 char name[SETTING_NAME_LEN];
357 config_setting_t *setting;
358 libconfig_group_t *group;
360 if (list_get_setting(list, index, path, name, &setting) < 0)
363 group = config_setting_get_member(setting, name);
364 if (group && config_setting_is_group(group))
370 int em_libconfig_list_lookup_int(
const libconfig_list_t *list,
int index,
371 const char *path,
int *value)
373 char name[SETTING_NAME_LEN];
374 config_setting_t *setting;
375 const config_setting_t *member;
377 if (list_get_setting(list, index, path, name, &setting) < 0)
380 member = config_setting_get_member(setting, name);
384 return config_setting_lookup_int(setting, name, value);
387 int em_libconfig_list_lookup_bool(
const libconfig_list_t *list,
int index,
388 const char *path,
bool *value)
391 char name[SETTING_NAME_LEN];
392 config_setting_t *setting;
393 const config_setting_t *member;
395 if (list_get_setting(list, index, path, name, &setting) < 0)
398 member = config_setting_get_member(setting, name);
402 if (!config_setting_lookup_bool(setting, name, &cfg_value))
405 *value = cfg_value ? true :
false;
409 int em_libconfig_list_lookup_string(
const libconfig_list_t *list,
int index,
410 const char *path,
const char **value)
412 char name[SETTING_NAME_LEN];
413 config_setting_t *setting;
414 const config_setting_t *member;
416 if (list_get_setting(list, index, path, name, &setting) < 0)
419 member = config_setting_get_member(setting, name);
423 return config_setting_lookup_string(setting, name, value);
433 static int group_get_setting(libconfig_list_t *group,
const char *path,
434 char *name, config_setting_t **setting)
439 depth = path_get_depth(path, delim[0]);
442 strncpy(name, path, SETTING_NAME_LEN - 1);
443 name[SETTING_NAME_LEN - 1] =
'\0';
448 return setting_get_child(group, path, delim, depth, name, setting);
452 *em_libconfig_group_lookup_group(libconfig_group_t *group,
const char *path)
454 char name[SETTING_NAME_LEN];
455 config_setting_t *setting;
456 libconfig_group_t *group_out;
458 if (group_get_setting(group, path, name, &setting) < 0)
461 group_out = config_setting_get_member(setting, name);
462 if (group_out && config_setting_is_group(group_out))
469 *em_libconfig_group_lookup_list(libconfig_group_t *group,
const char *path)
471 libconfig_list_t *list;
472 config_setting_t *setting;
473 char name[SETTING_NAME_LEN];
475 if (group_get_setting(group, path, name, &setting) < 0)
478 list = config_setting_get_member(setting, name);
479 if (list && config_setting_is_list(list))
485 int em_libconfig_group_lookup_int(
const libconfig_group_t *group,
486 const char *name,
int *value)
488 return config_setting_lookup_int(group, name, value);
491 int em_libconfig_group_lookup_bool(
const libconfig_group_t *group,
492 const char *name,
bool *value)
496 if (!config_setting_lookup_bool(group, name, &cfg_value))
499 *value = cfg_value ? true :
false;
503 int em_libconfig_group_lookup_string(
const libconfig_group_t *group,
504 const char *name,
const char **value)
506 return config_setting_lookup_string(group, name, value);
509 static int lookup_int(
const config_t *cfg,
510 const char *base_path,
511 const char *local_path,
518 snprintf(path,
sizeof(path),
"%s.%s.%s", base_path,
520 if (config_lookup_int(cfg, path, value) == CONFIG_TRUE)
524 snprintf(path,
sizeof(path),
"%s.%s", base_path, name);
525 if (config_lookup_int(cfg, path, value) == CONFIG_TRUE)
532 const char *base_path,
const char *local_path,
533 const char *name,
int *value )
536 base_path, local_path, name, value))
540 base_path, local_path, name, value))
551 FILE *file = tmpfile();
557 "\nEM_CONFIG_FILE default values:\n"
558 "-------------------------------\n\n") < 0)
565 "\nEM_CONFIG_FILE override values:\n"
566 "--------------------------------\n\n") < 0)
574 while ((c = fgetc(file)) != EOF)
575 EM_PRINT(
"%c", (
char)c);