1 /* Copyright(C) 2006 The gtkmm Development Team
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or(at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the Free
15 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <glibmm/ustring.h>
21 #include <glibmm/arrayhandle.h>
22 #include <glibmm/error.h>
23 #include <glibmm/utility.h>
26 #ifndef DOXYGEN_SHOULD_SKIP_THIS
27 extern "C" { typedef struct _GKeyFile GKeyFile; }
33 _WRAP_ENUM(KeyFileFlags, GKeyFileFlags, NO_GTYPE)
35 /** Exception class for KeyFile errors.
37 _WRAP_GERROR(KeyFileError, GKeyFileError, G_KEY_FILE_ERROR, NO_GTYPE)
39 /** This class lets you parse, edit or create files containing groups of key-value pairs, which we call key files
40 * for lack of a better name. Several freedesktop.org specifications use key files now, e.g the Desktop Entry
41 * Specification and the Icon Theme Specification.
43 * The syntax of key files is described in detail in the Desktop Entry Specification, here is a quick summary: Key
44 * files consists of groups of key-value pairs, interspersed with comments.
47 * # this is just an example
48 * # there can be comments before the first group
52 * Name=Key File Example\tthis value shows\nescaping
54 * # localized strings are stored in multiple key-value pairs
64 * Booleans=true;false;true;true
67 * Lines beginning with a '#' and blank lines are considered comments.
69 * Groups are started by a header line containing the group name enclosed in '[' and ']', and ended implicitly by
70 * the start of the next group or the end of the file. Each key-value pair must be contained in a group.
72 * Key-value pairs generally have the form key=value, with the exception of localized strings, which have the form
73 * key[locale]=value. Space before and after the '=' character are ignored. Newline, tab, carriage return and
74 * backslash characters in value are escaped as \\n, \\t, \\r, and \\\\, respectively. To preserve leading spaces in
75 * values, these can also be escaped as \\s.
77 * Key files can store strings (possibly with localized variants), integers, booleans and lists of these. Lists are
78 * separated by a separator character, typically ';' or ','. To use the list separator character in a value in a
79 * list, it has to be escaped by prefixing it with a backslash.
81 * This syntax is obviously inspired by the .ini files commonly met on Windows, but there are some important
83 * - .ini files use the ';' character to begin comments, key files use the '#' character.
84 * - Key files allow only comments before the first group.
85 * - Key files are always encoded in UTF-8.
86 * - Key and Group names are case-sensitive, for example a group called [GROUP] is a different group from [group].
88 * Note that in contrast to the Desktop Entry Specification, groups in key files may contain the same key multiple
89 * times; the last entry wins. Key files may also contain multiple groups with the same name; they are merged
90 * together. Another difference is that keys and group names in key files are not restricted to ASCII characters.
96 _CLASS_GENERIC(KeyFile, GKeyFile)
99 /** Creates a new, empty KeyFile object.
106 _IGNORE(g_key_file_free)
108 /** Creates a glibmm KeyFile wrapper for a GKeyFile object.
109 * Note, when using this that when the wrapper is deleted,
110 * it will not automatically deleted the GKeyFile unless you
111 * set the delete_c_instance boolean to true.
112 * @param castitem The C instance to wrap
113 * @param delete_c_instance If the C instance should be deleted when
114 * the wrapper is deleted.
116 KeyFile(GKeyFile* castitem, bool takes_ownership = false);
120 _WRAP_METHOD(bool load_from_file(const std::string& filename, KeyFileFlags flags = Glib::KEY_FILE_NONE), g_key_file_load_from_file, errthrow)
122 /** Loads a KeyFile from memory
123 * @param data The data to use as a KeyFile
124 * @param flags Bitwise combination of the flags to use for the KeyFile
125 * @return true if the KeyFile was successfully loaded, false otherwise
126 * @throw Glib::KeyFileError
129 bool load_from_data(const Glib::ustring& data, KeyFileFlags flags = Glib::KEY_FILE_NONE);
130 _IGNORE(g_key_file_load_from_data)
134 gboolean g_key_file_load_from_dirs (GKeyFile *key_file,
136 const gchar **search_dirs,
142 /** Looks for a KeyFile named @a file in the paths returned from
143 * g_get_user_data_dir() and g_get_system_data_dirs() and loads them
144 * into the keyfile object, placing the full path to the file in
146 * @param file The file to search for
147 * @param full_path Return location for a string containing the full path of the file
148 * @param flags Bitwise combination of the flags to use for the KeyFile
149 * @return true if the KeyFile was successfully loaded, false otherwise
150 * @throw Glib::KeyFileError
151 * @throw Glib::FileError
153 bool load_from_data_dirs(const std::string& file, std::string& full_path, KeyFileFlags flags = Glib::KEY_FILE_NONE);
154 _IGNORE(g_key_file_load_from_data_dirs)
156 /** Outputs the KeyFile as a string
157 * @return A string object holding the contents of KeyFile
159 Glib::ustring to_data();
160 _IGNORE(g_key_file_to_data)
162 _WRAP_METHOD(Glib::ustring get_start_group() const, g_key_file_get_start_group)
164 /** Gets a list of all groups in the KeyFile
165 * @returns A list containing the names of the groups
167 Glib::ArrayHandle<Glib::ustring> get_groups() const;
168 _IGNORE(g_key_file_get_groups)
170 /** Gets a list of all keys from the group @a group_name.
171 * @param group_name The name of a group
172 * @returns A list containing the names of the keys in @a group_name
174 Glib::ArrayHandle<Glib::ustring> get_keys(const Glib::ustring& group_name) const;
175 _IGNORE(g_key_file_get_keys)
177 _WRAP_METHOD(bool has_group(const Glib::ustring& group_name) const, g_key_file_has_group)
178 _WRAP_METHOD(bool has_key(const Glib::ustring& group_name, const Glib::ustring& key) const, g_key_file_has_key, errthrow)
180 _WRAP_METHOD(Glib::ustring get_value(const Glib::ustring& group_name, const Glib::ustring& key) const, g_key_file_get_value, errthrow)
181 _WRAP_METHOD(Glib::ustring get_string(const Glib::ustring& group_name, const Glib::ustring& key) const, g_key_file_get_string, errthrow)
183 /** Gets the value associated with @a key under @a group_name translated
184 * into the current locale.
186 Glib::ustring get_locale_string(const Glib::ustring& group_name, const Glib::ustring& key) const;
188 _WRAP_METHOD(Glib::ustring get_locale_string(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& locale) const, g_key_file_get_locale_string, errthrow)
189 _WRAP_METHOD(bool get_boolean(const Glib::ustring& group_name, const Glib::ustring& key) const, g_key_file_get_boolean, errthrow)
191 /** Gets the value in the first group, under @a key, interpreting it as
193 * @param key The name of the key
194 * @return The value of @a key as an integer
195 * @throws Glib::KeyFileError
197 #ifdef GLIBMM_EXCEPTIONS_ENABLED
198 int get_integer(const Glib::ustring& key) const;
200 int get_integer(const Glib::ustring& key, std::auto_ptr<Glib::Error>& error) const;
202 _WRAP_METHOD(int get_integer(const Glib::ustring& group_name, const Glib::ustring& key) const, g_key_file_get_integer, errthrow)
204 /** Gets the value in the first group, under @a key, interpreting it as
206 * @param key The name of the key
207 * @return The value of @a key as an double
208 * @throws Glib::KeyFileError
212 #ifdef GLIBMM_EXCEPTIONS_ENABLED
213 double get_double(const Glib::ustring& key) const;
215 double get_double(const Glib::ustring& key, std::auto_ptr<Glib::Error>& error) const;
217 _WRAP_METHOD(double get_double(const Glib::ustring& group_name, const Glib::ustring& key) const, g_key_file_get_double, errthrow)
219 _WRAP_METHOD(void set_double(const Glib::ustring& group_name, const Glib::ustring& key, double value), g_key_file_set_double)
221 /** Associates a new double value with @a key under the start group.
222 * If @a key cannot be found then it is created.
226 * @param value An double value.
228 void set_double(const Glib::ustring& key, double value);
230 /** Returns the values associated with @a key under @a group_name
231 * @param group_name The name of a group
232 * @param key The name of a key
233 * @return A list containing the values requested
234 * @throws Glib::KeyFileError
236 #ifdef GLIBMM_EXCEPTIONS_ENABLED
237 Glib::ArrayHandle<Glib::ustring> get_string_list(const Glib::ustring& group_name, const Glib::ustring& key) const;
239 Glib::ArrayHandle<Glib::ustring> get_string_list(const Glib::ustring& group_name, const Glib::ustring& key, std::auto_ptr<Glib::Error>& error) const;
241 _IGNORE(g_key_file_get_string_list)
243 /** Returns the values associated with @a key under @a group_name
244 * translated into the current locale, if available.
245 * @param group_name The name of a group
246 * @param key The name of a key
247 * @return A list containing the values requested
248 * @throws Glib::KeyFileError
250 #ifdef GLIBMM_EXCEPTIONS_ENABLED
251 Glib::ArrayHandle<Glib::ustring> get_locale_string_list(const Glib::ustring& group_name, const Glib::ustring& key) const;
253 Glib::ArrayHandle<Glib::ustring> get_locale_string_list(const Glib::ustring& group_name, const Glib::ustring& key, std::auto_ptr<Glib::Error>& error) const;
256 /** Returns the values associated with @a key under @a group_name
257 * translated into @a locale, if available.
258 * @param group_name The name of a group
259 * @param key The name of a key
260 * @param locale The name of a locale
261 * @return A list containing the values requested
262 * @throws Glib::KeyFileError
264 #ifdef GLIBMM_EXCEPTIONS_ENABLED
265 Glib::ArrayHandle<Glib::ustring> get_locale_string_list(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& locale) const;
267 Glib::ArrayHandle<Glib::ustring> get_locale_string_list(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& locale, std::auto_ptr<Glib::Error>& error) const;
269 _IGNORE(g_key_file_get_locale_string_list)
271 /** Returns the values associated with @a key under @a group_name
272 * @param group_name The name of a group
273 * @param key The name of a key
274 * @return A list of booleans
275 * @throws Glib::KeyFileError
277 #ifdef GLIBMM_EXCEPTIONS_ENABLED
278 Glib::ArrayHandle<bool> get_boolean_list(const Glib::ustring& group_name, const Glib::ustring& key) const;
280 Glib::ArrayHandle<bool> get_boolean_list(const Glib::ustring& group_name, const Glib::ustring& key,
281 std::auto_ptr<Glib::Error>& error) const;
283 _IGNORE(g_key_file_get_boolean_list)
285 /** Returns the values associated with @a key under @a group_name
286 * @param group_name The name of a group
287 * @param key The name of a key
288 * @return A list of integers
289 * @throws Glib::KeyFileError
291 #ifdef GLIBMM_EXCEPTIONS_ENABLED
292 Glib::ArrayHandle<int> get_integer_list(const Glib::ustring& group_name, const Glib::ustring& key) const;
294 Glib::ArrayHandle<int> get_integer_list(const Glib::ustring& group_name, const Glib::ustring& key,
295 std::auto_ptr<Glib::Error>& error) const;
297 _IGNORE(g_key_file_get_integer_list)
299 /** Returns the values associated with @a key under @a group_name
300 * @param group_name The name of a group
301 * @param key The name of a key
302 * @return A list of doubles
303 * @throws Glib::KeyFileError
305 #ifdef GLIBMM_EXCEPTIONS_ENABLED
306 Glib::ArrayHandle<double> get_double_list(const Glib::ustring& group_name, const Glib::ustring& key) const;
308 Glib::ArrayHandle<double> get_double_list(const Glib::ustring& group_name, const Glib::ustring& key,
309 std::auto_ptr<Glib::Error>& error) const;
311 _IGNORE(g_key_file_get_double_list)
313 /** Get comment from top of file
314 * @return The comment
316 #ifdef GLIBMM_EXCEPTIONS_ENABLED
317 Glib::ustring get_comment() const;
319 Glib::ustring get_comment(std::auto_ptr<Glib::Error>& error) const;
322 /** Get comment from above a group
323 * @param group_name The group
324 * @return The comment
326 #ifdef GLIBMM_EXCEPTIONS_ENABLED
327 Glib::ustring get_comment(const Glib::ustring& group_name) const;
329 Glib::ustring get_comment(const Glib::ustring& group_name, std::auto_ptr<Glib::Error>& error) const;
332 _WRAP_METHOD(Glib::ustring get_comment(const Glib::ustring& group_name, const Glib::ustring& key) const, g_key_file_get_comment, errthrow)
334 _WRAP_METHOD(void set_list_separator(gchar separator), g_key_file_set_list_separator)
335 _WRAP_METHOD(void set_value(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& value), g_key_file_set_value)
336 _WRAP_METHOD(void set_string(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& string), g_key_file_set_string)
337 _WRAP_METHOD(void set_locale_string(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& locale, const Glib::ustring& string), g_key_file_set_locale_string)
338 _WRAP_METHOD(void set_boolean(const Glib::ustring& group_name, const Glib::ustring& key, bool value), g_key_file_set_boolean)
339 _WRAP_METHOD(void set_integer(const Glib::ustring& group_name, const Glib::ustring& key, int value), g_key_file_set_integer)
341 /** Sets a list of string values for @a key under @a group_name. If
342 * key cannot be found it is created. If @a group_name cannot be found
344 * @param group_name The name of a group
345 * @param key The name of a key
346 * @param list A list holding objects of type Glib::ustring
348 void set_string_list(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ArrayHandle<Glib::ustring>& list);
349 _IGNORE(g_key_file_set_string_list)
351 /** Sets a list of string values for the @a key under @a group_name and marks
352 * them as being for @a locale. If the @a key or @a group_name cannot be
353 * found, they are created.
354 * @param group_name The name of a group
355 * @param key The name of a key
356 * @param locale A locale
357 * @param list A list holding objects of type Glib::ustring
359 void set_locale_string_list(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& locale, const Glib::ArrayHandle<Glib::ustring>& list);
360 _IGNORE(g_key_file_set_locale_string_list)
362 /** Sets a list of booleans for the @a key under @a group_name.
363 * If either the @a key or @a group_name cannot be found they are created.
364 * @param group_name The name of a group
365 * @param key The name of a key
366 * @param list A list holding object of type bool
368 void set_boolean_list(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ArrayHandle<bool>& list);
369 _IGNORE(g_key_file_set_boolean_list)
371 /** Sets a list of integers for the @a key under @a group_name.
372 * If either the @a key or @a group_name cannot be found they are created.
373 * @param group_name The name of a group
374 * @param key The name of a key
375 * @param list A list holding object of type int
377 void set_integer_list(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ArrayHandle<int>& list);
378 _IGNORE(g_key_file_set_integer_list)
380 /** Sets a list of doubles for the @a key under @a group_name.
381 * If either the @a key or @a group_name cannot be found they are created.
382 * @param group_name The name of a group
383 * @param key The name of a key
384 * @param list A list holding object of type int
388 void set_double_list(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ArrayHandle<double>& list);
389 _IGNORE(g_key_file_set_double_list)
392 /** Places @a comment at the start of the file, before the first group.
393 * @param comment The Comment
395 #ifdef GLIBMM_EXCEPTIONS_ENABLED
396 void set_comment(const Glib::ustring& comment);
398 void set_comment(const Glib::ustring& comment, std::auto_ptr<Glib::Error>& error);
401 /** Places @a comment above @a group_name.
402 * @param group_name The Group the comment should be above
403 * @param comment The comment
405 #ifdef GLIBMM_EXCEPTIONS_ENABLED
406 void set_comment(const Glib::ustring& group_name, const Glib::ustring& comment);
408 void set_comment(const Glib::ustring& group_name, const Glib::ustring& comment,
409 std::auto_ptr<Glib::Error>& error);
412 /** Places a comment above @a key from @a group_name.
413 * @param key Key comment should be above
414 * @param group_name Group comment is in
415 * @param comment The comment
417 _WRAP_METHOD(void set_comment(const Glib::ustring& group_name, const Glib::ustring& key, const Glib::ustring& comment), g_key_file_set_comment, errthrow)
419 _WRAP_METHOD(void remove_comment(const Glib::ustring& group_name, const Glib::ustring& key), g_key_file_remove_comment, errthrow)
420 _WRAP_METHOD(void remove_key(const Glib::ustring& group_name, const Glib::ustring& key), g_key_file_remove_key, errthrow)
421 _WRAP_METHOD(void remove_group(const Glib::ustring& group_name), g_key_file_remove_group, errthrow)
423 GKeyFile* gobj() { return gobject_; }
424 const GKeyFile* gobj() const { return gobject_; }
432 KeyFile(const KeyFile&);
433 KeyFile& operator=(const KeyFile&);