OpenVAS Libraries  9.0.3
settings.c
Go to the documentation of this file.
1 /* openvas-libraries/base
2  * $Id$
3  * Description: Implementation of API to handle configuration file management
4  *
5  * Authors:
6  * Matthew Mundell <matthew.mundell@intevation.de>
7  * Michael Wiegand <michael.wiegand@intevation.de>
8  *
9  * Copyright:
10  * Copyright (C) 2010 Greenbone Networks GmbH
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26 
33 #include <string.h>
34 #include <stdio.h>
35 #include <errno.h>
36 #include <sys/stat.h>
37 
38 #include "settings.h"
39 
49 int
50 settings_init_from_file (settings_t * settings, const gchar * filename,
51  const gchar * group)
52 {
53  GError *error = NULL;
54 
55  if (filename == NULL || group == NULL)
56  return -1;
57 
58  gchar *contents = NULL;
59 
60  if (!g_file_get_contents (filename, &contents, NULL, &error))
61  {
62  g_error_free (error);
63  return -1;
64  }
65 
66  if (contents != NULL)
67  {
68  gchar *contents_with_group = g_strjoin ("\n", "[Misc]", contents, NULL);
69  settings->key_file = g_key_file_new ();
70 
71  if (!g_key_file_load_from_data
72  (settings->key_file, contents_with_group, strlen (contents_with_group),
73  G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error))
74  {
75  g_warning ("Failed to load configuration from %s: %s", filename,
76  error->message);
77  g_error_free (error);
78  g_free (contents_with_group);
79  g_free (contents);
80  return -1;
81  }
82  g_free (contents_with_group);
83  g_free (contents);
84  }
85 
86  settings->group_name = g_strdup (group);
87  settings->file_name = g_strdup (filename);
88 
89  return 0;
90 }
91 
97 void
99 {
100  g_free (settings->group_name);
101  g_free (settings->file_name);
102  g_key_file_free (settings->key_file);
103 }
104 
114 int
116  filename, const gchar * group)
117 {
118  int ret;
119  gsize keys_length;
120  GError *error = NULL;
121 
122  ret = settings_init_from_file (&iterator->settings, filename, group);
123  if (ret)
124  return ret;
125 
126  iterator->keys =
127  g_key_file_get_keys (iterator->settings.key_file, group, &keys_length,
128  &error);
129 
130  if (iterator->keys == NULL)
131  {
132  if (error)
133  {
134  g_warning ("Failed to retrieve keys of group %s from %s: %s", group,
135  filename, error->message);
136  g_error_free (error);
137  }
138  g_key_file_free (iterator->settings.key_file);
139  return -1;
140  }
141 
142  iterator->current_key = iterator->keys - 1;
143  iterator->last_key = iterator->keys + keys_length - 1;
144 
145  return 0;
146 }
147 
153 void
155 {
156  g_strfreev (iterator->keys);
157  settings_cleanup (&iterator->settings);
158 }
159 
167 gboolean
169 {
170  if (iterator->current_key == iterator->last_key)
171  return FALSE;
172  iterator->current_key++;
173  return TRUE;
174 }
175 
183 const gchar *
185 {
186  return *iterator->current_key;
187 }
188 
196 const gchar *
198 {
199  return g_key_file_get_value (iterator->settings.key_file,
200  iterator->settings.group_name,
201  *iterator->current_key, NULL);
202 }
const gchar * settings_iterator_name(settings_iterator_t *iterator)
Get the name from a settings iterator.
Definition: settings.c:184
GKeyFile * key_file
Definition: settings.h:43
gboolean settings_iterator_next(settings_iterator_t *iterator)
Increment an iterator.
Definition: settings.c:168
void settings_cleanup(settings_t *settings)
Cleanup a settings structure.
Definition: settings.c:98
gchar ** current_key
Definition: settings.h:52
gchar ** last_key
Definition: settings.h:53
void cleanup_settings_iterator(settings_iterator_t *iterator)
Cleanup a settings iterator.
Definition: settings.c:154
gchar * file_name
Definition: settings.h:41
const gchar * settings_iterator_value(settings_iterator_t *iterator)
Get the value from a settings iterator.
Definition: settings.c:197
gchar * group_name
Definition: settings.h:42
Protos and data structures for configuration file management.
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:115
int settings_init_from_file(settings_t *settings, const gchar *filename, const gchar *group)
Initialise a settings struct from a file.
Definition: settings.c:50
settings_t settings
Definition: settings.h:51