summaryrefslogtreecommitdiff
path: root/glib/src/keyfile.ccg
blob: da3f8216164bcabba9b68a9364b361fd82a2c13d (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* Copyright 2006 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

namespace Glib
{

/**** Glib::KeyFile ********************************************************/

KeyFile::KeyFile()
{
  gobject_ = g_key_file_new();
  owns_gobject_ = true;
}

KeyFile::KeyFile(GKeyFile* castitem, bool takes_ownership)
{
  gobject_ = castitem;
  owns_gobject_ = takes_ownership;
}

KeyFile::~KeyFile()
{
  if (owns_gobject_)
    g_key_file_free(gobject_);
}

bool KeyFile::load_from_data(const Glib::ustring& data, KeyFileFlags flags)
{
  GError* error = 0;

  const gboolean result = g_key_file_load_from_data(
      gobj(), data.c_str(), data.bytes(),
      static_cast<GKeyFileFlags>(unsigned(flags)),
      &error);

  if(error)
    Glib::Error::throw_exception(error);

  return (result != 0);
}

bool KeyFile::load_from_data_dirs(const std::string& file, std::string& full_path,
                                  KeyFileFlags flags)
{
  GError* error = 0;
  char* full_path_c = 0;

  const gboolean result = g_key_file_load_from_data_dirs(
      gobj(), file.c_str(), &full_path_c,
      static_cast<GKeyFileFlags>(unsigned(flags)),
      &error);

  if(error)
    Glib::Error::throw_exception(error);

  if(full_path_c)
    full_path = Glib::ScopedPtr<char>(full_path_c).get();
  else
    full_path.erase();

  return (result != 0);
}

Glib::ustring KeyFile::to_data()
{
  GError* error = 0;
  char *const str = g_key_file_to_data(gobj(), 0, &error);

  if(error)
    Glib::Error::throw_exception(error);

  return Glib::convert_return_gchar_ptr_to_ustring(str);
}

Glib::ArrayHandle<Glib::ustring> KeyFile::get_groups() const
{
  gsize length = 0;
  char** const array = g_key_file_get_groups(const_cast<GKeyFile*>(gobj()), &length);

  return Glib::ArrayHandle<Glib::ustring>(array, length, Glib::OWNERSHIP_DEEP);
}

Glib::ArrayHandle<Glib::ustring> KeyFile::get_keys(const Glib::ustring& group_name) const
{
  gsize length  = 0;
  GError* error = 0;

  char** const array = g_key_file_get_keys(
      const_cast<GKeyFile*>(gobj()),
      (group_name.empty()) ? 0 : group_name.c_str(),
      &length, &error);

  if(error)
    Glib::Error::throw_exception(error);

  return Glib::ArrayHandle<Glib::ustring>(array, length, Glib::OWNERSHIP_DEEP);
}

Glib::ustring KeyFile::get_locale_string(const Glib::ustring& group_name,
                                         const Glib::ustring& key) const
{
  GError* error = 0;
  char *const str = g_key_file_get_locale_string(
      const_cast<GKeyFile*>(gobj()),
      (group_name.empty()) ? 0 : group_name.c_str(),
      key.c_str(), 0, &error);

  if(error)
    Glib::Error::throw_exception(error);

  return Glib::convert_return_gchar_ptr_to_ustring(str);
}

#ifdef GLIBMM_EXCEPTIONS_ENABLED
int KeyFile::get_integer(const Glib::ustring& key) const
#else
int KeyFile::get_integer(const Glib::ustring& key, std::auto_ptr<Glib::Error>& error) const
#endif //GLIBMM_EXCEPTIONS_ENABLED
{
  GError* gerror = 0;
  const int value = g_key_file_get_integer(const_cast<GKeyFile*>(gobj()),
                                           0, key.c_str(), &gerror);
  if(gerror)
#ifdef GLIBMM_EXCEPTIONS_ENABLED
    Glib::Error::throw_exception(gerror);
#else
    error = Glib::Error::throw_exception(gerror);
#endif
  return value;
}

#ifdef GLIBMM_EXCEPTIONS_ENABLED
double KeyFile::get_double(const Glib::ustring& key) const
#else
double KeyFile::get_double(const Glib::ustring& key, std::auto_ptr<Glib::Error>& error) const
#endif //GLIBMM_EXCEPTIONS_ENABLED
{
  GError* gerror = 0;
  double retvalue = g_key_file_get_double(const_cast<GKeyFile*>(gobj()), NULL, key.c_str(), &(gerror));
#ifdef GLIBMM_EXCEPTIONS_ENABLED
  if(gerror)
    ::Glib::Error::throw_exception(gerror);
#else
  if(gerror)
    error = ::Glib::Error::throw_exception(gerror);
#endif //GLIBMM_EXCEPTIONS_ENABLED

  return retvalue;
}

void KeyFile::set_double(const Glib::ustring& key, double value)
{
  g_key_file_set_double(gobj(), 0, key.c_str(), value); 
}

// TODO: alternative code path with exceptions disabled
Glib::ArrayHandle<Glib::ustring> KeyFile::get_string_list(const Glib::ustring& group_name,
                                                          const Glib::ustring& key) const
{
  gsize length  = 0;
  GError* error = 0;

  char** const array = g_key_file_get_string_list(
      const_cast<GKeyFile*>(gobj()),
      (group_name.empty()) ? 0 : group_name.c_str(),
      key.c_str(), &length, &error);

  if(error)
    Glib::Error::throw_exception(error);

  return Glib::ArrayHandle<Glib::ustring>(array, length, Glib::OWNERSHIP_DEEP);
}

// TODO: alternative code path with exceptions disabled
Glib::ArrayHandle<Glib::ustring> KeyFile::get_locale_string_list(const Glib::ustring& group_name,
                                                                 const Glib::ustring& key,
                                                                 const Glib::ustring& locale) const
{
  gsize length  = 0;
  GError* error = 0;

  char** const array = g_key_file_get_locale_string_list(
      const_cast<GKeyFile*>(gobj()),
      (group_name.empty()) ? 0 : group_name.c_str(),
      key.c_str(), locale.c_str(), &length, &error);

  if(error)
    Glib::Error::throw_exception(error);

  return Glib::ArrayHandle<Glib::ustring>(array, length, Glib::OWNERSHIP_DEEP);
}

// TODO: alternative code path with exceptions disabled
Glib::ArrayHandle<bool> KeyFile::get_boolean_list(const Glib::ustring& group_name,
                                                  const Glib::ustring& key) const
{
  gsize length  = 0;
  GError* error = 0;

  gboolean *const array = g_key_file_get_boolean_list(
      const_cast<GKeyFile*>(gobj()),
      (group_name.empty()) ? 0 : group_name.c_str(),
      key.c_str(), &length, &error);

  if(error)
    Glib::Error::throw_exception(error);

  return Glib::ArrayHandle<bool>(array, length, Glib::OWNERSHIP_SHALLOW);
}

Glib::ArrayHandle<int> KeyFile::get_integer_list(const Glib::ustring& group_name,
                                                 const Glib::ustring& key) const
{
  gsize length  = 0;
  GError* error = 0;

  int *const array = g_key_file_get_integer_list(
      const_cast<GKeyFile*>(gobj()),
      (group_name.empty()) ? 0 : group_name.c_str(),
      key.c_str(), &length, &error);

  if(error)
    Glib::Error::throw_exception(error);

  return Glib::ArrayHandle<int>(array, length, Glib::OWNERSHIP_SHALLOW);
}

Glib::ArrayHandle<double> KeyFile::get_double_list(const Glib::ustring& group_name, const Glib::ustring& key) const
{
  gdouble* integer_list   = 0;
  gsize length_of_list = 0; 
  GError* error        = 0;
  integer_list = g_key_file_get_double_list(const_cast<GKeyFile*>(gobj()), group_name.c_str(), key.c_str(), &length_of_list, &error);
  if(error)
    Glib::Error::throw_exception(error);
  return Glib::ArrayHandle<double>(integer_list, length_of_list, Glib::OWNERSHIP_DEEP);
}

void KeyFile::set_string_list(const Glib::ustring& group_name, const Glib::ustring& key,
                              const Glib::ArrayHandle<Glib::ustring>& list)
{
  g_key_file_set_string_list(gobj(), (group_name.empty()) ? 0 : group_name.c_str(),
                             key.c_str(), list.data(), list.size());
}

void KeyFile::set_locale_string_list(const Glib::ustring& group_name,
                                     const Glib::ustring& key, const Glib::ustring& locale,
                                     const Glib::ArrayHandle<Glib::ustring>& list)
{
  g_key_file_set_locale_string_list(gobj(), (group_name.empty()) ? 0 : group_name.c_str(),
                                    key.c_str(), locale.c_str(), list.data(), list.size());
}

void KeyFile::set_integer_list(const Glib::ustring& group_name, const Glib::ustring& key,
                               const Glib::ArrayHandle<int>& list)
{
  g_key_file_set_integer_list(gobj(), (group_name.empty()) ? 0 : group_name.c_str(),
                              key.c_str(), const_cast<int*>(list.data()), list.size());
}

void KeyFile::set_double_list(const Glib::ustring& group_name, const Glib::ustring& key,
                              const Glib::ArrayHandle<double>& list)
{
  g_key_file_set_double_list(gobj(), group_name.c_str(), key.c_str(),
                             const_cast<double*>(list.data()), list.size());
}

void KeyFile::set_boolean_list(const Glib::ustring& group_name, const Glib::ustring& key,
                               const Glib::ArrayHandle<bool>& list)
{
  g_key_file_set_boolean_list(gobj(), (group_name.empty()) ? 0 : group_name.c_str(),
                              key.c_str(), const_cast<gboolean*>(list.data()), list.size());
}

Glib::ustring KeyFile::get_comment() const
{
  GError* error = 0;
  char *const str = g_key_file_get_comment(const_cast<GKeyFile*>(gobj()), 0, 0, &error);

  if(error)
    Glib::Error::throw_exception(error);

  return Glib::convert_return_gchar_ptr_to_ustring(str);
}

Glib::ustring KeyFile::get_comment(const Glib::ustring& group_name) const
{
  GError* error = 0;
  char *const str = g_key_file_get_comment(const_cast<GKeyFile*>(gobj()),
                                           (group_name.empty()) ? 0 : group_name.c_str(),
                                           0, &error);
  if(error)
    Glib::Error::throw_exception(error);

  return Glib::convert_return_gchar_ptr_to_ustring(str);
}

void KeyFile::set_comment(const Glib::ustring& comment)
{
  GError* error = 0;
  g_key_file_set_comment(gobj(), 0, 0, comment.c_str(), &error);

  if(error)
    Glib::Error::throw_exception(error);
}

void KeyFile::set_comment(const Glib::ustring& group_name, const Glib::ustring& comment)
{
  GError* error = 0;
  g_key_file_set_comment(gobj(), (group_name.empty()) ? 0 : group_name.c_str(),
                         0, comment.c_str(), &error);
  if(error)
    Glib::Error::throw_exception(error);
}

} // namespace Glib