docs: Small improvements to glib-mkenums man page
[glib.git] / glib / gkeyfile.c
blob732e05b08f4e01af331dbac797553968d98091ae
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 > 0)
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_string_list:
2267 * @key_file: a #GKeyFile
2268 * @group_name: a group name
2269 * @key: a key
2270 * @locale: (nullable): a locale identifier or %NULL
2271 * @length: (out) (optional): return location for the number of returned strings or %NULL
2272 * @error: return location for a #GError or %NULL
2274 * Returns the values associated with @key under @group_name
2275 * translated in the given @locale if available. If @locale is
2276 * %NULL then the current locale is assumed.
2278 * If @locale is to be non-%NULL, or if the current locale will change over
2279 * the lifetime of the #GKeyFile, it must be loaded with
2280 * %G_KEY_FILE_KEEP_TRANSLATIONS in order to load strings for all locales.
2282 * If @key cannot be found then %NULL is returned and @error is set
2283 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2284 * with @key cannot be interpreted or no suitable translations
2285 * can be found then the untranslated values are returned. The
2286 * returned array is %NULL-terminated, so @length may optionally
2287 * be %NULL.
2289 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2290 * or %NULL if the key isn't found. The string array should be freed
2291 * with g_strfreev().
2293 * Since: 2.6
2295 gchar **
2296 g_key_file_get_locale_string_list (GKeyFile *key_file,
2297 const gchar *group_name,
2298 const gchar *key,
2299 const gchar *locale,
2300 gsize *length,
2301 GError **error)
2303 GError *key_file_error;
2304 gchar **values, *value;
2305 char list_separator[2];
2306 gsize len;
2308 g_return_val_if_fail (key_file != NULL, NULL);
2309 g_return_val_if_fail (group_name != NULL, NULL);
2310 g_return_val_if_fail (key != NULL, NULL);
2312 key_file_error = NULL;
2314 value = g_key_file_get_locale_string (key_file, group_name,
2315 key, locale,
2316 &key_file_error);
2318 if (key_file_error)
2319 g_propagate_error (error, key_file_error);
2321 if (!value)
2323 if (length)
2324 *length = 0;
2325 return NULL;
2328 len = strlen (value);
2329 if (value[len - 1] == key_file->list_separator)
2330 value[len - 1] = '\0';
2332 list_separator[0] = key_file->list_separator;
2333 list_separator[1] = '\0';
2334 values = g_strsplit (value, list_separator, 0);
2336 g_free (value);
2338 if (length)
2339 *length = g_strv_length (values);
2341 return values;
2345 * g_key_file_set_locale_string_list:
2346 * @key_file: a #GKeyFile
2347 * @group_name: a group name
2348 * @key: a key
2349 * @locale: a locale identifier
2350 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2351 * @length: the length of @list
2353 * Associates a list of string values for @key and @locale under
2354 * @group_name. If the translation for @key cannot be found then
2355 * it is created.
2357 * Since: 2.6
2359 void
2360 g_key_file_set_locale_string_list (GKeyFile *key_file,
2361 const gchar *group_name,
2362 const gchar *key,
2363 const gchar *locale,
2364 const gchar * const list[],
2365 gsize length)
2367 GString *value_list;
2368 gchar *full_key;
2369 gsize i;
2371 g_return_if_fail (key_file != NULL);
2372 g_return_if_fail (key != NULL);
2373 g_return_if_fail (locale != NULL);
2374 g_return_if_fail (length != 0);
2376 value_list = g_string_sized_new (length * 128);
2377 for (i = 0; i < length && list[i] != NULL; i++)
2379 gchar *value;
2381 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2382 g_string_append (value_list, value);
2383 g_string_append_c (value_list, key_file->list_separator);
2385 g_free (value);
2388 full_key = g_strdup_printf ("%s[%s]", key, locale);
2389 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2390 g_free (full_key);
2391 g_string_free (value_list, TRUE);
2395 * g_key_file_get_boolean:
2396 * @key_file: a #GKeyFile
2397 * @group_name: a group name
2398 * @key: a key
2399 * @error: return location for a #GError
2401 * Returns the value associated with @key under @group_name as a
2402 * boolean.
2404 * If @key cannot be found then %FALSE is returned and @error is set
2405 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2406 * associated with @key cannot be interpreted as a boolean then %FALSE
2407 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2409 * Returns: the value associated with the key as a boolean,
2410 * or %FALSE if the key was not found or could not be parsed.
2412 * Since: 2.6
2414 gboolean
2415 g_key_file_get_boolean (GKeyFile *key_file,
2416 const gchar *group_name,
2417 const gchar *key,
2418 GError **error)
2420 GError *key_file_error = NULL;
2421 gchar *value;
2422 gboolean bool_value;
2424 g_return_val_if_fail (key_file != NULL, FALSE);
2425 g_return_val_if_fail (group_name != NULL, FALSE);
2426 g_return_val_if_fail (key != NULL, FALSE);
2428 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2430 if (!value)
2432 g_propagate_error (error, key_file_error);
2433 return FALSE;
2436 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2437 &key_file_error);
2438 g_free (value);
2440 if (key_file_error)
2442 if (g_error_matches (key_file_error,
2443 G_KEY_FILE_ERROR,
2444 G_KEY_FILE_ERROR_INVALID_VALUE))
2446 g_set_error (error, G_KEY_FILE_ERROR,
2447 G_KEY_FILE_ERROR_INVALID_VALUE,
2448 _("Key file contains key “%s” "
2449 "which has a value that cannot be interpreted."),
2450 key);
2451 g_error_free (key_file_error);
2453 else
2454 g_propagate_error (error, key_file_error);
2457 return bool_value;
2461 * g_key_file_set_boolean:
2462 * @key_file: a #GKeyFile
2463 * @group_name: a group name
2464 * @key: a key
2465 * @value: %TRUE or %FALSE
2467 * Associates a new boolean value with @key under @group_name.
2468 * If @key cannot be found then it is created.
2470 * Since: 2.6
2472 void
2473 g_key_file_set_boolean (GKeyFile *key_file,
2474 const gchar *group_name,
2475 const gchar *key,
2476 gboolean value)
2478 gchar *result;
2480 g_return_if_fail (key_file != NULL);
2482 result = g_key_file_parse_boolean_as_value (key_file, value);
2483 g_key_file_set_value (key_file, group_name, key, result);
2484 g_free (result);
2488 * g_key_file_get_boolean_list:
2489 * @key_file: a #GKeyFile
2490 * @group_name: a group name
2491 * @key: a key
2492 * @length: (out): the number of booleans returned
2493 * @error: return location for a #GError
2495 * Returns the values associated with @key under @group_name as
2496 * booleans.
2498 * If @key cannot be found then %NULL is returned and @error is set to
2499 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2500 * with @key cannot be interpreted as booleans then %NULL is returned
2501 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2503 * Returns: (array length=length) (element-type gboolean) (transfer container):
2504 * the values associated with the key as a list of booleans, or %NULL if the
2505 * key was not found or could not be parsed. The returned list of booleans
2506 * should be freed with g_free() when no longer needed.
2508 * Since: 2.6
2510 gboolean *
2511 g_key_file_get_boolean_list (GKeyFile *key_file,
2512 const gchar *group_name,
2513 const gchar *key,
2514 gsize *length,
2515 GError **error)
2517 GError *key_file_error;
2518 gchar **values;
2519 gboolean *bool_values;
2520 gsize i, num_bools;
2522 g_return_val_if_fail (key_file != NULL, NULL);
2523 g_return_val_if_fail (group_name != NULL, NULL);
2524 g_return_val_if_fail (key != NULL, NULL);
2526 if (length)
2527 *length = 0;
2529 key_file_error = NULL;
2531 values = g_key_file_get_string_list (key_file, group_name, key,
2532 &num_bools, &key_file_error);
2534 if (key_file_error)
2535 g_propagate_error (error, key_file_error);
2537 if (!values)
2538 return NULL;
2540 bool_values = g_new (gboolean, num_bools);
2542 for (i = 0; i < num_bools; i++)
2544 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2545 values[i],
2546 &key_file_error);
2548 if (key_file_error)
2550 g_propagate_error (error, key_file_error);
2551 g_strfreev (values);
2552 g_free (bool_values);
2554 return NULL;
2557 g_strfreev (values);
2559 if (length)
2560 *length = num_bools;
2562 return bool_values;
2566 * g_key_file_set_boolean_list:
2567 * @key_file: a #GKeyFile
2568 * @group_name: a group name
2569 * @key: a key
2570 * @list: (array length=length): an array of boolean values
2571 * @length: length of @list
2573 * Associates a list of boolean values with @key under @group_name.
2574 * If @key cannot be found then it is created.
2575 * If @group_name is %NULL, the start_group is used.
2577 * Since: 2.6
2579 void
2580 g_key_file_set_boolean_list (GKeyFile *key_file,
2581 const gchar *group_name,
2582 const gchar *key,
2583 gboolean list[],
2584 gsize length)
2586 GString *value_list;
2587 gsize i;
2589 g_return_if_fail (key_file != NULL);
2590 g_return_if_fail (list != NULL);
2592 value_list = g_string_sized_new (length * 8);
2593 for (i = 0; i < length; i++)
2595 gchar *value;
2597 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2599 g_string_append (value_list, value);
2600 g_string_append_c (value_list, key_file->list_separator);
2602 g_free (value);
2605 g_key_file_set_value (key_file, group_name, key, value_list->str);
2606 g_string_free (value_list, TRUE);
2610 * g_key_file_get_integer:
2611 * @key_file: a #GKeyFile
2612 * @group_name: a group name
2613 * @key: a key
2614 * @error: return location for a #GError
2616 * Returns the value associated with @key under @group_name as an
2617 * integer.
2619 * If @key cannot be found then 0 is returned and @error is set to
2620 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2621 * with @key cannot be interpreted as an integer, or is out of range
2622 * for a #gint, then 0 is returned
2623 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2625 * Returns: the value associated with the key as an integer, or
2626 * 0 if the key was not found or could not be parsed.
2628 * Since: 2.6
2630 gint
2631 g_key_file_get_integer (GKeyFile *key_file,
2632 const gchar *group_name,
2633 const gchar *key,
2634 GError **error)
2636 GError *key_file_error;
2637 gchar *value;
2638 gint int_value;
2640 g_return_val_if_fail (key_file != NULL, -1);
2641 g_return_val_if_fail (group_name != NULL, -1);
2642 g_return_val_if_fail (key != NULL, -1);
2644 key_file_error = NULL;
2646 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2648 if (key_file_error)
2650 g_propagate_error (error, key_file_error);
2651 return 0;
2654 int_value = g_key_file_parse_value_as_integer (key_file, value,
2655 &key_file_error);
2656 g_free (value);
2658 if (key_file_error)
2660 if (g_error_matches (key_file_error,
2661 G_KEY_FILE_ERROR,
2662 G_KEY_FILE_ERROR_INVALID_VALUE))
2664 g_set_error (error, G_KEY_FILE_ERROR,
2665 G_KEY_FILE_ERROR_INVALID_VALUE,
2666 _("Key file contains key “%s” in group “%s” "
2667 "which has a value that cannot be interpreted."),
2668 key, group_name);
2669 g_error_free (key_file_error);
2671 else
2672 g_propagate_error (error, key_file_error);
2675 return int_value;
2679 * g_key_file_set_integer:
2680 * @key_file: a #GKeyFile
2681 * @group_name: a group name
2682 * @key: a key
2683 * @value: an integer value
2685 * Associates a new integer value with @key under @group_name.
2686 * If @key cannot be found then it is created.
2688 * Since: 2.6
2690 void
2691 g_key_file_set_integer (GKeyFile *key_file,
2692 const gchar *group_name,
2693 const gchar *key,
2694 gint value)
2696 gchar *result;
2698 g_return_if_fail (key_file != NULL);
2700 result = g_key_file_parse_integer_as_value (key_file, value);
2701 g_key_file_set_value (key_file, group_name, key, result);
2702 g_free (result);
2706 * g_key_file_get_int64:
2707 * @key_file: a non-%NULL #GKeyFile
2708 * @group_name: a non-%NULL group name
2709 * @key: a non-%NULL key
2710 * @error: return location for a #GError
2712 * Returns the value associated with @key under @group_name as a signed
2713 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2714 * 64-bit results without truncation.
2716 * Returns: the value associated with the key as a signed 64-bit integer, or
2717 * 0 if the key was not found or could not be parsed.
2719 * Since: 2.26
2721 gint64
2722 g_key_file_get_int64 (GKeyFile *key_file,
2723 const gchar *group_name,
2724 const gchar *key,
2725 GError **error)
2727 gchar *s, *end;
2728 gint64 v;
2730 g_return_val_if_fail (key_file != NULL, -1);
2731 g_return_val_if_fail (group_name != NULL, -1);
2732 g_return_val_if_fail (key != NULL, -1);
2734 s = g_key_file_get_value (key_file, group_name, key, error);
2736 if (s == NULL)
2737 return 0;
2739 v = g_ascii_strtoll (s, &end, 10);
2741 if (*s == '\0' || *end != '\0')
2743 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2744 _("Key “%s” in group “%s” has value “%s” "
2745 "where %s was expected"),
2746 key, group_name, s, "int64");
2747 g_free (s);
2748 return 0;
2751 g_free (s);
2752 return v;
2756 * g_key_file_set_int64:
2757 * @key_file: a #GKeyFile
2758 * @group_name: a group name
2759 * @key: a key
2760 * @value: an integer value
2762 * Associates a new integer value with @key under @group_name.
2763 * If @key cannot be found then it is created.
2765 * Since: 2.26
2767 void
2768 g_key_file_set_int64 (GKeyFile *key_file,
2769 const gchar *group_name,
2770 const gchar *key,
2771 gint64 value)
2773 gchar *result;
2775 g_return_if_fail (key_file != NULL);
2777 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2778 g_key_file_set_value (key_file, group_name, key, result);
2779 g_free (result);
2783 * g_key_file_get_uint64:
2784 * @key_file: a non-%NULL #GKeyFile
2785 * @group_name: a non-%NULL group name
2786 * @key: a non-%NULL key
2787 * @error: return location for a #GError
2789 * Returns the value associated with @key under @group_name as an unsigned
2790 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2791 * large positive results without truncation.
2793 * Returns: the value associated with the key as an unsigned 64-bit integer,
2794 * or 0 if the key was not found or could not be parsed.
2796 * Since: 2.26
2798 guint64
2799 g_key_file_get_uint64 (GKeyFile *key_file,
2800 const gchar *group_name,
2801 const gchar *key,
2802 GError **error)
2804 gchar *s, *end;
2805 guint64 v;
2807 g_return_val_if_fail (key_file != NULL, -1);
2808 g_return_val_if_fail (group_name != NULL, -1);
2809 g_return_val_if_fail (key != NULL, -1);
2811 s = g_key_file_get_value (key_file, group_name, key, error);
2813 if (s == NULL)
2814 return 0;
2816 v = g_ascii_strtoull (s, &end, 10);
2818 if (*s == '\0' || *end != '\0')
2820 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2821 _("Key “%s” in group “%s” has value “%s” "
2822 "where %s was expected"),
2823 key, group_name, s, "uint64");
2824 g_free (s);
2825 return 0;
2828 g_free (s);
2829 return v;
2833 * g_key_file_set_uint64:
2834 * @key_file: a #GKeyFile
2835 * @group_name: a group name
2836 * @key: a key
2837 * @value: an integer value
2839 * Associates a new integer value with @key under @group_name.
2840 * If @key cannot be found then it is created.
2842 * Since: 2.26
2844 void
2845 g_key_file_set_uint64 (GKeyFile *key_file,
2846 const gchar *group_name,
2847 const gchar *key,
2848 guint64 value)
2850 gchar *result;
2852 g_return_if_fail (key_file != NULL);
2854 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2855 g_key_file_set_value (key_file, group_name, key, result);
2856 g_free (result);
2860 * g_key_file_get_integer_list:
2861 * @key_file: a #GKeyFile
2862 * @group_name: a group name
2863 * @key: a key
2864 * @length: (out): the number of integers returned
2865 * @error: return location for a #GError
2867 * Returns the values associated with @key under @group_name as
2868 * integers.
2870 * If @key cannot be found then %NULL is returned and @error is set to
2871 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2872 * with @key cannot be interpreted as integers, or are out of range for
2873 * #gint, then %NULL is returned
2874 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2876 * Returns: (array length=length) (element-type gint) (transfer container):
2877 * the values associated with the key as a list of integers, or %NULL if
2878 * the key was not found or could not be parsed. The returned list of
2879 * integers should be freed with g_free() when no longer needed.
2881 * Since: 2.6
2883 gint *
2884 g_key_file_get_integer_list (GKeyFile *key_file,
2885 const gchar *group_name,
2886 const gchar *key,
2887 gsize *length,
2888 GError **error)
2890 GError *key_file_error = NULL;
2891 gchar **values;
2892 gint *int_values;
2893 gsize i, num_ints;
2895 g_return_val_if_fail (key_file != NULL, NULL);
2896 g_return_val_if_fail (group_name != NULL, NULL);
2897 g_return_val_if_fail (key != NULL, NULL);
2899 if (length)
2900 *length = 0;
2902 values = g_key_file_get_string_list (key_file, group_name, key,
2903 &num_ints, &key_file_error);
2905 if (key_file_error)
2906 g_propagate_error (error, key_file_error);
2908 if (!values)
2909 return NULL;
2911 int_values = g_new (gint, num_ints);
2913 for (i = 0; i < num_ints; i++)
2915 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2916 values[i],
2917 &key_file_error);
2919 if (key_file_error)
2921 g_propagate_error (error, key_file_error);
2922 g_strfreev (values);
2923 g_free (int_values);
2925 return NULL;
2928 g_strfreev (values);
2930 if (length)
2931 *length = num_ints;
2933 return int_values;
2937 * g_key_file_set_integer_list:
2938 * @key_file: a #GKeyFile
2939 * @group_name: a group name
2940 * @key: a key
2941 * @list: (array length=length): an array of integer values
2942 * @length: number of integer values in @list
2944 * Associates a list of integer values with @key under @group_name.
2945 * If @key cannot be found then it is created.
2947 * Since: 2.6
2949 void
2950 g_key_file_set_integer_list (GKeyFile *key_file,
2951 const gchar *group_name,
2952 const gchar *key,
2953 gint list[],
2954 gsize length)
2956 GString *values;
2957 gsize i;
2959 g_return_if_fail (key_file != NULL);
2960 g_return_if_fail (list != NULL);
2962 values = g_string_sized_new (length * 16);
2963 for (i = 0; i < length; i++)
2965 gchar *value;
2967 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2969 g_string_append (values, value);
2970 g_string_append_c (values, key_file->list_separator);
2972 g_free (value);
2975 g_key_file_set_value (key_file, group_name, key, values->str);
2976 g_string_free (values, TRUE);
2980 * g_key_file_get_double:
2981 * @key_file: a #GKeyFile
2982 * @group_name: a group name
2983 * @key: a key
2984 * @error: return location for a #GError
2986 * Returns the value associated with @key under @group_name as a
2987 * double. If @group_name is %NULL, the start_group is used.
2989 * If @key cannot be found then 0.0 is returned and @error is set to
2990 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2991 * with @key cannot be interpreted as a double then 0.0 is returned
2992 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2994 * Returns: the value associated with the key as a double, or
2995 * 0.0 if the key was not found or could not be parsed.
2997 * Since: 2.12
2999 gdouble
3000 g_key_file_get_double (GKeyFile *key_file,
3001 const gchar *group_name,
3002 const gchar *key,
3003 GError **error)
3005 GError *key_file_error;
3006 gchar *value;
3007 gdouble double_value;
3009 g_return_val_if_fail (key_file != NULL, -1);
3010 g_return_val_if_fail (group_name != NULL, -1);
3011 g_return_val_if_fail (key != NULL, -1);
3013 key_file_error = NULL;
3015 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
3017 if (key_file_error)
3019 g_propagate_error (error, key_file_error);
3020 return 0;
3023 double_value = g_key_file_parse_value_as_double (key_file, value,
3024 &key_file_error);
3025 g_free (value);
3027 if (key_file_error)
3029 if (g_error_matches (key_file_error,
3030 G_KEY_FILE_ERROR,
3031 G_KEY_FILE_ERROR_INVALID_VALUE))
3033 g_set_error (error, G_KEY_FILE_ERROR,
3034 G_KEY_FILE_ERROR_INVALID_VALUE,
3035 _("Key file contains key “%s” in group “%s” "
3036 "which has a value that cannot be interpreted."),
3037 key, group_name);
3038 g_error_free (key_file_error);
3040 else
3041 g_propagate_error (error, key_file_error);
3044 return double_value;
3048 * g_key_file_set_double:
3049 * @key_file: a #GKeyFile
3050 * @group_name: a group name
3051 * @key: a key
3052 * @value: an double value
3054 * Associates a new double value with @key under @group_name.
3055 * If @key cannot be found then it is created.
3057 * Since: 2.12
3059 void
3060 g_key_file_set_double (GKeyFile *key_file,
3061 const gchar *group_name,
3062 const gchar *key,
3063 gdouble value)
3065 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3067 g_return_if_fail (key_file != NULL);
3069 g_ascii_dtostr (result, sizeof (result), value);
3070 g_key_file_set_value (key_file, group_name, key, result);
3074 * g_key_file_get_double_list:
3075 * @key_file: a #GKeyFile
3076 * @group_name: a group name
3077 * @key: a key
3078 * @length: (out): the number of doubles returned
3079 * @error: return location for a #GError
3081 * Returns the values associated with @key under @group_name as
3082 * doubles.
3084 * If @key cannot be found then %NULL is returned and @error is set to
3085 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
3086 * with @key cannot be interpreted as doubles then %NULL is returned
3087 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
3089 * Returns: (array length=length) (element-type gdouble) (transfer container):
3090 * the values associated with the key as a list of doubles, or %NULL if the
3091 * key was not found or could not be parsed. The returned list of doubles
3092 * should be freed with g_free() when no longer needed.
3094 * Since: 2.12
3096 gdouble *
3097 g_key_file_get_double_list (GKeyFile *key_file,
3098 const gchar *group_name,
3099 const gchar *key,
3100 gsize *length,
3101 GError **error)
3103 GError *key_file_error = NULL;
3104 gchar **values;
3105 gdouble *double_values;
3106 gsize i, num_doubles;
3108 g_return_val_if_fail (key_file != NULL, NULL);
3109 g_return_val_if_fail (group_name != NULL, NULL);
3110 g_return_val_if_fail (key != NULL, NULL);
3112 if (length)
3113 *length = 0;
3115 values = g_key_file_get_string_list (key_file, group_name, key,
3116 &num_doubles, &key_file_error);
3118 if (key_file_error)
3119 g_propagate_error (error, key_file_error);
3121 if (!values)
3122 return NULL;
3124 double_values = g_new (gdouble, num_doubles);
3126 for (i = 0; i < num_doubles; i++)
3128 double_values[i] = g_key_file_parse_value_as_double (key_file,
3129 values[i],
3130 &key_file_error);
3132 if (key_file_error)
3134 g_propagate_error (error, key_file_error);
3135 g_strfreev (values);
3136 g_free (double_values);
3138 return NULL;
3141 g_strfreev (values);
3143 if (length)
3144 *length = num_doubles;
3146 return double_values;
3150 * g_key_file_set_double_list:
3151 * @key_file: a #GKeyFile
3152 * @group_name: a group name
3153 * @key: a key
3154 * @list: (array length=length): an array of double values
3155 * @length: number of double values in @list
3157 * Associates a list of double values with @key under
3158 * @group_name. If @key cannot be found then it is created.
3160 * Since: 2.12
3162 void
3163 g_key_file_set_double_list (GKeyFile *key_file,
3164 const gchar *group_name,
3165 const gchar *key,
3166 gdouble list[],
3167 gsize length)
3169 GString *values;
3170 gsize i;
3172 g_return_if_fail (key_file != NULL);
3173 g_return_if_fail (list != NULL);
3175 values = g_string_sized_new (length * 16);
3176 for (i = 0; i < length; i++)
3178 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3180 g_ascii_dtostr( result, sizeof (result), list[i] );
3182 g_string_append (values, result);
3183 g_string_append_c (values, key_file->list_separator);
3186 g_key_file_set_value (key_file, group_name, key, values->str);
3187 g_string_free (values, TRUE);
3190 static gboolean
3191 g_key_file_set_key_comment (GKeyFile *key_file,
3192 const gchar *group_name,
3193 const gchar *key,
3194 const gchar *comment,
3195 GError **error)
3197 GKeyFileGroup *group;
3198 GKeyFileKeyValuePair *pair;
3199 GList *key_node, *comment_node, *tmp;
3201 group = g_key_file_lookup_group (key_file, group_name);
3202 if (!group)
3204 g_set_error (error, G_KEY_FILE_ERROR,
3205 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3206 _("Key file does not have group “%s”"),
3207 group_name ? group_name : "(null)");
3209 return FALSE;
3212 /* First find the key the comments are supposed to be
3213 * associated with
3215 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3217 if (key_node == NULL)
3219 set_not_found_key_error (group->name, key, error);
3220 return FALSE;
3223 /* Then find all the comments already associated with the
3224 * key and free them
3226 tmp = key_node->next;
3227 while (tmp != NULL)
3229 pair = (GKeyFileKeyValuePair *) tmp->data;
3231 if (pair->key != NULL)
3232 break;
3234 comment_node = tmp;
3235 tmp = tmp->next;
3236 g_key_file_remove_key_value_pair_node (key_file, group,
3237 comment_node);
3240 if (comment == NULL)
3241 return TRUE;
3243 /* Now we can add our new comment
3245 pair = g_slice_new (GKeyFileKeyValuePair);
3246 pair->key = NULL;
3247 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3249 key_node = g_list_insert (key_node, pair, 1);
3251 return TRUE;
3254 static gboolean
3255 g_key_file_set_group_comment (GKeyFile *key_file,
3256 const gchar *group_name,
3257 const gchar *comment,
3258 GError **error)
3260 GKeyFileGroup *group;
3262 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3264 group = g_key_file_lookup_group (key_file, group_name);
3265 if (!group)
3267 g_set_error (error, G_KEY_FILE_ERROR,
3268 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3269 _("Key file does not have group “%s”"),
3270 group_name ? group_name : "(null)");
3272 return FALSE;
3275 /* First remove any existing comment
3277 if (group->comment)
3279 g_key_file_key_value_pair_free (group->comment);
3280 group->comment = NULL;
3283 if (comment == NULL)
3284 return TRUE;
3286 /* Now we can add our new comment
3288 group->comment = g_slice_new (GKeyFileKeyValuePair);
3289 group->comment->key = NULL;
3290 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3292 return TRUE;
3295 static gboolean
3296 g_key_file_set_top_comment (GKeyFile *key_file,
3297 const gchar *comment,
3298 GError **error)
3300 GList *group_node;
3301 GKeyFileGroup *group;
3302 GKeyFileKeyValuePair *pair;
3304 /* The last group in the list should be the top (comments only)
3305 * group in the file
3307 g_warn_if_fail (key_file->groups != NULL);
3308 group_node = g_list_last (key_file->groups);
3309 group = (GKeyFileGroup *) group_node->data;
3310 g_warn_if_fail (group->name == NULL);
3312 /* Note all keys must be comments at the top of
3313 * the file, so we can just free it all.
3315 g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3316 group->key_value_pairs = NULL;
3318 if (comment == NULL)
3319 return TRUE;
3321 pair = g_slice_new (GKeyFileKeyValuePair);
3322 pair->key = NULL;
3323 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3325 group->key_value_pairs =
3326 g_list_prepend (group->key_value_pairs, pair);
3328 return TRUE;
3332 * g_key_file_set_comment:
3333 * @key_file: a #GKeyFile
3334 * @group_name: (nullable): a group name, or %NULL
3335 * @key: (nullable): a key
3336 * @comment: a comment
3337 * @error: return location for a #GError
3339 * Places a comment above @key from @group_name.
3341 * If @key is %NULL then @comment will be written above @group_name.
3342 * If both @key and @group_name are %NULL, then @comment will be
3343 * written above the first group in the file.
3345 * Note that this function prepends a '#' comment marker to
3346 * each line of @comment.
3348 * Returns: %TRUE if the comment was written, %FALSE otherwise
3350 * Since: 2.6
3352 gboolean
3353 g_key_file_set_comment (GKeyFile *key_file,
3354 const gchar *group_name,
3355 const gchar *key,
3356 const gchar *comment,
3357 GError **error)
3359 g_return_val_if_fail (key_file != NULL, FALSE);
3361 if (group_name != NULL && key != NULL)
3363 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3364 return FALSE;
3366 else if (group_name != NULL)
3368 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3369 return FALSE;
3371 else
3373 if (!g_key_file_set_top_comment (key_file, comment, error))
3374 return FALSE;
3377 return TRUE;
3380 static gchar *
3381 g_key_file_get_key_comment (GKeyFile *key_file,
3382 const gchar *group_name,
3383 const gchar *key,
3384 GError **error)
3386 GKeyFileGroup *group;
3387 GKeyFileKeyValuePair *pair;
3388 GList *key_node, *tmp;
3389 GString *string;
3390 gchar *comment;
3392 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3394 group = g_key_file_lookup_group (key_file, group_name);
3395 if (!group)
3397 g_set_error (error, G_KEY_FILE_ERROR,
3398 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3399 _("Key file does not have group “%s”"),
3400 group_name ? group_name : "(null)");
3402 return NULL;
3405 /* First find the key the comments are supposed to be
3406 * associated with
3408 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3410 if (key_node == NULL)
3412 set_not_found_key_error (group->name, key, error);
3413 return NULL;
3416 string = NULL;
3418 /* Then find all the comments already associated with the
3419 * key and concatentate them.
3421 tmp = key_node->next;
3422 if (!key_node->next)
3423 return NULL;
3425 pair = (GKeyFileKeyValuePair *) tmp->data;
3426 if (pair->key != NULL)
3427 return NULL;
3429 while (tmp->next)
3431 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3433 if (pair->key != NULL)
3434 break;
3436 tmp = tmp->next;
3439 while (tmp != key_node)
3441 pair = (GKeyFileKeyValuePair *) tmp->data;
3443 if (string == NULL)
3444 string = g_string_sized_new (512);
3446 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3447 g_string_append (string, comment);
3448 g_free (comment);
3450 tmp = tmp->prev;
3453 if (string != NULL)
3455 comment = string->str;
3456 g_string_free (string, FALSE);
3458 else
3459 comment = NULL;
3461 return comment;
3464 static gchar *
3465 get_group_comment (GKeyFile *key_file,
3466 GKeyFileGroup *group,
3467 GError **error)
3469 GString *string;
3470 GList *tmp;
3471 gchar *comment;
3473 string = NULL;
3475 tmp = group->key_value_pairs;
3476 while (tmp)
3478 GKeyFileKeyValuePair *pair;
3480 pair = (GKeyFileKeyValuePair *) tmp->data;
3482 if (pair->key != NULL)
3484 tmp = tmp->prev;
3485 break;
3488 if (tmp->next == NULL)
3489 break;
3491 tmp = tmp->next;
3494 while (tmp != NULL)
3496 GKeyFileKeyValuePair *pair;
3498 pair = (GKeyFileKeyValuePair *) tmp->data;
3500 if (string == NULL)
3501 string = g_string_sized_new (512);
3503 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3504 g_string_append (string, comment);
3505 g_free (comment);
3507 tmp = tmp->prev;
3510 if (string != NULL)
3511 return g_string_free (string, FALSE);
3513 return NULL;
3516 static gchar *
3517 g_key_file_get_group_comment (GKeyFile *key_file,
3518 const gchar *group_name,
3519 GError **error)
3521 GList *group_node;
3522 GKeyFileGroup *group;
3524 group = g_key_file_lookup_group (key_file, group_name);
3525 if (!group)
3527 g_set_error (error, G_KEY_FILE_ERROR,
3528 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3529 _("Key file does not have group “%s”"),
3530 group_name ? group_name : "(null)");
3532 return NULL;
3535 if (group->comment)
3536 return g_strdup (group->comment->value);
3538 group_node = g_key_file_lookup_group_node (key_file, group_name);
3539 group_node = group_node->next;
3540 group = (GKeyFileGroup *)group_node->data;
3541 return get_group_comment (key_file, group, error);
3544 static gchar *
3545 g_key_file_get_top_comment (GKeyFile *key_file,
3546 GError **error)
3548 GList *group_node;
3549 GKeyFileGroup *group;
3551 /* The last group in the list should be the top (comments only)
3552 * group in the file
3554 g_warn_if_fail (key_file->groups != NULL);
3555 group_node = g_list_last (key_file->groups);
3556 group = (GKeyFileGroup *) group_node->data;
3557 g_warn_if_fail (group->name == NULL);
3559 return get_group_comment (key_file, group, error);
3563 * g_key_file_get_comment:
3564 * @key_file: a #GKeyFile
3565 * @group_name: (nullable): a group name, or %NULL
3566 * @key: a key
3567 * @error: return location for a #GError
3569 * Retrieves a comment above @key from @group_name.
3570 * If @key is %NULL then @comment will be read from above
3571 * @group_name. If both @key and @group_name are %NULL, then
3572 * @comment will be read from above the first group in the file.
3574 * Note that the returned string includes the '#' comment markers.
3576 * Returns: a comment that should be freed with g_free()
3578 * Since: 2.6
3580 gchar *
3581 g_key_file_get_comment (GKeyFile *key_file,
3582 const gchar *group_name,
3583 const gchar *key,
3584 GError **error)
3586 g_return_val_if_fail (key_file != NULL, NULL);
3588 if (group_name != NULL && key != NULL)
3589 return g_key_file_get_key_comment (key_file, group_name, key, error);
3590 else if (group_name != NULL)
3591 return g_key_file_get_group_comment (key_file, group_name, error);
3592 else
3593 return g_key_file_get_top_comment (key_file, error);
3597 * g_key_file_remove_comment:
3598 * @key_file: a #GKeyFile
3599 * @group_name: (nullable): a group name, or %NULL
3600 * @key: (nullable): a key
3601 * @error: return location for a #GError
3603 * Removes a comment above @key from @group_name.
3604 * If @key is %NULL then @comment will be removed above @group_name.
3605 * If both @key and @group_name are %NULL, then @comment will
3606 * be removed above the first group in the file.
3608 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3610 * Since: 2.6
3613 gboolean
3614 g_key_file_remove_comment (GKeyFile *key_file,
3615 const gchar *group_name,
3616 const gchar *key,
3617 GError **error)
3619 g_return_val_if_fail (key_file != NULL, FALSE);
3621 if (group_name != NULL && key != NULL)
3622 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3623 else if (group_name != NULL)
3624 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3625 else
3626 return g_key_file_set_top_comment (key_file, NULL, error);
3630 * g_key_file_has_group:
3631 * @key_file: a #GKeyFile
3632 * @group_name: a group name
3634 * Looks whether the key file has the group @group_name.
3636 * Returns: %TRUE if @group_name is a part of @key_file, %FALSE
3637 * otherwise.
3638 * Since: 2.6
3640 gboolean
3641 g_key_file_has_group (GKeyFile *key_file,
3642 const gchar *group_name)
3644 g_return_val_if_fail (key_file != NULL, FALSE);
3645 g_return_val_if_fail (group_name != NULL, FALSE);
3647 return g_key_file_lookup_group (key_file, group_name) != NULL;
3650 /* This code remains from a historical attempt to add a new public API
3651 * which respects the GError rules.
3653 static gboolean
3654 g_key_file_has_key_full (GKeyFile *key_file,
3655 const gchar *group_name,
3656 const gchar *key,
3657 gboolean *has_key,
3658 GError **error)
3660 GKeyFileKeyValuePair *pair;
3661 GKeyFileGroup *group;
3663 g_return_val_if_fail (key_file != NULL, FALSE);
3664 g_return_val_if_fail (group_name != NULL, FALSE);
3665 g_return_val_if_fail (key != NULL, FALSE);
3667 group = g_key_file_lookup_group (key_file, group_name);
3669 if (!group)
3671 g_set_error (error, G_KEY_FILE_ERROR,
3672 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3673 _("Key file does not have group “%s”"),
3674 group_name);
3676 return FALSE;
3679 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3681 if (has_key)
3682 *has_key = pair != NULL;
3683 return TRUE;
3687 * g_key_file_has_key: (skip)
3688 * @key_file: a #GKeyFile
3689 * @group_name: a group name
3690 * @key: a key name
3691 * @error: return location for a #GError
3693 * Looks whether the key file has the key @key in the group
3694 * @group_name.
3696 * Note that this function does not follow the rules for #GError strictly;
3697 * the return value both carries meaning and signals an error. To use
3698 * this function, you must pass a #GError pointer in @error, and check
3699 * whether it is not %NULL to see if an error occurred.
3701 * Language bindings should use g_key_file_get_value() to test whether
3702 * or not a key exists.
3704 * Returns: %TRUE if @key is a part of @group_name, %FALSE otherwise
3706 * Since: 2.6
3708 gboolean
3709 g_key_file_has_key (GKeyFile *key_file,
3710 const gchar *group_name,
3711 const gchar *key,
3712 GError **error)
3714 GError *temp_error = NULL;
3715 gboolean has_key;
3717 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3719 return has_key;
3721 else
3723 g_propagate_error (error, temp_error);
3724 return FALSE;
3728 static void
3729 g_key_file_add_group (GKeyFile *key_file,
3730 const gchar *group_name)
3732 GKeyFileGroup *group;
3734 g_return_if_fail (key_file != NULL);
3735 g_return_if_fail (g_key_file_is_group_name (group_name));
3737 group = g_key_file_lookup_group (key_file, group_name);
3738 if (group != NULL)
3740 key_file->current_group = group;
3741 return;
3744 group = g_slice_new0 (GKeyFileGroup);
3745 group->name = g_strdup (group_name);
3746 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3747 key_file->groups = g_list_prepend (key_file->groups, group);
3748 key_file->current_group = group;
3750 if (key_file->start_group == NULL)
3751 key_file->start_group = group;
3753 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3756 static void
3757 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3759 if (pair != NULL)
3761 g_free (pair->key);
3762 g_free (pair->value);
3763 g_slice_free (GKeyFileKeyValuePair, pair);
3767 /* Be careful not to call this function on a node with data in the
3768 * lookup map without removing it from the lookup map, first.
3770 * Some current cases where this warning is not a concern are
3771 * when:
3772 * - the node being removed is a comment node
3773 * - the entire lookup map is getting destroyed soon after
3774 * anyway.
3776 static void
3777 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3778 GKeyFileGroup *group,
3779 GList *pair_node)
3782 GKeyFileKeyValuePair *pair;
3784 pair = (GKeyFileKeyValuePair *) pair_node->data;
3786 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3788 g_warn_if_fail (pair->value != NULL);
3790 g_key_file_key_value_pair_free (pair);
3792 g_list_free_1 (pair_node);
3795 static void
3796 g_key_file_remove_group_node (GKeyFile *key_file,
3797 GList *group_node)
3799 GKeyFileGroup *group;
3800 GList *tmp;
3802 group = (GKeyFileGroup *) group_node->data;
3804 if (group->name)
3805 g_hash_table_remove (key_file->group_hash, group->name);
3807 /* If the current group gets deleted make the current group the last
3808 * added group.
3810 if (key_file->current_group == group)
3812 /* groups should always contain at least the top comment group,
3813 * unless g_key_file_clear has been called
3815 if (key_file->groups)
3816 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3817 else
3818 key_file->current_group = NULL;
3821 /* If the start group gets deleted make the start group the first
3822 * added group.
3824 if (key_file->start_group == group)
3826 tmp = g_list_last (key_file->groups);
3827 while (tmp != NULL)
3829 if (tmp != group_node &&
3830 ((GKeyFileGroup *) tmp->data)->name != NULL)
3831 break;
3833 tmp = tmp->prev;
3836 if (tmp)
3837 key_file->start_group = (GKeyFileGroup *) tmp->data;
3838 else
3839 key_file->start_group = NULL;
3842 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3844 tmp = group->key_value_pairs;
3845 while (tmp != NULL)
3847 GList *pair_node;
3849 pair_node = tmp;
3850 tmp = tmp->next;
3851 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3854 g_warn_if_fail (group->key_value_pairs == NULL);
3856 if (group->comment)
3858 g_key_file_key_value_pair_free (group->comment);
3859 group->comment = NULL;
3862 if (group->lookup_map)
3864 g_hash_table_destroy (group->lookup_map);
3865 group->lookup_map = NULL;
3868 g_free ((gchar *) group->name);
3869 g_slice_free (GKeyFileGroup, group);
3870 g_list_free_1 (group_node);
3874 * g_key_file_remove_group:
3875 * @key_file: a #GKeyFile
3876 * @group_name: a group name
3877 * @error: return location for a #GError or %NULL
3879 * Removes the specified group, @group_name,
3880 * from the key file.
3882 * Returns: %TRUE if the group was removed, %FALSE otherwise
3884 * Since: 2.6
3886 gboolean
3887 g_key_file_remove_group (GKeyFile *key_file,
3888 const gchar *group_name,
3889 GError **error)
3891 GList *group_node;
3893 g_return_val_if_fail (key_file != NULL, FALSE);
3894 g_return_val_if_fail (group_name != NULL, FALSE);
3896 group_node = g_key_file_lookup_group_node (key_file, group_name);
3898 if (!group_node)
3900 g_set_error (error, G_KEY_FILE_ERROR,
3901 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3902 _("Key file does not have group “%s”"),
3903 group_name);
3904 return FALSE;
3907 g_key_file_remove_group_node (key_file, group_node);
3909 return TRUE;
3912 static void
3913 g_key_file_add_key_value_pair (GKeyFile *key_file,
3914 GKeyFileGroup *group,
3915 GKeyFileKeyValuePair *pair)
3917 g_hash_table_replace (group->lookup_map, pair->key, pair);
3918 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3921 static void
3922 g_key_file_add_key (GKeyFile *key_file,
3923 GKeyFileGroup *group,
3924 const gchar *key,
3925 const gchar *value)
3927 GKeyFileKeyValuePair *pair;
3929 pair = g_slice_new (GKeyFileKeyValuePair);
3930 pair->key = g_strdup (key);
3931 pair->value = g_strdup (value);
3933 g_key_file_add_key_value_pair (key_file, group, pair);
3937 * g_key_file_remove_key:
3938 * @key_file: a #GKeyFile
3939 * @group_name: a group name
3940 * @key: a key name to remove
3941 * @error: return location for a #GError or %NULL
3943 * Removes @key in @group_name from the key file.
3945 * Returns: %TRUE if the key was removed, %FALSE otherwise
3947 * Since: 2.6
3949 gboolean
3950 g_key_file_remove_key (GKeyFile *key_file,
3951 const gchar *group_name,
3952 const gchar *key,
3953 GError **error)
3955 GKeyFileGroup *group;
3956 GKeyFileKeyValuePair *pair;
3958 g_return_val_if_fail (key_file != NULL, FALSE);
3959 g_return_val_if_fail (group_name != NULL, FALSE);
3960 g_return_val_if_fail (key != NULL, FALSE);
3962 pair = NULL;
3964 group = g_key_file_lookup_group (key_file, group_name);
3965 if (!group)
3967 g_set_error (error, G_KEY_FILE_ERROR,
3968 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3969 _("Key file does not have group “%s”"),
3970 group_name);
3971 return FALSE;
3974 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3976 if (!pair)
3978 set_not_found_key_error (group->name, key, error);
3979 return FALSE;
3982 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3983 g_hash_table_remove (group->lookup_map, pair->key);
3984 g_key_file_key_value_pair_free (pair);
3986 return TRUE;
3989 static GList *
3990 g_key_file_lookup_group_node (GKeyFile *key_file,
3991 const gchar *group_name)
3993 GKeyFileGroup *group;
3994 GList *tmp;
3996 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3998 group = (GKeyFileGroup *) tmp->data;
4000 if (group && group->name && strcmp (group->name, group_name) == 0)
4001 break;
4004 return tmp;
4007 static GKeyFileGroup *
4008 g_key_file_lookup_group (GKeyFile *key_file,
4009 const gchar *group_name)
4011 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
4014 static GList *
4015 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
4016 GKeyFileGroup *group,
4017 const gchar *key)
4019 GList *key_node;
4021 for (key_node = group->key_value_pairs;
4022 key_node != NULL;
4023 key_node = key_node->next)
4025 GKeyFileKeyValuePair *pair;
4027 pair = (GKeyFileKeyValuePair *) key_node->data;
4029 if (pair->key && strcmp (pair->key, key) == 0)
4030 break;
4033 return key_node;
4036 static GKeyFileKeyValuePair *
4037 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
4038 GKeyFileGroup *group,
4039 const gchar *key)
4041 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
4044 /* Lines starting with # or consisting entirely of whitespace are merely
4045 * recorded, not parsed. This function assumes all leading whitespace
4046 * has been stripped.
4048 static gboolean
4049 g_key_file_line_is_comment (const gchar *line)
4051 return (*line == '#' || *line == '\0' || *line == '\n');
4054 static gboolean
4055 g_key_file_is_group_name (const gchar *name)
4057 gchar *p, *q;
4059 if (name == NULL)
4060 return FALSE;
4062 p = q = (gchar *) name;
4063 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
4064 q = g_utf8_find_next_char (q, NULL);
4066 if (*q != '\0' || q == p)
4067 return FALSE;
4069 return TRUE;
4072 static gboolean
4073 g_key_file_is_key_name (const gchar *name)
4075 gchar *p, *q;
4077 if (name == NULL)
4078 return FALSE;
4080 p = q = (gchar *) name;
4081 /* We accept a little more than the desktop entry spec says,
4082 * since gnome-vfs uses mime-types as keys in its cache.
4084 while (*q && *q != '=' && *q != '[' && *q != ']')
4085 q = g_utf8_find_next_char (q, NULL);
4087 /* No empty keys, please */
4088 if (q == p)
4089 return FALSE;
4091 /* We accept spaces in the middle of keys to not break
4092 * existing apps, but we don't tolerate initial or final
4093 * spaces, which would lead to silent corruption when
4094 * rereading the file.
4096 if (*p == ' ' || q[-1] == ' ')
4097 return FALSE;
4099 if (*q == '[')
4101 q++;
4102 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
4103 q = g_utf8_find_next_char (q, NULL);
4105 if (*q != ']')
4106 return FALSE;
4108 q++;
4111 if (*q != '\0')
4112 return FALSE;
4114 return TRUE;
4117 /* A group in a key file is made up of a starting '[' followed by one
4118 * or more letters making up the group name followed by ']'.
4120 static gboolean
4121 g_key_file_line_is_group (const gchar *line)
4123 gchar *p;
4125 p = (gchar *) line;
4126 if (*p != '[')
4127 return FALSE;
4129 p++;
4131 while (*p && *p != ']')
4132 p = g_utf8_find_next_char (p, NULL);
4134 if (*p != ']')
4135 return FALSE;
4137 /* silently accept whitespace after the ] */
4138 p = g_utf8_find_next_char (p, NULL);
4139 while (*p == ' ' || *p == '\t')
4140 p = g_utf8_find_next_char (p, NULL);
4142 if (*p)
4143 return FALSE;
4145 return TRUE;
4148 static gboolean
4149 g_key_file_line_is_key_value_pair (const gchar *line)
4151 gchar *p;
4153 p = (gchar *) g_utf8_strchr (line, -1, '=');
4155 if (!p)
4156 return FALSE;
4158 /* Key must be non-empty
4160 if (*p == line[0])
4161 return FALSE;
4163 return TRUE;
4166 static gchar *
4167 g_key_file_parse_value_as_string (GKeyFile *key_file,
4168 const gchar *value,
4169 GSList **pieces,
4170 GError **error)
4172 gchar *string_value, *p, *q0, *q;
4174 string_value = g_new (gchar, strlen (value) + 1);
4176 p = (gchar *) value;
4177 q0 = q = string_value;
4178 while (*p)
4180 if (*p == '\\')
4182 p++;
4184 switch (*p)
4186 case 's':
4187 *q = ' ';
4188 break;
4190 case 'n':
4191 *q = '\n';
4192 break;
4194 case 't':
4195 *q = '\t';
4196 break;
4198 case 'r':
4199 *q = '\r';
4200 break;
4202 case '\\':
4203 *q = '\\';
4204 break;
4206 case '\0':
4207 g_set_error_literal (error, G_KEY_FILE_ERROR,
4208 G_KEY_FILE_ERROR_INVALID_VALUE,
4209 _("Key file contains escape character "
4210 "at end of line"));
4211 break;
4213 default:
4214 if (pieces && *p == key_file->list_separator)
4215 *q = key_file->list_separator;
4216 else
4218 *q++ = '\\';
4219 *q = *p;
4221 if (*error == NULL)
4223 gchar sequence[3];
4225 sequence[0] = '\\';
4226 sequence[1] = *p;
4227 sequence[2] = '\0';
4229 g_set_error (error, G_KEY_FILE_ERROR,
4230 G_KEY_FILE_ERROR_INVALID_VALUE,
4231 _("Key file contains invalid escape "
4232 "sequence “%s”"), sequence);
4235 break;
4238 else
4240 *q = *p;
4241 if (pieces && (*p == key_file->list_separator))
4243 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4244 q0 = q + 1;
4248 if (*p == '\0')
4249 break;
4251 q++;
4252 p++;
4255 *q = '\0';
4256 if (pieces)
4258 if (q0 < q)
4259 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4260 *pieces = g_slist_reverse (*pieces);
4263 return string_value;
4266 static gchar *
4267 g_key_file_parse_string_as_value (GKeyFile *key_file,
4268 const gchar *string,
4269 gboolean escape_separator)
4271 gchar *value, *p, *q;
4272 gsize length;
4273 gboolean parsing_leading_space;
4275 length = strlen (string) + 1;
4277 /* Worst case would be that every character needs to be escaped.
4278 * In other words every character turns to two characters
4280 value = g_new (gchar, 2 * length);
4282 p = (gchar *) string;
4283 q = value;
4284 parsing_leading_space = TRUE;
4285 while (p < (string + length - 1))
4287 gchar escaped_character[3] = { '\\', 0, 0 };
4289 switch (*p)
4291 case ' ':
4292 if (parsing_leading_space)
4294 escaped_character[1] = 's';
4295 strcpy (q, escaped_character);
4296 q += 2;
4298 else
4300 *q = *p;
4301 q++;
4303 break;
4304 case '\t':
4305 if (parsing_leading_space)
4307 escaped_character[1] = 't';
4308 strcpy (q, escaped_character);
4309 q += 2;
4311 else
4313 *q = *p;
4314 q++;
4316 break;
4317 case '\n':
4318 escaped_character[1] = 'n';
4319 strcpy (q, escaped_character);
4320 q += 2;
4321 break;
4322 case '\r':
4323 escaped_character[1] = 'r';
4324 strcpy (q, escaped_character);
4325 q += 2;
4326 break;
4327 case '\\':
4328 escaped_character[1] = '\\';
4329 strcpy (q, escaped_character);
4330 q += 2;
4331 parsing_leading_space = FALSE;
4332 break;
4333 default:
4334 if (escape_separator && *p == key_file->list_separator)
4336 escaped_character[1] = key_file->list_separator;
4337 strcpy (q, escaped_character);
4338 q += 2;
4339 parsing_leading_space = TRUE;
4341 else
4343 *q = *p;
4344 q++;
4345 parsing_leading_space = FALSE;
4347 break;
4349 p++;
4351 *q = '\0';
4353 return value;
4356 static gint
4357 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4358 const gchar *value,
4359 GError **error)
4361 gchar *eof_int;
4362 glong long_value;
4363 gint int_value;
4364 int errsv;
4366 errno = 0;
4367 long_value = strtol (value, &eof_int, 10);
4368 errsv = errno;
4370 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4372 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4373 g_set_error (error, G_KEY_FILE_ERROR,
4374 G_KEY_FILE_ERROR_INVALID_VALUE,
4375 _("Value “%s” cannot be interpreted "
4376 "as a number."), value_utf8);
4377 g_free (value_utf8);
4379 return 0;
4382 int_value = long_value;
4383 if (int_value != long_value || errsv == ERANGE)
4385 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4386 g_set_error (error,
4387 G_KEY_FILE_ERROR,
4388 G_KEY_FILE_ERROR_INVALID_VALUE,
4389 _("Integer value “%s” out of range"),
4390 value_utf8);
4391 g_free (value_utf8);
4393 return 0;
4396 return int_value;
4399 static gchar *
4400 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4401 gint value)
4404 return g_strdup_printf ("%d", value);
4407 static gdouble
4408 g_key_file_parse_value_as_double (GKeyFile *key_file,
4409 const gchar *value,
4410 GError **error)
4412 gchar *end_of_valid_d;
4413 gdouble double_value = 0;
4415 double_value = g_ascii_strtod (value, &end_of_valid_d);
4417 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4419 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4420 g_set_error (error, G_KEY_FILE_ERROR,
4421 G_KEY_FILE_ERROR_INVALID_VALUE,
4422 _("Value “%s” cannot be interpreted "
4423 "as a float number."),
4424 value_utf8);
4425 g_free (value_utf8);
4427 double_value = 0;
4430 return double_value;
4433 static gint
4434 strcmp_sized (const gchar *s1, size_t len1, const gchar *s2)
4436 size_t len2 = strlen (s2);
4437 return strncmp (s1, s2, MAX (len1, len2));
4440 static gboolean
4441 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4442 const gchar *value,
4443 GError **error)
4445 gchar *value_utf8;
4446 gint i, length = 0;
4448 /* Count the number of non-whitespace characters */
4449 for (i = 0; value[i]; i++)
4450 if (!g_ascii_isspace (value[i]))
4451 length = i + 1;
4453 if (strcmp_sized (value, length, "true") == 0 || strcmp_sized (value, length, "1") == 0)
4454 return TRUE;
4455 else if (strcmp_sized (value, length, "false") == 0 || strcmp_sized (value, length, "0") == 0)
4456 return FALSE;
4458 value_utf8 = g_utf8_make_valid (value, -1);
4459 g_set_error (error, G_KEY_FILE_ERROR,
4460 G_KEY_FILE_ERROR_INVALID_VALUE,
4461 _("Value “%s” cannot be interpreted "
4462 "as a boolean."), value_utf8);
4463 g_free (value_utf8);
4465 return FALSE;
4468 static gchar *
4469 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4470 gboolean value)
4472 if (value)
4473 return g_strdup ("true");
4474 else
4475 return g_strdup ("false");
4478 static gchar *
4479 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4480 const gchar *value)
4482 GString *string;
4483 gchar **lines;
4484 gsize i;
4486 string = g_string_sized_new (512);
4488 lines = g_strsplit (value, "\n", 0);
4490 for (i = 0; lines[i] != NULL; i++)
4492 if (lines[i][0] != '#')
4493 g_string_append_printf (string, "%s\n", lines[i]);
4494 else
4495 g_string_append_printf (string, "%s\n", lines[i] + 1);
4497 g_strfreev (lines);
4499 return g_string_free (string, FALSE);
4502 static gchar *
4503 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4504 const gchar *comment)
4506 GString *string;
4507 gchar **lines;
4508 gsize i;
4510 string = g_string_sized_new (512);
4512 lines = g_strsplit (comment, "\n", 0);
4514 for (i = 0; lines[i] != NULL; i++)
4515 g_string_append_printf (string, "#%s%s", lines[i],
4516 lines[i + 1] == NULL? "" : "\n");
4517 g_strfreev (lines);
4519 return g_string_free (string, FALSE);
4523 * g_key_file_save_to_file:
4524 * @key_file: a #GKeyFile
4525 * @filename: the name of the file to write to
4526 * @error: a pointer to a %NULL #GError, or %NULL
4528 * Writes the contents of @key_file to @filename using
4529 * g_file_set_contents().
4531 * This function can fail for any of the reasons that
4532 * g_file_set_contents() may fail.
4534 * Returns: %TRUE if successful, else %FALSE with @error set
4536 * Since: 2.40
4538 gboolean
4539 g_key_file_save_to_file (GKeyFile *key_file,
4540 const gchar *filename,
4541 GError **error)
4543 gchar *contents;
4544 gboolean success;
4545 gsize length;
4547 g_return_val_if_fail (key_file != NULL, FALSE);
4548 g_return_val_if_fail (filename != NULL, FALSE);
4549 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4551 contents = g_key_file_to_data (key_file, &length, NULL);
4552 g_assert (contents != NULL);
4554 success = g_file_set_contents (filename, contents, length, error);
4555 g_free (contents);
4557 return success;