Add support for g_auto[s]list(Type)
[glib.git] / glib / gkeyfile.c
blobe45661a697a6db483f75d1d0ed762b4285ef90e7
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 @key cannot be found then %NULL is returned and @error is set
2187 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2188 * with @key cannot be interpreted or no suitable translation can
2189 * be found then the untranslated value is returned.
2191 * Returns: a newly allocated string or %NULL if the specified
2192 * key cannot be found.
2194 * Since: 2.6
2196 gchar *
2197 g_key_file_get_locale_string (GKeyFile *key_file,
2198 const gchar *group_name,
2199 const gchar *key,
2200 const gchar *locale,
2201 GError **error)
2203 gchar *candidate_key, *translated_value;
2204 GError *key_file_error;
2205 gchar **languages;
2206 gboolean free_languages = FALSE;
2207 gint i;
2209 g_return_val_if_fail (key_file != NULL, NULL);
2210 g_return_val_if_fail (group_name != NULL, NULL);
2211 g_return_val_if_fail (key != NULL, NULL);
2213 candidate_key = NULL;
2214 translated_value = NULL;
2215 key_file_error = NULL;
2217 if (locale)
2219 languages = g_get_locale_variants (locale);
2220 free_languages = TRUE;
2222 else
2224 languages = (gchar **) g_get_language_names ();
2225 free_languages = FALSE;
2228 for (i = 0; languages[i]; i++)
2230 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2232 translated_value = g_key_file_get_string (key_file,
2233 group_name,
2234 candidate_key, NULL);
2235 g_free (candidate_key);
2237 if (translated_value)
2238 break;
2240 g_free (translated_value);
2241 translated_value = NULL;
2244 /* Fallback to untranslated key
2246 if (!translated_value)
2248 translated_value = g_key_file_get_string (key_file, group_name, key,
2249 &key_file_error);
2251 if (!translated_value)
2252 g_propagate_error (error, key_file_error);
2255 if (free_languages)
2256 g_strfreev (languages);
2258 return translated_value;
2262 * g_key_file_get_locale_string_list:
2263 * @key_file: a #GKeyFile
2264 * @group_name: a group name
2265 * @key: a key
2266 * @locale: (nullable): a locale identifier or %NULL
2267 * @length: (out) (optional): return location for the number of returned strings or %NULL
2268 * @error: return location for a #GError or %NULL
2270 * Returns the values associated with @key under @group_name
2271 * translated in the given @locale if available. If @locale is
2272 * %NULL then the current locale is assumed.
2274 * If @key cannot be found then %NULL is returned and @error is set
2275 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2276 * with @key cannot be interpreted or no suitable translations
2277 * can be found then the untranslated values are returned. The
2278 * returned array is %NULL-terminated, so @length may optionally
2279 * be %NULL.
2281 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2282 * or %NULL if the key isn't found. The string array should be freed
2283 * with g_strfreev().
2285 * Since: 2.6
2287 gchar **
2288 g_key_file_get_locale_string_list (GKeyFile *key_file,
2289 const gchar *group_name,
2290 const gchar *key,
2291 const gchar *locale,
2292 gsize *length,
2293 GError **error)
2295 GError *key_file_error;
2296 gchar **values, *value;
2297 char list_separator[2];
2298 gsize len;
2300 g_return_val_if_fail (key_file != NULL, NULL);
2301 g_return_val_if_fail (group_name != NULL, NULL);
2302 g_return_val_if_fail (key != NULL, NULL);
2304 key_file_error = NULL;
2306 value = g_key_file_get_locale_string (key_file, group_name,
2307 key, locale,
2308 &key_file_error);
2310 if (key_file_error)
2311 g_propagate_error (error, key_file_error);
2313 if (!value)
2315 if (length)
2316 *length = 0;
2317 return NULL;
2320 len = strlen (value);
2321 if (value[len - 1] == key_file->list_separator)
2322 value[len - 1] = '\0';
2324 list_separator[0] = key_file->list_separator;
2325 list_separator[1] = '\0';
2326 values = g_strsplit (value, list_separator, 0);
2328 g_free (value);
2330 if (length)
2331 *length = g_strv_length (values);
2333 return values;
2337 * g_key_file_set_locale_string_list:
2338 * @key_file: a #GKeyFile
2339 * @group_name: a group name
2340 * @key: a key
2341 * @locale: a locale identifier
2342 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2343 * @length: the length of @list
2345 * Associates a list of string values for @key and @locale under
2346 * @group_name. If the translation for @key cannot be found then
2347 * it is created.
2349 * Since: 2.6
2351 void
2352 g_key_file_set_locale_string_list (GKeyFile *key_file,
2353 const gchar *group_name,
2354 const gchar *key,
2355 const gchar *locale,
2356 const gchar * const list[],
2357 gsize length)
2359 GString *value_list;
2360 gchar *full_key;
2361 gsize i;
2363 g_return_if_fail (key_file != NULL);
2364 g_return_if_fail (key != NULL);
2365 g_return_if_fail (locale != NULL);
2366 g_return_if_fail (length != 0);
2368 value_list = g_string_sized_new (length * 128);
2369 for (i = 0; i < length && list[i] != NULL; i++)
2371 gchar *value;
2373 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2374 g_string_append (value_list, value);
2375 g_string_append_c (value_list, key_file->list_separator);
2377 g_free (value);
2380 full_key = g_strdup_printf ("%s[%s]", key, locale);
2381 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2382 g_free (full_key);
2383 g_string_free (value_list, TRUE);
2387 * g_key_file_get_boolean:
2388 * @key_file: a #GKeyFile
2389 * @group_name: a group name
2390 * @key: a key
2391 * @error: return location for a #GError
2393 * Returns the value associated with @key under @group_name as a
2394 * boolean.
2396 * If @key cannot be found then %FALSE is returned and @error is set
2397 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2398 * associated with @key cannot be interpreted as a boolean then %FALSE
2399 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2401 * Returns: the value associated with the key as a boolean,
2402 * or %FALSE if the key was not found or could not be parsed.
2404 * Since: 2.6
2406 gboolean
2407 g_key_file_get_boolean (GKeyFile *key_file,
2408 const gchar *group_name,
2409 const gchar *key,
2410 GError **error)
2412 GError *key_file_error = NULL;
2413 gchar *value;
2414 gboolean bool_value;
2416 g_return_val_if_fail (key_file != NULL, FALSE);
2417 g_return_val_if_fail (group_name != NULL, FALSE);
2418 g_return_val_if_fail (key != NULL, FALSE);
2420 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2422 if (!value)
2424 g_propagate_error (error, key_file_error);
2425 return FALSE;
2428 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2429 &key_file_error);
2430 g_free (value);
2432 if (key_file_error)
2434 if (g_error_matches (key_file_error,
2435 G_KEY_FILE_ERROR,
2436 G_KEY_FILE_ERROR_INVALID_VALUE))
2438 g_set_error (error, G_KEY_FILE_ERROR,
2439 G_KEY_FILE_ERROR_INVALID_VALUE,
2440 _("Key file contains key “%s” "
2441 "which has a value that cannot be interpreted."),
2442 key);
2443 g_error_free (key_file_error);
2445 else
2446 g_propagate_error (error, key_file_error);
2449 return bool_value;
2453 * g_key_file_set_boolean:
2454 * @key_file: a #GKeyFile
2455 * @group_name: a group name
2456 * @key: a key
2457 * @value: %TRUE or %FALSE
2459 * Associates a new boolean value with @key under @group_name.
2460 * If @key cannot be found then it is created.
2462 * Since: 2.6
2464 void
2465 g_key_file_set_boolean (GKeyFile *key_file,
2466 const gchar *group_name,
2467 const gchar *key,
2468 gboolean value)
2470 gchar *result;
2472 g_return_if_fail (key_file != NULL);
2474 result = g_key_file_parse_boolean_as_value (key_file, value);
2475 g_key_file_set_value (key_file, group_name, key, result);
2476 g_free (result);
2480 * g_key_file_get_boolean_list:
2481 * @key_file: a #GKeyFile
2482 * @group_name: a group name
2483 * @key: a key
2484 * @length: (out): the number of booleans returned
2485 * @error: return location for a #GError
2487 * Returns the values associated with @key under @group_name as
2488 * booleans.
2490 * If @key cannot be found then %NULL is returned and @error is set to
2491 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2492 * with @key cannot be interpreted as booleans then %NULL is returned
2493 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2495 * Returns: (array length=length) (element-type gboolean) (transfer container):
2496 * the values associated with the key as a list of booleans, or %NULL if the
2497 * key was not found or could not be parsed. The returned list of booleans
2498 * should be freed with g_free() when no longer needed.
2500 * Since: 2.6
2502 gboolean *
2503 g_key_file_get_boolean_list (GKeyFile *key_file,
2504 const gchar *group_name,
2505 const gchar *key,
2506 gsize *length,
2507 GError **error)
2509 GError *key_file_error;
2510 gchar **values;
2511 gboolean *bool_values;
2512 gsize i, num_bools;
2514 g_return_val_if_fail (key_file != NULL, NULL);
2515 g_return_val_if_fail (group_name != NULL, NULL);
2516 g_return_val_if_fail (key != NULL, NULL);
2518 if (length)
2519 *length = 0;
2521 key_file_error = NULL;
2523 values = g_key_file_get_string_list (key_file, group_name, key,
2524 &num_bools, &key_file_error);
2526 if (key_file_error)
2527 g_propagate_error (error, key_file_error);
2529 if (!values)
2530 return NULL;
2532 bool_values = g_new (gboolean, num_bools);
2534 for (i = 0; i < num_bools; i++)
2536 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2537 values[i],
2538 &key_file_error);
2540 if (key_file_error)
2542 g_propagate_error (error, key_file_error);
2543 g_strfreev (values);
2544 g_free (bool_values);
2546 return NULL;
2549 g_strfreev (values);
2551 if (length)
2552 *length = num_bools;
2554 return bool_values;
2558 * g_key_file_set_boolean_list:
2559 * @key_file: a #GKeyFile
2560 * @group_name: a group name
2561 * @key: a key
2562 * @list: (array length=length): an array of boolean values
2563 * @length: length of @list
2565 * Associates a list of boolean values with @key under @group_name.
2566 * If @key cannot be found then it is created.
2567 * If @group_name is %NULL, the start_group is used.
2569 * Since: 2.6
2571 void
2572 g_key_file_set_boolean_list (GKeyFile *key_file,
2573 const gchar *group_name,
2574 const gchar *key,
2575 gboolean list[],
2576 gsize length)
2578 GString *value_list;
2579 gsize i;
2581 g_return_if_fail (key_file != NULL);
2582 g_return_if_fail (list != NULL);
2584 value_list = g_string_sized_new (length * 8);
2585 for (i = 0; i < length; i++)
2587 gchar *value;
2589 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2591 g_string_append (value_list, value);
2592 g_string_append_c (value_list, key_file->list_separator);
2594 g_free (value);
2597 g_key_file_set_value (key_file, group_name, key, value_list->str);
2598 g_string_free (value_list, TRUE);
2602 * g_key_file_get_integer:
2603 * @key_file: a #GKeyFile
2604 * @group_name: a group name
2605 * @key: a key
2606 * @error: return location for a #GError
2608 * Returns the value associated with @key under @group_name as an
2609 * integer.
2611 * If @key cannot be found then 0 is returned and @error is set to
2612 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2613 * with @key cannot be interpreted as an integer, or is out of range
2614 * for a #gint, then 0 is returned
2615 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2617 * Returns: the value associated with the key as an integer, or
2618 * 0 if the key was not found or could not be parsed.
2620 * Since: 2.6
2622 gint
2623 g_key_file_get_integer (GKeyFile *key_file,
2624 const gchar *group_name,
2625 const gchar *key,
2626 GError **error)
2628 GError *key_file_error;
2629 gchar *value;
2630 gint int_value;
2632 g_return_val_if_fail (key_file != NULL, -1);
2633 g_return_val_if_fail (group_name != NULL, -1);
2634 g_return_val_if_fail (key != NULL, -1);
2636 key_file_error = NULL;
2638 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2640 if (key_file_error)
2642 g_propagate_error (error, key_file_error);
2643 return 0;
2646 int_value = g_key_file_parse_value_as_integer (key_file, value,
2647 &key_file_error);
2648 g_free (value);
2650 if (key_file_error)
2652 if (g_error_matches (key_file_error,
2653 G_KEY_FILE_ERROR,
2654 G_KEY_FILE_ERROR_INVALID_VALUE))
2656 g_set_error (error, G_KEY_FILE_ERROR,
2657 G_KEY_FILE_ERROR_INVALID_VALUE,
2658 _("Key file contains key “%s” in group “%s” "
2659 "which has a value that cannot be interpreted."),
2660 key, group_name);
2661 g_error_free (key_file_error);
2663 else
2664 g_propagate_error (error, key_file_error);
2667 return int_value;
2671 * g_key_file_set_integer:
2672 * @key_file: a #GKeyFile
2673 * @group_name: a group name
2674 * @key: a key
2675 * @value: an integer value
2677 * Associates a new integer value with @key under @group_name.
2678 * If @key cannot be found then it is created.
2680 * Since: 2.6
2682 void
2683 g_key_file_set_integer (GKeyFile *key_file,
2684 const gchar *group_name,
2685 const gchar *key,
2686 gint value)
2688 gchar *result;
2690 g_return_if_fail (key_file != NULL);
2692 result = g_key_file_parse_integer_as_value (key_file, value);
2693 g_key_file_set_value (key_file, group_name, key, result);
2694 g_free (result);
2698 * g_key_file_get_int64:
2699 * @key_file: a non-%NULL #GKeyFile
2700 * @group_name: a non-%NULL group name
2701 * @key: a non-%NULL key
2702 * @error: return location for a #GError
2704 * Returns the value associated with @key under @group_name as a signed
2705 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2706 * 64-bit results without truncation.
2708 * Returns: the value associated with the key as a signed 64-bit integer, or
2709 * 0 if the key was not found or could not be parsed.
2711 * Since: 2.26
2713 gint64
2714 g_key_file_get_int64 (GKeyFile *key_file,
2715 const gchar *group_name,
2716 const gchar *key,
2717 GError **error)
2719 gchar *s, *end;
2720 gint64 v;
2722 g_return_val_if_fail (key_file != NULL, -1);
2723 g_return_val_if_fail (group_name != NULL, -1);
2724 g_return_val_if_fail (key != NULL, -1);
2726 s = g_key_file_get_value (key_file, group_name, key, error);
2728 if (s == NULL)
2729 return 0;
2731 v = g_ascii_strtoll (s, &end, 10);
2733 if (*s == '\0' || *end != '\0')
2735 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2736 _("Key “%s” in group “%s” has value “%s” "
2737 "where %s was expected"),
2738 key, group_name, s, "int64");
2739 g_free (s);
2740 return 0;
2743 g_free (s);
2744 return v;
2748 * g_key_file_set_int64:
2749 * @key_file: a #GKeyFile
2750 * @group_name: a group name
2751 * @key: a key
2752 * @value: an integer value
2754 * Associates a new integer value with @key under @group_name.
2755 * If @key cannot be found then it is created.
2757 * Since: 2.26
2759 void
2760 g_key_file_set_int64 (GKeyFile *key_file,
2761 const gchar *group_name,
2762 const gchar *key,
2763 gint64 value)
2765 gchar *result;
2767 g_return_if_fail (key_file != NULL);
2769 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2770 g_key_file_set_value (key_file, group_name, key, result);
2771 g_free (result);
2775 * g_key_file_get_uint64:
2776 * @key_file: a non-%NULL #GKeyFile
2777 * @group_name: a non-%NULL group name
2778 * @key: a non-%NULL key
2779 * @error: return location for a #GError
2781 * Returns the value associated with @key under @group_name as an unsigned
2782 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2783 * large positive results without truncation.
2785 * Returns: the value associated with the key as an unsigned 64-bit integer,
2786 * or 0 if the key was not found or could not be parsed.
2788 * Since: 2.26
2790 guint64
2791 g_key_file_get_uint64 (GKeyFile *key_file,
2792 const gchar *group_name,
2793 const gchar *key,
2794 GError **error)
2796 gchar *s, *end;
2797 guint64 v;
2799 g_return_val_if_fail (key_file != NULL, -1);
2800 g_return_val_if_fail (group_name != NULL, -1);
2801 g_return_val_if_fail (key != NULL, -1);
2803 s = g_key_file_get_value (key_file, group_name, key, error);
2805 if (s == NULL)
2806 return 0;
2808 v = g_ascii_strtoull (s, &end, 10);
2810 if (*s == '\0' || *end != '\0')
2812 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2813 _("Key “%s” in group “%s” has value “%s” "
2814 "where %s was expected"),
2815 key, group_name, s, "uint64");
2816 g_free (s);
2817 return 0;
2820 g_free (s);
2821 return v;
2825 * g_key_file_set_uint64:
2826 * @key_file: a #GKeyFile
2827 * @group_name: a group name
2828 * @key: a key
2829 * @value: an integer value
2831 * Associates a new integer value with @key under @group_name.
2832 * If @key cannot be found then it is created.
2834 * Since: 2.26
2836 void
2837 g_key_file_set_uint64 (GKeyFile *key_file,
2838 const gchar *group_name,
2839 const gchar *key,
2840 guint64 value)
2842 gchar *result;
2844 g_return_if_fail (key_file != NULL);
2846 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2847 g_key_file_set_value (key_file, group_name, key, result);
2848 g_free (result);
2852 * g_key_file_get_integer_list:
2853 * @key_file: a #GKeyFile
2854 * @group_name: a group name
2855 * @key: a key
2856 * @length: (out): the number of integers returned
2857 * @error: return location for a #GError
2859 * Returns the values associated with @key under @group_name as
2860 * integers.
2862 * If @key cannot be found then %NULL is returned and @error is set to
2863 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2864 * with @key cannot be interpreted as integers, or are out of range for
2865 * #gint, then %NULL is returned
2866 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2868 * Returns: (array length=length) (element-type gint) (transfer container):
2869 * the values associated with the key as a list of integers, or %NULL if
2870 * the key was not found or could not be parsed. The returned list of
2871 * integers should be freed with g_free() when no longer needed.
2873 * Since: 2.6
2875 gint *
2876 g_key_file_get_integer_list (GKeyFile *key_file,
2877 const gchar *group_name,
2878 const gchar *key,
2879 gsize *length,
2880 GError **error)
2882 GError *key_file_error = NULL;
2883 gchar **values;
2884 gint *int_values;
2885 gsize i, num_ints;
2887 g_return_val_if_fail (key_file != NULL, NULL);
2888 g_return_val_if_fail (group_name != NULL, NULL);
2889 g_return_val_if_fail (key != NULL, NULL);
2891 if (length)
2892 *length = 0;
2894 values = g_key_file_get_string_list (key_file, group_name, key,
2895 &num_ints, &key_file_error);
2897 if (key_file_error)
2898 g_propagate_error (error, key_file_error);
2900 if (!values)
2901 return NULL;
2903 int_values = g_new (gint, num_ints);
2905 for (i = 0; i < num_ints; i++)
2907 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2908 values[i],
2909 &key_file_error);
2911 if (key_file_error)
2913 g_propagate_error (error, key_file_error);
2914 g_strfreev (values);
2915 g_free (int_values);
2917 return NULL;
2920 g_strfreev (values);
2922 if (length)
2923 *length = num_ints;
2925 return int_values;
2929 * g_key_file_set_integer_list:
2930 * @key_file: a #GKeyFile
2931 * @group_name: a group name
2932 * @key: a key
2933 * @list: (array length=length): an array of integer values
2934 * @length: number of integer values in @list
2936 * Associates a list of integer values with @key under @group_name.
2937 * If @key cannot be found then it is created.
2939 * Since: 2.6
2941 void
2942 g_key_file_set_integer_list (GKeyFile *key_file,
2943 const gchar *group_name,
2944 const gchar *key,
2945 gint list[],
2946 gsize length)
2948 GString *values;
2949 gsize i;
2951 g_return_if_fail (key_file != NULL);
2952 g_return_if_fail (list != NULL);
2954 values = g_string_sized_new (length * 16);
2955 for (i = 0; i < length; i++)
2957 gchar *value;
2959 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2961 g_string_append (values, value);
2962 g_string_append_c (values, key_file->list_separator);
2964 g_free (value);
2967 g_key_file_set_value (key_file, group_name, key, values->str);
2968 g_string_free (values, TRUE);
2972 * g_key_file_get_double:
2973 * @key_file: a #GKeyFile
2974 * @group_name: a group name
2975 * @key: a key
2976 * @error: return location for a #GError
2978 * Returns the value associated with @key under @group_name as a
2979 * double. If @group_name is %NULL, the start_group is used.
2981 * If @key cannot be found then 0.0 is returned and @error is set to
2982 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2983 * with @key cannot be interpreted as a double then 0.0 is returned
2984 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2986 * Returns: the value associated with the key as a double, or
2987 * 0.0 if the key was not found or could not be parsed.
2989 * Since: 2.12
2991 gdouble
2992 g_key_file_get_double (GKeyFile *key_file,
2993 const gchar *group_name,
2994 const gchar *key,
2995 GError **error)
2997 GError *key_file_error;
2998 gchar *value;
2999 gdouble double_value;
3001 g_return_val_if_fail (key_file != NULL, -1);
3002 g_return_val_if_fail (group_name != NULL, -1);
3003 g_return_val_if_fail (key != NULL, -1);
3005 key_file_error = NULL;
3007 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
3009 if (key_file_error)
3011 g_propagate_error (error, key_file_error);
3012 return 0;
3015 double_value = g_key_file_parse_value_as_double (key_file, value,
3016 &key_file_error);
3017 g_free (value);
3019 if (key_file_error)
3021 if (g_error_matches (key_file_error,
3022 G_KEY_FILE_ERROR,
3023 G_KEY_FILE_ERROR_INVALID_VALUE))
3025 g_set_error (error, G_KEY_FILE_ERROR,
3026 G_KEY_FILE_ERROR_INVALID_VALUE,
3027 _("Key file contains key “%s” in group “%s” "
3028 "which has a value that cannot be interpreted."),
3029 key, group_name);
3030 g_error_free (key_file_error);
3032 else
3033 g_propagate_error (error, key_file_error);
3036 return double_value;
3040 * g_key_file_set_double:
3041 * @key_file: a #GKeyFile
3042 * @group_name: a group name
3043 * @key: a key
3044 * @value: an double value
3046 * Associates a new double value with @key under @group_name.
3047 * If @key cannot be found then it is created.
3049 * Since: 2.12
3051 void
3052 g_key_file_set_double (GKeyFile *key_file,
3053 const gchar *group_name,
3054 const gchar *key,
3055 gdouble value)
3057 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3059 g_return_if_fail (key_file != NULL);
3061 g_ascii_dtostr (result, sizeof (result), value);
3062 g_key_file_set_value (key_file, group_name, key, result);
3066 * g_key_file_get_double_list:
3067 * @key_file: a #GKeyFile
3068 * @group_name: a group name
3069 * @key: a key
3070 * @length: (out): the number of doubles returned
3071 * @error: return location for a #GError
3073 * Returns the values associated with @key under @group_name as
3074 * doubles.
3076 * If @key cannot be found then %NULL is returned and @error is set to
3077 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
3078 * with @key cannot be interpreted as doubles then %NULL is returned
3079 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
3081 * Returns: (array length=length) (element-type gdouble) (transfer container):
3082 * the values associated with the key as a list of doubles, or %NULL if the
3083 * key was not found or could not be parsed. The returned list of doubles
3084 * should be freed with g_free() when no longer needed.
3086 * Since: 2.12
3088 gdouble *
3089 g_key_file_get_double_list (GKeyFile *key_file,
3090 const gchar *group_name,
3091 const gchar *key,
3092 gsize *length,
3093 GError **error)
3095 GError *key_file_error = NULL;
3096 gchar **values;
3097 gdouble *double_values;
3098 gsize i, num_doubles;
3100 g_return_val_if_fail (key_file != NULL, NULL);
3101 g_return_val_if_fail (group_name != NULL, NULL);
3102 g_return_val_if_fail (key != NULL, NULL);
3104 if (length)
3105 *length = 0;
3107 values = g_key_file_get_string_list (key_file, group_name, key,
3108 &num_doubles, &key_file_error);
3110 if (key_file_error)
3111 g_propagate_error (error, key_file_error);
3113 if (!values)
3114 return NULL;
3116 double_values = g_new (gdouble, num_doubles);
3118 for (i = 0; i < num_doubles; i++)
3120 double_values[i] = g_key_file_parse_value_as_double (key_file,
3121 values[i],
3122 &key_file_error);
3124 if (key_file_error)
3126 g_propagate_error (error, key_file_error);
3127 g_strfreev (values);
3128 g_free (double_values);
3130 return NULL;
3133 g_strfreev (values);
3135 if (length)
3136 *length = num_doubles;
3138 return double_values;
3142 * g_key_file_set_double_list:
3143 * @key_file: a #GKeyFile
3144 * @group_name: a group name
3145 * @key: a key
3146 * @list: (array length=length): an array of double values
3147 * @length: number of double values in @list
3149 * Associates a list of double values with @key under
3150 * @group_name. If @key cannot be found then it is created.
3152 * Since: 2.12
3154 void
3155 g_key_file_set_double_list (GKeyFile *key_file,
3156 const gchar *group_name,
3157 const gchar *key,
3158 gdouble list[],
3159 gsize length)
3161 GString *values;
3162 gsize i;
3164 g_return_if_fail (key_file != NULL);
3165 g_return_if_fail (list != NULL);
3167 values = g_string_sized_new (length * 16);
3168 for (i = 0; i < length; i++)
3170 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3172 g_ascii_dtostr( result, sizeof (result), list[i] );
3174 g_string_append (values, result);
3175 g_string_append_c (values, key_file->list_separator);
3178 g_key_file_set_value (key_file, group_name, key, values->str);
3179 g_string_free (values, TRUE);
3182 static gboolean
3183 g_key_file_set_key_comment (GKeyFile *key_file,
3184 const gchar *group_name,
3185 const gchar *key,
3186 const gchar *comment,
3187 GError **error)
3189 GKeyFileGroup *group;
3190 GKeyFileKeyValuePair *pair;
3191 GList *key_node, *comment_node, *tmp;
3193 group = g_key_file_lookup_group (key_file, group_name);
3194 if (!group)
3196 g_set_error (error, G_KEY_FILE_ERROR,
3197 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3198 _("Key file does not have group “%s”"),
3199 group_name ? group_name : "(null)");
3201 return FALSE;
3204 /* First find the key the comments are supposed to be
3205 * associated with
3207 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3209 if (key_node == NULL)
3211 set_not_found_key_error (group->name, key, error);
3212 return FALSE;
3215 /* Then find all the comments already associated with the
3216 * key and free them
3218 tmp = key_node->next;
3219 while (tmp != NULL)
3221 pair = (GKeyFileKeyValuePair *) tmp->data;
3223 if (pair->key != NULL)
3224 break;
3226 comment_node = tmp;
3227 tmp = tmp->next;
3228 g_key_file_remove_key_value_pair_node (key_file, group,
3229 comment_node);
3232 if (comment == NULL)
3233 return TRUE;
3235 /* Now we can add our new comment
3237 pair = g_slice_new (GKeyFileKeyValuePair);
3238 pair->key = NULL;
3239 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3241 key_node = g_list_insert (key_node, pair, 1);
3243 return TRUE;
3246 static gboolean
3247 g_key_file_set_group_comment (GKeyFile *key_file,
3248 const gchar *group_name,
3249 const gchar *comment,
3250 GError **error)
3252 GKeyFileGroup *group;
3254 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3256 group = g_key_file_lookup_group (key_file, group_name);
3257 if (!group)
3259 g_set_error (error, G_KEY_FILE_ERROR,
3260 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3261 _("Key file does not have group “%s”"),
3262 group_name ? group_name : "(null)");
3264 return FALSE;
3267 /* First remove any existing comment
3269 if (group->comment)
3271 g_key_file_key_value_pair_free (group->comment);
3272 group->comment = NULL;
3275 if (comment == NULL)
3276 return TRUE;
3278 /* Now we can add our new comment
3280 group->comment = g_slice_new (GKeyFileKeyValuePair);
3281 group->comment->key = NULL;
3282 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3284 return TRUE;
3287 static gboolean
3288 g_key_file_set_top_comment (GKeyFile *key_file,
3289 const gchar *comment,
3290 GError **error)
3292 GList *group_node;
3293 GKeyFileGroup *group;
3294 GKeyFileKeyValuePair *pair;
3296 /* The last group in the list should be the top (comments only)
3297 * group in the file
3299 g_warn_if_fail (key_file->groups != NULL);
3300 group_node = g_list_last (key_file->groups);
3301 group = (GKeyFileGroup *) group_node->data;
3302 g_warn_if_fail (group->name == NULL);
3304 /* Note all keys must be comments at the top of
3305 * the file, so we can just free it all.
3307 g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3308 group->key_value_pairs = NULL;
3310 if (comment == NULL)
3311 return TRUE;
3313 pair = g_slice_new (GKeyFileKeyValuePair);
3314 pair->key = NULL;
3315 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3317 group->key_value_pairs =
3318 g_list_prepend (group->key_value_pairs, pair);
3320 return TRUE;
3324 * g_key_file_set_comment:
3325 * @key_file: a #GKeyFile
3326 * @group_name: (nullable): a group name, or %NULL
3327 * @key: (nullable): a key
3328 * @comment: a comment
3329 * @error: return location for a #GError
3331 * Places a comment above @key from @group_name.
3333 * If @key is %NULL then @comment will be written above @group_name.
3334 * If both @key and @group_name are %NULL, then @comment will be
3335 * written above the first group in the file.
3337 * Note that this function prepends a '#' comment marker to
3338 * each line of @comment.
3340 * Returns: %TRUE if the comment was written, %FALSE otherwise
3342 * Since: 2.6
3344 gboolean
3345 g_key_file_set_comment (GKeyFile *key_file,
3346 const gchar *group_name,
3347 const gchar *key,
3348 const gchar *comment,
3349 GError **error)
3351 g_return_val_if_fail (key_file != NULL, FALSE);
3353 if (group_name != NULL && key != NULL)
3355 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3356 return FALSE;
3358 else if (group_name != NULL)
3360 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3361 return FALSE;
3363 else
3365 if (!g_key_file_set_top_comment (key_file, comment, error))
3366 return FALSE;
3369 return TRUE;
3372 static gchar *
3373 g_key_file_get_key_comment (GKeyFile *key_file,
3374 const gchar *group_name,
3375 const gchar *key,
3376 GError **error)
3378 GKeyFileGroup *group;
3379 GKeyFileKeyValuePair *pair;
3380 GList *key_node, *tmp;
3381 GString *string;
3382 gchar *comment;
3384 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3386 group = g_key_file_lookup_group (key_file, group_name);
3387 if (!group)
3389 g_set_error (error, G_KEY_FILE_ERROR,
3390 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3391 _("Key file does not have group “%s”"),
3392 group_name ? group_name : "(null)");
3394 return NULL;
3397 /* First find the key the comments are supposed to be
3398 * associated with
3400 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3402 if (key_node == NULL)
3404 set_not_found_key_error (group->name, key, error);
3405 return NULL;
3408 string = NULL;
3410 /* Then find all the comments already associated with the
3411 * key and concatentate them.
3413 tmp = key_node->next;
3414 if (!key_node->next)
3415 return NULL;
3417 pair = (GKeyFileKeyValuePair *) tmp->data;
3418 if (pair->key != NULL)
3419 return NULL;
3421 while (tmp->next)
3423 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3425 if (pair->key != NULL)
3426 break;
3428 tmp = tmp->next;
3431 while (tmp != key_node)
3433 pair = (GKeyFileKeyValuePair *) tmp->data;
3435 if (string == NULL)
3436 string = g_string_sized_new (512);
3438 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3439 g_string_append (string, comment);
3440 g_free (comment);
3442 tmp = tmp->prev;
3445 if (string != NULL)
3447 comment = string->str;
3448 g_string_free (string, FALSE);
3450 else
3451 comment = NULL;
3453 return comment;
3456 static gchar *
3457 get_group_comment (GKeyFile *key_file,
3458 GKeyFileGroup *group,
3459 GError **error)
3461 GString *string;
3462 GList *tmp;
3463 gchar *comment;
3465 string = NULL;
3467 tmp = group->key_value_pairs;
3468 while (tmp)
3470 GKeyFileKeyValuePair *pair;
3472 pair = (GKeyFileKeyValuePair *) tmp->data;
3474 if (pair->key != NULL)
3476 tmp = tmp->prev;
3477 break;
3480 if (tmp->next == NULL)
3481 break;
3483 tmp = tmp->next;
3486 while (tmp != NULL)
3488 GKeyFileKeyValuePair *pair;
3490 pair = (GKeyFileKeyValuePair *) tmp->data;
3492 if (string == NULL)
3493 string = g_string_sized_new (512);
3495 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3496 g_string_append (string, comment);
3497 g_free (comment);
3499 tmp = tmp->prev;
3502 if (string != NULL)
3503 return g_string_free (string, FALSE);
3505 return NULL;
3508 static gchar *
3509 g_key_file_get_group_comment (GKeyFile *key_file,
3510 const gchar *group_name,
3511 GError **error)
3513 GList *group_node;
3514 GKeyFileGroup *group;
3516 group = g_key_file_lookup_group (key_file, group_name);
3517 if (!group)
3519 g_set_error (error, G_KEY_FILE_ERROR,
3520 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3521 _("Key file does not have group “%s”"),
3522 group_name ? group_name : "(null)");
3524 return NULL;
3527 if (group->comment)
3528 return g_strdup (group->comment->value);
3530 group_node = g_key_file_lookup_group_node (key_file, group_name);
3531 group_node = group_node->next;
3532 group = (GKeyFileGroup *)group_node->data;
3533 return get_group_comment (key_file, group, error);
3536 static gchar *
3537 g_key_file_get_top_comment (GKeyFile *key_file,
3538 GError **error)
3540 GList *group_node;
3541 GKeyFileGroup *group;
3543 /* The last group in the list should be the top (comments only)
3544 * group in the file
3546 g_warn_if_fail (key_file->groups != NULL);
3547 group_node = g_list_last (key_file->groups);
3548 group = (GKeyFileGroup *) group_node->data;
3549 g_warn_if_fail (group->name == NULL);
3551 return get_group_comment (key_file, group, error);
3555 * g_key_file_get_comment:
3556 * @key_file: a #GKeyFile
3557 * @group_name: (nullable): a group name, or %NULL
3558 * @key: a key
3559 * @error: return location for a #GError
3561 * Retrieves a comment above @key from @group_name.
3562 * If @key is %NULL then @comment will be read from above
3563 * @group_name. If both @key and @group_name are %NULL, then
3564 * @comment will be read from above the first group in the file.
3566 * Note that the returned string includes the '#' comment markers.
3568 * Returns: a comment that should be freed with g_free()
3570 * Since: 2.6
3572 gchar *
3573 g_key_file_get_comment (GKeyFile *key_file,
3574 const gchar *group_name,
3575 const gchar *key,
3576 GError **error)
3578 g_return_val_if_fail (key_file != NULL, NULL);
3580 if (group_name != NULL && key != NULL)
3581 return g_key_file_get_key_comment (key_file, group_name, key, error);
3582 else if (group_name != NULL)
3583 return g_key_file_get_group_comment (key_file, group_name, error);
3584 else
3585 return g_key_file_get_top_comment (key_file, error);
3589 * g_key_file_remove_comment:
3590 * @key_file: a #GKeyFile
3591 * @group_name: (nullable): a group name, or %NULL
3592 * @key: (nullable): a key
3593 * @error: return location for a #GError
3595 * Removes a comment above @key from @group_name.
3596 * If @key is %NULL then @comment will be removed above @group_name.
3597 * If both @key and @group_name are %NULL, then @comment will
3598 * be removed above the first group in the file.
3600 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3602 * Since: 2.6
3605 gboolean
3606 g_key_file_remove_comment (GKeyFile *key_file,
3607 const gchar *group_name,
3608 const gchar *key,
3609 GError **error)
3611 g_return_val_if_fail (key_file != NULL, FALSE);
3613 if (group_name != NULL && key != NULL)
3614 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3615 else if (group_name != NULL)
3616 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3617 else
3618 return g_key_file_set_top_comment (key_file, NULL, error);
3622 * g_key_file_has_group:
3623 * @key_file: a #GKeyFile
3624 * @group_name: a group name
3626 * Looks whether the key file has the group @group_name.
3628 * Returns: %TRUE if @group_name is a part of @key_file, %FALSE
3629 * otherwise.
3630 * Since: 2.6
3632 gboolean
3633 g_key_file_has_group (GKeyFile *key_file,
3634 const gchar *group_name)
3636 g_return_val_if_fail (key_file != NULL, FALSE);
3637 g_return_val_if_fail (group_name != NULL, FALSE);
3639 return g_key_file_lookup_group (key_file, group_name) != NULL;
3642 /* This code remains from a historical attempt to add a new public API
3643 * which respects the GError rules.
3645 static gboolean
3646 g_key_file_has_key_full (GKeyFile *key_file,
3647 const gchar *group_name,
3648 const gchar *key,
3649 gboolean *has_key,
3650 GError **error)
3652 GKeyFileKeyValuePair *pair;
3653 GKeyFileGroup *group;
3655 g_return_val_if_fail (key_file != NULL, FALSE);
3656 g_return_val_if_fail (group_name != NULL, FALSE);
3657 g_return_val_if_fail (key != NULL, FALSE);
3659 group = g_key_file_lookup_group (key_file, group_name);
3661 if (!group)
3663 g_set_error (error, G_KEY_FILE_ERROR,
3664 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3665 _("Key file does not have group “%s”"),
3666 group_name);
3668 return FALSE;
3671 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3673 if (has_key)
3674 *has_key = pair != NULL;
3675 return TRUE;
3679 * g_key_file_has_key: (skip)
3680 * @key_file: a #GKeyFile
3681 * @group_name: a group name
3682 * @key: a key name
3683 * @error: return location for a #GError
3685 * Looks whether the key file has the key @key in the group
3686 * @group_name.
3688 * Note that this function does not follow the rules for #GError strictly;
3689 * the return value both carries meaning and signals an error. To use
3690 * this function, you must pass a #GError pointer in @error, and check
3691 * whether it is not %NULL to see if an error occurred.
3693 * Language bindings should use g_key_file_get_value() to test whether
3694 * or not a key exists.
3696 * Returns: %TRUE if @key is a part of @group_name, %FALSE otherwise
3698 * Since: 2.6
3700 gboolean
3701 g_key_file_has_key (GKeyFile *key_file,
3702 const gchar *group_name,
3703 const gchar *key,
3704 GError **error)
3706 GError *temp_error = NULL;
3707 gboolean has_key;
3709 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3711 return has_key;
3713 else
3715 g_propagate_error (error, temp_error);
3716 return FALSE;
3720 static void
3721 g_key_file_add_group (GKeyFile *key_file,
3722 const gchar *group_name)
3724 GKeyFileGroup *group;
3726 g_return_if_fail (key_file != NULL);
3727 g_return_if_fail (g_key_file_is_group_name (group_name));
3729 group = g_key_file_lookup_group (key_file, group_name);
3730 if (group != NULL)
3732 key_file->current_group = group;
3733 return;
3736 group = g_slice_new0 (GKeyFileGroup);
3737 group->name = g_strdup (group_name);
3738 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3739 key_file->groups = g_list_prepend (key_file->groups, group);
3740 key_file->current_group = group;
3742 if (key_file->start_group == NULL)
3743 key_file->start_group = group;
3745 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3748 static void
3749 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3751 if (pair != NULL)
3753 g_free (pair->key);
3754 g_free (pair->value);
3755 g_slice_free (GKeyFileKeyValuePair, pair);
3759 /* Be careful not to call this function on a node with data in the
3760 * lookup map without removing it from the lookup map, first.
3762 * Some current cases where this warning is not a concern are
3763 * when:
3764 * - the node being removed is a comment node
3765 * - the entire lookup map is getting destroyed soon after
3766 * anyway.
3768 static void
3769 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3770 GKeyFileGroup *group,
3771 GList *pair_node)
3774 GKeyFileKeyValuePair *pair;
3776 pair = (GKeyFileKeyValuePair *) pair_node->data;
3778 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3780 g_warn_if_fail (pair->value != NULL);
3782 g_key_file_key_value_pair_free (pair);
3784 g_list_free_1 (pair_node);
3787 static void
3788 g_key_file_remove_group_node (GKeyFile *key_file,
3789 GList *group_node)
3791 GKeyFileGroup *group;
3792 GList *tmp;
3794 group = (GKeyFileGroup *) group_node->data;
3796 if (group->name)
3797 g_hash_table_remove (key_file->group_hash, group->name);
3799 /* If the current group gets deleted make the current group the last
3800 * added group.
3802 if (key_file->current_group == group)
3804 /* groups should always contain at least the top comment group,
3805 * unless g_key_file_clear has been called
3807 if (key_file->groups)
3808 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3809 else
3810 key_file->current_group = NULL;
3813 /* If the start group gets deleted make the start group the first
3814 * added group.
3816 if (key_file->start_group == group)
3818 tmp = g_list_last (key_file->groups);
3819 while (tmp != NULL)
3821 if (tmp != group_node &&
3822 ((GKeyFileGroup *) tmp->data)->name != NULL)
3823 break;
3825 tmp = tmp->prev;
3828 if (tmp)
3829 key_file->start_group = (GKeyFileGroup *) tmp->data;
3830 else
3831 key_file->start_group = NULL;
3834 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3836 tmp = group->key_value_pairs;
3837 while (tmp != NULL)
3839 GList *pair_node;
3841 pair_node = tmp;
3842 tmp = tmp->next;
3843 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3846 g_warn_if_fail (group->key_value_pairs == NULL);
3848 if (group->comment)
3850 g_key_file_key_value_pair_free (group->comment);
3851 group->comment = NULL;
3854 if (group->lookup_map)
3856 g_hash_table_destroy (group->lookup_map);
3857 group->lookup_map = NULL;
3860 g_free ((gchar *) group->name);
3861 g_slice_free (GKeyFileGroup, group);
3862 g_list_free_1 (group_node);
3866 * g_key_file_remove_group:
3867 * @key_file: a #GKeyFile
3868 * @group_name: a group name
3869 * @error: return location for a #GError or %NULL
3871 * Removes the specified group, @group_name,
3872 * from the key file.
3874 * Returns: %TRUE if the group was removed, %FALSE otherwise
3876 * Since: 2.6
3878 gboolean
3879 g_key_file_remove_group (GKeyFile *key_file,
3880 const gchar *group_name,
3881 GError **error)
3883 GList *group_node;
3885 g_return_val_if_fail (key_file != NULL, FALSE);
3886 g_return_val_if_fail (group_name != NULL, FALSE);
3888 group_node = g_key_file_lookup_group_node (key_file, group_name);
3890 if (!group_node)
3892 g_set_error (error, G_KEY_FILE_ERROR,
3893 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3894 _("Key file does not have group “%s”"),
3895 group_name);
3896 return FALSE;
3899 g_key_file_remove_group_node (key_file, group_node);
3901 return TRUE;
3904 static void
3905 g_key_file_add_key_value_pair (GKeyFile *key_file,
3906 GKeyFileGroup *group,
3907 GKeyFileKeyValuePair *pair)
3909 g_hash_table_replace (group->lookup_map, pair->key, pair);
3910 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3913 static void
3914 g_key_file_add_key (GKeyFile *key_file,
3915 GKeyFileGroup *group,
3916 const gchar *key,
3917 const gchar *value)
3919 GKeyFileKeyValuePair *pair;
3921 pair = g_slice_new (GKeyFileKeyValuePair);
3922 pair->key = g_strdup (key);
3923 pair->value = g_strdup (value);
3925 g_key_file_add_key_value_pair (key_file, group, pair);
3929 * g_key_file_remove_key:
3930 * @key_file: a #GKeyFile
3931 * @group_name: a group name
3932 * @key: a key name to remove
3933 * @error: return location for a #GError or %NULL
3935 * Removes @key in @group_name from the key file.
3937 * Returns: %TRUE if the key was removed, %FALSE otherwise
3939 * Since: 2.6
3941 gboolean
3942 g_key_file_remove_key (GKeyFile *key_file,
3943 const gchar *group_name,
3944 const gchar *key,
3945 GError **error)
3947 GKeyFileGroup *group;
3948 GKeyFileKeyValuePair *pair;
3950 g_return_val_if_fail (key_file != NULL, FALSE);
3951 g_return_val_if_fail (group_name != NULL, FALSE);
3952 g_return_val_if_fail (key != NULL, FALSE);
3954 pair = NULL;
3956 group = g_key_file_lookup_group (key_file, group_name);
3957 if (!group)
3959 g_set_error (error, G_KEY_FILE_ERROR,
3960 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3961 _("Key file does not have group “%s”"),
3962 group_name);
3963 return FALSE;
3966 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3968 if (!pair)
3970 set_not_found_key_error (group->name, key, error);
3971 return FALSE;
3974 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3975 g_hash_table_remove (group->lookup_map, pair->key);
3976 g_key_file_key_value_pair_free (pair);
3978 return TRUE;
3981 static GList *
3982 g_key_file_lookup_group_node (GKeyFile *key_file,
3983 const gchar *group_name)
3985 GKeyFileGroup *group;
3986 GList *tmp;
3988 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3990 group = (GKeyFileGroup *) tmp->data;
3992 if (group && group->name && strcmp (group->name, group_name) == 0)
3993 break;
3996 return tmp;
3999 static GKeyFileGroup *
4000 g_key_file_lookup_group (GKeyFile *key_file,
4001 const gchar *group_name)
4003 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
4006 static GList *
4007 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
4008 GKeyFileGroup *group,
4009 const gchar *key)
4011 GList *key_node;
4013 for (key_node = group->key_value_pairs;
4014 key_node != NULL;
4015 key_node = key_node->next)
4017 GKeyFileKeyValuePair *pair;
4019 pair = (GKeyFileKeyValuePair *) key_node->data;
4021 if (pair->key && strcmp (pair->key, key) == 0)
4022 break;
4025 return key_node;
4028 static GKeyFileKeyValuePair *
4029 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
4030 GKeyFileGroup *group,
4031 const gchar *key)
4033 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
4036 /* Lines starting with # or consisting entirely of whitespace are merely
4037 * recorded, not parsed. This function assumes all leading whitespace
4038 * has been stripped.
4040 static gboolean
4041 g_key_file_line_is_comment (const gchar *line)
4043 return (*line == '#' || *line == '\0' || *line == '\n');
4046 static gboolean
4047 g_key_file_is_group_name (const gchar *name)
4049 gchar *p, *q;
4051 if (name == NULL)
4052 return FALSE;
4054 p = q = (gchar *) name;
4055 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
4056 q = g_utf8_find_next_char (q, NULL);
4058 if (*q != '\0' || q == p)
4059 return FALSE;
4061 return TRUE;
4064 static gboolean
4065 g_key_file_is_key_name (const gchar *name)
4067 gchar *p, *q;
4069 if (name == NULL)
4070 return FALSE;
4072 p = q = (gchar *) name;
4073 /* We accept a little more than the desktop entry spec says,
4074 * since gnome-vfs uses mime-types as keys in its cache.
4076 while (*q && *q != '=' && *q != '[' && *q != ']')
4077 q = g_utf8_find_next_char (q, NULL);
4079 /* No empty keys, please */
4080 if (q == p)
4081 return FALSE;
4083 /* We accept spaces in the middle of keys to not break
4084 * existing apps, but we don't tolerate initial or final
4085 * spaces, which would lead to silent corruption when
4086 * rereading the file.
4088 if (*p == ' ' || q[-1] == ' ')
4089 return FALSE;
4091 if (*q == '[')
4093 q++;
4094 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
4095 q = g_utf8_find_next_char (q, NULL);
4097 if (*q != ']')
4098 return FALSE;
4100 q++;
4103 if (*q != '\0')
4104 return FALSE;
4106 return TRUE;
4109 /* A group in a key file is made up of a starting '[' followed by one
4110 * or more letters making up the group name followed by ']'.
4112 static gboolean
4113 g_key_file_line_is_group (const gchar *line)
4115 gchar *p;
4117 p = (gchar *) line;
4118 if (*p != '[')
4119 return FALSE;
4121 p++;
4123 while (*p && *p != ']')
4124 p = g_utf8_find_next_char (p, NULL);
4126 if (*p != ']')
4127 return FALSE;
4129 /* silently accept whitespace after the ] */
4130 p = g_utf8_find_next_char (p, NULL);
4131 while (*p == ' ' || *p == '\t')
4132 p = g_utf8_find_next_char (p, NULL);
4134 if (*p)
4135 return FALSE;
4137 return TRUE;
4140 static gboolean
4141 g_key_file_line_is_key_value_pair (const gchar *line)
4143 gchar *p;
4145 p = (gchar *) g_utf8_strchr (line, -1, '=');
4147 if (!p)
4148 return FALSE;
4150 /* Key must be non-empty
4152 if (*p == line[0])
4153 return FALSE;
4155 return TRUE;
4158 static gchar *
4159 g_key_file_parse_value_as_string (GKeyFile *key_file,
4160 const gchar *value,
4161 GSList **pieces,
4162 GError **error)
4164 gchar *string_value, *p, *q0, *q;
4166 string_value = g_new (gchar, strlen (value) + 1);
4168 p = (gchar *) value;
4169 q0 = q = string_value;
4170 while (*p)
4172 if (*p == '\\')
4174 p++;
4176 switch (*p)
4178 case 's':
4179 *q = ' ';
4180 break;
4182 case 'n':
4183 *q = '\n';
4184 break;
4186 case 't':
4187 *q = '\t';
4188 break;
4190 case 'r':
4191 *q = '\r';
4192 break;
4194 case '\\':
4195 *q = '\\';
4196 break;
4198 case '\0':
4199 g_set_error_literal (error, G_KEY_FILE_ERROR,
4200 G_KEY_FILE_ERROR_INVALID_VALUE,
4201 _("Key file contains escape character "
4202 "at end of line"));
4203 break;
4205 default:
4206 if (pieces && *p == key_file->list_separator)
4207 *q = key_file->list_separator;
4208 else
4210 *q++ = '\\';
4211 *q = *p;
4213 if (*error == NULL)
4215 gchar sequence[3];
4217 sequence[0] = '\\';
4218 sequence[1] = *p;
4219 sequence[2] = '\0';
4221 g_set_error (error, G_KEY_FILE_ERROR,
4222 G_KEY_FILE_ERROR_INVALID_VALUE,
4223 _("Key file contains invalid escape "
4224 "sequence “%s”"), sequence);
4227 break;
4230 else
4232 *q = *p;
4233 if (pieces && (*p == key_file->list_separator))
4235 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4236 q0 = q + 1;
4240 if (*p == '\0')
4241 break;
4243 q++;
4244 p++;
4247 *q = '\0';
4248 if (pieces)
4250 if (q0 < q)
4251 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4252 *pieces = g_slist_reverse (*pieces);
4255 return string_value;
4258 static gchar *
4259 g_key_file_parse_string_as_value (GKeyFile *key_file,
4260 const gchar *string,
4261 gboolean escape_separator)
4263 gchar *value, *p, *q;
4264 gsize length;
4265 gboolean parsing_leading_space;
4267 length = strlen (string) + 1;
4269 /* Worst case would be that every character needs to be escaped.
4270 * In other words every character turns to two characters
4272 value = g_new (gchar, 2 * length);
4274 p = (gchar *) string;
4275 q = value;
4276 parsing_leading_space = TRUE;
4277 while (p < (string + length - 1))
4279 gchar escaped_character[3] = { '\\', 0, 0 };
4281 switch (*p)
4283 case ' ':
4284 if (parsing_leading_space)
4286 escaped_character[1] = 's';
4287 strcpy (q, escaped_character);
4288 q += 2;
4290 else
4292 *q = *p;
4293 q++;
4295 break;
4296 case '\t':
4297 if (parsing_leading_space)
4299 escaped_character[1] = 't';
4300 strcpy (q, escaped_character);
4301 q += 2;
4303 else
4305 *q = *p;
4306 q++;
4308 break;
4309 case '\n':
4310 escaped_character[1] = 'n';
4311 strcpy (q, escaped_character);
4312 q += 2;
4313 break;
4314 case '\r':
4315 escaped_character[1] = 'r';
4316 strcpy (q, escaped_character);
4317 q += 2;
4318 break;
4319 case '\\':
4320 escaped_character[1] = '\\';
4321 strcpy (q, escaped_character);
4322 q += 2;
4323 parsing_leading_space = FALSE;
4324 break;
4325 default:
4326 if (escape_separator && *p == key_file->list_separator)
4328 escaped_character[1] = key_file->list_separator;
4329 strcpy (q, escaped_character);
4330 q += 2;
4331 parsing_leading_space = TRUE;
4333 else
4335 *q = *p;
4336 q++;
4337 parsing_leading_space = FALSE;
4339 break;
4341 p++;
4343 *q = '\0';
4345 return value;
4348 static gint
4349 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4350 const gchar *value,
4351 GError **error)
4353 gchar *eof_int;
4354 glong long_value;
4355 gint int_value;
4356 int errsv;
4358 errno = 0;
4359 long_value = strtol (value, &eof_int, 10);
4360 errsv = errno;
4362 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4364 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4365 g_set_error (error, G_KEY_FILE_ERROR,
4366 G_KEY_FILE_ERROR_INVALID_VALUE,
4367 _("Value “%s” cannot be interpreted "
4368 "as a number."), value_utf8);
4369 g_free (value_utf8);
4371 return 0;
4374 int_value = long_value;
4375 if (int_value != long_value || errsv == ERANGE)
4377 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4378 g_set_error (error,
4379 G_KEY_FILE_ERROR,
4380 G_KEY_FILE_ERROR_INVALID_VALUE,
4381 _("Integer value “%s” out of range"),
4382 value_utf8);
4383 g_free (value_utf8);
4385 return 0;
4388 return int_value;
4391 static gchar *
4392 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4393 gint value)
4396 return g_strdup_printf ("%d", value);
4399 static gdouble
4400 g_key_file_parse_value_as_double (GKeyFile *key_file,
4401 const gchar *value,
4402 GError **error)
4404 gchar *end_of_valid_d;
4405 gdouble double_value = 0;
4407 double_value = g_ascii_strtod (value, &end_of_valid_d);
4409 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4411 gchar *value_utf8 = g_utf8_make_valid (value, -1);
4412 g_set_error (error, G_KEY_FILE_ERROR,
4413 G_KEY_FILE_ERROR_INVALID_VALUE,
4414 _("Value “%s” cannot be interpreted "
4415 "as a float number."),
4416 value_utf8);
4417 g_free (value_utf8);
4419 double_value = 0;
4422 return double_value;
4425 static gint
4426 strcmp_sized (const gchar *s1, size_t len1, const gchar *s2)
4428 size_t len2 = strlen (s2);
4429 return strncmp (s1, s2, MAX (len1, len2));
4432 static gboolean
4433 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4434 const gchar *value,
4435 GError **error)
4437 gchar *value_utf8;
4438 gint i, length = 0;
4440 /* Count the number of non-whitespace characters */
4441 for (i = 0; value[i]; i++)
4442 if (!g_ascii_isspace (value[i]))
4443 length = i + 1;
4445 if (strcmp_sized (value, length, "true") == 0 || strcmp_sized (value, length, "1") == 0)
4446 return TRUE;
4447 else if (strcmp_sized (value, length, "false") == 0 || strcmp_sized (value, length, "0") == 0)
4448 return FALSE;
4450 value_utf8 = g_utf8_make_valid (value, -1);
4451 g_set_error (error, G_KEY_FILE_ERROR,
4452 G_KEY_FILE_ERROR_INVALID_VALUE,
4453 _("Value “%s” cannot be interpreted "
4454 "as a boolean."), value_utf8);
4455 g_free (value_utf8);
4457 return FALSE;
4460 static gchar *
4461 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4462 gboolean value)
4464 if (value)
4465 return g_strdup ("true");
4466 else
4467 return g_strdup ("false");
4470 static gchar *
4471 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4472 const gchar *value)
4474 GString *string;
4475 gchar **lines;
4476 gsize i;
4478 string = g_string_sized_new (512);
4480 lines = g_strsplit (value, "\n", 0);
4482 for (i = 0; lines[i] != NULL; i++)
4484 if (lines[i][0] != '#')
4485 g_string_append_printf (string, "%s\n", lines[i]);
4486 else
4487 g_string_append_printf (string, "%s\n", lines[i] + 1);
4489 g_strfreev (lines);
4491 return g_string_free (string, FALSE);
4494 static gchar *
4495 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4496 const gchar *comment)
4498 GString *string;
4499 gchar **lines;
4500 gsize i;
4502 string = g_string_sized_new (512);
4504 lines = g_strsplit (comment, "\n", 0);
4506 for (i = 0; lines[i] != NULL; i++)
4507 g_string_append_printf (string, "#%s%s", lines[i],
4508 lines[i + 1] == NULL? "" : "\n");
4509 g_strfreev (lines);
4511 return g_string_free (string, FALSE);
4515 * g_key_file_save_to_file:
4516 * @key_file: a #GKeyFile
4517 * @filename: the name of the file to write to
4518 * @error: a pointer to a %NULL #GError, or %NULL
4520 * Writes the contents of @key_file to @filename using
4521 * g_file_set_contents().
4523 * This function can fail for any of the reasons that
4524 * g_file_set_contents() may fail.
4526 * Returns: %TRUE if successful, else %FALSE with @error set
4528 * Since: 2.40
4530 gboolean
4531 g_key_file_save_to_file (GKeyFile *key_file,
4532 const gchar *filename,
4533 GError **error)
4535 gchar *contents;
4536 gboolean success;
4537 gsize length;
4539 g_return_val_if_fail (key_file != NULL, FALSE);
4540 g_return_val_if_fail (filename != NULL, FALSE);
4541 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4543 contents = g_key_file_to_data (key_file, &length, NULL);
4544 g_assert (contents != NULL);
4546 success = g_file_set_contents (filename, contents, length, error);
4547 g_free (contents);
4549 return success;