tests: Mark gdbus-threading test as slow
[glib.git] / glib / gkeyfile.c
blobae3bbbc1d5726f27ca5af898db4189b5ad9fbab9
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.1 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.
156 * Here is an example of loading a key file and reading a value:
157 * |[<!-- language="C" -->
158 * g_autoptr(GError) error = NULL;
159 * g_autoptr(GKeyFile) key_file = g_key_file_new ();
161 * if (!g_key_file_load_from_file (key_file, "key-file.ini", flags, &error))
163 * if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
164 * g_warning ("Error loading key file: %s", error->message);
165 * return;
168 * g_autofree gchar *val = g_key_file_get_string (key_file, "Group Name", "SomeKey", &error);
169 * if (val == NULL &&
170 * !g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND))
172 * g_warning ("Error finding key in key file: %s", error->message);
173 * return;
175 * else if (val == NULL)
177 * // Fall back to a default value.
178 * val = g_strdup ("default-value");
180 * ]|
182 * Here is an example of creating and saving a key file:
183 * |[<!-- language="C" -->
184 * g_autoptr(GKeyFile) key_file = g_key_file_new ();
185 * const gchar *val = …;
186 * g_autoptr(GError) error = NULL;
188 * g_key_file_set_string (key_file, "Group Name", "SomeKey", val);
190 * // Save as a file.
191 * if (!g_key_file_save_to_file (key_file, "key-file.ini", &error))
193 * g_warning ("Error saving key file: %s", error->message);
194 * return;
197 * // Or store to a GBytes for use elsewhere.
198 * gsize data_len;
199 * g_autofree guint8 *data = (guint8 *) g_key_file_to_data (key_file, &data_len, &error);
200 * if (data == NULL)
202 * g_warning ("Error saving key file: %s", error->message);
203 * return;
205 * g_autoptr(GBytes) bytes = g_bytes_new_take (g_steal_pointer (&data), data_len);
206 * ]|
210 * G_KEY_FILE_ERROR:
212 * Error domain for key file parsing. Errors in this domain will
213 * be from the #GKeyFileError enumeration.
215 * See #GError for information on error domains.
219 * GKeyFileError:
220 * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
221 * an unknown encoding
222 * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
223 * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
224 * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
225 * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
226 * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
228 * Error codes returned by key file parsing.
232 * GKeyFileFlags:
233 * @G_KEY_FILE_NONE: No flags, default behaviour
234 * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
235 * (possibly modified) contents of the key file back to a file;
236 * otherwise all comments will be lost when the key file is
237 * written back.
238 * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
239 * (possibly modified) contents of the key file back to a file;
240 * otherwise only the translations for the current language will be
241 * written back.
243 * Flags which influence the parsing.
247 * G_KEY_FILE_DESKTOP_GROUP:
249 * The name of the main group of a desktop entry file, as defined in the
250 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec).
251 * Consult the specification for more
252 * details about the meanings of the keys below.
254 * Since: 2.14
258 * G_KEY_FILE_DESKTOP_KEY_TYPE:
260 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
261 * giving the type of the desktop entry. Usually
262 * #G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
263 * #G_KEY_FILE_DESKTOP_TYPE_LINK, or
264 * #G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
266 * Since: 2.14
270 * G_KEY_FILE_DESKTOP_KEY_VERSION:
272 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
273 * giving the version of the Desktop Entry Specification used for
274 * the desktop entry file.
276 * Since: 2.14
280 * G_KEY_FILE_DESKTOP_KEY_NAME:
282 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
283 * string giving the specific name of the desktop entry.
285 * Since: 2.14
289 * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
291 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
292 * string giving the generic name of the desktop entry.
294 * Since: 2.14
298 * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
300 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
301 * stating whether the desktop entry should be shown in menus.
303 * Since: 2.14
307 * G_KEY_FILE_DESKTOP_KEY_COMMENT:
309 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
310 * string giving the tooltip for the desktop entry.
312 * Since: 2.14
316 * G_KEY_FILE_DESKTOP_KEY_ICON:
318 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
319 * string giving the name of the icon to be displayed for the desktop
320 * entry.
322 * Since: 2.14
326 * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
328 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
329 * stating whether the desktop entry has been deleted by the user.
331 * Since: 2.14
335 * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
337 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
338 * strings identifying the environments that should display the
339 * desktop entry.
341 * Since: 2.14
345 * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
347 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
348 * strings identifying the environments that should not display the
349 * desktop entry.
351 * Since: 2.14
355 * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
357 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
358 * giving the file name of a binary on disk used to determine if the
359 * program is actually installed. It is only valid for desktop entries
360 * with the `Application` type.
362 * Since: 2.14
366 * G_KEY_FILE_DESKTOP_KEY_EXEC:
368 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
369 * giving the command line to execute. It is only valid for desktop
370 * entries with the `Application` type.
372 * Since: 2.14
376 * G_KEY_FILE_DESKTOP_KEY_PATH:
378 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
379 * containing the working directory to run the program in. It is only
380 * valid for desktop entries with the `Application` type.
382 * Since: 2.14
386 * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
388 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
389 * stating whether the program should be run in a terminal window.
390 * It is only valid for desktop entries with the
391 * `Application` type.
393 * Since: 2.14
397 * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
399 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
400 * of strings giving the MIME types supported by this desktop entry.
402 * Since: 2.14
406 * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
408 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
409 * of strings giving the categories in which the desktop entry
410 * should be shown in a menu.
412 * Since: 2.14
416 * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
418 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
419 * stating whether the application supports the
420 * [Startup Notification Protocol Specification](http://www.freedesktop.org/Standards/startup-notification-spec).
422 * Since: 2.14
426 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
428 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is string
429 * identifying the WM class or name hint of a window that the application
430 * will create, which can be used to emulate Startup Notification with
431 * older applications.
433 * Since: 2.14
437 * G_KEY_FILE_DESKTOP_KEY_URL:
439 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
440 * giving the URL to access. It is only valid for desktop entries
441 * with the `Link` type.
443 * Since: 2.14
447 * G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE:
449 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean set to true
450 * if the application is D-Bus activatable.
452 * Since: 2.38
456 * G_KEY_FILE_DESKTOP_KEY_ACTIONS:
458 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string list
459 * giving the available application actions.
461 * Since: 2.38
465 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
467 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
468 * entries representing applications.
470 * Since: 2.14
474 * G_KEY_FILE_DESKTOP_TYPE_LINK:
476 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
477 * entries representing links to documents.
479 * Since: 2.14
483 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
485 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
486 * entries representing directories.
488 * Since: 2.14
491 typedef struct _GKeyFileGroup GKeyFileGroup;
494 * GKeyFile:
496 * The GKeyFile struct contains only private data
497 * and should not be accessed directly.
499 struct _GKeyFile
501 GList *groups;
502 GHashTable *group_hash;
504 GKeyFileGroup *start_group;
505 GKeyFileGroup *current_group;
507 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
509 gchar list_separator;
511 GKeyFileFlags flags;
513 gchar **locales;
515 volatile gint ref_count;
518 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
520 struct _GKeyFileGroup
522 const gchar *name; /* NULL for above first group (which will be comments) */
524 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
526 GList *key_value_pairs;
528 /* Used in parallel with key_value_pairs for
529 * increased lookup performance
531 GHashTable *lookup_map;
534 struct _GKeyFileKeyValuePair
536 gchar *key; /* NULL for comments */
537 gchar *value;
540 static gint find_file_in_data_dirs (const gchar *file,
541 const gchar **data_dirs,
542 gchar **output_file,
543 GError **error);
544 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
545 gint fd,
546 GKeyFileFlags flags,
547 GError **error);
548 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
549 const gchar *group_name);
550 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
551 const gchar *group_name);
553 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
554 GKeyFileGroup *group,
555 const gchar *key);
556 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
557 GKeyFileGroup *group,
558 const gchar *key);
560 static void g_key_file_remove_group_node (GKeyFile *key_file,
561 GList *group_node);
562 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
563 GKeyFileGroup *group,
564 GList *pair_node);
566 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
567 GKeyFileGroup *group,
568 GKeyFileKeyValuePair *pair);
569 static void g_key_file_add_key (GKeyFile *key_file,
570 GKeyFileGroup *group,
571 const gchar *key,
572 const gchar *value);
573 static void g_key_file_add_group (GKeyFile *key_file,
574 const gchar *group_name);
575 static gboolean g_key_file_is_group_name (const gchar *name);
576 static gboolean g_key_file_is_key_name (const gchar *name);
577 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
578 static gboolean g_key_file_line_is_comment (const gchar *line);
579 static gboolean g_key_file_line_is_group (const gchar *line);
580 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
581 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
582 const gchar *value,
583 GSList **separators,
584 GError **error);
585 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
586 const gchar *string,
587 gboolean escape_separator);
588 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
589 const gchar *value,
590 GError **error);
591 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
592 gint value);
593 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
594 const gchar *value,
595 GError **error);
596 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
597 const gchar *value,
598 GError **error);
599 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
600 gboolean value);
601 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
602 const gchar *value);
603 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
604 const gchar *comment);
605 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
606 const gchar *line,
607 gsize length,
608 GError **error);
609 static void g_key_file_parse_comment (GKeyFile *key_file,
610 const gchar *line,
611 gsize length,
612 GError **error);
613 static void g_key_file_parse_group (GKeyFile *key_file,
614 const gchar *line,
615 gsize length,
616 GError **error);
617 static gchar *key_get_locale (const gchar *key);
618 static void g_key_file_parse_data (GKeyFile *key_file,
619 const gchar *data,
620 gsize length,
621 GError **error);
622 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
623 GError **error);
625 G_DEFINE_QUARK (g-key-file-error-quark, g_key_file_error)
627 static void
628 g_key_file_init (GKeyFile *key_file)
630 key_file->current_group = g_slice_new0 (GKeyFileGroup);
631 key_file->groups = g_list_prepend (NULL, key_file->current_group);
632 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
633 key_file->start_group = NULL;
634 key_file->parse_buffer = g_string_sized_new (128);
635 key_file->list_separator = ';';
636 key_file->flags = 0;
637 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
640 static void
641 g_key_file_clear (GKeyFile *key_file)
643 GList *tmp, *group_node;
645 if (key_file->locales)
647 g_strfreev (key_file->locales);
648 key_file->locales = NULL;
651 if (key_file->parse_buffer)
653 g_string_free (key_file->parse_buffer, TRUE);
654 key_file->parse_buffer = NULL;
657 tmp = key_file->groups;
658 while (tmp != NULL)
660 group_node = tmp;
661 tmp = tmp->next;
662 g_key_file_remove_group_node (key_file, group_node);
665 if (key_file->group_hash != NULL)
667 g_hash_table_destroy (key_file->group_hash);
668 key_file->group_hash = NULL;
671 g_warn_if_fail (key_file->groups == NULL);
676 * g_key_file_new:
678 * Creates a new empty #GKeyFile object. Use
679 * g_key_file_load_from_file(), g_key_file_load_from_data(),
680 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
681 * read an existing key file.
683 * Returns: (transfer full): an empty #GKeyFile.
685 * Since: 2.6
687 GKeyFile *
688 g_key_file_new (void)
690 GKeyFile *key_file;
692 key_file = g_slice_new0 (GKeyFile);
693 key_file->ref_count = 1;
694 g_key_file_init (key_file);
696 return key_file;
700 * g_key_file_set_list_separator:
701 * @key_file: a #GKeyFile
702 * @separator: the separator
704 * Sets the character which is used to separate
705 * values in lists. Typically ';' or ',' are used
706 * as separators. The default list separator is ';'.
708 * Since: 2.6
710 void
711 g_key_file_set_list_separator (GKeyFile *key_file,
712 gchar separator)
714 g_return_if_fail (key_file != NULL);
716 key_file->list_separator = separator;
720 /* Iterates through all the directories in *dirs trying to
721 * open file. When it successfully locates and opens a file it
722 * returns the file descriptor to the open file. It also
723 * outputs the absolute path of the file in output_file.
725 static gint
726 find_file_in_data_dirs (const gchar *file,
727 const gchar **dirs,
728 gchar **output_file,
729 GError **error)
731 const gchar **data_dirs, *data_dir;
732 gchar *path;
733 gint fd;
735 path = NULL;
736 fd = -1;
738 if (dirs == NULL)
739 return fd;
741 data_dirs = dirs;
743 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
745 gchar *candidate_file, *sub_dir;
747 candidate_file = (gchar *) file;
748 sub_dir = g_strdup ("");
749 while (candidate_file != NULL && fd == -1)
751 gchar *p;
753 path = g_build_filename (data_dir, sub_dir,
754 candidate_file, NULL);
756 fd = g_open (path, O_RDONLY, 0);
758 if (fd == -1)
760 g_free (path);
761 path = NULL;
764 candidate_file = strchr (candidate_file, '-');
766 if (candidate_file == NULL)
767 break;
769 candidate_file++;
771 g_free (sub_dir);
772 sub_dir = g_strndup (file, candidate_file - file - 1);
774 for (p = sub_dir; *p != '\0'; p++)
776 if (*p == '-')
777 *p = G_DIR_SEPARATOR;
780 g_free (sub_dir);
781 data_dirs++;
784 if (fd == -1)
786 g_set_error_literal (error, G_KEY_FILE_ERROR,
787 G_KEY_FILE_ERROR_NOT_FOUND,
788 _("Valid key file could not be "
789 "found in search dirs"));
792 if (output_file != NULL && fd != -1)
793 *output_file = g_strdup (path);
795 g_free (path);
797 return fd;
800 static gboolean
801 g_key_file_load_from_fd (GKeyFile *key_file,
802 gint fd,
803 GKeyFileFlags flags,
804 GError **error)
806 GError *key_file_error = NULL;
807 gssize bytes_read;
808 struct stat stat_buf;
809 gchar read_buf[4096];
810 gchar list_separator;
812 if (fstat (fd, &stat_buf) < 0)
814 int errsv = errno;
815 g_set_error_literal (error, G_FILE_ERROR,
816 g_file_error_from_errno (errsv),
817 g_strerror (errsv));
818 return FALSE;
821 if (!S_ISREG (stat_buf.st_mode))
823 g_set_error_literal (error, G_KEY_FILE_ERROR,
824 G_KEY_FILE_ERROR_PARSE,
825 _("Not a regular file"));
826 return FALSE;
829 list_separator = key_file->list_separator;
830 g_key_file_clear (key_file);
831 g_key_file_init (key_file);
832 key_file->list_separator = list_separator;
833 key_file->flags = flags;
837 int errsv;
839 bytes_read = read (fd, read_buf, 4096);
840 errsv = errno;
842 if (bytes_read == 0) /* End of File */
843 break;
845 if (bytes_read < 0)
847 if (errsv == EINTR || errsv == EAGAIN)
848 continue;
850 g_set_error_literal (error, G_FILE_ERROR,
851 g_file_error_from_errno (errsv),
852 g_strerror (errsv));
853 return FALSE;
856 g_key_file_parse_data (key_file,
857 read_buf, bytes_read,
858 &key_file_error);
860 while (!key_file_error);
862 if (key_file_error)
864 g_propagate_error (error, key_file_error);
865 return FALSE;
868 g_key_file_flush_parse_buffer (key_file, &key_file_error);
870 if (key_file_error)
872 g_propagate_error (error, key_file_error);
873 return FALSE;
876 return TRUE;
880 * g_key_file_load_from_file:
881 * @key_file: an empty #GKeyFile struct
882 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
883 * @flags: flags from #GKeyFileFlags
884 * @error: return location for a #GError, or %NULL
886 * Loads a key file into an empty #GKeyFile structure.
888 * If the OS returns an error when opening or reading the file, a
889 * %G_FILE_ERROR is returned. If there is a problem parsing the file, a
890 * %G_KEY_FILE_ERROR is returned.
892 * This function will never return a %G_KEY_FILE_ERROR_NOT_FOUND error. If the
893 * @file is not found, %G_FILE_ERROR_NOENT is returned.
895 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
897 * Since: 2.6
899 gboolean
900 g_key_file_load_from_file (GKeyFile *key_file,
901 const gchar *file,
902 GKeyFileFlags flags,
903 GError **error)
905 GError *key_file_error = NULL;
906 gint fd;
907 int errsv;
909 g_return_val_if_fail (key_file != NULL, FALSE);
910 g_return_val_if_fail (file != NULL, FALSE);
912 fd = g_open (file, O_RDONLY, 0);
913 errsv = errno;
915 if (fd == -1)
917 g_set_error_literal (error, G_FILE_ERROR,
918 g_file_error_from_errno (errsv),
919 g_strerror (errsv));
920 return FALSE;
923 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
924 close (fd);
926 if (key_file_error)
928 g_propagate_error (error, key_file_error);
929 return FALSE;
932 return TRUE;
936 * g_key_file_load_from_data:
937 * @key_file: an empty #GKeyFile struct
938 * @data: key file loaded in memory
939 * @length: the length of @data in bytes (or (gsize)-1 if data is nul-terminated)
940 * @flags: flags from #GKeyFileFlags
941 * @error: return location for a #GError, or %NULL
943 * Loads a key file from memory into an empty #GKeyFile structure.
944 * If the object cannot be created then %error is set to a #GKeyFileError.
946 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
948 * Since: 2.6
950 gboolean
951 g_key_file_load_from_data (GKeyFile *key_file,
952 const gchar *data,
953 gsize length,
954 GKeyFileFlags flags,
955 GError **error)
957 GError *key_file_error = NULL;
958 gchar list_separator;
960 g_return_val_if_fail (key_file != NULL, FALSE);
961 g_return_val_if_fail (data != NULL || length == 0, FALSE);
963 if (length == (gsize)-1)
964 length = strlen (data);
966 list_separator = key_file->list_separator;
967 g_key_file_clear (key_file);
968 g_key_file_init (key_file);
969 key_file->list_separator = list_separator;
970 key_file->flags = flags;
972 g_key_file_parse_data (key_file, data, length, &key_file_error);
974 if (key_file_error)
976 g_propagate_error (error, key_file_error);
977 return FALSE;
980 g_key_file_flush_parse_buffer (key_file, &key_file_error);
982 if (key_file_error)
984 g_propagate_error (error, key_file_error);
985 return FALSE;
988 return TRUE;
992 * g_key_file_load_from_bytes:
993 * @key_file: an empty #GKeyFile struct
994 * @bytes: a #GBytes
995 * @flags: flags from #GKeyFileFlags
996 * @error: return location for a #GError, or %NULL
998 * Loads a key file from the data in @bytes into an empty #GKeyFile structure.
999 * If the object cannot be created then %error is set to a #GKeyFileError.
1001 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1003 * Since: 2.50
1005 gboolean
1006 g_key_file_load_from_bytes (GKeyFile *key_file,
1007 GBytes *bytes,
1008 GKeyFileFlags flags,
1009 GError **error)
1011 const guchar *data;
1012 gsize size;
1014 g_return_val_if_fail (key_file != NULL, FALSE);
1015 g_return_val_if_fail (bytes != NULL, FALSE);
1017 data = g_bytes_get_data (bytes, &size);
1018 return g_key_file_load_from_data (key_file, (const gchar *) data, size, flags, error);
1022 * g_key_file_load_from_dirs:
1023 * @key_file: an empty #GKeyFile struct
1024 * @file: (type filename): a relative path to a filename to open and parse
1025 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
1026 * @full_path: (out) (type filename) (optional): return location for a string containing the full path
1027 * of the file, or %NULL
1028 * @flags: flags from #GKeyFileFlags
1029 * @error: return location for a #GError, or %NULL
1031 * This function looks for a key file named @file in the paths
1032 * specified in @search_dirs, loads the file into @key_file and
1033 * returns the file's full path in @full_path.
1035 * If the file could not be found in any of the @search_dirs,
1036 * %G_KEY_FILE_ERROR_NOT_FOUND is returned. If
1037 * the file is found but the OS returns an error when opening or reading the
1038 * file, a %G_FILE_ERROR is returned. If there is a problem parsing the file, a
1039 * %G_KEY_FILE_ERROR is returned.
1041 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1043 * Since: 2.14
1045 gboolean
1046 g_key_file_load_from_dirs (GKeyFile *key_file,
1047 const gchar *file,
1048 const gchar **search_dirs,
1049 gchar **full_path,
1050 GKeyFileFlags flags,
1051 GError **error)
1053 GError *key_file_error = NULL;
1054 const gchar **data_dirs;
1055 gchar *output_path;
1056 gint fd;
1057 gboolean found_file;
1059 g_return_val_if_fail (key_file != NULL, FALSE);
1060 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1061 g_return_val_if_fail (search_dirs != NULL, FALSE);
1063 found_file = FALSE;
1064 data_dirs = search_dirs;
1065 output_path = NULL;
1066 while (*data_dirs != NULL && !found_file)
1068 g_free (output_path);
1069 output_path = NULL;
1071 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
1072 &key_file_error);
1074 if (fd == -1)
1076 if (key_file_error)
1077 g_propagate_error (error, key_file_error);
1078 break;
1081 found_file = g_key_file_load_from_fd (key_file, fd, flags,
1082 &key_file_error);
1083 close (fd);
1085 if (key_file_error)
1087 g_propagate_error (error, key_file_error);
1088 break;
1092 if (found_file && full_path)
1093 *full_path = output_path;
1094 else
1095 g_free (output_path);
1097 return found_file;
1101 * g_key_file_load_from_data_dirs:
1102 * @key_file: an empty #GKeyFile struct
1103 * @file: (type filename): a relative path to a filename to open and parse
1104 * @full_path: (out) (type filename) (optional): return location for a string containing the full path
1105 * of the file, or %NULL
1106 * @flags: flags from #GKeyFileFlags
1107 * @error: return location for a #GError, or %NULL
1109 * This function looks for a key file named @file in the paths
1110 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1111 * loads the file into @key_file and returns the file's full path in
1112 * @full_path. If the file could not be loaded then an %error is
1113 * set to either a #GFileError or #GKeyFileError.
1115 * Returns: %TRUE if a key file could be loaded, %FALSE othewise
1116 * Since: 2.6
1118 gboolean
1119 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1120 const gchar *file,
1121 gchar **full_path,
1122 GKeyFileFlags flags,
1123 GError **error)
1125 gchar **all_data_dirs;
1126 const gchar * user_data_dir;
1127 const gchar * const * system_data_dirs;
1128 gsize i, j;
1129 gboolean found_file;
1131 g_return_val_if_fail (key_file != NULL, FALSE);
1132 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1134 user_data_dir = g_get_user_data_dir ();
1135 system_data_dirs = g_get_system_data_dirs ();
1136 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1138 i = 0;
1139 all_data_dirs[i++] = g_strdup (user_data_dir);
1141 j = 0;
1142 while (system_data_dirs[j] != NULL)
1143 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1144 all_data_dirs[i] = NULL;
1146 found_file = g_key_file_load_from_dirs (key_file,
1147 file,
1148 (const gchar **)all_data_dirs,
1149 full_path,
1150 flags,
1151 error);
1153 g_strfreev (all_data_dirs);
1155 return found_file;
1159 * g_key_file_ref: (skip)
1160 * @key_file: a #GKeyFile
1162 * Increases the reference count of @key_file.
1164 * Returns: the same @key_file.
1166 * Since: 2.32
1168 GKeyFile *
1169 g_key_file_ref (GKeyFile *key_file)
1171 g_return_val_if_fail (key_file != NULL, NULL);
1173 g_atomic_int_inc (&key_file->ref_count);
1175 return key_file;
1179 * g_key_file_free: (skip)
1180 * @key_file: a #GKeyFile
1182 * Clears all keys and groups from @key_file, and decreases the
1183 * reference count by 1. If the reference count reaches zero,
1184 * frees the key file and all its allocated memory.
1186 * Since: 2.6
1188 void
1189 g_key_file_free (GKeyFile *key_file)
1191 g_return_if_fail (key_file != NULL);
1193 g_key_file_clear (key_file);
1194 g_key_file_unref (key_file);
1198 * g_key_file_unref:
1199 * @key_file: a #GKeyFile
1201 * Decreases the reference count of @key_file by 1. If the reference count
1202 * reaches zero, frees the key file and all its allocated memory.
1204 * Since: 2.32
1206 void
1207 g_key_file_unref (GKeyFile *key_file)
1209 g_return_if_fail (key_file != NULL);
1211 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1213 g_key_file_clear (key_file);
1214 g_slice_free (GKeyFile, key_file);
1218 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1219 * true for locales that match those in g_get_language_names().
1221 static gboolean
1222 g_key_file_locale_is_interesting (GKeyFile *key_file,
1223 const gchar *locale)
1225 gsize i;
1227 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1228 return TRUE;
1230 for (i = 0; key_file->locales[i] != NULL; i++)
1232 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
1233 return TRUE;
1236 return FALSE;
1239 static void
1240 g_key_file_parse_line (GKeyFile *key_file,
1241 const gchar *line,
1242 gsize length,
1243 GError **error)
1245 GError *parse_error = NULL;
1246 gchar *line_start;
1248 g_return_if_fail (key_file != NULL);
1249 g_return_if_fail (line != NULL);
1251 line_start = (gchar *) line;
1252 while (g_ascii_isspace (*line_start))
1253 line_start++;
1255 if (g_key_file_line_is_comment (line_start))
1256 g_key_file_parse_comment (key_file, line, length, &parse_error);
1257 else if (g_key_file_line_is_group (line_start))
1258 g_key_file_parse_group (key_file, line_start,
1259 length - (line_start - line),
1260 &parse_error);
1261 else if (g_key_file_line_is_key_value_pair (line_start))
1262 g_key_file_parse_key_value_pair (key_file, line_start,
1263 length - (line_start - line),
1264 &parse_error);
1265 else
1267 gchar *line_utf8 = g_utf8_make_valid (line, length);
1268 g_set_error (error, G_KEY_FILE_ERROR,
1269 G_KEY_FILE_ERROR_PARSE,
1270 _("Key file contains line “%s” which is not "
1271 "a key-value pair, group, or comment"),
1272 line_utf8);
1273 g_free (line_utf8);
1275 return;
1278 if (parse_error)
1279 g_propagate_error (error, parse_error);
1282 static void
1283 g_key_file_parse_comment (GKeyFile *key_file,
1284 const gchar *line,
1285 gsize length,
1286 GError **error)
1288 GKeyFileKeyValuePair *pair;
1290 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1291 return;
1293 g_warn_if_fail (key_file->current_group != NULL);
1295 pair = g_slice_new (GKeyFileKeyValuePair);
1296 pair->key = NULL;
1297 pair->value = g_strndup (line, length);
1299 key_file->current_group->key_value_pairs =
1300 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1303 static void
1304 g_key_file_parse_group (GKeyFile *key_file,
1305 const gchar *line,
1306 gsize length,
1307 GError **error)
1309 gchar *group_name;
1310 const gchar *group_name_start, *group_name_end;
1312 /* advance past opening '['
1314 group_name_start = line + 1;
1315 group_name_end = line + length - 1;
1317 while (*group_name_end != ']')
1318 group_name_end--;
1320 group_name = g_strndup (group_name_start,
1321 group_name_end - group_name_start);
1323 if (!g_key_file_is_group_name (group_name))
1325 g_set_error (error, G_KEY_FILE_ERROR,
1326 G_KEY_FILE_ERROR_PARSE,
1327 _("Invalid group name: %s"), group_name);
1328 g_free (group_name);
1329 return;
1332 g_key_file_add_group (key_file, group_name);
1333 g_free (group_name);
1336 static void
1337 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1338 const gchar *line,
1339 gsize length,
1340 GError **error)
1342 gchar *key, *value, *key_end, *value_start, *locale;
1343 gsize key_len, value_len;
1345 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1347 g_set_error_literal (error, G_KEY_FILE_ERROR,
1348 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1349 _("Key file does not start with a group"));
1350 return;
1353 key_end = value_start = strchr (line, '=');
1355 g_warn_if_fail (key_end != NULL);
1357 key_end--;
1358 value_start++;
1360 /* Pull the key name from the line (chomping trailing whitespace)
1362 while (g_ascii_isspace (*key_end))
1363 key_end--;
1365 key_len = key_end - line + 2;
1367 g_warn_if_fail (key_len <= length);
1369 key = g_strndup (line, key_len - 1);
1371 if (!g_key_file_is_key_name (key))
1373 g_set_error (error, G_KEY_FILE_ERROR,
1374 G_KEY_FILE_ERROR_PARSE,
1375 _("Invalid key name: %s"), key);
1376 g_free (key);
1377 return;
1380 /* Pull the value from the line (chugging leading whitespace)
1382 while (g_ascii_isspace (*value_start))
1383 value_start++;
1385 value_len = line + length - value_start + 1;
1387 value = g_strndup (value_start, value_len);
1389 g_warn_if_fail (key_file->start_group != NULL);
1391 if (key_file->current_group
1392 && key_file->current_group->name
1393 && strcmp (key_file->start_group->name,
1394 key_file->current_group->name) == 0
1395 && strcmp (key, "Encoding") == 0)
1397 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
1399 gchar *value_utf8 = g_utf8_make_valid (value, value_len);
1400 g_set_error (error, G_KEY_FILE_ERROR,
1401 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1402 _("Key file contains unsupported "
1403 "encoding “%s”"), value_utf8);
1404 g_free (value_utf8);
1406 g_free (key);
1407 g_free (value);
1408 return;
1412 /* Is this key a translation? If so, is it one that we care about?
1414 locale = key_get_locale (key);
1416 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
1418 GKeyFileKeyValuePair *pair;
1420 pair = g_slice_new (GKeyFileKeyValuePair);
1421 pair->key = key;
1422 pair->value = value;
1424 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1426 else
1428 g_free (key);
1429 g_free (value);
1432 g_free (locale);
1435 static gchar *
1436 key_get_locale (const gchar *key)
1438 gchar *locale;
1440 locale = g_strrstr (key, "[");
1442 if (locale && strlen (locale) <= 2)
1443 locale = NULL;
1445 if (locale)
1446 locale = g_strndup (locale + 1, strlen (locale) - 2);
1448 return locale;
1451 static void
1452 g_key_file_parse_data (GKeyFile *key_file,
1453 const gchar *data,
1454 gsize length,
1455 GError **error)
1457 GError *parse_error;
1458 gsize i;
1460 g_return_if_fail (key_file != NULL);
1461 g_return_if_fail (data != NULL || length == 0);
1463 parse_error = NULL;
1465 i = 0;
1466 while (i < length)
1468 if (data[i] == '\n')
1470 if (key_file->parse_buffer->len > 0
1471 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1472 == '\r'))
1473 g_string_erase (key_file->parse_buffer,
1474 key_file->parse_buffer->len - 1,
1477 /* When a newline is encountered flush the parse buffer so that the
1478 * line can be parsed. Note that completely blank lines won't show
1479 * up in the parse buffer, so they get parsed directly.
1481 if (key_file->parse_buffer->len > 0)
1482 g_key_file_flush_parse_buffer (key_file, &parse_error);
1483 else
1484 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1486 if (parse_error)
1488 g_propagate_error (error, parse_error);
1489 return;
1491 i++;
1493 else
1495 const gchar *start_of_line;
1496 const gchar *end_of_line;
1497 gsize line_length;
1499 start_of_line = data + i;
1500 end_of_line = memchr (start_of_line, '\n', length - i);
1502 if (end_of_line == NULL)
1503 end_of_line = data + length;
1505 line_length = end_of_line - start_of_line;
1507 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1508 i += line_length;
1513 static void
1514 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1515 GError **error)
1517 GError *file_error = NULL;
1519 g_return_if_fail (key_file != NULL);
1521 file_error = NULL;
1523 if (key_file->parse_buffer->len > 0)
1525 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1526 key_file->parse_buffer->len,
1527 &file_error);
1528 g_string_erase (key_file->parse_buffer, 0, -1);
1530 if (file_error)
1532 g_propagate_error (error, file_error);
1533 return;
1539 * g_key_file_to_data:
1540 * @key_file: a #GKeyFile
1541 * @length: (out) (optional): return location for the length of the
1542 * returned string, or %NULL
1543 * @error: return location for a #GError, or %NULL
1545 * This function outputs @key_file as a string.
1547 * Note that this function never reports an error,
1548 * so it is safe to pass %NULL as @error.
1550 * Returns: a newly allocated string holding
1551 * the contents of the #GKeyFile
1553 * Since: 2.6
1555 gchar *
1556 g_key_file_to_data (GKeyFile *key_file,
1557 gsize *length,
1558 GError **error)
1560 GString *data_string;
1561 GList *group_node, *key_file_node;
1563 g_return_val_if_fail (key_file != NULL, NULL);
1565 data_string = g_string_new (NULL);
1567 for (group_node = g_list_last (key_file->groups);
1568 group_node != NULL;
1569 group_node = group_node->prev)
1571 GKeyFileGroup *group;
1573 group = (GKeyFileGroup *) group_node->data;
1575 /* separate groups by at least an empty line */
1576 if (data_string->len >= 2 &&
1577 data_string->str[data_string->len - 2] != '\n')
1578 g_string_append_c (data_string, '\n');
1580 if (group->comment != NULL)
1581 g_string_append_printf (data_string, "%s\n", group->comment->value);
1583 if (group->name != NULL)
1584 g_string_append_printf (data_string, "[%s]\n", group->name);
1586 for (key_file_node = g_list_last (group->key_value_pairs);
1587 key_file_node != NULL;
1588 key_file_node = key_file_node->prev)
1590 GKeyFileKeyValuePair *pair;
1592 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1594 if (pair->key != NULL)
1595 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1596 else
1597 g_string_append_printf (data_string, "%s\n", pair->value);
1601 if (length)
1602 *length = data_string->len;
1604 return g_string_free (data_string, FALSE);
1608 * g_key_file_get_keys:
1609 * @key_file: a #GKeyFile
1610 * @group_name: a group name
1611 * @length: (out) (optional): return location for the number of keys returned, or %NULL
1612 * @error: return location for a #GError, or %NULL
1614 * Returns all keys for the group name @group_name. The array of
1615 * returned keys will be %NULL-terminated, so @length may
1616 * optionally be %NULL. In the event that the @group_name cannot
1617 * be found, %NULL is returned and @error is set to
1618 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1620 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1621 * Use g_strfreev() to free it.
1623 * Since: 2.6
1625 gchar **
1626 g_key_file_get_keys (GKeyFile *key_file,
1627 const gchar *group_name,
1628 gsize *length,
1629 GError **error)
1631 GKeyFileGroup *group;
1632 GList *tmp;
1633 gchar **keys;
1634 gsize i, num_keys;
1636 g_return_val_if_fail (key_file != NULL, NULL);
1637 g_return_val_if_fail (group_name != NULL, NULL);
1639 group = g_key_file_lookup_group (key_file, group_name);
1641 if (!group)
1643 g_set_error (error, G_KEY_FILE_ERROR,
1644 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1645 _("Key file does not have group “%s”"),
1646 group_name);
1647 return NULL;
1650 num_keys = 0;
1651 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1653 GKeyFileKeyValuePair *pair;
1655 pair = (GKeyFileKeyValuePair *) tmp->data;
1657 if (pair->key)
1658 num_keys++;
1661 keys = g_new (gchar *, num_keys + 1);
1663 i = num_keys - 1;
1664 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1666 GKeyFileKeyValuePair *pair;
1668 pair = (GKeyFileKeyValuePair *) tmp->data;
1670 if (pair->key)
1672 keys[i] = g_strdup (pair->key);
1673 i--;
1677 keys[num_keys] = NULL;
1679 if (length)
1680 *length = num_keys;
1682 return keys;
1686 * g_key_file_get_start_group:
1687 * @key_file: a #GKeyFile
1689 * Returns the name of the start group of the file.
1691 * Returns: The start group of the key file.
1693 * Since: 2.6
1695 gchar *
1696 g_key_file_get_start_group (GKeyFile *key_file)
1698 g_return_val_if_fail (key_file != NULL, NULL);
1700 if (key_file->start_group)
1701 return g_strdup (key_file->start_group->name);
1703 return NULL;
1707 * g_key_file_get_groups:
1708 * @key_file: a #GKeyFile
1709 * @length: (out) (optional): return location for the number of returned groups, or %NULL
1711 * Returns all groups in the key file loaded with @key_file.
1712 * The array of returned groups will be %NULL-terminated, so
1713 * @length may optionally be %NULL.
1715 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1716 * Use g_strfreev() to free it.
1717 * Since: 2.6
1719 gchar **
1720 g_key_file_get_groups (GKeyFile *key_file,
1721 gsize *length)
1723 GList *group_node;
1724 gchar **groups;
1725 gsize i, num_groups;
1727 g_return_val_if_fail (key_file != NULL, NULL);
1729 num_groups = g_list_length (key_file->groups);
1731 g_return_val_if_fail (num_groups > 0, NULL);
1733 group_node = g_list_last (key_file->groups);
1735 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1737 /* Only need num_groups instead of num_groups + 1
1738 * because the first group of the file (last in the
1739 * list) is always the comment group at the top,
1740 * which we skip
1742 groups = g_new (gchar *, num_groups);
1745 i = 0;
1746 for (group_node = group_node->prev;
1747 group_node != NULL;
1748 group_node = group_node->prev)
1750 GKeyFileGroup *group;
1752 group = (GKeyFileGroup *) group_node->data;
1754 g_warn_if_fail (group->name != NULL);
1756 groups[i++] = g_strdup (group->name);
1758 groups[i] = NULL;
1760 if (length)
1761 *length = i;
1763 return groups;
1766 static void
1767 set_not_found_key_error (const char *group_name,
1768 const char *key,
1769 GError **error)
1771 g_set_error (error, G_KEY_FILE_ERROR,
1772 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1773 _("Key file does not have key “%s” in group “%s”"),
1774 key, group_name);
1778 * g_key_file_get_value:
1779 * @key_file: a #GKeyFile
1780 * @group_name: a group name
1781 * @key: a key
1782 * @error: return location for a #GError, or %NULL
1784 * Returns the raw value associated with @key under @group_name.
1785 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1787 * In the event the key cannot be found, %NULL is returned and
1788 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1789 * event that the @group_name cannot be found, %NULL is returned
1790 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1793 * Returns: a newly allocated string or %NULL if the specified
1794 * key cannot be found.
1796 * Since: 2.6
1798 gchar *
1799 g_key_file_get_value (GKeyFile *key_file,
1800 const gchar *group_name,
1801 const gchar *key,
1802 GError **error)
1804 GKeyFileGroup *group;
1805 GKeyFileKeyValuePair *pair;
1806 gchar *value = NULL;
1808 g_return_val_if_fail (key_file != NULL, NULL);
1809 g_return_val_if_fail (group_name != NULL, NULL);
1810 g_return_val_if_fail (key != NULL, NULL);
1812 group = g_key_file_lookup_group (key_file, group_name);
1814 if (!group)
1816 g_set_error (error, G_KEY_FILE_ERROR,
1817 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1818 _("Key file does not have group “%s”"),
1819 group_name);
1820 return NULL;
1823 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1825 if (pair)
1826 value = g_strdup (pair->value);
1827 else
1828 set_not_found_key_error (group_name, key, error);
1830 return value;
1834 * g_key_file_set_value:
1835 * @key_file: a #GKeyFile
1836 * @group_name: a group name
1837 * @key: a key
1838 * @value: a string
1840 * Associates a new value with @key under @group_name.
1842 * If @key cannot be found then it is created. If @group_name cannot
1843 * be found then it is created. To set an UTF-8 string which may contain
1844 * characters that need escaping (such as newlines or spaces), use
1845 * g_key_file_set_string().
1847 * Since: 2.6
1849 void
1850 g_key_file_set_value (GKeyFile *key_file,
1851 const gchar *group_name,
1852 const gchar *key,
1853 const gchar *value)
1855 GKeyFileGroup *group;
1856 GKeyFileKeyValuePair *pair;
1858 g_return_if_fail (key_file != NULL);
1859 g_return_if_fail (g_key_file_is_group_name (group_name));
1860 g_return_if_fail (g_key_file_is_key_name (key));
1861 g_return_if_fail (value != NULL);
1863 group = g_key_file_lookup_group (key_file, group_name);
1865 if (!group)
1867 g_key_file_add_group (key_file, group_name);
1868 group = (GKeyFileGroup *) key_file->groups->data;
1870 g_key_file_add_key (key_file, group, key, value);
1872 else
1874 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1876 if (!pair)
1877 g_key_file_add_key (key_file, group, key, value);
1878 else
1880 g_free (pair->value);
1881 pair->value = g_strdup (value);
1887 * g_key_file_get_string:
1888 * @key_file: a #GKeyFile
1889 * @group_name: a group name
1890 * @key: a key
1891 * @error: return location for a #GError, or %NULL
1893 * Returns the string value associated with @key under @group_name.
1894 * Unlike g_key_file_get_value(), this function handles escape sequences
1895 * like \s.
1897 * In the event the key cannot be found, %NULL is returned and
1898 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1899 * event that the @group_name cannot be found, %NULL is returned
1900 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1902 * Returns: a newly allocated string or %NULL if the specified
1903 * key cannot be found.
1905 * Since: 2.6
1907 gchar *
1908 g_key_file_get_string (GKeyFile *key_file,
1909 const gchar *group_name,
1910 const gchar *key,
1911 GError **error)
1913 gchar *value, *string_value;
1914 GError *key_file_error;
1916 g_return_val_if_fail (key_file != NULL, NULL);
1917 g_return_val_if_fail (group_name != NULL, NULL);
1918 g_return_val_if_fail (key != NULL, NULL);
1920 key_file_error = NULL;
1922 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1924 if (key_file_error)
1926 g_propagate_error (error, key_file_error);
1927 return NULL;
1930 if (!g_utf8_validate (value, -1, NULL))
1932 gchar *value_utf8 = g_utf8_make_valid (value, -1);
1933 g_set_error (error, G_KEY_FILE_ERROR,
1934 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1935 _("Key file contains key “%s” with value “%s” "
1936 "which is not UTF-8"), key, value_utf8);
1937 g_free (value_utf8);
1938 g_free (value);
1940 return NULL;
1943 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1944 &key_file_error);
1945 g_free (value);
1947 if (key_file_error)
1949 if (g_error_matches (key_file_error,
1950 G_KEY_FILE_ERROR,
1951 G_KEY_FILE_ERROR_INVALID_VALUE))
1953 g_set_error (error, G_KEY_FILE_ERROR,
1954 G_KEY_FILE_ERROR_INVALID_VALUE,
1955 _("Key file contains key “%s” "
1956 "which has a value that cannot be interpreted."),
1957 key);
1958 g_error_free (key_file_error);
1960 else
1961 g_propagate_error (error, key_file_error);
1964 return string_value;
1968 * g_key_file_set_string:
1969 * @key_file: a #GKeyFile
1970 * @group_name: a group name
1971 * @key: a key
1972 * @string: a string
1974 * Associates a new string value with @key under @group_name.
1975 * If @key cannot be found then it is created.
1976 * If @group_name cannot be found then it is created.
1977 * Unlike g_key_file_set_value(), this function handles characters
1978 * that need escaping, such as newlines.
1980 * Since: 2.6
1982 void
1983 g_key_file_set_string (GKeyFile *key_file,
1984 const gchar *group_name,
1985 const gchar *key,
1986 const gchar *string)
1988 gchar *value;
1990 g_return_if_fail (key_file != NULL);
1991 g_return_if_fail (string != NULL);
1993 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1994 g_key_file_set_value (key_file, group_name, key, value);
1995 g_free (value);
1999 * g_key_file_get_string_list:
2000 * @key_file: a #GKeyFile
2001 * @group_name: a group name
2002 * @key: a key
2003 * @length: (out) (optional): return location for the number of returned strings, or %NULL
2004 * @error: return location for a #GError, or %NULL
2006 * Returns the values associated with @key under @group_name.
2008 * In the event the key cannot be found, %NULL is returned and
2009 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
2010 * event that the @group_name cannot be found, %NULL is returned
2011 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
2013 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
2014 * a %NULL-terminated string array or %NULL if the specified
2015 * key cannot be found. The array should be freed with g_strfreev().
2017 * Since: 2.6
2019 gchar **
2020 g_key_file_get_string_list (GKeyFile *key_file,
2021 const gchar *group_name,
2022 const gchar *key,
2023 gsize *length,
2024 GError **error)
2026 GError *key_file_error = NULL;
2027 gchar *value, *string_value, **values;
2028 gint i, len;
2029 GSList *p, *pieces = NULL;
2031 g_return_val_if_fail (key_file != NULL, NULL);
2032 g_return_val_if_fail (group_name != NULL, NULL);
2033 g_return_val_if_fail (key != NULL, NULL);
2035 if (length)
2036 *length = 0;
2038 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2040 if (key_file_error)
2042 g_propagate_error (error, key_file_error);
2043 return NULL;
2046 if (!g_utf8_validate (value, -1, NULL))
2048 gchar *value_utf8 = g_utf8_make_valid (value, -1);
2049 g_set_error (error, G_KEY_FILE_ERROR,
2050 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
2051 _("Key file contains key “%s” with value “%s” "
2052 "which is not UTF-8"), key, value_utf8);
2053 g_free (value_utf8);
2054 g_free (value);
2056 return NULL;
2059 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
2060 g_free (value);
2061 g_free (string_value);
2063 if (key_file_error)
2065 if (g_error_matches (key_file_error,
2066 G_KEY_FILE_ERROR,
2067 G_KEY_FILE_ERROR_INVALID_VALUE))
2069 g_set_error (error, G_KEY_FILE_ERROR,
2070 G_KEY_FILE_ERROR_INVALID_VALUE,
2071 _("Key file contains key “%s” "
2072 "which has a value that cannot be interpreted."),
2073 key);
2074 g_error_free (key_file_error);
2076 else
2077 g_propagate_error (error, key_file_error);
2079 g_slist_free_full (pieces, g_free);
2080 return NULL;
2083 len = g_slist_length (pieces);
2084 values = g_new (gchar *, len + 1);
2085 for (p = pieces, i = 0; p; p = p->next)
2086 values[i++] = p->data;
2087 values[len] = NULL;
2089 g_slist_free (pieces);
2091 if (length)
2092 *length = len;
2094 return values;
2098 * g_key_file_set_string_list:
2099 * @key_file: a #GKeyFile
2100 * @group_name: a group name
2101 * @key: a key
2102 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
2103 * @length: number of string values in @list
2105 * Associates a list of string values for @key under @group_name.
2106 * If @key cannot be found then it is created.
2107 * If @group_name cannot be found then it is created.
2109 * Since: 2.6
2111 void
2112 g_key_file_set_string_list (GKeyFile *key_file,
2113 const gchar *group_name,
2114 const gchar *key,
2115 const gchar * const list[],
2116 gsize length)
2118 GString *value_list;
2119 gsize i;
2121 g_return_if_fail (key_file != NULL);
2122 g_return_if_fail (list != NULL || length == 0);
2124 value_list = g_string_sized_new (length * 128);
2125 for (i = 0; i < length && list[i] != NULL; i++)
2127 gchar *value;
2129 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2130 g_string_append (value_list, value);
2131 g_string_append_c (value_list, key_file->list_separator);
2133 g_free (value);
2136 g_key_file_set_value (key_file, group_name, key, value_list->str);
2137 g_string_free (value_list, TRUE);
2141 * g_key_file_set_locale_string:
2142 * @key_file: a #GKeyFile
2143 * @group_name: a group name
2144 * @key: a key
2145 * @locale: a locale identifier
2146 * @string: a string
2148 * Associates a string value for @key and @locale under @group_name.
2149 * If the translation for @key cannot be found then it is created.
2151 * Since: 2.6
2153 void
2154 g_key_file_set_locale_string (GKeyFile *key_file,
2155 const gchar *group_name,
2156 const gchar *key,
2157 const gchar *locale,
2158 const gchar *string)
2160 gchar *full_key, *value;
2162 g_return_if_fail (key_file != NULL);
2163 g_return_if_fail (key != NULL);
2164 g_return_if_fail (locale != NULL);
2165 g_return_if_fail (string != NULL);
2167 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2168 full_key = g_strdup_printf ("%s[%s]", key, locale);
2169 g_key_file_set_value (key_file, group_name, full_key, value);
2170 g_free (full_key);
2171 g_free (value);
2175 * g_key_file_get_locale_string:
2176 * @key_file: a #GKeyFile
2177 * @group_name: a group name
2178 * @key: a key
2179 * @locale: (nullable): a locale identifier or %NULL
2180 * @error: return location for a #GError, or %NULL
2182 * Returns the value associated with @key under @group_name
2183 * translated in the given @locale if available. If @locale is
2184 * %NULL then the current locale is assumed.
2186 * If @locale is to be non-%NULL, or if the current locale will change over
2187 * the lifetime of the #GKeyFile, it must be loaded with
2188 * %G_KEY_FILE_KEEP_TRANSLATIONS in order to load strings for all locales.
2190 * If @key cannot be found then %NULL is returned and @error is set
2191 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2192 * with @key cannot be interpreted or no suitable translation can
2193 * be found then the untranslated value is returned.
2195 * Returns: a newly allocated string or %NULL if the specified
2196 * key cannot be found.
2198 * Since: 2.6
2200 gchar *
2201 g_key_file_get_locale_string (GKeyFile *key_file,
2202 const gchar *group_name,
2203 const gchar *key,
2204 const gchar *locale,
2205 GError **error)
2207 gchar *candidate_key, *translated_value;
2208 GError *key_file_error;
2209 gchar **languages;
2210 gboolean free_languages = FALSE;
2211 gint i;
2213 g_return_val_if_fail (key_file != NULL, NULL);
2214 g_return_val_if_fail (group_name != NULL, NULL);
2215 g_return_val_if_fail (key != NULL, NULL);
2217 candidate_key = NULL;
2218 translated_value = NULL;
2219 key_file_error = NULL;
2221 if (locale)
2223 languages = g_get_locale_variants (locale);
2224 free_languages = TRUE;
2226 else
2228 languages = (gchar **) g_get_language_names ();
2229 free_languages = FALSE;
2232 for (i = 0; languages[i]; i++)
2234 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2236 translated_value = g_key_file_get_string (key_file,
2237 group_name,
2238 candidate_key, NULL);
2239 g_free (candidate_key);
2241 if (translated_value)
2242 break;
2244 g_free (translated_value);
2245 translated_value = NULL;
2248 /* Fallback to untranslated key
2250 if (!translated_value)
2252 translated_value = g_key_file_get_string (key_file, group_name, key,
2253 &key_file_error);
2255 if (!translated_value)
2256 g_propagate_error (error, key_file_error);
2259 if (free_languages)
2260 g_strfreev (languages);
2262 return translated_value;
2266 * g_key_file_get_locale_for_key:
2267 * @key_file: a #GKeyFile
2268 * @group_name: a group name
2269 * @key: a key
2270 * @locale: (nullable): a locale identifier or %NULL
2272 * Returns the actual locale which the result of
2273 * g_key_file_get_locale_string() or g_key_file_get_locale_string_list()
2274 * came from.
2276 * If calling g_key_file_get_locale_string() or
2277 * g_key_file_get_locale_string_list() with exactly the same @key_file,
2278 * @group_name, @key and @locale, the result of those functions will
2279 * have originally been tagged with the locale that is the result of
2280 * this function.
2282 * Returns: (nullable): the locale from the file, or %NULL if the key was not
2283 * found or the entry in the file was was untranslated
2285 * Since: 2.56
2287 gchar *
2288 g_key_file_get_locale_for_key (GKeyFile *key_file,
2289 const gchar *group_name,
2290 const gchar *key,
2291 const gchar *locale)
2293 gchar **languages_allocated = NULL;
2294 const gchar * const *languages;
2295 gchar *result = NULL;
2296 gsize i;
2298 g_return_val_if_fail (key_file != NULL, NULL);
2299 g_return_val_if_fail (group_name != NULL, NULL);
2300 g_return_val_if_fail (key != NULL, NULL);
2302 if (locale != NULL)
2304 languages_allocated = g_get_locale_variants (locale);
2305 languages = (const gchar * const *) languages_allocated;
2307 else
2308 languages = g_get_language_names ();
2310 for (i = 0; languages[i] != NULL; i++)
2312 gchar *candidate_key, *translated_value;
2314 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2315 translated_value = g_key_file_get_string (key_file, group_name, candidate_key, NULL);
2316 g_free (translated_value);
2317 g_free (candidate_key);
2319 if (translated_value != NULL)
2320 break;
2323 result = g_strdup (languages[i]);
2325 g_strfreev (languages_allocated);
2327 return result;
2331 * g_key_file_get_locale_string_list:
2332 * @key_file: a #GKeyFile
2333 * @group_name: a group name
2334 * @key: a key
2335 * @locale: (nullable): a locale identifier or %NULL
2336 * @length: (out) (optional): return location for the number of returned strings or %NULL
2337 * @error: return location for a #GError or %NULL
2339 * Returns the values associated with @key under @group_name
2340 * translated in the given @locale if available. If @locale is
2341 * %NULL then the current locale is assumed.
2343 * If @locale is to be non-%NULL, or if the current locale will change over
2344 * the lifetime of the #GKeyFile, it must be loaded with
2345 * %G_KEY_FILE_KEEP_TRANSLATIONS in order to load strings for all locales.
2347 * If @key cannot be found then %NULL is returned and @error is set
2348 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2349 * with @key cannot be interpreted or no suitable translations
2350 * can be found then the untranslated values are returned. The
2351 * returned array is %NULL-terminated, so @length may optionally
2352 * be %NULL.
2354 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2355 * or %NULL if the key isn't found. The string array should be freed
2356 * with g_strfreev().
2358 * Since: 2.6
2360 gchar **
2361 g_key_file_get_locale_string_list (GKeyFile *key_file,
2362 const gchar *group_name,
2363 const gchar *key,
2364 const gchar *locale,
2365 gsize *length,
2366 GError **error)
2368 GError *key_file_error;
2369 gchar **values, *value;
2370 char list_separator[2];
2371 gsize len;
2373 g_return_val_if_fail (key_file != NULL, NULL);
2374 g_return_val_if_fail (group_name != NULL, NULL);
2375 g_return_val_if_fail (key != NULL, NULL);
2377 key_file_error = NULL;
2379 value = g_key_file_get_locale_string (key_file, group_name,
2380 key, locale,
2381 &key_file_error);
2383 if (key_file_error)
2384 g_propagate_error (error, key_file_error);
2386 if (!value)
2388 if (length)
2389 *length = 0;
2390 return NULL;
2393 len = strlen (value);
2394 if (value[len - 1] == key_file->list_separator)
2395 value[len - 1] = '\0';
2397 list_separator[0] = key_file->list_separator;
2398 list_separator[1] = '\0';
2399 values = g_strsplit (value, list_separator, 0);
2401 g_free (value);
2403 if (length)
2404 *length = g_strv_length (values);
2406 return values;
2410 * g_key_file_set_locale_string_list:
2411 * @key_file: a #GKeyFile
2412 * @group_name: a group name
2413 * @key: a key
2414 * @locale: a locale identifier
2415 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2416 * @length: the length of @list
2418 * Associates a list of string values for @key and @locale under
2419 * @group_name. If the translation for @key cannot be found then
2420 * it is created.
2422 * Since: 2.6
2424 void
2425 g_key_file_set_locale_string_list (GKeyFile *key_file,
2426 const gchar *group_name,
2427 const gchar *key,
2428 const gchar *locale,
2429 const gchar * const list[],
2430 gsize length)
2432 GString *value_list;
2433 gchar *full_key;
2434 gsize i;
2436 g_return_if_fail (key_file != NULL);
2437 g_return_if_fail (key != NULL);
2438 g_return_if_fail (locale != NULL);
2439 g_return_if_fail (length != 0);
2441 value_list = g_string_sized_new (length * 128);
2442 for (i = 0; i < length && list[i] != NULL; i++)
2444 gchar *value;
2446 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2447 g_string_append (value_list, value);
2448 g_string_append_c (value_list, key_file->list_separator);
2450 g_free (value);
2453 full_key = g_strdup_printf ("%s[%s]", key, locale);
2454 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2455 g_free (full_key);
2456 g_string_free (value_list, TRUE);
2460 * g_key_file_get_boolean:
2461 * @key_file: a #GKeyFile
2462 * @group_name: a group name
2463 * @key: a key
2464 * @error: return location for a #GError
2466 * Returns the value associated with @key under @group_name as a
2467 * boolean.
2469 * If @key cannot be found then %FALSE is returned and @error is set
2470 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2471 * associated with @key cannot be interpreted as a boolean then %FALSE
2472 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2474 * Returns: the value associated with the key as a boolean,
2475 * or %FALSE if the key was not found or could not be parsed.
2477 * Since: 2.6
2479 gboolean
2480 g_key_file_get_boolean (GKeyFile *key_file,
2481 const gchar *group_name,
2482 const gchar *key,
2483 GError **error)
2485 GError *key_file_error = NULL;
2486 gchar *value;
2487 gboolean bool_value;
2489 g_return_val_if_fail (key_file != NULL, FALSE);
2490 g_return_val_if_fail (group_name != NULL, FALSE);
2491 g_return_val_if_fail (key != NULL, FALSE);
2493 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2495 if (!value)
2497 g_propagate_error (error, key_file_error);
2498 return FALSE;
2501 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2502 &key_file_error);
2503 g_free (value);
2505 if (key_file_error)
2507 if (g_error_matches (key_file_error,
2508 G_KEY_FILE_ERROR,
2509 G_KEY_FILE_ERROR_INVALID_VALUE))
2511 g_set_error (error, G_KEY_FILE_ERROR,
2512 G_KEY_FILE_ERROR_INVALID_VALUE,
2513 _("Key file contains key “%s” "
2514 "which has a value that cannot be interpreted."),
2515 key);
2516 g_error_free (key_file_error);
2518 else
2519 g_propagate_error (error, key_file_error);
2522 return bool_value;
2526 * g_key_file_set_boolean:
2527 * @key_file: a #GKeyFile
2528 * @group_name: a group name
2529 * @key: a key
2530 * @value: %TRUE or %FALSE
2532 * Associates a new boolean value with @key under @group_name.
2533 * If @key cannot be found then it is created.
2535 * Since: 2.6
2537 void
2538 g_key_file_set_boolean (GKeyFile *key_file,
2539 const gchar *group_name,
2540 const gchar *key,
2541 gboolean value)
2543 gchar *result;
2545 g_return_if_fail (key_file != NULL);
2547 result = g_key_file_parse_boolean_as_value (key_file, value);
2548 g_key_file_set_value (key_file, group_name, key, result);
2549 g_free (result);
2553 * g_key_file_get_boolean_list:
2554 * @key_file: a #GKeyFile
2555 * @group_name: a group name
2556 * @key: a key
2557 * @length: (out): the number of booleans returned
2558 * @error: return location for a #GError
2560 * Returns the values associated with @key under @group_name as
2561 * booleans.
2563 * If @key cannot be found then %NULL is returned and @error is set to
2564 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2565 * with @key cannot be interpreted as booleans then %NULL is returned
2566 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2568 * Returns: (array length=length) (element-type gboolean) (transfer container):
2569 * the values associated with the key as a list of booleans, or %NULL if the
2570 * key was not found or could not be parsed. The returned list of booleans
2571 * should be freed with g_free() when no longer needed.
2573 * Since: 2.6
2575 gboolean *
2576 g_key_file_get_boolean_list (GKeyFile *key_file,
2577 const gchar *group_name,
2578 const gchar *key,
2579 gsize *length,
2580 GError **error)
2582 GError *key_file_error;
2583 gchar **values;
2584 gboolean *bool_values;
2585 gsize i, num_bools;
2587 g_return_val_if_fail (key_file != NULL, NULL);
2588 g_return_val_if_fail (group_name != NULL, NULL);
2589 g_return_val_if_fail (key != NULL, NULL);
2591 if (length)
2592 *length = 0;
2594 key_file_error = NULL;
2596 values = g_key_file_get_string_list (key_file, group_name, key,
2597 &num_bools, &key_file_error);
2599 if (key_file_error)
2600 g_propagate_error (error, key_file_error);
2602 if (!values)
2603 return NULL;
2605 bool_values = g_new (gboolean, num_bools);
2607 for (i = 0; i < num_bools; i++)
2609 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2610 values[i],
2611 &key_file_error);
2613 if (key_file_error)
2615 g_propagate_error (error, key_file_error);
2616 g_strfreev (values);
2617 g_free (bool_values);
2619 return NULL;
2622 g_strfreev (values);
2624 if (length)
2625 *length = num_bools;
2627 return bool_values;
2631 * g_key_file_set_boolean_list:
2632 * @key_file: a #GKeyFile
2633 * @group_name: a group name
2634 * @key: a key
2635 * @list: (array length=length): an array of boolean values
2636 * @length: length of @list
2638 * Associates a list of boolean values with @key under @group_name.
2639 * If @key cannot be found then it is created.
2640 * If @group_name is %NULL, the start_group is used.
2642 * Since: 2.6
2644 void
2645 g_key_file_set_boolean_list (GKeyFile *key_file,
2646 const gchar *group_name,
2647 const gchar *key,
2648 gboolean list[],
2649 gsize length)
2651 GString *value_list;
2652 gsize i;
2654 g_return_if_fail (key_file != NULL);
2655 g_return_if_fail (list != NULL);
2657 value_list = g_string_sized_new (length * 8);
2658 for (i = 0; i < length; i++)
2660 gchar *value;
2662 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2664 g_string_append (value_list, value);
2665 g_string_append_c (value_list, key_file->list_separator);
2667 g_free (value);
2670 g_key_file_set_value (key_file, group_name, key, value_list->str);
2671 g_string_free (value_list, TRUE);
2675 * g_key_file_get_integer:
2676 * @key_file: a #GKeyFile
2677 * @group_name: a group name
2678 * @key: a key
2679 * @error: return location for a #GError
2681 * Returns the value associated with @key under @group_name as an
2682 * integer.
2684 * If @key cannot be found then 0 is returned and @error is set to
2685 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2686 * with @key cannot be interpreted as an integer, or is out of range
2687 * for a #gint, then 0 is returned
2688 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2690 * Returns: the value associated with the key as an integer, or
2691 * 0 if the key was not found or could not be parsed.
2693 * Since: 2.6
2695 gint
2696 g_key_file_get_integer (GKeyFile *key_file,
2697 const gchar *group_name,
2698 const gchar *key,
2699 GError **error)
2701 GError *key_file_error;
2702 gchar *value;
2703 gint int_value;
2705 g_return_val_if_fail (key_file != NULL, -1);
2706 g_return_val_if_fail (group_name != NULL, -1);
2707 g_return_val_if_fail (key != NULL, -1);
2709 key_file_error = NULL;
2711 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2713 if (key_file_error)
2715 g_propagate_error (error, key_file_error);
2716 return 0;
2719 int_value = g_key_file_parse_value_as_integer (key_file, value,
2720 &key_file_error);
2721 g_free (value);
2723 if (key_file_error)
2725 if (g_error_matches (key_file_error,
2726 G_KEY_FILE_ERROR,
2727 G_KEY_FILE_ERROR_INVALID_VALUE))
2729 g_set_error (error, G_KEY_FILE_ERROR,
2730 G_KEY_FILE_ERROR_INVALID_VALUE,
2731 _("Key file contains key “%s” in group “%s” "
2732 "which has a value that cannot be interpreted."),
2733 key, group_name);
2734 g_error_free (key_file_error);
2736 else
2737 g_propagate_error (error, key_file_error);
2740 return int_value;
2744 * g_key_file_set_integer:
2745 * @key_file: a #GKeyFile
2746 * @group_name: a group name
2747 * @key: a key
2748 * @value: an integer value
2750 * Associates a new integer value with @key under @group_name.
2751 * If @key cannot be found then it is created.
2753 * Since: 2.6
2755 void
2756 g_key_file_set_integer (GKeyFile *key_file,
2757 const gchar *group_name,
2758 const gchar *key,
2759 gint value)
2761 gchar *result;
2763 g_return_if_fail (key_file != NULL);
2765 result = g_key_file_parse_integer_as_value (key_file, value);
2766 g_key_file_set_value (key_file, group_name, key, result);
2767 g_free (result);
2771 * g_key_file_get_int64:
2772 * @key_file: a non-%NULL #GKeyFile
2773 * @group_name: a non-%NULL group name
2774 * @key: a non-%NULL key
2775 * @error: return location for a #GError
2777 * Returns the value associated with @key under @group_name as a signed
2778 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2779 * 64-bit results without truncation.
2781 * Returns: the value associated with the key as a signed 64-bit integer, or
2782 * 0 if the key was not found or could not be parsed.
2784 * Since: 2.26
2786 gint64
2787 g_key_file_get_int64 (GKeyFile *key_file,
2788 const gchar *group_name,
2789 const gchar *key,
2790 GError **error)
2792 gchar *s, *end;
2793 gint64 v;
2795 g_return_val_if_fail (key_file != NULL, -1);
2796 g_return_val_if_fail (group_name != NULL, -1);
2797 g_return_val_if_fail (key != NULL, -1);
2799 s = g_key_file_get_value (key_file, group_name, key, error);
2801 if (s == NULL)
2802 return 0;
2804 v = g_ascii_strtoll (s, &end, 10);
2806 if (*s == '\0' || *end != '\0')
2808 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2809 _("Key “%s” in group “%s” has value “%s” "
2810 "where %s was expected"),
2811 key, group_name, s, "int64");
2812 g_free (s);
2813 return 0;
2816 g_free (s);
2817 return v;
2821 * g_key_file_set_int64:
2822 * @key_file: a #GKeyFile
2823 * @group_name: a group name
2824 * @key: a key
2825 * @value: an integer value
2827 * Associates a new integer value with @key under @group_name.
2828 * If @key cannot be found then it is created.
2830 * Since: 2.26
2832 void
2833 g_key_file_set_int64 (GKeyFile *key_file,
2834 const gchar *group_name,
2835 const gchar *key,
2836 gint64 value)
2838 gchar *result;
2840 g_return_if_fail (key_file != NULL);
2842 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2843 g_key_file_set_value (key_file, group_name, key, result);
2844 g_free (result);
2848 * g_key_file_get_uint64:
2849 * @key_file: a non-%NULL #GKeyFile
2850 * @group_name: a non-%NULL group name
2851 * @key: a non-%NULL key
2852 * @error: return location for a #GError
2854 * Returns the value associated with @key under @group_name as an unsigned
2855 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2856 * large positive results without truncation.
2858 * Returns: the value associated with the key as an unsigned 64-bit integer,
2859 * or 0 if the key was not found or could not be parsed.
2861 * Since: 2.26
2863 guint64
2864 g_key_file_get_uint64 (GKeyFile *key_file,
2865 const gchar *group_name,
2866 const gchar *key,
2867 GError **error)
2869 gchar *s, *end;
2870 guint64 v;
2872 g_return_val_if_fail (key_file != NULL, -1);
2873 g_return_val_if_fail (group_name != NULL, -1);
2874 g_return_val_if_fail (key != NULL, -1);
2876 s = g_key_file_get_value (key_file, group_name, key, error);
2878 if (s == NULL)
2879 return 0;
2881 v = g_ascii_strtoull (s, &end, 10);
2883 if (*s == '\0' || *end != '\0')
2885 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2886 _("Key “%s” in group “%s” has value “%s” "
2887 "where %s was expected"),
2888 key, group_name, s, "uint64");
2889 g_free (s);
2890 return 0;
2893 g_free (s);
2894 return v;
2898 * g_key_file_set_uint64:
2899 * @key_file: a #GKeyFile
2900 * @group_name: a group name
2901 * @key: a key
2902 * @value: an integer value
2904 * Associates a new integer value with @key under @group_name.
2905 * If @key cannot be found then it is created.
2907 * Since: 2.26
2909 void
2910 g_key_file_set_uint64 (GKeyFile *key_file,
2911 const gchar *group_name,
2912 const gchar *key,
2913 guint64 value)
2915 gchar *result;
2917 g_return_if_fail (key_file != NULL);
2919 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2920 g_key_file_set_value (key_file, group_name, key, result);
2921 g_free (result);
2925 * g_key_file_get_integer_list:
2926 * @key_file: a #GKeyFile
2927 * @group_name: a group name
2928 * @key: a key
2929 * @length: (out): the number of integers returned
2930 * @error: return location for a #GError
2932 * Returns the values associated with @key under @group_name as
2933 * integers.
2935 * If @key cannot be found then %NULL is returned and @error is set to
2936 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2937 * with @key cannot be interpreted as integers, or are out of range for
2938 * #gint, then %NULL is returned
2939 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2941 * Returns: (array length=length) (element-type gint) (transfer container):
2942 * the values associated with the key as a list of integers, or %NULL if
2943 * the key was not found or could not be parsed. The returned list of
2944 * integers should be freed with g_free() when no longer needed.
2946 * Since: 2.6
2948 gint *
2949 g_key_file_get_integer_list (GKeyFile *key_file,
2950 const gchar *group_name,
2951 const gchar *key,
2952 gsize *length,
2953 GError **error)
2955 GError *key_file_error = NULL;
2956 gchar **values;
2957 gint *int_values;
2958 gsize i, num_ints;
2960 g_return_val_if_fail (key_file != NULL, NULL);
2961 g_return_val_if_fail (group_name != NULL, NULL);
2962 g_return_val_if_fail (key != NULL, NULL);
2964 if (length)
2965 *length = 0;
2967 values = g_key_file_get_string_list (key_file, group_name, key,
2968 &num_ints, &key_file_error);
2970 if (key_file_error)
2971 g_propagate_error (error, key_file_error);
2973 if (!values)
2974 return NULL;
2976 int_values = g_new (gint, num_ints);
2978 for (i = 0; i < num_ints; i++)
2980 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2981 values[i],
2982 &key_file_error);
2984 if (key_file_error)
2986 g_propagate_error (error, key_file_error);
2987 g_strfreev (values);
2988 g_free (int_values);
2990 return NULL;
2993 g_strfreev (values);
2995 if (length)
2996 *length = num_ints;
2998 return int_values;
3002 * g_key_file_set_integer_list:
3003 * @key_file: a #GKeyFile
3004 * @group_name: a group name
3005 * @key: a key
3006 * @list: (array length=length): an array of integer values
3007 * @length: number of integer values in @list
3009 * Associates a list of integer values with @key under @group_name.
3010 * If @key cannot be found then it is created.
3012 * Since: 2.6
3014 void
3015 g_key_file_set_integer_list (GKeyFile *key_file,
3016 const gchar *group_name,
3017 const gchar *key,
3018 gint list[],
3019 gsize length)
3021 GString *values;
3022 gsize i;
3024 g_return_if_fail (key_file != NULL);
3025 g_return_if_fail (list != NULL);
3027 values = g_string_sized_new (length * 16);
3028 for (i = 0; i < length; i++)
3030 gchar *value;
3032 value = g_key_file_parse_integer_as_value (key_file, list[i]);
3034 g_string_append (values, value);
3035 g_string_append_c (values, key_file->list_separator);
3037 g_free (value);
3040 g_key_file_set_value (key_file, group_name, key, values->str);
3041 g_string_free (values, TRUE);
3045 * g_key_file_get_double:
3046 * @key_file: a #GKeyFile
3047 * @group_name: a group name
3048 * @key: a key
3049 * @error: return location for a #GError
3051 * Returns the value associated with @key under @group_name as a
3052 * double. If @group_name is %NULL, the start_group is used.
3054 * If @key cannot be found then 0.0 is returned and @error is set to
3055 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
3056 * with @key cannot be interpreted as a double then 0.0 is returned
3057 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
3059 * Returns: the value associated with the key as a double, or
3060 * 0.0 if the key was not found or could not be parsed.
3062 * Since: 2.12
3064 gdouble
3065 g_key_file_get_double (GKeyFile *key_file,
3066 const gchar *group_name,
3067 const gchar *key,
3068 GError **error)
3070 GError *key_file_error;
3071 gchar *value;
3072 gdouble double_value;
3074 g_return_val_if_fail (key_file != NULL, -1);
3075 g_return_val_if_fail (group_name != NULL, -1);
3076 g_return_val_if_fail (key != NULL, -1);
3078 key_file_error = NULL;
3080 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
3082 if (key_file_error)
3084 g_propagate_error (error, key_file_error);
3085 return 0;
3088 double_value = g_key_file_parse_value_as_double (key_file, value,
3089 &key_file_error);
3090 g_free (value);
3092 if (key_file_error)
3094 if (g_error_matches (key_file_error,
3095 G_KEY_FILE_ERROR,
3096 G_KEY_FILE_ERROR_INVALID_VALUE))
3098 g_set_error (error, G_KEY_FILE_ERROR,
3099 G_KEY_FILE_ERROR_INVALID_VALUE,
3100 _("Key file contains key “%s” in group “%s” "
3101 "which has a value that cannot be interpreted."),
3102 key, group_name);
3103 g_error_free (key_file_error);
3105 else
3106 g_propagate_error (error, key_file_error);
3109 return double_value;
3113 * g_key_file_set_double:
3114 * @key_file: a #GKeyFile
3115 * @group_name: a group name
3116 * @key: a key
3117 * @value: an double value
3119 * Associates a new double value with @key under @group_name.
3120 * If @key cannot be found then it is created.
3122 * Since: 2.12
3124 void
3125 g_key_file_set_double (GKeyFile *key_file,
3126 const gchar *group_name,
3127 const gchar *key,
3128 gdouble value)
3130 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3132 g_return_if_fail (key_file != NULL);
3134 g_ascii_dtostr (result, sizeof (result), value);
3135 g_key_file_set_value (key_file, group_name, key, result);
3139 * g_key_file_get_double_list:
3140 * @key_file: a #GKeyFile
3141 * @group_name: a group name
3142 * @key: a key
3143 * @length: (out): the number of doubles returned
3144 * @error: return location for a #GError
3146 * Returns the values associated with @key under @group_name as
3147 * doubles.
3149 * If @key cannot be found then %NULL is returned and @error is set to
3150 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
3151 * with @key cannot be interpreted as doubles then %NULL is returned
3152 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
3154 * Returns: (array length=length) (element-type gdouble) (transfer container):
3155 * the values associated with the key as a list of doubles, or %NULL if the
3156 * key was not found or could not be parsed. The returned list of doubles
3157 * should be freed with g_free() when no longer needed.
3159 * Since: 2.12
3161 gdouble *
3162 g_key_file_get_double_list (GKeyFile *key_file,
3163 const gchar *group_name,
3164 const gchar *key,
3165 gsize *length,
3166 GError **error)
3168 GError *key_file_error = NULL;
3169 gchar **values;
3170 gdouble *double_values;
3171 gsize i, num_doubles;
3173 g_return_val_if_fail (key_file != NULL, NULL);
3174 g_return_val_if_fail (group_name != NULL, NULL);
3175 g_return_val_if_fail (key != NULL, NULL);
3177 if (length)
3178 *length = 0;
3180 values = g_key_file_get_string_list (key_file, group_name, key,
3181 &num_doubles, &key_file_error);
3183 if (key_file_error)
3184 g_propagate_error (error, key_file_error);
3186 if (!values)
3187 return NULL;
3189 double_values = g_new (gdouble, num_doubles);
3191 for (i = 0; i < num_doubles; i++)
3193 double_values[i] = g_key_file_parse_value_as_double (key_file,
3194 values[i],
3195 &key_file_error);
3197 if (key_file_error)
3199 g_propagate_error (error, key_file_error);
3200 g_strfreev (values);
3201 g_free (double_values);
3203 return NULL;
3206 g_strfreev (values);
3208 if (length)
3209 *length = num_doubles;
3211 return double_values;
3215 * g_key_file_set_double_list:
3216 * @key_file: a #GKeyFile
3217 * @group_name: a group name
3218 * @key: a key
3219 * @list: (array length=length): an array of double values
3220 * @length: number of double values in @list
3222 * Associates a list of double values with @key under
3223 * @group_name. If @key cannot be found then it is created.
3225 * Since: 2.12
3227 void
3228 g_key_file_set_double_list (GKeyFile *key_file,
3229 const gchar *group_name,
3230 const gchar *key,
3231 gdouble list[],
3232 gsize length)
3234 GString *values;
3235 gsize i;
3237 g_return_if_fail (key_file != NULL);
3238 g_return_if_fail (list != NULL);
3240 values = g_string_sized_new (length * 16);
3241 for (i = 0; i < length; i++)
3243 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3245 g_ascii_dtostr( result, sizeof (result), list[i] );
3247 g_string_append (values, result);
3248 g_string_append_c (values, key_file->list_separator);
3251 g_key_file_set_value (key_file, group_name, key, values->str);
3252 g_string_free (values, TRUE);
3255 static gboolean
3256 g_key_file_set_key_comment (GKeyFile *key_file,
3257 const gchar *group_name,
3258 const gchar *key,
3259 const gchar *comment,
3260 GError **error)
3262 GKeyFileGroup *group;
3263 GKeyFileKeyValuePair *pair;
3264 GList *key_node, *comment_node, *tmp;
3266 group = g_key_file_lookup_group (key_file, group_name);
3267 if (!group)
3269 g_set_error (error, G_KEY_FILE_ERROR,
3270 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3271 _("Key file does not have group “%s”"),
3272 group_name ? group_name : "(null)");
3274 return FALSE;
3277 /* First find the key the comments are supposed to be
3278 * associated with
3280 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3282 if (key_node == NULL)
3284 set_not_found_key_error (group->name, key, error);
3285 return FALSE;
3288 /* Then find all the comments already associated with the
3289 * key and free them
3291 tmp = key_node->next;
3292 while (tmp != NULL)
3294 pair = (GKeyFileKeyValuePair *) tmp->data;
3296 if (pair->key != NULL)
3297 break;
3299 comment_node = tmp;
3300 tmp = tmp->next;
3301 g_key_file_remove_key_value_pair_node (key_file, group,
3302 comment_node);
3305 if (comment == NULL)
3306 return TRUE;
3308 /* Now we can add our new comment
3310 pair = g_slice_new (GKeyFileKeyValuePair);
3311 pair->key = NULL;
3312 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3314 key_node = g_list_insert (key_node, pair, 1);
3316 return TRUE;
3319 static gboolean
3320 g_key_file_set_group_comment (GKeyFile *key_file,
3321 const gchar *group_name,
3322 const gchar *comment,
3323 GError **error)
3325 GKeyFileGroup *group;
3327 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3329 group = g_key_file_lookup_group (key_file, group_name);
3330 if (!group)
3332 g_set_error (error, G_KEY_FILE_ERROR,
3333 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3334 _("Key file does not have group “%s”"),
3335 group_name ? group_name : "(null)");
3337 return FALSE;
3340 /* First remove any existing comment
3342 if (group->comment)
3344 g_key_file_key_value_pair_free (group->comment);
3345 group->comment = NULL;
3348 if (comment == NULL)
3349 return TRUE;
3351 /* Now we can add our new comment
3353 group->comment = g_slice_new (GKeyFileKeyValuePair);
3354 group->comment->key = NULL;
3355 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3357 return TRUE;
3360 static gboolean
3361 g_key_file_set_top_comment (GKeyFile *key_file,
3362 const gchar *comment,
3363 GError **error)
3365 GList *group_node;
3366 GKeyFileGroup *group;
3367 GKeyFileKeyValuePair *pair;
3369 /* The last group in the list should be the top (comments only)
3370 * group in the file
3372 g_warn_if_fail (key_file->groups != NULL);
3373 group_node = g_list_last (key_file->groups);
3374 group = (GKeyFileGroup *) group_node->data;
3375 g_warn_if_fail (group->name == NULL);
3377 /* Note all keys must be comments at the top of
3378 * the file, so we can just free it all.
3380 g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3381 group->key_value_pairs = NULL;
3383 if (comment == NULL)
3384 return TRUE;
3386 pair = g_slice_new (GKeyFileKeyValuePair);
3387 pair->key = NULL;
3388 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3390 group->key_value_pairs =
3391 g_list_prepend (group->key_value_pairs, pair);
3393 return TRUE;
3397 * g_key_file_set_comment:
3398 * @key_file: a #GKeyFile
3399 * @group_name: (nullable): a group name, or %NULL
3400 * @key: (nullable): a key
3401 * @comment: a comment
3402 * @error: return location for a #GError
3404 * Places a comment above @key from @group_name.
3406 * If @key is %NULL then @comment will be written above @group_name.
3407 * If both @key and @group_name are %NULL, then @comment will be
3408 * written above the first group in the file.
3410 * Note that this function prepends a '#' comment marker to
3411 * each line of @comment.
3413 * Returns: %TRUE if the comment was written, %FALSE otherwise
3415 * Since: 2.6
3417 gboolean
3418 g_key_file_set_comment (GKeyFile *key_file,
3419 const gchar *group_name,
3420 const gchar *key,
3421 const gchar *comment,
3422 GError **error)
3424 g_return_val_if_fail (key_file != NULL, FALSE);
3426 if (group_name != NULL && key != NULL)
3428 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3429 return FALSE;
3431 else if (group_name != NULL)
3433 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3434 return FALSE;
3436 else
3438 if (!g_key_file_set_top_comment (key_file, comment, error))
3439 return FALSE;
3442 return TRUE;
3445 static gchar *
3446 g_key_file_get_key_comment (GKeyFile *key_file,
3447 const gchar *group_name,
3448 const gchar *key,
3449 GError **error)
3451 GKeyFileGroup *group;
3452 GKeyFileKeyValuePair *pair;
3453 GList *key_node, *tmp;
3454 GString *string;
3455 gchar *comment;
3457 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3459 group = g_key_file_lookup_group (key_file, group_name);
3460 if (!group)
3462 g_set_error (error, G_KEY_FILE_ERROR,
3463 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3464 _("Key file does not have group “%s”"),
3465 group_name ? group_name : "(null)");
3467 return NULL;
3470 /* First find the key the comments are supposed to be
3471 * associated with
3473 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3475 if (key_node == NULL)
3477 set_not_found_key_error (group->name, key, error);
3478 return NULL;
3481 string = NULL;
3483 /* Then find all the comments already associated with the
3484 * key and concatentate them.
3486 tmp = key_node->next;
3487 if (!key_node->next)
3488 return NULL;
3490 pair = (GKeyFileKeyValuePair *) tmp->data;
3491 if (pair->key != NULL)
3492 return NULL;
3494 while (tmp->next)
3496 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3498 if (pair->key != NULL)
3499 break;
3501 tmp = tmp->next;
3504 while (tmp != key_node)
3506 pair = (GKeyFileKeyValuePair *) tmp->data;
3508 if (string == NULL)
3509 string = g_string_sized_new (512);
3511 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3512 g_string_append (string, comment);
3513 g_free (comment);
3515 tmp = tmp->prev;
3518 if (string != NULL)
3520 comment = string->str;
3521 g_string_free (string, FALSE);
3523 else
3524 comment = NULL;
3526 return comment;
3529 static gchar *
3530 get_group_comment (GKeyFile *key_file,
3531 GKeyFileGroup *group,
3532 GError **error)
3534 GString *string;
3535 GList *tmp;
3536 gchar *comment;
3538 string = NULL;
3540 tmp = group->key_value_pairs;
3541 while (tmp)
3543 GKeyFileKeyValuePair *pair;
3545 pair = (GKeyFileKeyValuePair *) tmp->data;
3547 if (pair->key != NULL)
3549 tmp = tmp->prev;
3550 break;
3553 if (tmp->next == NULL)
3554 break;
3556 tmp = tmp->next;
3559 while (tmp != NULL)
3561 GKeyFileKeyValuePair *pair;
3563 pair = (GKeyFileKeyValuePair *) tmp->data;
3565 if (string == NULL)
3566 string = g_string_sized_new (512);
3568 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3569 g_string_append (string, comment);
3570 g_free (comment);
3572 tmp = tmp->prev;
3575 if (string != NULL)
3576 return g_string_free (string, FALSE);
3578 return NULL;
3581 static gchar *
3582 g_key_file_get_group_comment (GKeyFile *key_file,
3583 const gchar *group_name,
3584 GError **error)
3586 GList *group_node;
3587 GKeyFileGroup *group;
3589 group = g_key_file_lookup_group (key_file, group_name);
3590 if (!group)
3592 g_set_error (error, G_KEY_FILE_ERROR,
3593 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3594 _("Key file does not have group “%s”"),
3595 group_name ? group_name : "(null)");
3597 return NULL;
3600 if (group->comment)
3601 return g_strdup (group->comment->value);
3603 group_node = g_key_file_lookup_group_node (key_file, group_name);
3604 group_node = group_node->next;
3605 group = (GKeyFileGroup *)group_node->data;
3606 return get_group_comment (key_file, group, error);
3609 static gchar *
3610 g_key_file_get_top_comment (GKeyFile *key_file,
3611 GError **error)
3613 GList *group_node;
3614 GKeyFileGroup *group;
3616 /* The last group in the list should be the top (comments only)
3617 * group in the file
3619 g_warn_if_fail (key_file->groups != NULL);
3620 group_node = g_list_last (key_file->groups);
3621 group = (GKeyFileGroup *) group_node->data;
3622 g_warn_if_fail (group->name == NULL);
3624 return get_group_comment (key_file, group, error);
3628 * g_key_file_get_comment:
3629 * @key_file: a #GKeyFile
3630 * @group_name: (nullable): a group name, or %NULL
3631 * @key: a key
3632 * @error: return location for a #GError
3634 * Retrieves a comment above @key from @group_name.
3635 * If @key is %NULL then @comment will be read from above
3636 * @group_name. If both @key and @group_name are %NULL, then
3637 * @comment will be read from above the first group in the file.
3639 * Note that the returned string includes the '#' comment markers.
3641 * Returns: a comment that should be freed with g_free()
3643 * Since: 2.6
3645 gchar *
3646 g_key_file_get_comment (GKeyFile *key_file,
3647 const gchar *group_name,
3648 const gchar *key,
3649 GError **error)
3651 g_return_val_if_fail (key_file != NULL, NULL);
3653 if (group_name != NULL && key != NULL)
3654 return g_key_file_get_key_comment (key_file, group_name, key, error);
3655 else if (group_name != NULL)
3656 return g_key_file_get_group_comment (key_file, group_name, error);
3657 else
3658 return g_key_file_get_top_comment (key_file, error);
3662 * g_key_file_remove_comment:
3663 * @key_file: a #GKeyFile
3664 * @group_name: (nullable): a group name, or %NULL
3665 * @key: (nullable): a key
3666 * @error: return location for a #GError
3668 * Removes a comment above @key from @group_name.
3669 * If @key is %NULL then @comment will be removed above @group_name.
3670 * If both @key and @group_name are %NULL, then @comment will
3671 * be removed above the first group in the file.
3673 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3675 * Since: 2.6
3678 gboolean
3679 g_key_file_remove_comment (GKeyFile *key_file,
3680 const gchar *group_name,
3681 const gchar *key,
3682 GError **error)
3684 g_return_val_if_fail (key_file != NULL, FALSE);
3686 if (group_name != NULL && key != NULL)
3687 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3688 else if (group_name != NULL)
3689 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3690 else
3691 return g_key_file_set_top_comment (key_file, NULL, error);
3695 * g_key_file_has_group:
3696 * @key_file: a #GKeyFile
3697 * @group_name: a group name
3699 * Looks whether the key file has the group @group_name.
3701 * Returns: %TRUE if @group_name is a part of @key_file, %FALSE
3702 * otherwise.
3703 * Since: 2.6
3705 gboolean
3706 g_key_file_has_group (GKeyFile *key_file,
3707 const gchar *group_name)
3709 g_return_val_if_fail (key_file != NULL, FALSE);
3710 g_return_val_if_fail (group_name != NULL, FALSE);
3712 return g_key_file_lookup_group (key_file, group_name) != NULL;
3715 /* This code remains from a historical attempt to add a new public API
3716 * which respects the GError rules.
3718 static gboolean
3719 g_key_file_has_key_full (GKeyFile *key_file,
3720 const gchar *group_name,
3721 const gchar *key,
3722 gboolean *has_key,
3723 GError **error)
3725 GKeyFileKeyValuePair *pair;
3726 GKeyFileGroup *group;
3728 g_return_val_if_fail (key_file != NULL, FALSE);
3729 g_return_val_if_fail (group_name != NULL, FALSE);
3730 g_return_val_if_fail (key != NULL, FALSE);
3732 group = g_key_file_lookup_group (key_file, group_name);
3734 if (!group)
3736 g_set_error (error, G_KEY_FILE_ERROR,
3737 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3738 _("Key file does not have group “%s”"),
3739 group_name);
3741 return FALSE;
3744 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3746 if (has_key)
3747 *has_key = pair != NULL;
3748 return TRUE;
3752 * g_key_file_has_key: (skip)
3753 * @key_file: a #GKeyFile
3754 * @group_name: a group name
3755 * @key: a key name
3756 * @error: return location for a #GError
3758 * Looks whether the key file has the key @key in the group
3759 * @group_name.
3761 * Note that this function does not follow the rules for #GError strictly;
3762 * the return value both carries meaning and signals an error. To use
3763 * this function, you must pass a #GError pointer in @error, and check
3764 * whether it is not %NULL to see if an error occurred.
3766 * Language bindings should use g_key_file_get_value() to test whether
3767 * or not a key exists.
3769 * Returns: %TRUE if @key is a part of @group_name, %FALSE otherwise
3771 * Since: 2.6
3773 gboolean
3774 g_key_file_has_key (GKeyFile *key_file,
3775 const gchar *group_name,
3776 const gchar *key,
3777 GError **error)
3779 GError *temp_error = NULL;
3780 gboolean has_key;
3782 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3784 return has_key;
3786 else
3788 g_propagate_error (error, temp_error);
3789 return FALSE;
3793 static void
3794 g_key_file_add_group (GKeyFile *key_file,
3795 const gchar *group_name)
3797 GKeyFileGroup *group;
3799 g_return_if_fail (key_file != NULL);
3800 g_return_if_fail (g_key_file_is_group_name (group_name));
3802 group = g_key_file_lookup_group (key_file, group_name);
3803 if (group != NULL)
3805 key_file->current_group = group;
3806 return;
3809 group = g_slice_new0 (GKeyFileGroup);
3810 group->name = g_strdup (group_name);
3811 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3812 key_file->groups = g_list_prepend (key_file->groups, group);
3813 key_file->current_group = group;
3815 if (key_file->start_group == NULL)
3816 key_file->start_group = group;
3818 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3821 static void
3822 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3824 if (pair != NULL)
3826 g_free (pair->key);
3827 g_free (pair->value);
3828 g_slice_free (GKeyFileKeyValuePair, pair);
3832 /* Be careful not to call this function on a node with data in the
3833 * lookup map without removing it from the lookup map, first.
3835 * Some current cases where this warning is not a concern are
3836 * when:
3837 * - the node being removed is a comment node
3838 * - the entire lookup map is getting destroyed soon after
3839 * anyway.
3841 static void
3842 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3843 GKeyFileGroup *group,
3844 GList *pair_node)
3847 GKeyFileKeyValuePair *pair;
3849 pair = (GKeyFileKeyValuePair *) pair_node->data;
3851 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3853 g_warn_if_fail (pair->value != NULL);
3855 g_key_file_key_value_pair_free (pair);
3857 g_list_free_1 (pair_node);
3860 static void
3861 g_key_file_remove_group_node (GKeyFile *key_file,
3862 GList *group_node)
3864 GKeyFileGroup *group;
3865 GList *tmp;
3867 group = (GKeyFileGroup *) group_node->data;
3869 if (group->name)
3870 g_hash_table_remove (key_file->group_hash, group->name);
3872 /* If the current group gets deleted make the current group the last
3873 * added group.
3875 if (key_file->current_group == group)
3877 /* groups should always contain at least the top comment group,
3878 * unless g_key_file_clear has been called
3880 if (key_file->groups)
3881 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3882 else
3883 key_file->current_group = NULL;
3886 /* If the start group gets deleted make the start group the first
3887 * added group.
3889 if (key_file->start_group == group)
3891 tmp = g_list_last (key_file->groups);
3892 while (tmp != NULL)
3894 if (tmp != group_node &&
3895 ((GKeyFileGroup *) tmp->data)->name != NULL)
3896 break;
3898 tmp = tmp->prev;
3901 if (tmp)
3902 key_file->start_group = (GKeyFileGroup *) tmp->data;
3903 else
3904 key_file->start_group = NULL;
3907 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3909 tmp = group->key_value_pairs;
3910 while (tmp != NULL)
3912 GList *pair_node;
3914 pair_node = tmp;
3915 tmp = tmp->next;
3916 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3919 g_warn_if_fail (group->key_value_pairs == NULL);
3921 if (group->comment)
3923 g_key_file_key_value_pair_free (group->comment);
3924 group->comment = NULL;
3927 if (group->lookup_map)
3929 g_hash_table_destroy (group->lookup_map);
3930 group->lookup_map = NULL;
3933 g_free ((gchar *) group->name);
3934 g_slice_free (GKeyFileGroup, group);
3935 g_list_free_1 (group_node);
3939 * g_key_file_remove_group:
3940 * @key_file: a #GKeyFile
3941 * @group_name: a group name
3942 * @error: return location for a #GError or %NULL
3944 * Removes the specified group, @group_name,
3945 * from the key file.
3947 * Returns: %TRUE if the group was removed, %FALSE otherwise
3949 * Since: 2.6
3951 gboolean
3952 g_key_file_remove_group (GKeyFile *key_file,
3953 const gchar *group_name,
3954 GError **error)
3956 GList *group_node;
3958 g_return_val_if_fail (key_file != NULL, FALSE);
3959 g_return_val_if_fail (group_name != NULL, FALSE);
3961 group_node = g_key_file_lookup_group_node (key_file, group_name);
3963 if (!group_node)
3965 g_set_error (error, G_KEY_FILE_ERROR,
3966 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3967 _("Key file does not have group “%s”"),
3968 group_name);
3969 return FALSE;
3972 g_key_file_remove_group_node (key_file, group_node);
3974 return TRUE;
3977 static void
3978 g_key_file_add_key_value_pair (GKeyFile *key_file,
3979 GKeyFileGroup *group,
3980 GKeyFileKeyValuePair *pair)
3982 g_hash_table_replace (group->lookup_map, pair->key, pair);
3983 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3986 static void
3987 g_key_file_add_key (GKeyFile *key_file,
3988 GKeyFileGroup *group,
3989 const gchar *key,
3990 const gchar *value)
3992 GKeyFileKeyValuePair *pair;
3994 pair = g_slice_new (GKeyFileKeyValuePair);
3995 pair->key = g_strdup (key);
3996 pair->value = g_strdup (value);
3998 g_key_file_add_key_value_pair (key_file, group, pair);
4002 * g_key_file_remove_key:
4003 * @key_file: a #GKeyFile
4004 * @group_name: a group name
4005 * @key: a key name to remove
4006 * @error: return location for a #GError or %NULL
4008 * Removes @key in @group_name from the key file.
4010 * Returns: %TRUE if the key was removed, %FALSE otherwise
4012 * Since: 2.6
4014 gboolean
4015 g_key_file_remove_key (GKeyFile *key_file,
4016 const gchar *group_name,
4017 const gchar *key,
4018 GError **error)
4020 GKeyFileGroup *group;
4021 GKeyFileKeyValuePair *pair;
4023 g_return_val_if_fail (key_file != NULL, FALSE);
4024 g_return_val_if_fail (group_name != NULL, FALSE);
4025 g_return_val_if_fail (key != NULL, FALSE);
4027 pair = NULL;
4029 group = g_key_file_lookup_group (key_file, group_name);
4030 if (!group)
4032 g_set_error (error, G_KEY_FILE_ERROR,
4033 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
4034 _("Key file does not have group “%s”"),
4035 group_name);
4036 return FALSE;
4039 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
4041 if (!pair)
4043 set_not_found_key_error (group->name, key, error);
4044 return FALSE;
4047 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
4048 g_hash_table_remove (group->lookup_map, pair->key);
4049 g_key_file_key_value_pair_free (pair);
4051 return TRUE;
4054 static GList *
4055 g_key_file_lookup_group_node (GKeyFile *key_file,
4056 const gchar *group_name)
4058 GKeyFileGroup *group;
4059 GList *tmp;
4061 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
4063 group = (GKeyFileGroup *) tmp->data;
4065 if (group && group->name && strcmp (group->name, group_name) == 0)
4066 break;
4069 return tmp;
4072 static GKeyFileGroup *
4073 g_key_file_lookup_group (GKeyFile *key_file,
4074 const gchar *group_name)
4076 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
4079 static GList *
4080 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
4081 GKeyFileGroup *group,
4082 const gchar *key)
4084 GList *key_node;
4086 for (key_node = group->key_value_pairs;
4087 key_node != NULL;
4088 key_node = key_node->next)
4090 GKeyFileKeyValuePair *pair;
4092 pair = (GKeyFileKeyValuePair *) key_node->data;
4094 if (pair->key && strcmp (pair->key, key) == 0)
4095 break;
4098 return key_node;
4101 static GKeyFileKeyValuePair *
4102 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
4103 GKeyFileGroup *group,
4104 const gchar *key)
4106 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
4109 /* Lines starting with # or consisting entirely of whitespace are merely
4110 * recorded, not parsed. This function assumes all leading whitespace
4111 * has been stripped.
4113 static gboolean
4114 g_key_file_line_is_comment (const gchar *line)
4116 return (*line == '#' || *line == '\0' || *line == '\n');
4119 static gboolean
4120 g_key_file_is_group_name (const gchar *name)
4122 gchar *p, *q;
4124 if (name == NULL)
4125 return FALSE;
4127 p = q = (gchar *) name;
4128 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
4129 q = g_utf8_find_next_char (q, NULL);
4131 if (*q != '\0' || q == p)
4132 return FALSE;
4134 return TRUE;
4137 static gboolean
4138 g_key_file_is_key_name (const gchar *name)
4140 gchar *p, *q;
4142 if (name == NULL)
4143 return FALSE;
4145 p = q = (gchar *) name;
4146 /* We accept a little more than the desktop entry spec says,
4147 * since gnome-vfs uses mime-types as keys in its cache.
4149 while (*q && *q != '=' && *q != '[' && *q != ']')
4150 q = g_utf8_find_next_char (q, NULL);
4152 /* No empty keys, please */
4153 if (q == p)
4154 return FALSE;
4156 /* We accept spaces in the middle of keys to not break
4157 * existing apps, but we don't tolerate initial or final
4158 * spaces, which would lead to silent corruption when
4159 * rereading the file.
4161 if (*p == ' ' || q[-1] == ' ')
4162 return FALSE;
4164 if (*q == '[')
4166 q++;
4167 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
4168 q = g_utf8_find_next_char (q, NULL);
4170 if (*q != ']')
4171 return FALSE;
4173 q++;
4176 if (*q != '\0')
4177 return FALSE;
4179 return TRUE;
4182 /* A group in a key file is made up of a starting '[' followed by one
4183 * or more letters making up the group name followed by ']'.
4185 static gboolean
4186 g_key_file_line_is_group (const gchar *line)
4188 gchar *p;
4190 p = (gchar *) line;
4191 if (*p != '[')
4192 return FALSE;
4194 p++;
4196 while (*p && *p != ']')
4197 p = g_utf8_find_next_char (p, NULL);
4199 if (*p != ']')
4200 return FALSE;
4202 /* silently accept whitespace after the ] */
4203 p = g_utf8_find_next_char (p, NULL);
4204 while (*p == ' ' || *p == '\t')
4205 p = g_utf8_find_next_char (p, NULL);
4207 if (*p)
4208 return FALSE;
4210 return TRUE;
4213 static gboolean
4214 g_key_file_line_is_key_value_pair (const gchar *line)
4216 gchar *p;
4218 p = (gchar *) g_utf8_strchr (line, -1, '=');
4220 if (!p)
4221 return FALSE;
4223 /* Key must be non-empty
4225 if (*p == line[0])
4226 return FALSE;
4228 return TRUE;
4231 static gchar *
4232 g_key_file_parse_value_as_string (GKeyFile *key_file,
4233 const gchar *value,
4234 GSList **pieces,
4235 GError **error)
4237 gchar *string_value, *p, *q0, *q;
4239 string_value = g_new (gchar, strlen (value) + 1);
4241 p = (gchar *) value;
4242 q0 = q = string_value;
4243 while (*p)
4245 if (*p == '\\')
4247 p++;
4249 switch (*p)
4251 case 's':
4252 *q = ' ';
4253 break;
4255 case 'n':
4256 *q = '\n';
4257 break;
4259 case 't':
4260 *q = '\t';
4261 break;
4263 case 'r':
4264 *q = '\r';
4265 break;
4267 case '\\':
4268 *q = '\\';
4269 break;
4271 case '\0':
4272 g_set_error_literal (error, G_KEY_FILE_ERROR,
4273 G_KEY_FILE_ERROR_INVALID_VALUE,
4274 _("Key file contains escape character "
4275 "at end of line"));
4276 break;
4278 default:
4279 if (pieces && *p == key_file->list_separator)
4280 *q = key_file->list_separator;
4281 else
4283 *q++ = '\\';
4284 *q = *p;
4286 if (*error == NULL)
4288 gchar sequence[3];
4290 sequence[0] = '\\';
4291 sequence[1] = *p;
4292 sequence[2] = '\0';
4294 g_set_error (error, G_KEY_FILE_ERROR,
4295 G_KEY_FILE_ERROR_INVALID_VALUE,
4296 _("Key file contains invalid escape "
4297 "sequence “%s”"), sequence);
4300 break;
4303 else
4305 *q = *p;
4306 if (pieces && (*p == key_file->list_separator))
4308 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4309 q0 = q + 1;
4313 if (*p == '\0')
4314 break;
4316 q++;
4317 p++;
4320 *q = '\0';
4321 if (pieces)
4323 if (q0 < q)
4324 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4325 *pieces = g_slist_reverse (*pieces);
4328 return string_value;
4331 static gchar *
4332 g_key_file_parse_string_as_value (GKeyFile *key_file,
4333 const gchar *string,
4334 gboolean escape_separator)
4336 gchar *value, *p, *q;
4337 gsize length;
4338 gboolean parsing_leading_space;
4340 length = strlen (string) + 1;
4342 /* Worst case would be that every character needs to be escaped.
4343 * In other words every character turns to two characters
4345 value = g_new (gchar, 2 * length);
4347 p = (gchar *) string;
4348 q = value;
4349 parsing_leading_space = TRUE;
4350 while (p < (string + length - 1))
4352 gchar escaped_character[3] = { '\\', 0, 0 };
4354 switch (*p)
4356 case ' ':
4357 if (parsing_leading_space)
4359 escaped_character[1] = 's';
4360 strcpy (q, escaped_character);
4361 q += 2;
4363 else
4365 *q = *p;
4366 q++;
4368 break;
4369 case '\t':
4370 if (parsing_leading_space)
4372 escaped_character[1] = 't';
4373 strcpy (q, escaped_character);
4374 q += 2;
4376 else
4378 *q = *p;
4379 q++;
4381 break;
4382 case '\n':
4383 escaped_character[1] = 'n';
4384 strcpy (q, escaped_character);
4385 q += 2;
4386 break;
4387 case '\r':
4388 escaped_character[1] = 'r';
4389 strcpy (q, escaped_character);
4390 q += 2;
4391 break;
4392 case '\\':
4393 escaped_character[1] = '\\';
4394 strcpy (q, escaped_character);
4395 q += 2;
4396 parsing_leading_space = FALSE;
4397 break;
4398 default:
4399 if (escape_separator && *p == key_file->list_separator)
4401 escaped_character[1] = key_file->list_separator;
4402 strcpy (q, escaped_character);
4403 q += 2;
4404 parsing_leading_space = TRUE;
4406 else
4408 *q = *p;
4409 q++;
4410 parsing_leading_space = FALSE;
4412 break;
4414 p++;
4416 *q = '\0';
4418 return value;
4421 static gint
4422 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4423 const gchar *value,
4424 GError **error)
4426 gchar *eof_int;
4427 glong long_value;
4428 gint int_value;
4429 int errsv;
4431 errno = 0;
4432 long_value = strtol (value, &eof_int, 10);
4433 errsv = errno;
4435 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4437 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4438 g_set_error (error, G_KEY_FILE_ERROR,
4439 G_KEY_FILE_ERROR_INVALID_VALUE,
4440 _("Value “%s” cannot be interpreted "
4441 "as a number."), value_utf8);
4442 g_free (value_utf8);
4444 return 0;
4447 int_value = long_value;
4448 if (int_value != long_value || errsv == ERANGE)
4450 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4451 g_set_error (error,
4452 G_KEY_FILE_ERROR,
4453 G_KEY_FILE_ERROR_INVALID_VALUE,
4454 _("Integer value “%s” out of range"),
4455 value_utf8);
4456 g_free (value_utf8);
4458 return 0;
4461 return int_value;
4464 static gchar *
4465 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4466 gint value)
4469 return g_strdup_printf ("%d", value);
4472 static gdouble
4473 g_key_file_parse_value_as_double (GKeyFile *key_file,
4474 const gchar *value,
4475 GError **error)
4477 gchar *end_of_valid_d;
4478 gdouble double_value = 0;
4480 double_value = g_ascii_strtod (value, &end_of_valid_d);
4482 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4484 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4485 g_set_error (error, G_KEY_FILE_ERROR,
4486 G_KEY_FILE_ERROR_INVALID_VALUE,
4487 _("Value “%s” cannot be interpreted "
4488 "as a float number."),
4489 value_utf8);
4490 g_free (value_utf8);
4492 double_value = 0;
4495 return double_value;
4498 static gint
4499 strcmp_sized (const gchar *s1, size_t len1, const gchar *s2)
4501 size_t len2 = strlen (s2);
4502 return strncmp (s1, s2, MAX (len1, len2));
4505 static gboolean
4506 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4507 const gchar *value,
4508 GError **error)
4510 gchar *value_utf8;
4511 gint i, length = 0;
4513 /* Count the number of non-whitespace characters */
4514 for (i = 0; value[i]; i++)
4515 if (!g_ascii_isspace (value[i]))
4516 length = i + 1;
4518 if (strcmp_sized (value, length, "true") == 0 || strcmp_sized (value, length, "1") == 0)
4519 return TRUE;
4520 else if (strcmp_sized (value, length, "false") == 0 || strcmp_sized (value, length, "0") == 0)
4521 return FALSE;
4523 value_utf8 = g_utf8_make_valid (value, -1);
4524 g_set_error (error, G_KEY_FILE_ERROR,
4525 G_KEY_FILE_ERROR_INVALID_VALUE,
4526 _("Value “%s” cannot be interpreted "
4527 "as a boolean."), value_utf8);
4528 g_free (value_utf8);
4530 return FALSE;
4533 static gchar *
4534 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4535 gboolean value)
4537 if (value)
4538 return g_strdup ("true");
4539 else
4540 return g_strdup ("false");
4543 static gchar *
4544 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4545 const gchar *value)
4547 GString *string;
4548 gchar **lines;
4549 gsize i;
4551 string = g_string_sized_new (512);
4553 lines = g_strsplit (value, "\n", 0);
4555 for (i = 0; lines[i] != NULL; i++)
4557 if (lines[i][0] != '#')
4558 g_string_append_printf (string, "%s\n", lines[i]);
4559 else
4560 g_string_append_printf (string, "%s\n", lines[i] + 1);
4562 g_strfreev (lines);
4564 return g_string_free (string, FALSE);
4567 static gchar *
4568 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4569 const gchar *comment)
4571 GString *string;
4572 gchar **lines;
4573 gsize i;
4575 string = g_string_sized_new (512);
4577 lines = g_strsplit (comment, "\n", 0);
4579 for (i = 0; lines[i] != NULL; i++)
4580 g_string_append_printf (string, "#%s%s", lines[i],
4581 lines[i + 1] == NULL? "" : "\n");
4582 g_strfreev (lines);
4584 return g_string_free (string, FALSE);
4588 * g_key_file_save_to_file:
4589 * @key_file: a #GKeyFile
4590 * @filename: the name of the file to write to
4591 * @error: a pointer to a %NULL #GError, or %NULL
4593 * Writes the contents of @key_file to @filename using
4594 * g_file_set_contents().
4596 * This function can fail for any of the reasons that
4597 * g_file_set_contents() may fail.
4599 * Returns: %TRUE if successful, else %FALSE with @error set
4601 * Since: 2.40
4603 gboolean
4604 g_key_file_save_to_file (GKeyFile *key_file,
4605 const gchar *filename,
4606 GError **error)
4608 gchar *contents;
4609 gboolean success;
4610 gsize length;
4612 g_return_val_if_fail (key_file != NULL, FALSE);
4613 g_return_val_if_fail (filename != NULL, FALSE);
4614 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4616 contents = g_key_file_to_data (key_file, &length, NULL);
4617 g_assert (contents != NULL);
4619 success = g_file_set_contents (filename, contents, length, error);
4620 g_free (contents);
4622 return success;