Update German translation
[glib.git] / glib / gkeyfile.c
blob34814663a7041920090df1d3c5e7521671a8f796
1 /* gkeyfile.c - key file parser
3 * Copyright 2004 Red Hat, Inc.
4 * Copyright 2009-2010 Collabora Ltd.
5 * Copyright 2009 Nokia Corporation
7 * Written by Ray Strode <rstrode@redhat.com>
8 * Matthias Clasen <mclasen@redhat.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #include "config.h"
26 #include "gkeyfile.h"
27 #include "gutils.h"
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <locale.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #ifdef G_OS_UNIX
38 #include <unistd.h>
39 #endif
40 #ifdef G_OS_WIN32
41 #include <io.h>
43 #undef fstat
44 #define fstat(a,b) _fstati64(a,b)
45 #undef stat
46 #define stat _stati64
48 #ifndef S_ISREG
49 #define S_ISREG(mode) ((mode)&_S_IFREG)
50 #endif
52 #endif /* G_OS_WIN23 */
54 #include "gconvert.h"
55 #include "gdataset.h"
56 #include "gerror.h"
57 #include "gfileutils.h"
58 #include "ghash.h"
59 #include "glibintl.h"
60 #include "glist.h"
61 #include "gslist.h"
62 #include "gmem.h"
63 #include "gmessages.h"
64 #include "gstdio.h"
65 #include "gstring.h"
66 #include "gstrfuncs.h"
67 #include "gutils.h"
70 /**
71 * SECTION:keyfile
72 * @title: Key-value file parser
73 * @short_description: parses .ini-like config files
75 * #GKeyFile lets you parse, edit or create files containing groups of
76 * key-value pairs, which we call "key files" for lack of a better name.
77 * Several freedesktop.org specifications use key files now, e.g the
78 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec)
79 * and the
80 * [Icon Theme Specification](http://freedesktop.org/Standards/icon-theme-spec).
82 * The syntax of key files is described in detail in the
83 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
84 * here is a quick summary: Key files
85 * consists of groups of key-value pairs, interspersed with comments.
87 * |[
88 * # this is just an example
89 * # there can be comments before the first group
91 * [First Group]
93 * Name=Key File Example\tthis value shows\nescaping
95 * # localized strings are stored in multiple key-value pairs
96 * Welcome=Hello
97 * Welcome[de]=Hallo
98 * Welcome[fr_FR]=Bonjour
99 * Welcome[it]=Ciao
100 * Welcome[be@latin]=Hello
102 * [Another Group]
104 * Numbers=2;20;-200;0
106 * Booleans=true;false;true;true
107 * ]|
109 * Lines beginning with a '#' and blank lines are considered comments.
111 * Groups are started by a header line containing the group name enclosed
112 * in '[' and ']', and ended implicitly by the start of the next group or
113 * the end of the file. Each key-value pair must be contained in a group.
115 * Key-value pairs generally have the form `key=value`, with the
116 * exception of localized strings, which have the form
117 * `key[locale]=value`, with a locale identifier of the
118 * form `lang_COUNTRY@MODIFIER` where `COUNTRY` and `MODIFIER`
119 * are optional.
120 * Space before and after the '=' character are ignored. Newline, tab,
121 * carriage return and backslash characters in value are escaped as \n,
122 * \t, \r, and \\, respectively. To preserve leading spaces in values,
123 * these can also be escaped as \s.
125 * Key files can store strings (possibly with localized variants), integers,
126 * booleans and lists of these. Lists are separated by a separator character,
127 * typically ';' or ','. To use the list separator character in a value in
128 * a list, it has to be escaped by prefixing it with a backslash.
130 * This syntax is obviously inspired by the .ini files commonly met
131 * on Windows, but there are some important differences:
133 * - .ini files use the ';' character to begin comments,
134 * key files use the '#' character.
136 * - Key files do not allow for ungrouped keys meaning only
137 * comments can precede the first group.
139 * - Key files are always encoded in UTF-8.
141 * - Key and Group names are case-sensitive. For example, a group called
142 * [GROUP] is a different from [group].
144 * - .ini files don't have a strongly typed boolean entry type,
145 * they only have GetProfileInt(). In key files, only
146 * true and false (in lower case) are allowed.
148 * Note that in contrast to the
149 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
150 * groups in key files may contain the same
151 * key multiple times; the last entry wins. Key files may also contain
152 * multiple groups with the same name; they are merged together.
153 * Another difference is that keys and group names in key files are not
154 * restricted to ASCII characters.
158 * G_KEY_FILE_ERROR:
160 * Error domain for key file parsing. Errors in this domain will
161 * be from the #GKeyFileError enumeration.
163 * See #GError for information on error domains.
167 * GKeyFileError:
168 * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
169 * an unknown encoding
170 * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
171 * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
172 * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
173 * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
174 * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
176 * Error codes returned by key file parsing.
180 * GKeyFileFlags:
181 * @G_KEY_FILE_NONE: No flags, default behaviour
182 * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
183 * (possibly modified) contents of the key file back to a file;
184 * otherwise all comments will be lost when the key file is
185 * written back.
186 * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
187 * (possibly modified) contents of the key file back to a file;
188 * otherwise only the translations for the current language will be
189 * written back.
191 * Flags which influence the parsing.
195 * G_KEY_FILE_DESKTOP_GROUP:
197 * The name of the main group of a desktop entry file, as defined in the
198 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec).
199 * Consult the specification for more
200 * details about the meanings of the keys below.
202 * Since: 2.14
206 * G_KEY_FILE_DESKTOP_KEY_TYPE:
208 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
209 * giving the type of the desktop entry. Usually
210 * #G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
211 * #G_KEY_FILE_DESKTOP_TYPE_LINK, or
212 * #G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
214 * Since: 2.14
218 * G_KEY_FILE_DESKTOP_KEY_VERSION:
220 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
221 * giving the version of the Desktop Entry Specification used for
222 * the desktop entry file.
224 * Since: 2.14
228 * G_KEY_FILE_DESKTOP_KEY_NAME:
230 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
231 * string giving the specific name of the desktop entry.
233 * Since: 2.14
237 * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
239 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
240 * string giving the generic name of the desktop entry.
242 * Since: 2.14
246 * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
248 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
249 * stating whether the desktop entry should be shown in menus.
251 * Since: 2.14
255 * G_KEY_FILE_DESKTOP_KEY_COMMENT:
257 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
258 * string giving the tooltip for the desktop entry.
260 * Since: 2.14
264 * G_KEY_FILE_DESKTOP_KEY_ICON:
266 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
267 * string giving the name of the icon to be displayed for the desktop
268 * entry.
270 * Since: 2.14
274 * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
276 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
277 * stating whether the desktop entry has been deleted by the user.
279 * Since: 2.14
283 * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
285 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
286 * strings identifying the environments that should display the
287 * desktop entry.
289 * Since: 2.14
293 * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
295 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
296 * strings identifying the environments that should not display the
297 * desktop entry.
299 * Since: 2.14
303 * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
305 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
306 * giving the file name of a binary on disk used to determine if the
307 * program is actually installed. It is only valid for desktop entries
308 * with the `Application` type.
310 * Since: 2.14
314 * G_KEY_FILE_DESKTOP_KEY_EXEC:
316 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
317 * giving the command line to execute. It is only valid for desktop
318 * entries with the `Application` type.
320 * Since: 2.14
324 * G_KEY_FILE_DESKTOP_KEY_PATH:
326 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
327 * containing the working directory to run the program in. It is only
328 * valid for desktop entries with the `Application` type.
330 * Since: 2.14
334 * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
336 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
337 * stating whether the program should be run in a terminal window.
338 * It is only valid for desktop entries with the
339 * `Application` type.
341 * Since: 2.14
345 * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
347 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
348 * of strings giving the MIME types supported by this desktop entry.
350 * Since: 2.14
354 * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
356 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
357 * of strings giving the categories in which the desktop entry
358 * should be shown in a menu.
360 * Since: 2.14
364 * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
366 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
367 * stating whether the application supports the
368 * [Startup Notification Protocol Specification](http://www.freedesktop.org/Standards/startup-notification-spec).
370 * Since: 2.14
374 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
376 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is string
377 * identifying the WM class or name hint of a window that the application
378 * will create, which can be used to emulate Startup Notification with
379 * older applications.
381 * Since: 2.14
385 * G_KEY_FILE_DESKTOP_KEY_URL:
387 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
388 * giving the URL to access. It is only valid for desktop entries
389 * with the `Link` type.
391 * Since: 2.14
395 * G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE:
397 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean set to true
398 * if the application is D-Bus activatable.
400 * Since: 2.38
404 * G_KEY_FILE_DESKTOP_KEY_ACTIONS:
406 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string list
407 * giving the available application actions.
409 * Since: 2.38
413 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
415 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
416 * entries representing applications.
418 * Since: 2.14
422 * G_KEY_FILE_DESKTOP_TYPE_LINK:
424 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
425 * entries representing links to documents.
427 * Since: 2.14
431 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
433 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
434 * entries representing directories.
436 * Since: 2.14
439 typedef struct _GKeyFileGroup GKeyFileGroup;
442 * GKeyFile:
444 * The GKeyFile struct contains only private data
445 * and should not be accessed directly.
447 struct _GKeyFile
449 GList *groups;
450 GHashTable *group_hash;
452 GKeyFileGroup *start_group;
453 GKeyFileGroup *current_group;
455 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
457 gchar list_separator;
459 GKeyFileFlags flags;
461 gchar **locales;
463 volatile gint ref_count;
466 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
468 struct _GKeyFileGroup
470 const gchar *name; /* NULL for above first group (which will be comments) */
472 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
474 GList *key_value_pairs;
476 /* Used in parallel with key_value_pairs for
477 * increased lookup performance
479 GHashTable *lookup_map;
482 struct _GKeyFileKeyValuePair
484 gchar *key; /* NULL for comments */
485 gchar *value;
488 static gint find_file_in_data_dirs (const gchar *file,
489 const gchar **data_dirs,
490 gchar **output_file,
491 GError **error);
492 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
493 gint fd,
494 GKeyFileFlags flags,
495 GError **error);
496 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
497 const gchar *group_name);
498 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
499 const gchar *group_name);
501 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
502 GKeyFileGroup *group,
503 const gchar *key);
504 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
505 GKeyFileGroup *group,
506 const gchar *key);
508 static void g_key_file_remove_group_node (GKeyFile *key_file,
509 GList *group_node);
510 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
511 GKeyFileGroup *group,
512 GList *pair_node);
514 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
515 GKeyFileGroup *group,
516 GKeyFileKeyValuePair *pair);
517 static void g_key_file_add_key (GKeyFile *key_file,
518 GKeyFileGroup *group,
519 const gchar *key,
520 const gchar *value);
521 static void g_key_file_add_group (GKeyFile *key_file,
522 const gchar *group_name);
523 static gboolean g_key_file_is_group_name (const gchar *name);
524 static gboolean g_key_file_is_key_name (const gchar *name);
525 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
526 static gboolean g_key_file_line_is_comment (const gchar *line);
527 static gboolean g_key_file_line_is_group (const gchar *line);
528 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
529 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
530 const gchar *value,
531 GSList **separators,
532 GError **error);
533 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
534 const gchar *string,
535 gboolean escape_separator);
536 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
537 const gchar *value,
538 GError **error);
539 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
540 gint value);
541 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
542 const gchar *value,
543 GError **error);
544 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
545 const gchar *value,
546 GError **error);
547 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
548 gboolean value);
549 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
550 const gchar *value);
551 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
552 const gchar *comment);
553 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
554 const gchar *line,
555 gsize length,
556 GError **error);
557 static void g_key_file_parse_comment (GKeyFile *key_file,
558 const gchar *line,
559 gsize length,
560 GError **error);
561 static void g_key_file_parse_group (GKeyFile *key_file,
562 const gchar *line,
563 gsize length,
564 GError **error);
565 static gchar *key_get_locale (const gchar *key);
566 static void g_key_file_parse_data (GKeyFile *key_file,
567 const gchar *data,
568 gsize length,
569 GError **error);
570 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
571 GError **error);
573 G_DEFINE_QUARK (g-key-file-error-quark, g_key_file_error)
575 static void
576 g_key_file_init (GKeyFile *key_file)
578 key_file->current_group = g_slice_new0 (GKeyFileGroup);
579 key_file->groups = g_list_prepend (NULL, key_file->current_group);
580 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
581 key_file->start_group = NULL;
582 key_file->parse_buffer = g_string_sized_new (128);
583 key_file->list_separator = ';';
584 key_file->flags = 0;
585 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
588 static void
589 g_key_file_clear (GKeyFile *key_file)
591 GList *tmp, *group_node;
593 if (key_file->locales)
595 g_strfreev (key_file->locales);
596 key_file->locales = NULL;
599 if (key_file->parse_buffer)
601 g_string_free (key_file->parse_buffer, TRUE);
602 key_file->parse_buffer = NULL;
605 tmp = key_file->groups;
606 while (tmp != NULL)
608 group_node = tmp;
609 tmp = tmp->next;
610 g_key_file_remove_group_node (key_file, group_node);
613 if (key_file->group_hash != NULL)
615 g_hash_table_destroy (key_file->group_hash);
616 key_file->group_hash = NULL;
619 g_warn_if_fail (key_file->groups == NULL);
624 * g_key_file_new:
626 * Creates a new empty #GKeyFile object. Use
627 * g_key_file_load_from_file(), g_key_file_load_from_data(),
628 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
629 * read an existing key file.
631 * Returns: (transfer full): an empty #GKeyFile.
633 * Since: 2.6
635 GKeyFile *
636 g_key_file_new (void)
638 GKeyFile *key_file;
640 key_file = g_slice_new0 (GKeyFile);
641 key_file->ref_count = 1;
642 g_key_file_init (key_file);
644 return key_file;
648 * g_key_file_set_list_separator:
649 * @key_file: a #GKeyFile
650 * @separator: the separator
652 * Sets the character which is used to separate
653 * values in lists. Typically ';' or ',' are used
654 * as separators. The default list separator is ';'.
656 * Since: 2.6
658 void
659 g_key_file_set_list_separator (GKeyFile *key_file,
660 gchar separator)
662 g_return_if_fail (key_file != NULL);
664 key_file->list_separator = separator;
668 /* Iterates through all the directories in *dirs trying to
669 * open file. When it successfully locates and opens a file it
670 * returns the file descriptor to the open file. It also
671 * outputs the absolute path of the file in output_file.
673 static gint
674 find_file_in_data_dirs (const gchar *file,
675 const gchar **dirs,
676 gchar **output_file,
677 GError **error)
679 const gchar **data_dirs, *data_dir;
680 gchar *path;
681 gint fd;
683 path = NULL;
684 fd = -1;
686 if (dirs == NULL)
687 return fd;
689 data_dirs = dirs;
691 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
693 gchar *candidate_file, *sub_dir;
695 candidate_file = (gchar *) file;
696 sub_dir = g_strdup ("");
697 while (candidate_file != NULL && fd == -1)
699 gchar *p;
701 path = g_build_filename (data_dir, sub_dir,
702 candidate_file, NULL);
704 fd = g_open (path, O_RDONLY, 0);
706 if (fd == -1)
708 g_free (path);
709 path = NULL;
712 candidate_file = strchr (candidate_file, '-');
714 if (candidate_file == NULL)
715 break;
717 candidate_file++;
719 g_free (sub_dir);
720 sub_dir = g_strndup (file, candidate_file - file - 1);
722 for (p = sub_dir; *p != '\0'; p++)
724 if (*p == '-')
725 *p = G_DIR_SEPARATOR;
728 g_free (sub_dir);
729 data_dirs++;
732 if (fd == -1)
734 g_set_error_literal (error, G_KEY_FILE_ERROR,
735 G_KEY_FILE_ERROR_NOT_FOUND,
736 _("Valid key file could not be "
737 "found in search dirs"));
740 if (output_file != NULL && fd > 0)
741 *output_file = g_strdup (path);
743 g_free (path);
745 return fd;
748 static gboolean
749 g_key_file_load_from_fd (GKeyFile *key_file,
750 gint fd,
751 GKeyFileFlags flags,
752 GError **error)
754 GError *key_file_error = NULL;
755 gssize bytes_read;
756 struct stat stat_buf;
757 gchar read_buf[4096];
758 gchar list_separator;
760 if (fstat (fd, &stat_buf) < 0)
762 g_set_error_literal (error, G_FILE_ERROR,
763 g_file_error_from_errno (errno),
764 g_strerror (errno));
765 return FALSE;
768 if (!S_ISREG (stat_buf.st_mode))
770 g_set_error_literal (error, G_KEY_FILE_ERROR,
771 G_KEY_FILE_ERROR_PARSE,
772 _("Not a regular file"));
773 return FALSE;
776 list_separator = key_file->list_separator;
777 g_key_file_clear (key_file);
778 g_key_file_init (key_file);
779 key_file->list_separator = list_separator;
780 key_file->flags = flags;
784 bytes_read = read (fd, read_buf, 4096);
786 if (bytes_read == 0) /* End of File */
787 break;
789 if (bytes_read < 0)
791 if (errno == EINTR || errno == EAGAIN)
792 continue;
794 g_set_error_literal (error, G_FILE_ERROR,
795 g_file_error_from_errno (errno),
796 g_strerror (errno));
797 return FALSE;
800 g_key_file_parse_data (key_file,
801 read_buf, bytes_read,
802 &key_file_error);
804 while (!key_file_error);
806 if (key_file_error)
808 g_propagate_error (error, key_file_error);
809 return FALSE;
812 g_key_file_flush_parse_buffer (key_file, &key_file_error);
814 if (key_file_error)
816 g_propagate_error (error, key_file_error);
817 return FALSE;
820 return TRUE;
824 * g_key_file_load_from_file:
825 * @key_file: an empty #GKeyFile struct
826 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
827 * @flags: flags from #GKeyFileFlags
828 * @error: return location for a #GError, or %NULL
830 * Loads a key file into an empty #GKeyFile structure.
832 * If the OS returns an error when opening or reading the file, a
833 * %G_FILE_ERROR is returned. If there is a problem parsing the file, a
834 * %G_KEY_FILE_ERROR is returned.
836 * This function will never return a %G_KEY_FILE_ERROR_NOT_FOUND error. If the
837 * @file is not found, %G_FILE_ERROR_NOENT is returned.
839 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
841 * Since: 2.6
843 gboolean
844 g_key_file_load_from_file (GKeyFile *key_file,
845 const gchar *file,
846 GKeyFileFlags flags,
847 GError **error)
849 GError *key_file_error = NULL;
850 gint fd;
852 g_return_val_if_fail (key_file != NULL, FALSE);
853 g_return_val_if_fail (file != NULL, FALSE);
855 fd = g_open (file, O_RDONLY, 0);
857 if (fd == -1)
859 g_set_error_literal (error, G_FILE_ERROR,
860 g_file_error_from_errno (errno),
861 g_strerror (errno));
862 return FALSE;
865 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
866 close (fd);
868 if (key_file_error)
870 g_propagate_error (error, key_file_error);
871 return FALSE;
874 return TRUE;
878 * g_key_file_load_from_data:
879 * @key_file: an empty #GKeyFile struct
880 * @data: key file loaded in memory
881 * @length: the length of @data in bytes (or (gsize)-1 if data is nul-terminated)
882 * @flags: flags from #GKeyFileFlags
883 * @error: return location for a #GError, or %NULL
885 * Loads a key file from memory into an empty #GKeyFile structure.
886 * If the object cannot be created then %error is set to a #GKeyFileError.
888 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
890 * Since: 2.6
892 gboolean
893 g_key_file_load_from_data (GKeyFile *key_file,
894 const gchar *data,
895 gsize length,
896 GKeyFileFlags flags,
897 GError **error)
899 GError *key_file_error = NULL;
900 gchar list_separator;
902 g_return_val_if_fail (key_file != NULL, FALSE);
903 g_return_val_if_fail (data != NULL || length == 0, FALSE);
905 if (length == (gsize)-1)
906 length = strlen (data);
908 list_separator = key_file->list_separator;
909 g_key_file_clear (key_file);
910 g_key_file_init (key_file);
911 key_file->list_separator = list_separator;
912 key_file->flags = flags;
914 g_key_file_parse_data (key_file, data, length, &key_file_error);
916 if (key_file_error)
918 g_propagate_error (error, key_file_error);
919 return FALSE;
922 g_key_file_flush_parse_buffer (key_file, &key_file_error);
924 if (key_file_error)
926 g_propagate_error (error, key_file_error);
927 return FALSE;
930 return TRUE;
934 * g_key_file_load_from_bytes:
935 * @key_file: an empty #GKeyFile struct
936 * @bytes: a #GBytes
937 * @flags: flags from #GKeyFileFlags
938 * @error: return location for a #GError, or %NULL
940 * Loads a key file from the data in @bytes into an empty #GKeyFile structure.
941 * If the object cannot be created then %error is set to a #GKeyFileError.
943 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
945 * Since: 2.50
947 gboolean
948 g_key_file_load_from_bytes (GKeyFile *key_file,
949 GBytes *bytes,
950 GKeyFileFlags flags,
951 GError **error)
953 const guchar *data;
954 gsize size;
956 g_return_val_if_fail (key_file != NULL, FALSE);
957 g_return_val_if_fail (bytes != NULL, FALSE);
959 data = g_bytes_get_data (bytes, &size);
960 return g_key_file_load_from_data (key_file, (const gchar *) data, size, flags, error);
964 * g_key_file_load_from_dirs:
965 * @key_file: an empty #GKeyFile struct
966 * @file: (type filename): a relative path to a filename to open and parse
967 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
968 * @full_path: (out) (type filename) (optional): return location for a string containing the full path
969 * of the file, or %NULL
970 * @flags: flags from #GKeyFileFlags
971 * @error: return location for a #GError, or %NULL
973 * This function looks for a key file named @file in the paths
974 * specified in @search_dirs, loads the file into @key_file and
975 * returns the file's full path in @full_path.
977 * If the file could not be found in any of the @search_dirs,
978 * %G_KEY_FILE_ERROR_NOT_FOUND is returned. If
979 * the file is found but the OS returns an error when opening or reading the
980 * file, a %G_FILE_ERROR is returned. If there is a problem parsing the file, a
981 * %G_KEY_FILE_ERROR is returned.
983 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
985 * Since: 2.14
987 gboolean
988 g_key_file_load_from_dirs (GKeyFile *key_file,
989 const gchar *file,
990 const gchar **search_dirs,
991 gchar **full_path,
992 GKeyFileFlags flags,
993 GError **error)
995 GError *key_file_error = NULL;
996 const gchar **data_dirs;
997 gchar *output_path;
998 gint fd;
999 gboolean found_file;
1001 g_return_val_if_fail (key_file != NULL, FALSE);
1002 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1003 g_return_val_if_fail (search_dirs != NULL, FALSE);
1005 found_file = FALSE;
1006 data_dirs = search_dirs;
1007 output_path = NULL;
1008 while (*data_dirs != NULL && !found_file)
1010 g_free (output_path);
1011 output_path = NULL;
1013 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
1014 &key_file_error);
1016 if (fd == -1)
1018 if (key_file_error)
1019 g_propagate_error (error, key_file_error);
1020 break;
1023 found_file = g_key_file_load_from_fd (key_file, fd, flags,
1024 &key_file_error);
1025 close (fd);
1027 if (key_file_error)
1029 g_propagate_error (error, key_file_error);
1030 break;
1034 if (found_file && full_path)
1035 *full_path = output_path;
1036 else
1037 g_free (output_path);
1039 return found_file;
1043 * g_key_file_load_from_data_dirs:
1044 * @key_file: an empty #GKeyFile struct
1045 * @file: (type filename): a relative path to a filename to open and parse
1046 * @full_path: (out) (type filename) (optional): return location for a string containing the full path
1047 * of the file, or %NULL
1048 * @flags: flags from #GKeyFileFlags
1049 * @error: return location for a #GError, or %NULL
1051 * This function looks for a key file named @file in the paths
1052 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1053 * loads the file into @key_file and returns the file's full path in
1054 * @full_path. If the file could not be loaded then an %error is
1055 * set to either a #GFileError or #GKeyFileError.
1057 * Returns: %TRUE if a key file could be loaded, %FALSE othewise
1058 * Since: 2.6
1060 gboolean
1061 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1062 const gchar *file,
1063 gchar **full_path,
1064 GKeyFileFlags flags,
1065 GError **error)
1067 gchar **all_data_dirs;
1068 const gchar * user_data_dir;
1069 const gchar * const * system_data_dirs;
1070 gsize i, j;
1071 gboolean found_file;
1073 g_return_val_if_fail (key_file != NULL, FALSE);
1074 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1076 user_data_dir = g_get_user_data_dir ();
1077 system_data_dirs = g_get_system_data_dirs ();
1078 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1080 i = 0;
1081 all_data_dirs[i++] = g_strdup (user_data_dir);
1083 j = 0;
1084 while (system_data_dirs[j] != NULL)
1085 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1086 all_data_dirs[i] = NULL;
1088 found_file = g_key_file_load_from_dirs (key_file,
1089 file,
1090 (const gchar **)all_data_dirs,
1091 full_path,
1092 flags,
1093 error);
1095 g_strfreev (all_data_dirs);
1097 return found_file;
1101 * g_key_file_ref: (skip)
1102 * @key_file: a #GKeyFile
1104 * Increases the reference count of @key_file.
1106 * Returns: the same @key_file.
1108 * Since: 2.32
1110 GKeyFile *
1111 g_key_file_ref (GKeyFile *key_file)
1113 g_return_val_if_fail (key_file != NULL, NULL);
1115 g_atomic_int_inc (&key_file->ref_count);
1117 return key_file;
1121 * g_key_file_free: (skip)
1122 * @key_file: a #GKeyFile
1124 * Clears all keys and groups from @key_file, and decreases the
1125 * reference count by 1. If the reference count reaches zero,
1126 * frees the key file and all its allocated memory.
1128 * Since: 2.6
1130 void
1131 g_key_file_free (GKeyFile *key_file)
1133 g_return_if_fail (key_file != NULL);
1135 g_key_file_clear (key_file);
1136 g_key_file_unref (key_file);
1140 * g_key_file_unref:
1141 * @key_file: a #GKeyFile
1143 * Decreases the reference count of @key_file by 1. If the reference count
1144 * reaches zero, frees the key file and all its allocated memory.
1146 * Since: 2.32
1148 void
1149 g_key_file_unref (GKeyFile *key_file)
1151 g_return_if_fail (key_file != NULL);
1153 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1155 g_key_file_clear (key_file);
1156 g_slice_free (GKeyFile, key_file);
1160 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1161 * true for locales that match those in g_get_language_names().
1163 static gboolean
1164 g_key_file_locale_is_interesting (GKeyFile *key_file,
1165 const gchar *locale)
1167 gsize i;
1169 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1170 return TRUE;
1172 for (i = 0; key_file->locales[i] != NULL; i++)
1174 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
1175 return TRUE;
1178 return FALSE;
1181 static void
1182 g_key_file_parse_line (GKeyFile *key_file,
1183 const gchar *line,
1184 gsize length,
1185 GError **error)
1187 GError *parse_error = NULL;
1188 gchar *line_start;
1190 g_return_if_fail (key_file != NULL);
1191 g_return_if_fail (line != NULL);
1193 line_start = (gchar *) line;
1194 while (g_ascii_isspace (*line_start))
1195 line_start++;
1197 if (g_key_file_line_is_comment (line_start))
1198 g_key_file_parse_comment (key_file, line, length, &parse_error);
1199 else if (g_key_file_line_is_group (line_start))
1200 g_key_file_parse_group (key_file, line_start,
1201 length - (line_start - line),
1202 &parse_error);
1203 else if (g_key_file_line_is_key_value_pair (line_start))
1204 g_key_file_parse_key_value_pair (key_file, line_start,
1205 length - (line_start - line),
1206 &parse_error);
1207 else
1209 gchar *line_utf8 = g_utf8_make_valid (line, length);
1210 g_set_error (error, G_KEY_FILE_ERROR,
1211 G_KEY_FILE_ERROR_PARSE,
1212 _("Key file contains line “%s” which is not "
1213 "a key-value pair, group, or comment"),
1214 line_utf8);
1215 g_free (line_utf8);
1217 return;
1220 if (parse_error)
1221 g_propagate_error (error, parse_error);
1224 static void
1225 g_key_file_parse_comment (GKeyFile *key_file,
1226 const gchar *line,
1227 gsize length,
1228 GError **error)
1230 GKeyFileKeyValuePair *pair;
1232 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1233 return;
1235 g_warn_if_fail (key_file->current_group != NULL);
1237 pair = g_slice_new (GKeyFileKeyValuePair);
1238 pair->key = NULL;
1239 pair->value = g_strndup (line, length);
1241 key_file->current_group->key_value_pairs =
1242 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1245 static void
1246 g_key_file_parse_group (GKeyFile *key_file,
1247 const gchar *line,
1248 gsize length,
1249 GError **error)
1251 gchar *group_name;
1252 const gchar *group_name_start, *group_name_end;
1254 /* advance past opening '['
1256 group_name_start = line + 1;
1257 group_name_end = line + length - 1;
1259 while (*group_name_end != ']')
1260 group_name_end--;
1262 group_name = g_strndup (group_name_start,
1263 group_name_end - group_name_start);
1265 if (!g_key_file_is_group_name (group_name))
1267 g_set_error (error, G_KEY_FILE_ERROR,
1268 G_KEY_FILE_ERROR_PARSE,
1269 _("Invalid group name: %s"), group_name);
1270 g_free (group_name);
1271 return;
1274 g_key_file_add_group (key_file, group_name);
1275 g_free (group_name);
1278 static void
1279 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1280 const gchar *line,
1281 gsize length,
1282 GError **error)
1284 gchar *key, *value, *key_end, *value_start, *locale;
1285 gsize key_len, value_len;
1287 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1289 g_set_error_literal (error, G_KEY_FILE_ERROR,
1290 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1291 _("Key file does not start with a group"));
1292 return;
1295 key_end = value_start = strchr (line, '=');
1297 g_warn_if_fail (key_end != NULL);
1299 key_end--;
1300 value_start++;
1302 /* Pull the key name from the line (chomping trailing whitespace)
1304 while (g_ascii_isspace (*key_end))
1305 key_end--;
1307 key_len = key_end - line + 2;
1309 g_warn_if_fail (key_len <= length);
1311 key = g_strndup (line, key_len - 1);
1313 if (!g_key_file_is_key_name (key))
1315 g_set_error (error, G_KEY_FILE_ERROR,
1316 G_KEY_FILE_ERROR_PARSE,
1317 _("Invalid key name: %s"), key);
1318 g_free (key);
1319 return;
1322 /* Pull the value from the line (chugging leading whitespace)
1324 while (g_ascii_isspace (*value_start))
1325 value_start++;
1327 value_len = line + length - value_start + 1;
1329 value = g_strndup (value_start, value_len);
1331 g_warn_if_fail (key_file->start_group != NULL);
1333 if (key_file->current_group
1334 && key_file->current_group->name
1335 && strcmp (key_file->start_group->name,
1336 key_file->current_group->name) == 0
1337 && strcmp (key, "Encoding") == 0)
1339 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
1341 gchar *value_utf8 = g_utf8_make_valid (value, value_len);
1342 g_set_error (error, G_KEY_FILE_ERROR,
1343 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1344 _("Key file contains unsupported "
1345 "encoding “%s”"), value_utf8);
1346 g_free (value_utf8);
1348 g_free (key);
1349 g_free (value);
1350 return;
1354 /* Is this key a translation? If so, is it one that we care about?
1356 locale = key_get_locale (key);
1358 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
1360 GKeyFileKeyValuePair *pair;
1362 pair = g_slice_new (GKeyFileKeyValuePair);
1363 pair->key = key;
1364 pair->value = value;
1366 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1368 else
1370 g_free (key);
1371 g_free (value);
1374 g_free (locale);
1377 static gchar *
1378 key_get_locale (const gchar *key)
1380 gchar *locale;
1382 locale = g_strrstr (key, "[");
1384 if (locale && strlen (locale) <= 2)
1385 locale = NULL;
1387 if (locale)
1388 locale = g_strndup (locale + 1, strlen (locale) - 2);
1390 return locale;
1393 static void
1394 g_key_file_parse_data (GKeyFile *key_file,
1395 const gchar *data,
1396 gsize length,
1397 GError **error)
1399 GError *parse_error;
1400 gsize i;
1402 g_return_if_fail (key_file != NULL);
1403 g_return_if_fail (data != NULL || length == 0);
1405 parse_error = NULL;
1407 i = 0;
1408 while (i < length)
1410 if (data[i] == '\n')
1412 if (key_file->parse_buffer->len > 0
1413 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1414 == '\r'))
1415 g_string_erase (key_file->parse_buffer,
1416 key_file->parse_buffer->len - 1,
1419 /* When a newline is encountered flush the parse buffer so that the
1420 * line can be parsed. Note that completely blank lines won't show
1421 * up in the parse buffer, so they get parsed directly.
1423 if (key_file->parse_buffer->len > 0)
1424 g_key_file_flush_parse_buffer (key_file, &parse_error);
1425 else
1426 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1428 if (parse_error)
1430 g_propagate_error (error, parse_error);
1431 return;
1433 i++;
1435 else
1437 const gchar *start_of_line;
1438 const gchar *end_of_line;
1439 gsize line_length;
1441 start_of_line = data + i;
1442 end_of_line = memchr (start_of_line, '\n', length - i);
1444 if (end_of_line == NULL)
1445 end_of_line = data + length;
1447 line_length = end_of_line - start_of_line;
1449 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1450 i += line_length;
1455 static void
1456 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1457 GError **error)
1459 GError *file_error = NULL;
1461 g_return_if_fail (key_file != NULL);
1463 file_error = NULL;
1465 if (key_file->parse_buffer->len > 0)
1467 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1468 key_file->parse_buffer->len,
1469 &file_error);
1470 g_string_erase (key_file->parse_buffer, 0, -1);
1472 if (file_error)
1474 g_propagate_error (error, file_error);
1475 return;
1481 * g_key_file_to_data:
1482 * @key_file: a #GKeyFile
1483 * @length: (out) (optional): return location for the length of the
1484 * returned string, or %NULL
1485 * @error: return location for a #GError, or %NULL
1487 * This function outputs @key_file as a string.
1489 * Note that this function never reports an error,
1490 * so it is safe to pass %NULL as @error.
1492 * Returns: a newly allocated string holding
1493 * the contents of the #GKeyFile
1495 * Since: 2.6
1497 gchar *
1498 g_key_file_to_data (GKeyFile *key_file,
1499 gsize *length,
1500 GError **error)
1502 GString *data_string;
1503 GList *group_node, *key_file_node;
1505 g_return_val_if_fail (key_file != NULL, NULL);
1507 data_string = g_string_new (NULL);
1509 for (group_node = g_list_last (key_file->groups);
1510 group_node != NULL;
1511 group_node = group_node->prev)
1513 GKeyFileGroup *group;
1515 group = (GKeyFileGroup *) group_node->data;
1517 /* separate groups by at least an empty line */
1518 if (data_string->len >= 2 &&
1519 data_string->str[data_string->len - 2] != '\n')
1520 g_string_append_c (data_string, '\n');
1522 if (group->comment != NULL)
1523 g_string_append_printf (data_string, "%s\n", group->comment->value);
1525 if (group->name != NULL)
1526 g_string_append_printf (data_string, "[%s]\n", group->name);
1528 for (key_file_node = g_list_last (group->key_value_pairs);
1529 key_file_node != NULL;
1530 key_file_node = key_file_node->prev)
1532 GKeyFileKeyValuePair *pair;
1534 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1536 if (pair->key != NULL)
1537 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1538 else
1539 g_string_append_printf (data_string, "%s\n", pair->value);
1543 if (length)
1544 *length = data_string->len;
1546 return g_string_free (data_string, FALSE);
1550 * g_key_file_get_keys:
1551 * @key_file: a #GKeyFile
1552 * @group_name: a group name
1553 * @length: (out) (optional): return location for the number of keys returned, or %NULL
1554 * @error: return location for a #GError, or %NULL
1556 * Returns all keys for the group name @group_name. The array of
1557 * returned keys will be %NULL-terminated, so @length may
1558 * optionally be %NULL. In the event that the @group_name cannot
1559 * be found, %NULL is returned and @error is set to
1560 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1562 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1563 * Use g_strfreev() to free it.
1565 * Since: 2.6
1567 gchar **
1568 g_key_file_get_keys (GKeyFile *key_file,
1569 const gchar *group_name,
1570 gsize *length,
1571 GError **error)
1573 GKeyFileGroup *group;
1574 GList *tmp;
1575 gchar **keys;
1576 gsize i, num_keys;
1578 g_return_val_if_fail (key_file != NULL, NULL);
1579 g_return_val_if_fail (group_name != NULL, NULL);
1581 group = g_key_file_lookup_group (key_file, group_name);
1583 if (!group)
1585 g_set_error (error, G_KEY_FILE_ERROR,
1586 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1587 _("Key file does not have group “%s”"),
1588 group_name);
1589 return NULL;
1592 num_keys = 0;
1593 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1595 GKeyFileKeyValuePair *pair;
1597 pair = (GKeyFileKeyValuePair *) tmp->data;
1599 if (pair->key)
1600 num_keys++;
1603 keys = g_new (gchar *, num_keys + 1);
1605 i = num_keys - 1;
1606 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1608 GKeyFileKeyValuePair *pair;
1610 pair = (GKeyFileKeyValuePair *) tmp->data;
1612 if (pair->key)
1614 keys[i] = g_strdup (pair->key);
1615 i--;
1619 keys[num_keys] = NULL;
1621 if (length)
1622 *length = num_keys;
1624 return keys;
1628 * g_key_file_get_start_group:
1629 * @key_file: a #GKeyFile
1631 * Returns the name of the start group of the file.
1633 * Returns: The start group of the key file.
1635 * Since: 2.6
1637 gchar *
1638 g_key_file_get_start_group (GKeyFile *key_file)
1640 g_return_val_if_fail (key_file != NULL, NULL);
1642 if (key_file->start_group)
1643 return g_strdup (key_file->start_group->name);
1645 return NULL;
1649 * g_key_file_get_groups:
1650 * @key_file: a #GKeyFile
1651 * @length: (out) (optional): return location for the number of returned groups, or %NULL
1653 * Returns all groups in the key file loaded with @key_file.
1654 * The array of returned groups will be %NULL-terminated, so
1655 * @length may optionally be %NULL.
1657 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1658 * Use g_strfreev() to free it.
1659 * Since: 2.6
1661 gchar **
1662 g_key_file_get_groups (GKeyFile *key_file,
1663 gsize *length)
1665 GList *group_node;
1666 gchar **groups;
1667 gsize i, num_groups;
1669 g_return_val_if_fail (key_file != NULL, NULL);
1671 num_groups = g_list_length (key_file->groups);
1673 g_return_val_if_fail (num_groups > 0, NULL);
1675 group_node = g_list_last (key_file->groups);
1677 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1679 /* Only need num_groups instead of num_groups + 1
1680 * because the first group of the file (last in the
1681 * list) is always the comment group at the top,
1682 * which we skip
1684 groups = g_new (gchar *, num_groups);
1687 i = 0;
1688 for (group_node = group_node->prev;
1689 group_node != NULL;
1690 group_node = group_node->prev)
1692 GKeyFileGroup *group;
1694 group = (GKeyFileGroup *) group_node->data;
1696 g_warn_if_fail (group->name != NULL);
1698 groups[i++] = g_strdup (group->name);
1700 groups[i] = NULL;
1702 if (length)
1703 *length = i;
1705 return groups;
1708 static void
1709 set_not_found_key_error (const char *group_name,
1710 const char *key,
1711 GError **error)
1713 g_set_error (error, G_KEY_FILE_ERROR,
1714 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1715 _("Key file does not have key “%s” in group “%s”"),
1716 key, group_name);
1720 * g_key_file_get_value:
1721 * @key_file: a #GKeyFile
1722 * @group_name: a group name
1723 * @key: a key
1724 * @error: return location for a #GError, or %NULL
1726 * Returns the raw value associated with @key under @group_name.
1727 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1729 * In the event the key cannot be found, %NULL is returned and
1730 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1731 * event that the @group_name cannot be found, %NULL is returned
1732 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1735 * Returns: a newly allocated string or %NULL if the specified
1736 * key cannot be found.
1738 * Since: 2.6
1740 gchar *
1741 g_key_file_get_value (GKeyFile *key_file,
1742 const gchar *group_name,
1743 const gchar *key,
1744 GError **error)
1746 GKeyFileGroup *group;
1747 GKeyFileKeyValuePair *pair;
1748 gchar *value = NULL;
1750 g_return_val_if_fail (key_file != NULL, NULL);
1751 g_return_val_if_fail (group_name != NULL, NULL);
1752 g_return_val_if_fail (key != NULL, NULL);
1754 group = g_key_file_lookup_group (key_file, group_name);
1756 if (!group)
1758 g_set_error (error, G_KEY_FILE_ERROR,
1759 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1760 _("Key file does not have group “%s”"),
1761 group_name);
1762 return NULL;
1765 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1767 if (pair)
1768 value = g_strdup (pair->value);
1769 else
1770 set_not_found_key_error (group_name, key, error);
1772 return value;
1776 * g_key_file_set_value:
1777 * @key_file: a #GKeyFile
1778 * @group_name: a group name
1779 * @key: a key
1780 * @value: a string
1782 * Associates a new value with @key under @group_name.
1784 * If @key cannot be found then it is created. If @group_name cannot
1785 * be found then it is created. To set an UTF-8 string which may contain
1786 * characters that need escaping (such as newlines or spaces), use
1787 * g_key_file_set_string().
1789 * Since: 2.6
1791 void
1792 g_key_file_set_value (GKeyFile *key_file,
1793 const gchar *group_name,
1794 const gchar *key,
1795 const gchar *value)
1797 GKeyFileGroup *group;
1798 GKeyFileKeyValuePair *pair;
1800 g_return_if_fail (key_file != NULL);
1801 g_return_if_fail (g_key_file_is_group_name (group_name));
1802 g_return_if_fail (g_key_file_is_key_name (key));
1803 g_return_if_fail (value != NULL);
1805 group = g_key_file_lookup_group (key_file, group_name);
1807 if (!group)
1809 g_key_file_add_group (key_file, group_name);
1810 group = (GKeyFileGroup *) key_file->groups->data;
1812 g_key_file_add_key (key_file, group, key, value);
1814 else
1816 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1818 if (!pair)
1819 g_key_file_add_key (key_file, group, key, value);
1820 else
1822 g_free (pair->value);
1823 pair->value = g_strdup (value);
1829 * g_key_file_get_string:
1830 * @key_file: a #GKeyFile
1831 * @group_name: a group name
1832 * @key: a key
1833 * @error: return location for a #GError, or %NULL
1835 * Returns the string value associated with @key under @group_name.
1836 * Unlike g_key_file_get_value(), this function handles escape sequences
1837 * like \s.
1839 * In the event the key cannot be found, %NULL is returned and
1840 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1841 * event that the @group_name cannot be found, %NULL is returned
1842 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1844 * Returns: a newly allocated string or %NULL if the specified
1845 * key cannot be found.
1847 * Since: 2.6
1849 gchar *
1850 g_key_file_get_string (GKeyFile *key_file,
1851 const gchar *group_name,
1852 const gchar *key,
1853 GError **error)
1855 gchar *value, *string_value;
1856 GError *key_file_error;
1858 g_return_val_if_fail (key_file != NULL, NULL);
1859 g_return_val_if_fail (group_name != NULL, NULL);
1860 g_return_val_if_fail (key != NULL, NULL);
1862 key_file_error = NULL;
1864 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1866 if (key_file_error)
1868 g_propagate_error (error, key_file_error);
1869 return NULL;
1872 if (!g_utf8_validate (value, -1, NULL))
1874 gchar *value_utf8 = g_utf8_make_valid (value, -1);
1875 g_set_error (error, G_KEY_FILE_ERROR,
1876 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1877 _("Key file contains key “%s” with value “%s” "
1878 "which is not UTF-8"), key, value_utf8);
1879 g_free (value_utf8);
1880 g_free (value);
1882 return NULL;
1885 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1886 &key_file_error);
1887 g_free (value);
1889 if (key_file_error)
1891 if (g_error_matches (key_file_error,
1892 G_KEY_FILE_ERROR,
1893 G_KEY_FILE_ERROR_INVALID_VALUE))
1895 g_set_error (error, G_KEY_FILE_ERROR,
1896 G_KEY_FILE_ERROR_INVALID_VALUE,
1897 _("Key file contains key “%s” "
1898 "which has a value that cannot be interpreted."),
1899 key);
1900 g_error_free (key_file_error);
1902 else
1903 g_propagate_error (error, key_file_error);
1906 return string_value;
1910 * g_key_file_set_string:
1911 * @key_file: a #GKeyFile
1912 * @group_name: a group name
1913 * @key: a key
1914 * @string: a string
1916 * Associates a new string value with @key under @group_name.
1917 * If @key cannot be found then it is created.
1918 * If @group_name cannot be found then it is created.
1919 * Unlike g_key_file_set_value(), this function handles characters
1920 * that need escaping, such as newlines.
1922 * Since: 2.6
1924 void
1925 g_key_file_set_string (GKeyFile *key_file,
1926 const gchar *group_name,
1927 const gchar *key,
1928 const gchar *string)
1930 gchar *value;
1932 g_return_if_fail (key_file != NULL);
1933 g_return_if_fail (string != NULL);
1935 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1936 g_key_file_set_value (key_file, group_name, key, value);
1937 g_free (value);
1941 * g_key_file_get_string_list:
1942 * @key_file: a #GKeyFile
1943 * @group_name: a group name
1944 * @key: a key
1945 * @length: (out) (optional): return location for the number of returned strings, or %NULL
1946 * @error: return location for a #GError, or %NULL
1948 * Returns the values associated with @key under @group_name.
1950 * In the event the key cannot be found, %NULL is returned and
1951 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1952 * event that the @group_name cannot be found, %NULL is returned
1953 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1955 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
1956 * a %NULL-terminated string array or %NULL if the specified
1957 * key cannot be found. The array should be freed with g_strfreev().
1959 * Since: 2.6
1961 gchar **
1962 g_key_file_get_string_list (GKeyFile *key_file,
1963 const gchar *group_name,
1964 const gchar *key,
1965 gsize *length,
1966 GError **error)
1968 GError *key_file_error = NULL;
1969 gchar *value, *string_value, **values;
1970 gint i, len;
1971 GSList *p, *pieces = NULL;
1973 g_return_val_if_fail (key_file != NULL, NULL);
1974 g_return_val_if_fail (group_name != NULL, NULL);
1975 g_return_val_if_fail (key != NULL, NULL);
1977 if (length)
1978 *length = 0;
1980 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1982 if (key_file_error)
1984 g_propagate_error (error, key_file_error);
1985 return NULL;
1988 if (!g_utf8_validate (value, -1, NULL))
1990 gchar *value_utf8 = g_utf8_make_valid (value, -1);
1991 g_set_error (error, G_KEY_FILE_ERROR,
1992 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1993 _("Key file contains key “%s” with value “%s” "
1994 "which is not UTF-8"), key, value_utf8);
1995 g_free (value_utf8);
1996 g_free (value);
1998 return NULL;
2001 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
2002 g_free (value);
2003 g_free (string_value);
2005 if (key_file_error)
2007 if (g_error_matches (key_file_error,
2008 G_KEY_FILE_ERROR,
2009 G_KEY_FILE_ERROR_INVALID_VALUE))
2011 g_set_error (error, G_KEY_FILE_ERROR,
2012 G_KEY_FILE_ERROR_INVALID_VALUE,
2013 _("Key file contains key “%s” "
2014 "which has a value that cannot be interpreted."),
2015 key);
2016 g_error_free (key_file_error);
2018 else
2019 g_propagate_error (error, key_file_error);
2021 g_slist_free_full (pieces, g_free);
2022 return NULL;
2025 len = g_slist_length (pieces);
2026 values = g_new (gchar *, len + 1);
2027 for (p = pieces, i = 0; p; p = p->next)
2028 values[i++] = p->data;
2029 values[len] = NULL;
2031 g_slist_free (pieces);
2033 if (length)
2034 *length = len;
2036 return values;
2040 * g_key_file_set_string_list:
2041 * @key_file: a #GKeyFile
2042 * @group_name: a group name
2043 * @key: a key
2044 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
2045 * @length: number of string values in @list
2047 * Associates a list of string values for @key under @group_name.
2048 * If @key cannot be found then it is created.
2049 * If @group_name cannot be found then it is created.
2051 * Since: 2.6
2053 void
2054 g_key_file_set_string_list (GKeyFile *key_file,
2055 const gchar *group_name,
2056 const gchar *key,
2057 const gchar * const list[],
2058 gsize length)
2060 GString *value_list;
2061 gsize i;
2063 g_return_if_fail (key_file != NULL);
2064 g_return_if_fail (list != NULL || length == 0);
2066 value_list = g_string_sized_new (length * 128);
2067 for (i = 0; i < length && list[i] != NULL; i++)
2069 gchar *value;
2071 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2072 g_string_append (value_list, value);
2073 g_string_append_c (value_list, key_file->list_separator);
2075 g_free (value);
2078 g_key_file_set_value (key_file, group_name, key, value_list->str);
2079 g_string_free (value_list, TRUE);
2083 * g_key_file_set_locale_string:
2084 * @key_file: a #GKeyFile
2085 * @group_name: a group name
2086 * @key: a key
2087 * @locale: a locale identifier
2088 * @string: a string
2090 * Associates a string value for @key and @locale under @group_name.
2091 * If the translation for @key cannot be found then it is created.
2093 * Since: 2.6
2095 void
2096 g_key_file_set_locale_string (GKeyFile *key_file,
2097 const gchar *group_name,
2098 const gchar *key,
2099 const gchar *locale,
2100 const gchar *string)
2102 gchar *full_key, *value;
2104 g_return_if_fail (key_file != NULL);
2105 g_return_if_fail (key != NULL);
2106 g_return_if_fail (locale != NULL);
2107 g_return_if_fail (string != NULL);
2109 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2110 full_key = g_strdup_printf ("%s[%s]", key, locale);
2111 g_key_file_set_value (key_file, group_name, full_key, value);
2112 g_free (full_key);
2113 g_free (value);
2117 * g_key_file_get_locale_string:
2118 * @key_file: a #GKeyFile
2119 * @group_name: a group name
2120 * @key: a key
2121 * @locale: (nullable): a locale identifier or %NULL
2122 * @error: return location for a #GError, or %NULL
2124 * Returns the value associated with @key under @group_name
2125 * translated in the given @locale if available. If @locale is
2126 * %NULL then the current locale is assumed.
2128 * If @key cannot be found then %NULL is returned and @error is set
2129 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2130 * with @key cannot be interpreted or no suitable translation can
2131 * be found then the untranslated value is returned.
2133 * Returns: a newly allocated string or %NULL if the specified
2134 * key cannot be found.
2136 * Since: 2.6
2138 gchar *
2139 g_key_file_get_locale_string (GKeyFile *key_file,
2140 const gchar *group_name,
2141 const gchar *key,
2142 const gchar *locale,
2143 GError **error)
2145 gchar *candidate_key, *translated_value;
2146 GError *key_file_error;
2147 gchar **languages;
2148 gboolean free_languages = FALSE;
2149 gint i;
2151 g_return_val_if_fail (key_file != NULL, NULL);
2152 g_return_val_if_fail (group_name != NULL, NULL);
2153 g_return_val_if_fail (key != NULL, NULL);
2155 candidate_key = NULL;
2156 translated_value = NULL;
2157 key_file_error = NULL;
2159 if (locale)
2161 languages = g_get_locale_variants (locale);
2162 free_languages = TRUE;
2164 else
2166 languages = (gchar **) g_get_language_names ();
2167 free_languages = FALSE;
2170 for (i = 0; languages[i]; i++)
2172 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2174 translated_value = g_key_file_get_string (key_file,
2175 group_name,
2176 candidate_key, NULL);
2177 g_free (candidate_key);
2179 if (translated_value)
2180 break;
2182 g_free (translated_value);
2183 translated_value = NULL;
2186 /* Fallback to untranslated key
2188 if (!translated_value)
2190 translated_value = g_key_file_get_string (key_file, group_name, key,
2191 &key_file_error);
2193 if (!translated_value)
2194 g_propagate_error (error, key_file_error);
2197 if (free_languages)
2198 g_strfreev (languages);
2200 return translated_value;
2204 * g_key_file_get_locale_string_list:
2205 * @key_file: a #GKeyFile
2206 * @group_name: a group name
2207 * @key: a key
2208 * @locale: (nullable): a locale identifier or %NULL
2209 * @length: (out) (optional): return location for the number of returned strings or %NULL
2210 * @error: return location for a #GError or %NULL
2212 * Returns the values associated with @key under @group_name
2213 * translated in the given @locale if available. If @locale is
2214 * %NULL then the current locale is assumed.
2216 * If @key cannot be found then %NULL is returned and @error is set
2217 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2218 * with @key cannot be interpreted or no suitable translations
2219 * can be found then the untranslated values are returned. The
2220 * returned array is %NULL-terminated, so @length may optionally
2221 * be %NULL.
2223 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2224 * or %NULL if the key isn't found. The string array should be freed
2225 * with g_strfreev().
2227 * Since: 2.6
2229 gchar **
2230 g_key_file_get_locale_string_list (GKeyFile *key_file,
2231 const gchar *group_name,
2232 const gchar *key,
2233 const gchar *locale,
2234 gsize *length,
2235 GError **error)
2237 GError *key_file_error;
2238 gchar **values, *value;
2239 char list_separator[2];
2240 gsize len;
2242 g_return_val_if_fail (key_file != NULL, NULL);
2243 g_return_val_if_fail (group_name != NULL, NULL);
2244 g_return_val_if_fail (key != NULL, NULL);
2246 key_file_error = NULL;
2248 value = g_key_file_get_locale_string (key_file, group_name,
2249 key, locale,
2250 &key_file_error);
2252 if (key_file_error)
2253 g_propagate_error (error, key_file_error);
2255 if (!value)
2257 if (length)
2258 *length = 0;
2259 return NULL;
2262 len = strlen (value);
2263 if (value[len - 1] == key_file->list_separator)
2264 value[len - 1] = '\0';
2266 list_separator[0] = key_file->list_separator;
2267 list_separator[1] = '\0';
2268 values = g_strsplit (value, list_separator, 0);
2270 g_free (value);
2272 if (length)
2273 *length = g_strv_length (values);
2275 return values;
2279 * g_key_file_set_locale_string_list:
2280 * @key_file: a #GKeyFile
2281 * @group_name: a group name
2282 * @key: a key
2283 * @locale: a locale identifier
2284 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2285 * @length: the length of @list
2287 * Associates a list of string values for @key and @locale under
2288 * @group_name. If the translation for @key cannot be found then
2289 * it is created.
2291 * Since: 2.6
2293 void
2294 g_key_file_set_locale_string_list (GKeyFile *key_file,
2295 const gchar *group_name,
2296 const gchar *key,
2297 const gchar *locale,
2298 const gchar * const list[],
2299 gsize length)
2301 GString *value_list;
2302 gchar *full_key;
2303 gsize i;
2305 g_return_if_fail (key_file != NULL);
2306 g_return_if_fail (key != NULL);
2307 g_return_if_fail (locale != NULL);
2308 g_return_if_fail (length != 0);
2310 value_list = g_string_sized_new (length * 128);
2311 for (i = 0; i < length && list[i] != NULL; i++)
2313 gchar *value;
2315 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2316 g_string_append (value_list, value);
2317 g_string_append_c (value_list, key_file->list_separator);
2319 g_free (value);
2322 full_key = g_strdup_printf ("%s[%s]", key, locale);
2323 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2324 g_free (full_key);
2325 g_string_free (value_list, TRUE);
2329 * g_key_file_get_boolean:
2330 * @key_file: a #GKeyFile
2331 * @group_name: a group name
2332 * @key: a key
2333 * @error: return location for a #GError
2335 * Returns the value associated with @key under @group_name as a
2336 * boolean.
2338 * If @key cannot be found then %FALSE is returned and @error is set
2339 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2340 * associated with @key cannot be interpreted as a boolean then %FALSE
2341 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2343 * Returns: the value associated with the key as a boolean,
2344 * or %FALSE if the key was not found or could not be parsed.
2346 * Since: 2.6
2348 gboolean
2349 g_key_file_get_boolean (GKeyFile *key_file,
2350 const gchar *group_name,
2351 const gchar *key,
2352 GError **error)
2354 GError *key_file_error = NULL;
2355 gchar *value;
2356 gboolean bool_value;
2358 g_return_val_if_fail (key_file != NULL, FALSE);
2359 g_return_val_if_fail (group_name != NULL, FALSE);
2360 g_return_val_if_fail (key != NULL, FALSE);
2362 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2364 if (!value)
2366 g_propagate_error (error, key_file_error);
2367 return FALSE;
2370 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2371 &key_file_error);
2372 g_free (value);
2374 if (key_file_error)
2376 if (g_error_matches (key_file_error,
2377 G_KEY_FILE_ERROR,
2378 G_KEY_FILE_ERROR_INVALID_VALUE))
2380 g_set_error (error, G_KEY_FILE_ERROR,
2381 G_KEY_FILE_ERROR_INVALID_VALUE,
2382 _("Key file contains key “%s” "
2383 "which has a value that cannot be interpreted."),
2384 key);
2385 g_error_free (key_file_error);
2387 else
2388 g_propagate_error (error, key_file_error);
2391 return bool_value;
2395 * g_key_file_set_boolean:
2396 * @key_file: a #GKeyFile
2397 * @group_name: a group name
2398 * @key: a key
2399 * @value: %TRUE or %FALSE
2401 * Associates a new boolean value with @key under @group_name.
2402 * If @key cannot be found then it is created.
2404 * Since: 2.6
2406 void
2407 g_key_file_set_boolean (GKeyFile *key_file,
2408 const gchar *group_name,
2409 const gchar *key,
2410 gboolean value)
2412 gchar *result;
2414 g_return_if_fail (key_file != NULL);
2416 result = g_key_file_parse_boolean_as_value (key_file, value);
2417 g_key_file_set_value (key_file, group_name, key, result);
2418 g_free (result);
2422 * g_key_file_get_boolean_list:
2423 * @key_file: a #GKeyFile
2424 * @group_name: a group name
2425 * @key: a key
2426 * @length: (out): the number of booleans returned
2427 * @error: return location for a #GError
2429 * Returns the values associated with @key under @group_name as
2430 * booleans.
2432 * If @key cannot be found then %NULL is returned and @error is set to
2433 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2434 * with @key cannot be interpreted as booleans then %NULL is returned
2435 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2437 * Returns: (array length=length) (element-type gboolean) (transfer container):
2438 * the values associated with the key as a list of booleans, or %NULL if the
2439 * key was not found or could not be parsed. The returned list of booleans
2440 * should be freed with g_free() when no longer needed.
2442 * Since: 2.6
2444 gboolean *
2445 g_key_file_get_boolean_list (GKeyFile *key_file,
2446 const gchar *group_name,
2447 const gchar *key,
2448 gsize *length,
2449 GError **error)
2451 GError *key_file_error;
2452 gchar **values;
2453 gboolean *bool_values;
2454 gsize i, num_bools;
2456 g_return_val_if_fail (key_file != NULL, NULL);
2457 g_return_val_if_fail (group_name != NULL, NULL);
2458 g_return_val_if_fail (key != NULL, NULL);
2460 if (length)
2461 *length = 0;
2463 key_file_error = NULL;
2465 values = g_key_file_get_string_list (key_file, group_name, key,
2466 &num_bools, &key_file_error);
2468 if (key_file_error)
2469 g_propagate_error (error, key_file_error);
2471 if (!values)
2472 return NULL;
2474 bool_values = g_new (gboolean, num_bools);
2476 for (i = 0; i < num_bools; i++)
2478 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2479 values[i],
2480 &key_file_error);
2482 if (key_file_error)
2484 g_propagate_error (error, key_file_error);
2485 g_strfreev (values);
2486 g_free (bool_values);
2488 return NULL;
2491 g_strfreev (values);
2493 if (length)
2494 *length = num_bools;
2496 return bool_values;
2500 * g_key_file_set_boolean_list:
2501 * @key_file: a #GKeyFile
2502 * @group_name: a group name
2503 * @key: a key
2504 * @list: (array length=length): an array of boolean values
2505 * @length: length of @list
2507 * Associates a list of boolean values with @key under @group_name.
2508 * If @key cannot be found then it is created.
2509 * If @group_name is %NULL, the start_group is used.
2511 * Since: 2.6
2513 void
2514 g_key_file_set_boolean_list (GKeyFile *key_file,
2515 const gchar *group_name,
2516 const gchar *key,
2517 gboolean list[],
2518 gsize length)
2520 GString *value_list;
2521 gsize i;
2523 g_return_if_fail (key_file != NULL);
2524 g_return_if_fail (list != NULL);
2526 value_list = g_string_sized_new (length * 8);
2527 for (i = 0; i < length; i++)
2529 gchar *value;
2531 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2533 g_string_append (value_list, value);
2534 g_string_append_c (value_list, key_file->list_separator);
2536 g_free (value);
2539 g_key_file_set_value (key_file, group_name, key, value_list->str);
2540 g_string_free (value_list, TRUE);
2544 * g_key_file_get_integer:
2545 * @key_file: a #GKeyFile
2546 * @group_name: a group name
2547 * @key: a key
2548 * @error: return location for a #GError
2550 * Returns the value associated with @key under @group_name as an
2551 * integer.
2553 * If @key cannot be found then 0 is returned and @error is set to
2554 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2555 * with @key cannot be interpreted as an integer, or is out of range
2556 * for a #gint, then 0 is returned
2557 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2559 * Returns: the value associated with the key as an integer, or
2560 * 0 if the key was not found or could not be parsed.
2562 * Since: 2.6
2564 gint
2565 g_key_file_get_integer (GKeyFile *key_file,
2566 const gchar *group_name,
2567 const gchar *key,
2568 GError **error)
2570 GError *key_file_error;
2571 gchar *value;
2572 gint int_value;
2574 g_return_val_if_fail (key_file != NULL, -1);
2575 g_return_val_if_fail (group_name != NULL, -1);
2576 g_return_val_if_fail (key != NULL, -1);
2578 key_file_error = NULL;
2580 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2582 if (key_file_error)
2584 g_propagate_error (error, key_file_error);
2585 return 0;
2588 int_value = g_key_file_parse_value_as_integer (key_file, value,
2589 &key_file_error);
2590 g_free (value);
2592 if (key_file_error)
2594 if (g_error_matches (key_file_error,
2595 G_KEY_FILE_ERROR,
2596 G_KEY_FILE_ERROR_INVALID_VALUE))
2598 g_set_error (error, G_KEY_FILE_ERROR,
2599 G_KEY_FILE_ERROR_INVALID_VALUE,
2600 _("Key file contains key “%s” in group “%s” "
2601 "which has a value that cannot be interpreted."),
2602 key, group_name);
2603 g_error_free (key_file_error);
2605 else
2606 g_propagate_error (error, key_file_error);
2609 return int_value;
2613 * g_key_file_set_integer:
2614 * @key_file: a #GKeyFile
2615 * @group_name: a group name
2616 * @key: a key
2617 * @value: an integer value
2619 * Associates a new integer value with @key under @group_name.
2620 * If @key cannot be found then it is created.
2622 * Since: 2.6
2624 void
2625 g_key_file_set_integer (GKeyFile *key_file,
2626 const gchar *group_name,
2627 const gchar *key,
2628 gint value)
2630 gchar *result;
2632 g_return_if_fail (key_file != NULL);
2634 result = g_key_file_parse_integer_as_value (key_file, value);
2635 g_key_file_set_value (key_file, group_name, key, result);
2636 g_free (result);
2640 * g_key_file_get_int64:
2641 * @key_file: a non-%NULL #GKeyFile
2642 * @group_name: a non-%NULL group name
2643 * @key: a non-%NULL key
2644 * @error: return location for a #GError
2646 * Returns the value associated with @key under @group_name as a signed
2647 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2648 * 64-bit results without truncation.
2650 * Returns: the value associated with the key as a signed 64-bit integer, or
2651 * 0 if the key was not found or could not be parsed.
2653 * Since: 2.26
2655 gint64
2656 g_key_file_get_int64 (GKeyFile *key_file,
2657 const gchar *group_name,
2658 const gchar *key,
2659 GError **error)
2661 gchar *s, *end;
2662 gint64 v;
2664 g_return_val_if_fail (key_file != NULL, -1);
2665 g_return_val_if_fail (group_name != NULL, -1);
2666 g_return_val_if_fail (key != NULL, -1);
2668 s = g_key_file_get_value (key_file, group_name, key, error);
2670 if (s == NULL)
2671 return 0;
2673 v = g_ascii_strtoll (s, &end, 10);
2675 if (*s == '\0' || *end != '\0')
2677 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2678 _("Key “%s” in group “%s” has value “%s” "
2679 "where %s was expected"),
2680 key, group_name, s, "int64");
2681 g_free (s);
2682 return 0;
2685 g_free (s);
2686 return v;
2690 * g_key_file_set_int64:
2691 * @key_file: a #GKeyFile
2692 * @group_name: a group name
2693 * @key: a key
2694 * @value: an integer value
2696 * Associates a new integer value with @key under @group_name.
2697 * If @key cannot be found then it is created.
2699 * Since: 2.26
2701 void
2702 g_key_file_set_int64 (GKeyFile *key_file,
2703 const gchar *group_name,
2704 const gchar *key,
2705 gint64 value)
2707 gchar *result;
2709 g_return_if_fail (key_file != NULL);
2711 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2712 g_key_file_set_value (key_file, group_name, key, result);
2713 g_free (result);
2717 * g_key_file_get_uint64:
2718 * @key_file: a non-%NULL #GKeyFile
2719 * @group_name: a non-%NULL group name
2720 * @key: a non-%NULL key
2721 * @error: return location for a #GError
2723 * Returns the value associated with @key under @group_name as an unsigned
2724 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2725 * large positive results without truncation.
2727 * Returns: the value associated with the key as an unsigned 64-bit integer,
2728 * or 0 if the key was not found or could not be parsed.
2730 * Since: 2.26
2732 guint64
2733 g_key_file_get_uint64 (GKeyFile *key_file,
2734 const gchar *group_name,
2735 const gchar *key,
2736 GError **error)
2738 gchar *s, *end;
2739 guint64 v;
2741 g_return_val_if_fail (key_file != NULL, -1);
2742 g_return_val_if_fail (group_name != NULL, -1);
2743 g_return_val_if_fail (key != NULL, -1);
2745 s = g_key_file_get_value (key_file, group_name, key, error);
2747 if (s == NULL)
2748 return 0;
2750 v = g_ascii_strtoull (s, &end, 10);
2752 if (*s == '\0' || *end != '\0')
2754 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2755 _("Key “%s” in group “%s” has value “%s” "
2756 "where %s was expected"),
2757 key, group_name, s, "uint64");
2758 g_free (s);
2759 return 0;
2762 g_free (s);
2763 return v;
2767 * g_key_file_set_uint64:
2768 * @key_file: a #GKeyFile
2769 * @group_name: a group name
2770 * @key: a key
2771 * @value: an integer value
2773 * Associates a new integer value with @key under @group_name.
2774 * If @key cannot be found then it is created.
2776 * Since: 2.26
2778 void
2779 g_key_file_set_uint64 (GKeyFile *key_file,
2780 const gchar *group_name,
2781 const gchar *key,
2782 guint64 value)
2784 gchar *result;
2786 g_return_if_fail (key_file != NULL);
2788 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2789 g_key_file_set_value (key_file, group_name, key, result);
2790 g_free (result);
2794 * g_key_file_get_integer_list:
2795 * @key_file: a #GKeyFile
2796 * @group_name: a group name
2797 * @key: a key
2798 * @length: (out): the number of integers returned
2799 * @error: return location for a #GError
2801 * Returns the values associated with @key under @group_name as
2802 * integers.
2804 * If @key cannot be found then %NULL is returned and @error is set to
2805 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2806 * with @key cannot be interpreted as integers, or are out of range for
2807 * #gint, then %NULL is returned
2808 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2810 * Returns: (array length=length) (element-type gint) (transfer container):
2811 * the values associated with the key as a list of integers, or %NULL if
2812 * the key was not found or could not be parsed. The returned list of
2813 * integers should be freed with g_free() when no longer needed.
2815 * Since: 2.6
2817 gint *
2818 g_key_file_get_integer_list (GKeyFile *key_file,
2819 const gchar *group_name,
2820 const gchar *key,
2821 gsize *length,
2822 GError **error)
2824 GError *key_file_error = NULL;
2825 gchar **values;
2826 gint *int_values;
2827 gsize i, num_ints;
2829 g_return_val_if_fail (key_file != NULL, NULL);
2830 g_return_val_if_fail (group_name != NULL, NULL);
2831 g_return_val_if_fail (key != NULL, NULL);
2833 if (length)
2834 *length = 0;
2836 values = g_key_file_get_string_list (key_file, group_name, key,
2837 &num_ints, &key_file_error);
2839 if (key_file_error)
2840 g_propagate_error (error, key_file_error);
2842 if (!values)
2843 return NULL;
2845 int_values = g_new (gint, num_ints);
2847 for (i = 0; i < num_ints; i++)
2849 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2850 values[i],
2851 &key_file_error);
2853 if (key_file_error)
2855 g_propagate_error (error, key_file_error);
2856 g_strfreev (values);
2857 g_free (int_values);
2859 return NULL;
2862 g_strfreev (values);
2864 if (length)
2865 *length = num_ints;
2867 return int_values;
2871 * g_key_file_set_integer_list:
2872 * @key_file: a #GKeyFile
2873 * @group_name: a group name
2874 * @key: a key
2875 * @list: (array length=length): an array of integer values
2876 * @length: number of integer values in @list
2878 * Associates a list of integer values with @key under @group_name.
2879 * If @key cannot be found then it is created.
2881 * Since: 2.6
2883 void
2884 g_key_file_set_integer_list (GKeyFile *key_file,
2885 const gchar *group_name,
2886 const gchar *key,
2887 gint list[],
2888 gsize length)
2890 GString *values;
2891 gsize i;
2893 g_return_if_fail (key_file != NULL);
2894 g_return_if_fail (list != NULL);
2896 values = g_string_sized_new (length * 16);
2897 for (i = 0; i < length; i++)
2899 gchar *value;
2901 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2903 g_string_append (values, value);
2904 g_string_append_c (values, key_file->list_separator);
2906 g_free (value);
2909 g_key_file_set_value (key_file, group_name, key, values->str);
2910 g_string_free (values, TRUE);
2914 * g_key_file_get_double:
2915 * @key_file: a #GKeyFile
2916 * @group_name: a group name
2917 * @key: a key
2918 * @error: return location for a #GError
2920 * Returns the value associated with @key under @group_name as a
2921 * double. If @group_name is %NULL, the start_group is used.
2923 * If @key cannot be found then 0.0 is returned and @error is set to
2924 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2925 * with @key cannot be interpreted as a double then 0.0 is returned
2926 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2928 * Returns: the value associated with the key as a double, or
2929 * 0.0 if the key was not found or could not be parsed.
2931 * Since: 2.12
2933 gdouble
2934 g_key_file_get_double (GKeyFile *key_file,
2935 const gchar *group_name,
2936 const gchar *key,
2937 GError **error)
2939 GError *key_file_error;
2940 gchar *value;
2941 gdouble double_value;
2943 g_return_val_if_fail (key_file != NULL, -1);
2944 g_return_val_if_fail (group_name != NULL, -1);
2945 g_return_val_if_fail (key != NULL, -1);
2947 key_file_error = NULL;
2949 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2951 if (key_file_error)
2953 g_propagate_error (error, key_file_error);
2954 return 0;
2957 double_value = g_key_file_parse_value_as_double (key_file, value,
2958 &key_file_error);
2959 g_free (value);
2961 if (key_file_error)
2963 if (g_error_matches (key_file_error,
2964 G_KEY_FILE_ERROR,
2965 G_KEY_FILE_ERROR_INVALID_VALUE))
2967 g_set_error (error, G_KEY_FILE_ERROR,
2968 G_KEY_FILE_ERROR_INVALID_VALUE,
2969 _("Key file contains key “%s” in group “%s” "
2970 "which has a value that cannot be interpreted."),
2971 key, group_name);
2972 g_error_free (key_file_error);
2974 else
2975 g_propagate_error (error, key_file_error);
2978 return double_value;
2982 * g_key_file_set_double:
2983 * @key_file: a #GKeyFile
2984 * @group_name: a group name
2985 * @key: a key
2986 * @value: an double value
2988 * Associates a new double value with @key under @group_name.
2989 * If @key cannot be found then it is created.
2991 * Since: 2.12
2993 void
2994 g_key_file_set_double (GKeyFile *key_file,
2995 const gchar *group_name,
2996 const gchar *key,
2997 gdouble value)
2999 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3001 g_return_if_fail (key_file != NULL);
3003 g_ascii_dtostr (result, sizeof (result), value);
3004 g_key_file_set_value (key_file, group_name, key, result);
3008 * g_key_file_get_double_list:
3009 * @key_file: a #GKeyFile
3010 * @group_name: a group name
3011 * @key: a key
3012 * @length: (out): the number of doubles returned
3013 * @error: return location for a #GError
3015 * Returns the values associated with @key under @group_name as
3016 * doubles.
3018 * If @key cannot be found then %NULL is returned and @error is set to
3019 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
3020 * with @key cannot be interpreted as doubles then %NULL is returned
3021 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
3023 * Returns: (array length=length) (element-type gdouble) (transfer container):
3024 * the values associated with the key as a list of doubles, or %NULL if the
3025 * key was not found or could not be parsed. The returned list of doubles
3026 * should be freed with g_free() when no longer needed.
3028 * Since: 2.12
3030 gdouble *
3031 g_key_file_get_double_list (GKeyFile *key_file,
3032 const gchar *group_name,
3033 const gchar *key,
3034 gsize *length,
3035 GError **error)
3037 GError *key_file_error = NULL;
3038 gchar **values;
3039 gdouble *double_values;
3040 gsize i, num_doubles;
3042 g_return_val_if_fail (key_file != NULL, NULL);
3043 g_return_val_if_fail (group_name != NULL, NULL);
3044 g_return_val_if_fail (key != NULL, NULL);
3046 if (length)
3047 *length = 0;
3049 values = g_key_file_get_string_list (key_file, group_name, key,
3050 &num_doubles, &key_file_error);
3052 if (key_file_error)
3053 g_propagate_error (error, key_file_error);
3055 if (!values)
3056 return NULL;
3058 double_values = g_new (gdouble, num_doubles);
3060 for (i = 0; i < num_doubles; i++)
3062 double_values[i] = g_key_file_parse_value_as_double (key_file,
3063 values[i],
3064 &key_file_error);
3066 if (key_file_error)
3068 g_propagate_error (error, key_file_error);
3069 g_strfreev (values);
3070 g_free (double_values);
3072 return NULL;
3075 g_strfreev (values);
3077 if (length)
3078 *length = num_doubles;
3080 return double_values;
3084 * g_key_file_set_double_list:
3085 * @key_file: a #GKeyFile
3086 * @group_name: a group name
3087 * @key: a key
3088 * @list: (array length=length): an array of double values
3089 * @length: number of double values in @list
3091 * Associates a list of double values with @key under
3092 * @group_name. If @key cannot be found then it is created.
3094 * Since: 2.12
3096 void
3097 g_key_file_set_double_list (GKeyFile *key_file,
3098 const gchar *group_name,
3099 const gchar *key,
3100 gdouble list[],
3101 gsize length)
3103 GString *values;
3104 gsize i;
3106 g_return_if_fail (key_file != NULL);
3107 g_return_if_fail (list != NULL);
3109 values = g_string_sized_new (length * 16);
3110 for (i = 0; i < length; i++)
3112 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3114 g_ascii_dtostr( result, sizeof (result), list[i] );
3116 g_string_append (values, result);
3117 g_string_append_c (values, key_file->list_separator);
3120 g_key_file_set_value (key_file, group_name, key, values->str);
3121 g_string_free (values, TRUE);
3124 static gboolean
3125 g_key_file_set_key_comment (GKeyFile *key_file,
3126 const gchar *group_name,
3127 const gchar *key,
3128 const gchar *comment,
3129 GError **error)
3131 GKeyFileGroup *group;
3132 GKeyFileKeyValuePair *pair;
3133 GList *key_node, *comment_node, *tmp;
3135 group = g_key_file_lookup_group (key_file, group_name);
3136 if (!group)
3138 g_set_error (error, G_KEY_FILE_ERROR,
3139 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3140 _("Key file does not have group “%s”"),
3141 group_name ? group_name : "(null)");
3143 return FALSE;
3146 /* First find the key the comments are supposed to be
3147 * associated with
3149 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3151 if (key_node == NULL)
3153 set_not_found_key_error (group->name, key, error);
3154 return FALSE;
3157 /* Then find all the comments already associated with the
3158 * key and free them
3160 tmp = key_node->next;
3161 while (tmp != NULL)
3163 pair = (GKeyFileKeyValuePair *) tmp->data;
3165 if (pair->key != NULL)
3166 break;
3168 comment_node = tmp;
3169 tmp = tmp->next;
3170 g_key_file_remove_key_value_pair_node (key_file, group,
3171 comment_node);
3174 if (comment == NULL)
3175 return TRUE;
3177 /* Now we can add our new comment
3179 pair = g_slice_new (GKeyFileKeyValuePair);
3180 pair->key = NULL;
3181 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3183 key_node = g_list_insert (key_node, pair, 1);
3185 return TRUE;
3188 static gboolean
3189 g_key_file_set_group_comment (GKeyFile *key_file,
3190 const gchar *group_name,
3191 const gchar *comment,
3192 GError **error)
3194 GKeyFileGroup *group;
3196 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3198 group = g_key_file_lookup_group (key_file, group_name);
3199 if (!group)
3201 g_set_error (error, G_KEY_FILE_ERROR,
3202 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3203 _("Key file does not have group “%s”"),
3204 group_name ? group_name : "(null)");
3206 return FALSE;
3209 /* First remove any existing comment
3211 if (group->comment)
3213 g_key_file_key_value_pair_free (group->comment);
3214 group->comment = NULL;
3217 if (comment == NULL)
3218 return TRUE;
3220 /* Now we can add our new comment
3222 group->comment = g_slice_new (GKeyFileKeyValuePair);
3223 group->comment->key = NULL;
3224 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3226 return TRUE;
3229 static gboolean
3230 g_key_file_set_top_comment (GKeyFile *key_file,
3231 const gchar *comment,
3232 GError **error)
3234 GList *group_node;
3235 GKeyFileGroup *group;
3236 GKeyFileKeyValuePair *pair;
3238 /* The last group in the list should be the top (comments only)
3239 * group in the file
3241 g_warn_if_fail (key_file->groups != NULL);
3242 group_node = g_list_last (key_file->groups);
3243 group = (GKeyFileGroup *) group_node->data;
3244 g_warn_if_fail (group->name == NULL);
3246 /* Note all keys must be comments at the top of
3247 * the file, so we can just free it all.
3249 g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3250 group->key_value_pairs = NULL;
3252 if (comment == NULL)
3253 return TRUE;
3255 pair = g_slice_new (GKeyFileKeyValuePair);
3256 pair->key = NULL;
3257 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3259 group->key_value_pairs =
3260 g_list_prepend (group->key_value_pairs, pair);
3262 return TRUE;
3266 * g_key_file_set_comment:
3267 * @key_file: a #GKeyFile
3268 * @group_name: (nullable): a group name, or %NULL
3269 * @key: (nullable): a key
3270 * @comment: a comment
3271 * @error: return location for a #GError
3273 * Places a comment above @key from @group_name.
3275 * If @key is %NULL then @comment will be written above @group_name.
3276 * If both @key and @group_name are %NULL, then @comment will be
3277 * written above the first group in the file.
3279 * Note that this function prepends a '#' comment marker to
3280 * each line of @comment.
3282 * Returns: %TRUE if the comment was written, %FALSE otherwise
3284 * Since: 2.6
3286 gboolean
3287 g_key_file_set_comment (GKeyFile *key_file,
3288 const gchar *group_name,
3289 const gchar *key,
3290 const gchar *comment,
3291 GError **error)
3293 g_return_val_if_fail (key_file != NULL, FALSE);
3295 if (group_name != NULL && key != NULL)
3297 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3298 return FALSE;
3300 else if (group_name != NULL)
3302 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3303 return FALSE;
3305 else
3307 if (!g_key_file_set_top_comment (key_file, comment, error))
3308 return FALSE;
3311 return TRUE;
3314 static gchar *
3315 g_key_file_get_key_comment (GKeyFile *key_file,
3316 const gchar *group_name,
3317 const gchar *key,
3318 GError **error)
3320 GKeyFileGroup *group;
3321 GKeyFileKeyValuePair *pair;
3322 GList *key_node, *tmp;
3323 GString *string;
3324 gchar *comment;
3326 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3328 group = g_key_file_lookup_group (key_file, group_name);
3329 if (!group)
3331 g_set_error (error, G_KEY_FILE_ERROR,
3332 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3333 _("Key file does not have group “%s”"),
3334 group_name ? group_name : "(null)");
3336 return NULL;
3339 /* First find the key the comments are supposed to be
3340 * associated with
3342 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3344 if (key_node == NULL)
3346 set_not_found_key_error (group->name, key, error);
3347 return NULL;
3350 string = NULL;
3352 /* Then find all the comments already associated with the
3353 * key and concatentate them.
3355 tmp = key_node->next;
3356 if (!key_node->next)
3357 return NULL;
3359 pair = (GKeyFileKeyValuePair *) tmp->data;
3360 if (pair->key != NULL)
3361 return NULL;
3363 while (tmp->next)
3365 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3367 if (pair->key != NULL)
3368 break;
3370 tmp = tmp->next;
3373 while (tmp != key_node)
3375 pair = (GKeyFileKeyValuePair *) tmp->data;
3377 if (string == NULL)
3378 string = g_string_sized_new (512);
3380 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3381 g_string_append (string, comment);
3382 g_free (comment);
3384 tmp = tmp->prev;
3387 if (string != NULL)
3389 comment = string->str;
3390 g_string_free (string, FALSE);
3392 else
3393 comment = NULL;
3395 return comment;
3398 static gchar *
3399 get_group_comment (GKeyFile *key_file,
3400 GKeyFileGroup *group,
3401 GError **error)
3403 GString *string;
3404 GList *tmp;
3405 gchar *comment;
3407 string = NULL;
3409 tmp = group->key_value_pairs;
3410 while (tmp)
3412 GKeyFileKeyValuePair *pair;
3414 pair = (GKeyFileKeyValuePair *) tmp->data;
3416 if (pair->key != NULL)
3418 tmp = tmp->prev;
3419 break;
3422 if (tmp->next == NULL)
3423 break;
3425 tmp = tmp->next;
3428 while (tmp != NULL)
3430 GKeyFileKeyValuePair *pair;
3432 pair = (GKeyFileKeyValuePair *) tmp->data;
3434 if (string == NULL)
3435 string = g_string_sized_new (512);
3437 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3438 g_string_append (string, comment);
3439 g_free (comment);
3441 tmp = tmp->prev;
3444 if (string != NULL)
3445 return g_string_free (string, FALSE);
3447 return NULL;
3450 static gchar *
3451 g_key_file_get_group_comment (GKeyFile *key_file,
3452 const gchar *group_name,
3453 GError **error)
3455 GList *group_node;
3456 GKeyFileGroup *group;
3458 group = g_key_file_lookup_group (key_file, group_name);
3459 if (!group)
3461 g_set_error (error, G_KEY_FILE_ERROR,
3462 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3463 _("Key file does not have group “%s”"),
3464 group_name ? group_name : "(null)");
3466 return NULL;
3469 if (group->comment)
3470 return g_strdup (group->comment->value);
3472 group_node = g_key_file_lookup_group_node (key_file, group_name);
3473 group_node = group_node->next;
3474 group = (GKeyFileGroup *)group_node->data;
3475 return get_group_comment (key_file, group, error);
3478 static gchar *
3479 g_key_file_get_top_comment (GKeyFile *key_file,
3480 GError **error)
3482 GList *group_node;
3483 GKeyFileGroup *group;
3485 /* The last group in the list should be the top (comments only)
3486 * group in the file
3488 g_warn_if_fail (key_file->groups != NULL);
3489 group_node = g_list_last (key_file->groups);
3490 group = (GKeyFileGroup *) group_node->data;
3491 g_warn_if_fail (group->name == NULL);
3493 return get_group_comment (key_file, group, error);
3497 * g_key_file_get_comment:
3498 * @key_file: a #GKeyFile
3499 * @group_name: (nullable): a group name, or %NULL
3500 * @key: a key
3501 * @error: return location for a #GError
3503 * Retrieves a comment above @key from @group_name.
3504 * If @key is %NULL then @comment will be read from above
3505 * @group_name. If both @key and @group_name are %NULL, then
3506 * @comment will be read from above the first group in the file.
3508 * Note that the returned string includes the '#' comment markers.
3510 * Returns: a comment that should be freed with g_free()
3512 * Since: 2.6
3514 gchar *
3515 g_key_file_get_comment (GKeyFile *key_file,
3516 const gchar *group_name,
3517 const gchar *key,
3518 GError **error)
3520 g_return_val_if_fail (key_file != NULL, NULL);
3522 if (group_name != NULL && key != NULL)
3523 return g_key_file_get_key_comment (key_file, group_name, key, error);
3524 else if (group_name != NULL)
3525 return g_key_file_get_group_comment (key_file, group_name, error);
3526 else
3527 return g_key_file_get_top_comment (key_file, error);
3531 * g_key_file_remove_comment:
3532 * @key_file: a #GKeyFile
3533 * @group_name: (nullable): a group name, or %NULL
3534 * @key: (nullable): a key
3535 * @error: return location for a #GError
3537 * Removes a comment above @key from @group_name.
3538 * If @key is %NULL then @comment will be removed above @group_name.
3539 * If both @key and @group_name are %NULL, then @comment will
3540 * be removed above the first group in the file.
3542 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3544 * Since: 2.6
3547 gboolean
3548 g_key_file_remove_comment (GKeyFile *key_file,
3549 const gchar *group_name,
3550 const gchar *key,
3551 GError **error)
3553 g_return_val_if_fail (key_file != NULL, FALSE);
3555 if (group_name != NULL && key != NULL)
3556 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3557 else if (group_name != NULL)
3558 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3559 else
3560 return g_key_file_set_top_comment (key_file, NULL, error);
3564 * g_key_file_has_group:
3565 * @key_file: a #GKeyFile
3566 * @group_name: a group name
3568 * Looks whether the key file has the group @group_name.
3570 * Returns: %TRUE if @group_name is a part of @key_file, %FALSE
3571 * otherwise.
3572 * Since: 2.6
3574 gboolean
3575 g_key_file_has_group (GKeyFile *key_file,
3576 const gchar *group_name)
3578 g_return_val_if_fail (key_file != NULL, FALSE);
3579 g_return_val_if_fail (group_name != NULL, FALSE);
3581 return g_key_file_lookup_group (key_file, group_name) != NULL;
3584 /* This code remains from a historical attempt to add a new public API
3585 * which respects the GError rules.
3587 static gboolean
3588 g_key_file_has_key_full (GKeyFile *key_file,
3589 const gchar *group_name,
3590 const gchar *key,
3591 gboolean *has_key,
3592 GError **error)
3594 GKeyFileKeyValuePair *pair;
3595 GKeyFileGroup *group;
3597 g_return_val_if_fail (key_file != NULL, FALSE);
3598 g_return_val_if_fail (group_name != NULL, FALSE);
3599 g_return_val_if_fail (key != NULL, FALSE);
3601 group = g_key_file_lookup_group (key_file, group_name);
3603 if (!group)
3605 g_set_error (error, G_KEY_FILE_ERROR,
3606 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3607 _("Key file does not have group “%s”"),
3608 group_name);
3610 return FALSE;
3613 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3615 if (has_key)
3616 *has_key = pair != NULL;
3617 return TRUE;
3621 * g_key_file_has_key: (skip)
3622 * @key_file: a #GKeyFile
3623 * @group_name: a group name
3624 * @key: a key name
3625 * @error: return location for a #GError
3627 * Looks whether the key file has the key @key in the group
3628 * @group_name.
3630 * Note that this function does not follow the rules for #GError strictly;
3631 * the return value both carries meaning and signals an error. To use
3632 * this function, you must pass a #GError pointer in @error, and check
3633 * whether it is not %NULL to see if an error occurred.
3635 * Language bindings should use g_key_file_get_value() to test whether
3636 * or not a key exists.
3638 * Returns: %TRUE if @key is a part of @group_name, %FALSE otherwise
3640 * Since: 2.6
3642 gboolean
3643 g_key_file_has_key (GKeyFile *key_file,
3644 const gchar *group_name,
3645 const gchar *key,
3646 GError **error)
3648 GError *temp_error = NULL;
3649 gboolean has_key;
3651 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3653 return has_key;
3655 else
3657 g_propagate_error (error, temp_error);
3658 return FALSE;
3662 static void
3663 g_key_file_add_group (GKeyFile *key_file,
3664 const gchar *group_name)
3666 GKeyFileGroup *group;
3668 g_return_if_fail (key_file != NULL);
3669 g_return_if_fail (g_key_file_is_group_name (group_name));
3671 group = g_key_file_lookup_group (key_file, group_name);
3672 if (group != NULL)
3674 key_file->current_group = group;
3675 return;
3678 group = g_slice_new0 (GKeyFileGroup);
3679 group->name = g_strdup (group_name);
3680 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3681 key_file->groups = g_list_prepend (key_file->groups, group);
3682 key_file->current_group = group;
3684 if (key_file->start_group == NULL)
3685 key_file->start_group = group;
3687 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3690 static void
3691 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3693 if (pair != NULL)
3695 g_free (pair->key);
3696 g_free (pair->value);
3697 g_slice_free (GKeyFileKeyValuePair, pair);
3701 /* Be careful not to call this function on a node with data in the
3702 * lookup map without removing it from the lookup map, first.
3704 * Some current cases where this warning is not a concern are
3705 * when:
3706 * - the node being removed is a comment node
3707 * - the entire lookup map is getting destroyed soon after
3708 * anyway.
3710 static void
3711 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3712 GKeyFileGroup *group,
3713 GList *pair_node)
3716 GKeyFileKeyValuePair *pair;
3718 pair = (GKeyFileKeyValuePair *) pair_node->data;
3720 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3722 g_warn_if_fail (pair->value != NULL);
3724 g_key_file_key_value_pair_free (pair);
3726 g_list_free_1 (pair_node);
3729 static void
3730 g_key_file_remove_group_node (GKeyFile *key_file,
3731 GList *group_node)
3733 GKeyFileGroup *group;
3734 GList *tmp;
3736 group = (GKeyFileGroup *) group_node->data;
3738 if (group->name)
3739 g_hash_table_remove (key_file->group_hash, group->name);
3741 /* If the current group gets deleted make the current group the last
3742 * added group.
3744 if (key_file->current_group == group)
3746 /* groups should always contain at least the top comment group,
3747 * unless g_key_file_clear has been called
3749 if (key_file->groups)
3750 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3751 else
3752 key_file->current_group = NULL;
3755 /* If the start group gets deleted make the start group the first
3756 * added group.
3758 if (key_file->start_group == group)
3760 tmp = g_list_last (key_file->groups);
3761 while (tmp != NULL)
3763 if (tmp != group_node &&
3764 ((GKeyFileGroup *) tmp->data)->name != NULL)
3765 break;
3767 tmp = tmp->prev;
3770 if (tmp)
3771 key_file->start_group = (GKeyFileGroup *) tmp->data;
3772 else
3773 key_file->start_group = NULL;
3776 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3778 tmp = group->key_value_pairs;
3779 while (tmp != NULL)
3781 GList *pair_node;
3783 pair_node = tmp;
3784 tmp = tmp->next;
3785 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3788 g_warn_if_fail (group->key_value_pairs == NULL);
3790 if (group->comment)
3792 g_key_file_key_value_pair_free (group->comment);
3793 group->comment = NULL;
3796 if (group->lookup_map)
3798 g_hash_table_destroy (group->lookup_map);
3799 group->lookup_map = NULL;
3802 g_free ((gchar *) group->name);
3803 g_slice_free (GKeyFileGroup, group);
3804 g_list_free_1 (group_node);
3808 * g_key_file_remove_group:
3809 * @key_file: a #GKeyFile
3810 * @group_name: a group name
3811 * @error: return location for a #GError or %NULL
3813 * Removes the specified group, @group_name,
3814 * from the key file.
3816 * Returns: %TRUE if the group was removed, %FALSE otherwise
3818 * Since: 2.6
3820 gboolean
3821 g_key_file_remove_group (GKeyFile *key_file,
3822 const gchar *group_name,
3823 GError **error)
3825 GList *group_node;
3827 g_return_val_if_fail (key_file != NULL, FALSE);
3828 g_return_val_if_fail (group_name != NULL, FALSE);
3830 group_node = g_key_file_lookup_group_node (key_file, group_name);
3832 if (!group_node)
3834 g_set_error (error, G_KEY_FILE_ERROR,
3835 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3836 _("Key file does not have group “%s”"),
3837 group_name);
3838 return FALSE;
3841 g_key_file_remove_group_node (key_file, group_node);
3843 return TRUE;
3846 static void
3847 g_key_file_add_key_value_pair (GKeyFile *key_file,
3848 GKeyFileGroup *group,
3849 GKeyFileKeyValuePair *pair)
3851 g_hash_table_replace (group->lookup_map, pair->key, pair);
3852 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3855 static void
3856 g_key_file_add_key (GKeyFile *key_file,
3857 GKeyFileGroup *group,
3858 const gchar *key,
3859 const gchar *value)
3861 GKeyFileKeyValuePair *pair;
3863 pair = g_slice_new (GKeyFileKeyValuePair);
3864 pair->key = g_strdup (key);
3865 pair->value = g_strdup (value);
3867 g_key_file_add_key_value_pair (key_file, group, pair);
3871 * g_key_file_remove_key:
3872 * @key_file: a #GKeyFile
3873 * @group_name: a group name
3874 * @key: a key name to remove
3875 * @error: return location for a #GError or %NULL
3877 * Removes @key in @group_name from the key file.
3879 * Returns: %TRUE if the key was removed, %FALSE otherwise
3881 * Since: 2.6
3883 gboolean
3884 g_key_file_remove_key (GKeyFile *key_file,
3885 const gchar *group_name,
3886 const gchar *key,
3887 GError **error)
3889 GKeyFileGroup *group;
3890 GKeyFileKeyValuePair *pair;
3892 g_return_val_if_fail (key_file != NULL, FALSE);
3893 g_return_val_if_fail (group_name != NULL, FALSE);
3894 g_return_val_if_fail (key != NULL, FALSE);
3896 pair = NULL;
3898 group = g_key_file_lookup_group (key_file, group_name);
3899 if (!group)
3901 g_set_error (error, G_KEY_FILE_ERROR,
3902 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3903 _("Key file does not have group “%s”"),
3904 group_name);
3905 return FALSE;
3908 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3910 if (!pair)
3912 set_not_found_key_error (group->name, key, error);
3913 return FALSE;
3916 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3917 g_hash_table_remove (group->lookup_map, pair->key);
3918 g_key_file_key_value_pair_free (pair);
3920 return TRUE;
3923 static GList *
3924 g_key_file_lookup_group_node (GKeyFile *key_file,
3925 const gchar *group_name)
3927 GKeyFileGroup *group;
3928 GList *tmp;
3930 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3932 group = (GKeyFileGroup *) tmp->data;
3934 if (group && group->name && strcmp (group->name, group_name) == 0)
3935 break;
3938 return tmp;
3941 static GKeyFileGroup *
3942 g_key_file_lookup_group (GKeyFile *key_file,
3943 const gchar *group_name)
3945 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3948 static GList *
3949 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3950 GKeyFileGroup *group,
3951 const gchar *key)
3953 GList *key_node;
3955 for (key_node = group->key_value_pairs;
3956 key_node != NULL;
3957 key_node = key_node->next)
3959 GKeyFileKeyValuePair *pair;
3961 pair = (GKeyFileKeyValuePair *) key_node->data;
3963 if (pair->key && strcmp (pair->key, key) == 0)
3964 break;
3967 return key_node;
3970 static GKeyFileKeyValuePair *
3971 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3972 GKeyFileGroup *group,
3973 const gchar *key)
3975 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3978 /* Lines starting with # or consisting entirely of whitespace are merely
3979 * recorded, not parsed. This function assumes all leading whitespace
3980 * has been stripped.
3982 static gboolean
3983 g_key_file_line_is_comment (const gchar *line)
3985 return (*line == '#' || *line == '\0' || *line == '\n');
3988 static gboolean
3989 g_key_file_is_group_name (const gchar *name)
3991 gchar *p, *q;
3993 if (name == NULL)
3994 return FALSE;
3996 p = q = (gchar *) name;
3997 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3998 q = g_utf8_find_next_char (q, NULL);
4000 if (*q != '\0' || q == p)
4001 return FALSE;
4003 return TRUE;
4006 static gboolean
4007 g_key_file_is_key_name (const gchar *name)
4009 gchar *p, *q;
4011 if (name == NULL)
4012 return FALSE;
4014 p = q = (gchar *) name;
4015 /* We accept a little more than the desktop entry spec says,
4016 * since gnome-vfs uses mime-types as keys in its cache.
4018 while (*q && *q != '=' && *q != '[' && *q != ']')
4019 q = g_utf8_find_next_char (q, NULL);
4021 /* No empty keys, please */
4022 if (q == p)
4023 return FALSE;
4025 /* We accept spaces in the middle of keys to not break
4026 * existing apps, but we don't tolerate initial or final
4027 * spaces, which would lead to silent corruption when
4028 * rereading the file.
4030 if (*p == ' ' || q[-1] == ' ')
4031 return FALSE;
4033 if (*q == '[')
4035 q++;
4036 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
4037 q = g_utf8_find_next_char (q, NULL);
4039 if (*q != ']')
4040 return FALSE;
4042 q++;
4045 if (*q != '\0')
4046 return FALSE;
4048 return TRUE;
4051 /* A group in a key file is made up of a starting '[' followed by one
4052 * or more letters making up the group name followed by ']'.
4054 static gboolean
4055 g_key_file_line_is_group (const gchar *line)
4057 gchar *p;
4059 p = (gchar *) line;
4060 if (*p != '[')
4061 return FALSE;
4063 p++;
4065 while (*p && *p != ']')
4066 p = g_utf8_find_next_char (p, NULL);
4068 if (*p != ']')
4069 return FALSE;
4071 /* silently accept whitespace after the ] */
4072 p = g_utf8_find_next_char (p, NULL);
4073 while (*p == ' ' || *p == '\t')
4074 p = g_utf8_find_next_char (p, NULL);
4076 if (*p)
4077 return FALSE;
4079 return TRUE;
4082 static gboolean
4083 g_key_file_line_is_key_value_pair (const gchar *line)
4085 gchar *p;
4087 p = (gchar *) g_utf8_strchr (line, -1, '=');
4089 if (!p)
4090 return FALSE;
4092 /* Key must be non-empty
4094 if (*p == line[0])
4095 return FALSE;
4097 return TRUE;
4100 static gchar *
4101 g_key_file_parse_value_as_string (GKeyFile *key_file,
4102 const gchar *value,
4103 GSList **pieces,
4104 GError **error)
4106 gchar *string_value, *p, *q0, *q;
4108 string_value = g_new (gchar, strlen (value) + 1);
4110 p = (gchar *) value;
4111 q0 = q = string_value;
4112 while (*p)
4114 if (*p == '\\')
4116 p++;
4118 switch (*p)
4120 case 's':
4121 *q = ' ';
4122 break;
4124 case 'n':
4125 *q = '\n';
4126 break;
4128 case 't':
4129 *q = '\t';
4130 break;
4132 case 'r':
4133 *q = '\r';
4134 break;
4136 case '\\':
4137 *q = '\\';
4138 break;
4140 case '\0':
4141 g_set_error_literal (error, G_KEY_FILE_ERROR,
4142 G_KEY_FILE_ERROR_INVALID_VALUE,
4143 _("Key file contains escape character "
4144 "at end of line"));
4145 break;
4147 default:
4148 if (pieces && *p == key_file->list_separator)
4149 *q = key_file->list_separator;
4150 else
4152 *q++ = '\\';
4153 *q = *p;
4155 if (*error == NULL)
4157 gchar sequence[3];
4159 sequence[0] = '\\';
4160 sequence[1] = *p;
4161 sequence[2] = '\0';
4163 g_set_error (error, G_KEY_FILE_ERROR,
4164 G_KEY_FILE_ERROR_INVALID_VALUE,
4165 _("Key file contains invalid escape "
4166 "sequence “%s”"), sequence);
4169 break;
4172 else
4174 *q = *p;
4175 if (pieces && (*p == key_file->list_separator))
4177 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4178 q0 = q + 1;
4182 if (*p == '\0')
4183 break;
4185 q++;
4186 p++;
4189 *q = '\0';
4190 if (pieces)
4192 if (q0 < q)
4193 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4194 *pieces = g_slist_reverse (*pieces);
4197 return string_value;
4200 static gchar *
4201 g_key_file_parse_string_as_value (GKeyFile *key_file,
4202 const gchar *string,
4203 gboolean escape_separator)
4205 gchar *value, *p, *q;
4206 gsize length;
4207 gboolean parsing_leading_space;
4209 length = strlen (string) + 1;
4211 /* Worst case would be that every character needs to be escaped.
4212 * In other words every character turns to two characters
4214 value = g_new (gchar, 2 * length);
4216 p = (gchar *) string;
4217 q = value;
4218 parsing_leading_space = TRUE;
4219 while (p < (string + length - 1))
4221 gchar escaped_character[3] = { '\\', 0, 0 };
4223 switch (*p)
4225 case ' ':
4226 if (parsing_leading_space)
4228 escaped_character[1] = 's';
4229 strcpy (q, escaped_character);
4230 q += 2;
4232 else
4234 *q = *p;
4235 q++;
4237 break;
4238 case '\t':
4239 if (parsing_leading_space)
4241 escaped_character[1] = 't';
4242 strcpy (q, escaped_character);
4243 q += 2;
4245 else
4247 *q = *p;
4248 q++;
4250 break;
4251 case '\n':
4252 escaped_character[1] = 'n';
4253 strcpy (q, escaped_character);
4254 q += 2;
4255 break;
4256 case '\r':
4257 escaped_character[1] = 'r';
4258 strcpy (q, escaped_character);
4259 q += 2;
4260 break;
4261 case '\\':
4262 escaped_character[1] = '\\';
4263 strcpy (q, escaped_character);
4264 q += 2;
4265 parsing_leading_space = FALSE;
4266 break;
4267 default:
4268 if (escape_separator && *p == key_file->list_separator)
4270 escaped_character[1] = key_file->list_separator;
4271 strcpy (q, escaped_character);
4272 q += 2;
4273 parsing_leading_space = TRUE;
4275 else
4277 *q = *p;
4278 q++;
4279 parsing_leading_space = FALSE;
4281 break;
4283 p++;
4285 *q = '\0';
4287 return value;
4290 static gint
4291 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4292 const gchar *value,
4293 GError **error)
4295 gchar *eof_int;
4296 glong long_value;
4297 gint int_value;
4299 errno = 0;
4300 long_value = strtol (value, &eof_int, 10);
4302 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4304 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4305 g_set_error (error, G_KEY_FILE_ERROR,
4306 G_KEY_FILE_ERROR_INVALID_VALUE,
4307 _("Value “%s” cannot be interpreted "
4308 "as a number."), value_utf8);
4309 g_free (value_utf8);
4311 return 0;
4314 int_value = long_value;
4315 if (int_value != long_value || errno == ERANGE)
4317 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4318 g_set_error (error,
4319 G_KEY_FILE_ERROR,
4320 G_KEY_FILE_ERROR_INVALID_VALUE,
4321 _("Integer value “%s” out of range"),
4322 value_utf8);
4323 g_free (value_utf8);
4325 return 0;
4328 return int_value;
4331 static gchar *
4332 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4333 gint value)
4336 return g_strdup_printf ("%d", value);
4339 static gdouble
4340 g_key_file_parse_value_as_double (GKeyFile *key_file,
4341 const gchar *value,
4342 GError **error)
4344 gchar *end_of_valid_d;
4345 gdouble double_value = 0;
4347 double_value = g_ascii_strtod (value, &end_of_valid_d);
4349 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4351 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4352 g_set_error (error, G_KEY_FILE_ERROR,
4353 G_KEY_FILE_ERROR_INVALID_VALUE,
4354 _("Value “%s” cannot be interpreted "
4355 "as a float number."),
4356 value_utf8);
4357 g_free (value_utf8);
4359 double_value = 0;
4362 return double_value;
4365 static gint
4366 strcmp_sized (const gchar *s1, size_t len1, const gchar *s2)
4368 size_t len2 = strlen (s2);
4369 return strncmp (s1, s2, MAX (len1, len2));
4372 static gboolean
4373 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4374 const gchar *value,
4375 GError **error)
4377 gchar *value_utf8;
4378 gint i, length = 0;
4380 /* Count the number of non-whitespace characters */
4381 for (i = 0; value[i]; i++)
4382 if (!g_ascii_isspace (value[i]))
4383 length = i + 1;
4385 if (strcmp_sized (value, length, "true") == 0 || strcmp_sized (value, length, "1") == 0)
4386 return TRUE;
4387 else if (strcmp_sized (value, length, "false") == 0 || strcmp_sized (value, length, "0") == 0)
4388 return FALSE;
4390 value_utf8 = g_utf8_make_valid (value, -1);
4391 g_set_error (error, G_KEY_FILE_ERROR,
4392 G_KEY_FILE_ERROR_INVALID_VALUE,
4393 _("Value “%s” cannot be interpreted "
4394 "as a boolean."), value_utf8);
4395 g_free (value_utf8);
4397 return FALSE;
4400 static gchar *
4401 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4402 gboolean value)
4404 if (value)
4405 return g_strdup ("true");
4406 else
4407 return g_strdup ("false");
4410 static gchar *
4411 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4412 const gchar *value)
4414 GString *string;
4415 gchar **lines;
4416 gsize i;
4418 string = g_string_sized_new (512);
4420 lines = g_strsplit (value, "\n", 0);
4422 for (i = 0; lines[i] != NULL; i++)
4424 if (lines[i][0] != '#')
4425 g_string_append_printf (string, "%s\n", lines[i]);
4426 else
4427 g_string_append_printf (string, "%s\n", lines[i] + 1);
4429 g_strfreev (lines);
4431 return g_string_free (string, FALSE);
4434 static gchar *
4435 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4436 const gchar *comment)
4438 GString *string;
4439 gchar **lines;
4440 gsize i;
4442 string = g_string_sized_new (512);
4444 lines = g_strsplit (comment, "\n", 0);
4446 for (i = 0; lines[i] != NULL; i++)
4447 g_string_append_printf (string, "#%s%s", lines[i],
4448 lines[i + 1] == NULL? "" : "\n");
4449 g_strfreev (lines);
4451 return g_string_free (string, FALSE);
4455 * g_key_file_save_to_file:
4456 * @key_file: a #GKeyFile
4457 * @filename: the name of the file to write to
4458 * @error: a pointer to a %NULL #GError, or %NULL
4460 * Writes the contents of @key_file to @filename using
4461 * g_file_set_contents().
4463 * This function can fail for any of the reasons that
4464 * g_file_set_contents() may fail.
4466 * Returns: %TRUE if successful, else %FALSE with @error set
4468 * Since: 2.40
4470 gboolean
4471 g_key_file_save_to_file (GKeyFile *key_file,
4472 const gchar *filename,
4473 GError **error)
4475 gchar *contents;
4476 gboolean success;
4477 gsize length;
4479 g_return_val_if_fail (key_file != NULL, FALSE);
4480 g_return_val_if_fail (filename != NULL, FALSE);
4481 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4483 contents = g_key_file_to_data (key_file, &length, NULL);
4484 g_assert (contents != NULL);
4486 success = g_file_set_contents (filename, contents, length, error);
4487 g_free (contents);
4489 return success;