1
// IniConfigurator.cc --- Configuration Access
3
// Copyright (C) 2005, 2006, 2007, 2008 Rob Caelers <robc@krandor.nl>
4
// All rights reserved.
6
// This program is free software: you can redistribute it and/or modify
7
// it under the terms of the GNU General Public License as published by
8
// the Free Software Foundation, either version 3 of the License, or
9
// (at your option) any later version.
11
// This program is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
// GNU General Public License for more details.
16
// You should have received a copy of the GNU General Public License
17
// along with this program. If not, see <http://www.gnu.org/licenses/>.
32
#include "GlibIniConfigurator.hh"
37
GlibIniConfigurator::GlibIniConfigurator()
43
GlibIniConfigurator::~GlibIniConfigurator()
47
g_key_file_free(config);
53
GlibIniConfigurator::load(string filename)
58
last_filename = filename;
60
TRACE_ENTER_MSG("GlibIniConfigurator::load", filename)
61
config = g_key_file_new();
63
r = g_key_file_load_from_file(config, filename.c_str(),
64
G_KEY_FILE_KEEP_COMMENTS, &error);
81
GlibIniConfigurator::save(string filename)
84
char *str = g_key_file_to_data(config, NULL, &error);
86
TRACE_ENTER_MSG("GlibIniConfigurator::save", filename);
93
ofstream config_file(filename.c_str());
101
return error == NULL;
106
GlibIniConfigurator::save()
108
return save(last_filename);
113
GlibIniConfigurator::remove_key(const std::string &key)
116
GError *error = NULL;
120
TRACE_ENTER_MSG("GlibIniConfigurator::remove_key", key);
121
split_key(key, group, inikey);
122
inikey = key_inify(inikey);
124
g_key_file_remove_key(config, group.c_str(), inikey.c_str(), &error);
138
GlibIniConfigurator::get_value(const std::string &key, VariantType type,
142
GError *error = NULL;
146
TRACE_ENTER_MSG("GlibIniConfigurator::get_value", key);
147
split_key(key, group, inikey);
148
inikey = key_inify(inikey);
154
case VARIANT_TYPE_INT:
155
out.int_value = g_key_file_get_integer(config, group.c_str(), inikey.c_str(), &error);
158
case VARIANT_TYPE_BOOL:
159
out.bool_value = g_key_file_get_boolean(config, group.c_str(), inikey.c_str(), &error);
162
case VARIANT_TYPE_DOUBLE:
164
char *s = g_key_file_get_string(config, group.c_str(), inikey.c_str(), &error);
165
if (error == NULL && s != NULL)
167
sscanf(s, "%lf", &out.double_value);
172
case VARIANT_TYPE_NONE:
173
out.type = VARIANT_TYPE_STRING;
176
case VARIANT_TYPE_STRING:
178
char *s = g_key_file_get_string(config, group.c_str(), inikey.c_str(), &error);
179
if (error == NULL && s != NULL)
181
out.string_value = s;
201
GlibIniConfigurator::set_value(const std::string &key, Variant &value)
207
split_key(key, group, inikey);
208
inikey = key_inify(inikey);
212
case VARIANT_TYPE_INT:
213
g_key_file_set_integer(config, group.c_str(), inikey.c_str(), value.int_value);
216
case VARIANT_TYPE_BOOL:
217
g_key_file_set_boolean(config, group.c_str(), inikey.c_str(), value.bool_value);
220
case VARIANT_TYPE_DOUBLE:
223
sprintf(buf, "%f", value.double_value);
225
split_key(key, group, inikey);
226
inikey = key_inify(inikey);
228
g_key_file_set_string(config, group.c_str(), inikey.c_str(), buf);
232
case VARIANT_TYPE_NONE:
233
case VARIANT_TYPE_STRING:
234
g_key_file_set_string(config, group.c_str(), inikey.c_str(), value.string_value.c_str());
246
GlibIniConfigurator::split_key(const string &key, string &group, string &out_key) const
248
const char *s = key.c_str();
249
const char *slash = strchr(s, '/');
252
group = key.substr(0, slash-s);
264
GlibIniConfigurator::key_inify(const string &key) const
267
for (unsigned int i = 0; i < rc.length(); i++)