mirror of
https://gitlab.com/mobian1/eg25-manager.git
synced 2025-08-29 15:22:20 +02:00
src: add helper functions for reading config files
This commit is contained in:
83
src/config.c
Normal file
83
src/config.c
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Arnaud Ferraris <arnaud.ferraris@gmail.com>
|
||||
*
|
||||
* 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;
|
||||
}
|
26
src/config.h
Normal file
26
src/config.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Arnaud Ferraris <arnaud.ferraris@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#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);
|
@@ -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;
|
||||
|
@@ -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',
|
||||
|
Reference in New Issue
Block a user