1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/*
* Copyright (C) 2010-2011 Robert Ancell.
* Author: Robert Ancell <robert.ancell@canonical.com>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version. See http://www.gnu.org/copyleft/gpl.html the full text of the
* license.
*/
#include "configuration.h"
struct ConfigurationPrivate
{
GKeyFile *key_file;
};
G_DEFINE_TYPE (Configuration, config, G_TYPE_OBJECT);
static Configuration *configuration_instance = NULL;
Configuration *
config_get_instance (void)
{
if (!configuration_instance)
configuration_instance = g_object_new (CONFIGURATION_TYPE, NULL);
return configuration_instance;
}
gboolean
config_load_from_file (Configuration *config, const gchar *path, GError **error)
{
GKeyFile *key_file;
gchar **groups;
int i;
key_file = g_key_file_new ();
if (!g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, error))
return FALSE;
groups = g_key_file_get_groups (key_file, NULL);
for (i = 0; groups[i]; i++)
{
gchar **keys;
int j;
keys = g_key_file_get_keys (key_file, groups[i], NULL, error);
if (!keys)
break;
for (j = 0; keys[j]; j++)
g_key_file_set_value (config->priv->key_file, groups[i], keys[j], g_key_file_get_value (key_file, groups[i], keys[j], NULL));
g_strfreev (keys);
}
g_strfreev (groups);
return TRUE;
}
gchar **
config_get_groups (Configuration *config)
{
return g_key_file_get_groups (config->priv->key_file, NULL);
}
gchar **
config_get_keys (Configuration *config, const gchar *group_name)
{
return g_key_file_get_keys (config->priv->key_file, group_name, NULL, NULL);
}
gboolean
config_has_key (Configuration *config, const gchar *section, const gchar *key)
{
return g_key_file_has_key (config->priv->key_file, section, key, NULL);
}
void
config_set_string (Configuration *config, const gchar *section, const gchar *key, const gchar *value)
{
g_key_file_set_string (config->priv->key_file, section, key, value);
}
gchar *
config_get_string (Configuration *config, const gchar *section, const gchar *key)
{
return g_key_file_get_string (config->priv->key_file, section, key, NULL);
}
void
config_set_string_list (Configuration *config, const gchar *section, const gchar *key, const gchar **value, gsize length)
{
g_key_file_set_string_list (config->priv->key_file, section, key, value, length);
}
gchar **
config_get_string_list (Configuration *config, const gchar *section, const gchar *key)
{
return g_key_file_get_string_list (config->priv->key_file, section, key, NULL, NULL);
}
void
config_set_integer (Configuration *config, const gchar *section, const gchar *key, gint value)
{
g_key_file_set_integer (config->priv->key_file, section, key, value);
}
gint
config_get_integer (Configuration *config, const gchar *section, const gchar *key)
{
return g_key_file_get_integer (config->priv->key_file, section, key, NULL);
}
void
config_set_boolean (Configuration *config, const gchar *section, const gchar *key, gboolean value)
{
g_key_file_set_boolean (config->priv->key_file, section, key, value);
}
gboolean
config_get_boolean (Configuration *config, const gchar *section, const gchar *key)
{
return g_key_file_get_boolean (config->priv->key_file, section, key, NULL);
}
static void
config_init (Configuration *config)
{
config->priv = G_TYPE_INSTANCE_GET_PRIVATE (config, CONFIGURATION_TYPE, ConfigurationPrivate);
config->priv->key_file = g_key_file_new ();
}
static void
config_finalize (GObject *object)
{
Configuration *self;
self = CONFIGURATION (object);
g_key_file_free (self->priv->key_file);
G_OBJECT_CLASS (config_parent_class)->finalize (object);
}
static void
config_class_init (ConfigurationClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = config_finalize;
g_type_class_add_private (klass, sizeof (ConfigurationPrivate));
}
|