settings.c
Go to the documentation of this file.
/* SPDX-FileCopyrightText: 2010-2023 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "settings.h"
#include <stdio.h>
#include <string.h> /* for strlen */
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "libgvm base"
static int
settings_init_from_file (settings_t *settings, const gchar *filename,
const gchar *group)
{
GError *error = NULL;
if (filename == NULL || group == NULL)
return -1;
gchar *contents = NULL;
if (!g_file_get_contents (filename, &contents, NULL, &error))
{
g_error_free (error);
return -1;
}
if (contents != NULL)
{
gchar *contents_with_group = g_strjoin ("\n", "[Misc]", contents, NULL);
settings->key_file = g_key_file_new ();
if (!g_key_file_load_from_data (
settings->key_file, contents_with_group,
strlen (contents_with_group),
G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error))
{
g_warning ("Failed to load configuration from %s: %s", filename,
error->message);
g_error_free (error);
g_free (contents_with_group);
g_free (contents);
return -1;
}
g_free (contents_with_group);
g_free (contents);
}
settings->group_name = g_strdup (group);
settings->file_name = g_strdup (filename);
return 0;
}
void
settings_cleanup (settings_t *settings)
{
g_free (settings->group_name);
g_free (settings->file_name);
g_key_file_free (settings->key_file);
}
int
init_settings_iterator_from_file (settings_iterator_t *iterator,
const gchar *filename, const gchar *group)
{
int ret;
gsize keys_length;
GError *error = NULL;
ret = settings_init_from_file (&iterator->settings, filename, group);
if (ret)
return ret;
iterator->keys = g_key_file_get_keys (iterator->settings.key_file, group,
&keys_length, &error);
if (iterator->keys == NULL)
{
if (error)
{
g_warning ("Failed to retrieve keys of group %s from %s: %s", group,
filename, error->message);
g_error_free (error);
}
g_key_file_free (iterator->settings.key_file);
return -1;
}
iterator->current_key = iterator->keys - 1;
iterator->last_key = iterator->keys + keys_length - 1;
return 0;
}
void
cleanup_settings_iterator (settings_iterator_t *iterator)
{
g_strfreev (iterator->keys);
settings_cleanup (&iterator->settings);
}
gboolean
settings_iterator_next (settings_iterator_t *iterator)
{
if (iterator->current_key == iterator->last_key)
return FALSE;
iterator->current_key++;
return TRUE;
}
const gchar *
settings_iterator_name (settings_iterator_t *iterator)
{
return *iterator->current_key;
}
gchar *
settings_iterator_value (settings_iterator_t *iterator)
{
return g_key_file_get_value (iterator->settings.key_file,
iterator->settings.group_name,
*iterator->current_key, NULL);
}
settings_init_from_file
static int settings_init_from_file(settings_t *settings, const gchar *filename, const gchar *group)
Initialise a settings struct from a file.
Definition settings.c:32
settings_iterator_next
gboolean settings_iterator_next(settings_iterator_t *iterator)
Increment an iterator.
Definition settings.c:150
settings_cleanup
void settings_cleanup(settings_t *settings)
Cleanup a settings structure.
Definition settings.c:81
cleanup_settings_iterator
void cleanup_settings_iterator(settings_iterator_t *iterator)
Cleanup a settings iterator.
Definition settings.c:136
init_settings_iterator_from_file
int init_settings_iterator_from_file(settings_iterator_t *iterator, const gchar *filename, const gchar *group)
Initialise a settings iterator from a file.
Definition settings.c:98
settings_iterator_value
gchar * settings_iterator_value(settings_iterator_t *iterator)
Get the value from a settings iterator.
Definition settings.c:179
settings_iterator_name
const gchar * settings_iterator_name(settings_iterator_t *iterator)
Get the name from a settings iterator.
Definition settings.c:166
settings.h
Protos and data structures for configuration file management.
settings_iterator_t
Struct holding options to iterate over a GKeyFile.
Definition settings.h:36
settings_iterator_t::current_key
gchar ** current_key
Definition settings.h:39
settings_iterator_t::settings
settings_t settings
Definition settings.h:38
settings_iterator_t::keys
gchar ** keys
Definition settings.h:37
settings_iterator_t::last_key
gchar ** last_key
Definition settings.h:40
settings_t
Struct holding options for settings taken from a key-value config file.
Definition settings.h:23
settings_t::key_file
GKeyFile * key_file
Definition settings.h:26
settings_t::group_name
gchar * group_name
Definition settings.h:25
settings_t::file_name
gchar * file_name
Definition settings.h:24