Greenbone Vulnerability Management Libraries: base/settings.c Source File

Greenbone Vulnerability Management Libraries 23.1.1

settings.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2010-2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
10
11#include "settings.h"
12
13#include <stdio.h>
14#include <string.h> /* for strlen */
15
16#undef G_LOG_DOMAIN
20#define G_LOG_DOMAIN "libgvm base"
21
31static int
32settings_init_from_file (settings_t *settings, const gchar *filename,
33 const gchar *group)
34{
35 GError *error = NULL;
36
37 if (filename == NULL || group == NULL)
38 return -1;
39
40 gchar *contents = NULL;
41
42 if (!g_file_get_contents (filename, &contents, NULL, &error))
43 {
44 g_error_free (error);
45 return -1;
46 }
47
48 if (contents != NULL)
49 {
50 gchar *contents_with_group = g_strjoin ("\n", "[Misc]", contents, NULL);
51 settings->key_file = g_key_file_new ();
52
53 if (!g_key_file_load_from_data (
54 settings->key_file, contents_with_group,
55 strlen (contents_with_group),
56 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error))
57 {
58 g_warning ("Failed to load configuration from %s: %s", filename,
59 error->message);
60 g_error_free (error);
61 g_free (contents_with_group);
62 g_free (contents);
63 return -1;
64 }
65 g_free (contents_with_group);
66 g_free (contents);
67 }
68
69 settings->group_name = g_strdup (group);
70 settings->file_name = g_strdup (filename);
71
72 return 0;
73}
74
80void
81settings_cleanup (settings_t *settings)
82{
83 g_free (settings->group_name);
84 g_free (settings->file_name);
85 g_key_file_free (settings->key_file);
86}
87
97int
98init_settings_iterator_from_file (settings_iterator_t *iterator,
99 const gchar *filename, const gchar *group)
100{
101 int ret;
102 gsize keys_length;
103 GError *error = NULL;
104
105 ret = settings_init_from_file (&iterator->settings, filename, group);
106 if (ret)
107 return ret;
108
109 iterator->keys = g_key_file_get_keys (iterator->settings.key_file, group,
110 &keys_length, &error);
111
112 if (iterator->keys == NULL)
113 {
114 if (error)
115 {
116 g_warning ("Failed to retrieve keys of group %s from %s: %s", group,
117 filename, error->message);
118 g_error_free (error);
119 }
120 g_key_file_free (iterator->settings.key_file);
121 return -1;
122 }
123
124 iterator->current_key = iterator->keys - 1;
125 iterator->last_key = iterator->keys + keys_length - 1;
126
127 return 0;
128}
129
135void
136cleanup_settings_iterator (settings_iterator_t *iterator)
137{
138 g_strfreev (iterator->keys);
139 settings_cleanup (&iterator->settings);
140}
141
149gboolean
150settings_iterator_next (settings_iterator_t *iterator)
151{
152 if (iterator->current_key == iterator->last_key)
153 return FALSE;
154 iterator->current_key++;
155 return TRUE;
156}
157
165const gchar *
166settings_iterator_name (settings_iterator_t *iterator)
167{
168 return *iterator->current_key;
169}
170
178gchar *
179settings_iterator_value (settings_iterator_t *iterator)
180{
181 return g_key_file_get_value (iterator->settings.key_file,
182 iterator->settings.group_name,
183 *iterator->current_key, NULL);
184}
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