From 55ed2dc39cc543da2db4b6d7a35119710e5aa12d Mon Sep 17 00:00:00 2001 From: Arnaud Ferraris Date: Mon, 4 Oct 2021 19:29:49 +0200 Subject: [PATCH] src: add helper functions for reading config files --- src/config.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ src/config.h | 26 ++++++++++++++++ src/manager.h | 6 ++++ src/meson.build | 1 + 4 files changed, 116 insertions(+) create mode 100644 src/config.c create mode 100644 src/config.h diff --git a/src/config.c b/src/config.c new file mode 100644 index 0000000..d4ce151 --- /dev/null +++ b/src/config.c @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2020 Arnaud Ferraris + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include "config.h" +#include "toml.h" + +gboolean config_get_bool(toml_table_t **config, const gchar *key, gboolean *result) +{ + toml_datum_t value = { .ok = 0 }; + + if (config[EG25_CONFIG_USER]) + value = toml_bool_in(config[EG25_CONFIG_USER], key); + if (!value.ok) + value = toml_bool_in(config[EG25_CONFIG_SYS], key); + if (value.ok && result) + *result = value.u.b; + + return !!value.ok; +} + +gboolean config_get_int(toml_table_t **config, const gchar *key, gint *result) +{ + toml_datum_t value = { .ok = 0 }; + + if (config[EG25_CONFIG_USER]) + value = toml_int_in(config[EG25_CONFIG_USER], key); + if (!value.ok) + value = toml_int_in(config[EG25_CONFIG_SYS], key); + if (value.ok && result) + *result = value.u.i; + + return !!value.ok; +} + +gboolean config_get_uint(toml_table_t **config, const gchar *key, guint *result) +{ + gint value; + gboolean found; + + found = config_get_int(config, key, &value); + if (found) { + if (value <= 0 || value >= G_MAXUINT) { + g_message("Value out of range for [%s], discarding", key); + found = FALSE; + } + } + + if (found && result) + *result = (guint) value; + + return found; +} + +gboolean config_get_string(toml_table_t **config, const gchar *key, gchar **result) +{ + toml_datum_t value = { .ok = 0 }; + + if (config[EG25_CONFIG_USER]) + value = toml_string_in(config[EG25_CONFIG_USER], key); + if (!value.ok) + value = toml_string_in(config[EG25_CONFIG_SYS], key); + if (value.ok && result) + *result = value.u.s; + + return !!value.ok; +} + +gboolean config_get_array(toml_table_t **config, const gchar *key, toml_array_t **result) +{ + toml_array_t *array = NULL; + + if (config[EG25_CONFIG_USER]) + array = toml_array_in(config[EG25_CONFIG_USER], key); + if (!array) + array = toml_array_in(config[EG25_CONFIG_SYS], key); + if (array && result) + *result = array; + + return !!array; +} diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..d1a214f --- /dev/null +++ b/src/config.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2020 Arnaud Ferraris + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include + +#include "manager.h" +#include "toml.h" + +/* + * Helper functions for parsing config files: each function retrieves the + * value for key `key`, with the user config file having priority over the + * default config file. The values are stored in `result`. + * + * They all return TRUE if the value was found, FALSE otherwise. + */ + +gboolean config_get_bool(toml_table_t **config, const gchar *key, gboolean *result); +gboolean config_get_int(toml_table_t **config, const gchar *key, gint *result); +gboolean config_get_uint(toml_table_t **config, const gchar *key, guint *result); +gboolean config_get_string(toml_table_t **config, const gchar *key, gchar **result); +gboolean config_get_array(toml_table_t **config, const gchar *key, toml_array_t **result); diff --git a/src/manager.h b/src/manager.h index 5e2440e..d2598d8 100644 --- a/src/manager.h +++ b/src/manager.h @@ -62,6 +62,12 @@ enum ModemIface { MODEM_IFACE_OFONO }; +enum EG25Config { + EG25_CONFIG_SYS = 0, + EG25_CONFIG_USER, + EG25_CONFIG_COUNT +}; + struct EG25Manager { GMainLoop *loop; guint reset_timer; diff --git a/src/meson.build b/src/meson.build index 69a7e7e..cc13d2f 100644 --- a/src/meson.build +++ b/src/meson.build @@ -9,6 +9,7 @@ subdir('libgdbofono') src = [ 'at.c', 'at.h', + 'config.c', 'config.h', 'gpio.c', 'gpio.h', 'manager.c', 'manager.h', 'ofono-iface.c', 'ofono-iface.h',