Updated Italian translation
[glib.git] / glib / gkeyfile.c
bloba33e686dfb5b61f1218ab19777518d4a602e0820
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 * GLib is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * GLib 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
21 * License along with GLib; see the file COPYING.LIB. If not,
22 * see <http://www.gnu.org/licenses/>.
25 #include "config.h"
27 #include "gkeyfile.h"
28 #include "gutils.h"
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <locale.h>
33 #include <string.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #ifdef G_OS_UNIX
39 #include <unistd.h>
40 #endif
41 #ifdef G_OS_WIN32
42 #include <io.h>
44 #undef fstat
45 #define fstat(a,b) _fstati64(a,b)
46 #undef stat
47 #define stat _stati64
49 #ifndef S_ISREG
50 #define S_ISREG(mode) ((mode)&_S_IFREG)
51 #endif
53 #endif /* G_OS_WIN23 */
55 #include "gconvert.h"
56 #include "gdataset.h"
57 #include "gerror.h"
58 #include "gfileutils.h"
59 #include "ghash.h"
60 #include "glibintl.h"
61 #include "glist.h"
62 #include "gslist.h"
63 #include "gmem.h"
64 #include "gmessages.h"
65 #include "gstdio.h"
66 #include "gstring.h"
67 #include "gstrfuncs.h"
68 #include "gutils.h"
71 /**
72 * SECTION:keyfile
73 * @title: Key-value file parser
74 * @short_description: parses .ini-like config files
76 * #GKeyFile lets you parse, edit or create files containing groups of
77 * key-value pairs, which we call "key files" for lack of a better name.
78 * Several freedesktop.org specifications use key files now, e.g the
79 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec)
80 * and the
81 * [Icon Theme Specification](http://freedesktop.org/Standards/icon-theme-spec).
83 * The syntax of key files is described in detail in the
84 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
85 * here is a quick summary: Key files
86 * consists of groups of key-value pairs, interspersed with comments.
88 * |[
89 * # this is just an example
90 * # there can be comments before the first group
92 * [First Group]
94 * Name=Key File Example\tthis value shows\nescaping
96 * # localized strings are stored in multiple key-value pairs
97 * Welcome=Hello
98 * Welcome[de]=Hallo
99 * Welcome[fr_FR]=Bonjour
100 * Welcome[it]=Ciao
101 * Welcome[be@latin]=Hello
103 * [Another Group]
105 * Numbers=2;20;-200;0
107 * Booleans=true;false;true;true
108 * ]|
110 * Lines beginning with a '#' and blank lines are considered comments.
112 * Groups are started by a header line containing the group name enclosed
113 * in '[' and ']', and ended implicitly by the start of the next group or
114 * the end of the file. Each key-value pair must be contained in a group.
116 * Key-value pairs generally have the form `key=value`, with the
117 * exception of localized strings, which have the form
118 * `key[locale]=value`, with a locale identifier of the
119 * form `lang_COUNTRY@MODIFIER` where `COUNTRY` and `MODIFIER`
120 * are optional.
121 * Space before and after the '=' character are ignored. Newline, tab,
122 * carriage return and backslash characters in value are escaped as \n,
123 * \t, \r, and \\, respectively. To preserve leading spaces in values,
124 * these can also be escaped as \s.
126 * Key files can store strings (possibly with localized variants), integers,
127 * booleans and lists of these. Lists are separated by a separator character,
128 * typically ';' or ','. To use the list separator character in a value in
129 * a list, it has to be escaped by prefixing it with a backslash.
131 * This syntax is obviously inspired by the .ini files commonly met
132 * on Windows, but there are some important differences:
134 * - .ini files use the ';' character to begin comments,
135 * key files use the '#' character.
137 * - Key files do not allow for ungrouped keys meaning only
138 * comments can precede the first group.
140 * - Key files are always encoded in UTF-8.
142 * - Key and Group names are case-sensitive. For example, a group called
143 * [GROUP] is a different from [group].
145 * - .ini files don't have a strongly typed boolean entry type,
146 * they only have GetProfileInt(). In key files, only
147 * true and false (in lower case) are allowed.
149 * Note that in contrast to the
150 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec),
151 * groups in key files may contain the same
152 * key multiple times; the last entry wins. Key files may also contain
153 * multiple groups with the same name; they are merged together.
154 * Another difference is that keys and group names in key files are not
155 * restricted to ASCII characters.
159 * G_KEY_FILE_ERROR:
161 * Error domain for key file parsing. Errors in this domain will
162 * be from the #GKeyFileError enumeration.
164 * See #GError for information on error domains.
168 * GKeyFileError:
169 * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
170 * an unknown encoding
171 * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
172 * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
173 * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
174 * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
175 * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
177 * Error codes returned by key file parsing.
181 * GKeyFileFlags:
182 * @G_KEY_FILE_NONE: No flags, default behaviour
183 * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
184 * (possibly modified) contents of the key file back to a file;
185 * otherwise all comments will be lost when the key file is
186 * written back.
187 * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
188 * (possibly modified) contents of the key file back to a file;
189 * otherwise only the translations for the current language will be
190 * written back.
192 * Flags which influence the parsing.
196 * G_KEY_FILE_DESKTOP_GROUP:
198 * The name of the main group of a desktop entry file, as defined in the
199 * [Desktop Entry Specification](http://freedesktop.org/Standards/desktop-entry-spec).
200 * Consult the specification for more
201 * details about the meanings of the keys below.
203 * Since: 2.14
207 * G_KEY_FILE_DESKTOP_KEY_TYPE:
209 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
210 * giving the type of the desktop entry. Usually
211 * #G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
212 * #G_KEY_FILE_DESKTOP_TYPE_LINK, or
213 * #G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
215 * Since: 2.14
219 * G_KEY_FILE_DESKTOP_KEY_VERSION:
221 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
222 * giving the version of the Desktop Entry Specification used for
223 * the desktop entry file.
225 * Since: 2.14
229 * G_KEY_FILE_DESKTOP_KEY_NAME:
231 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
232 * string giving the specific name of the desktop entry.
234 * Since: 2.14
238 * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
240 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
241 * string giving the generic name of the desktop entry.
243 * Since: 2.14
247 * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
249 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
250 * stating whether the desktop entry should be shown in menus.
252 * Since: 2.14
256 * G_KEY_FILE_DESKTOP_KEY_COMMENT:
258 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
259 * string giving the tooltip for the desktop entry.
261 * Since: 2.14
265 * G_KEY_FILE_DESKTOP_KEY_ICON:
267 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
268 * string giving the name of the icon to be displayed for the desktop
269 * entry.
271 * Since: 2.14
275 * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
277 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
278 * stating whether the desktop entry has been deleted by the user.
280 * Since: 2.14
284 * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
286 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
287 * strings identifying the environments that should display the
288 * desktop entry.
290 * Since: 2.14
294 * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
296 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
297 * strings identifying the environments that should not display the
298 * desktop entry.
300 * Since: 2.14
304 * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
306 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
307 * giving the file name of a binary on disk used to determine if the
308 * program is actually installed. It is only valid for desktop entries
309 * with the `Application` type.
311 * Since: 2.14
315 * G_KEY_FILE_DESKTOP_KEY_EXEC:
317 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
318 * giving the command line to execute. It is only valid for desktop
319 * entries with the `Application` type.
321 * Since: 2.14
325 * G_KEY_FILE_DESKTOP_KEY_PATH:
327 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
328 * containing the working directory to run the program in. It is only
329 * valid for desktop entries with the `Application` type.
331 * Since: 2.14
335 * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
337 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
338 * stating whether the program should be run in a terminal window.
339 * It is only valid for desktop entries with the
340 * `Application` type.
342 * Since: 2.14
346 * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
348 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
349 * of strings giving the MIME types supported by this desktop entry.
351 * Since: 2.14
355 * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
357 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
358 * of strings giving the categories in which the desktop entry
359 * should be shown in a menu.
361 * Since: 2.14
365 * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
367 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
368 * stating whether the application supports the
369 * [Startup Notification Protocol Specification](http://www.freedesktop.org/Standards/startup-notification-spec).
371 * Since: 2.14
375 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
377 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is string
378 * identifying the WM class or name hint of a window that the application
379 * will create, which can be used to emulate Startup Notification with
380 * older applications.
382 * Since: 2.14
386 * G_KEY_FILE_DESKTOP_KEY_URL:
388 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
389 * giving the URL to access. It is only valid for desktop entries
390 * with the `Link` type.
392 * Since: 2.14
396 * G_KEY_FILE_DESKTOP_KEY_DBUS_ACTIVATABLE:
398 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean set to true
399 * if the application is D-Bus activatable.
401 * Since: 2.38
405 * G_KEY_FILE_DESKTOP_KEY_ACTIONS:
407 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string list
408 * giving the available application actions.
410 * Since: 2.38
414 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
416 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
417 * entries representing applications.
419 * Since: 2.14
423 * G_KEY_FILE_DESKTOP_TYPE_LINK:
425 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
426 * entries representing links to documents.
428 * Since: 2.14
432 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
434 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
435 * entries representing directories.
437 * Since: 2.14
440 typedef struct _GKeyFileGroup GKeyFileGroup;
443 * GKeyFile:
445 * The GKeyFile struct contains only private data
446 * and should not be accessed directly.
448 struct _GKeyFile
450 GList *groups;
451 GHashTable *group_hash;
453 GKeyFileGroup *start_group;
454 GKeyFileGroup *current_group;
456 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
458 gchar list_separator;
460 GKeyFileFlags flags;
462 gchar **locales;
464 volatile gint ref_count;
467 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
469 struct _GKeyFileGroup
471 const gchar *name; /* NULL for above first group (which will be comments) */
473 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
475 GList *key_value_pairs;
477 /* Used in parallel with key_value_pairs for
478 * increased lookup performance
480 GHashTable *lookup_map;
483 struct _GKeyFileKeyValuePair
485 gchar *key; /* NULL for comments */
486 gchar *value;
489 static gint find_file_in_data_dirs (const gchar *file,
490 const gchar **data_dirs,
491 gchar **output_file,
492 GError **error);
493 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
494 gint fd,
495 GKeyFileFlags flags,
496 GError **error);
497 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
498 const gchar *group_name);
499 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
500 const gchar *group_name);
502 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
503 GKeyFileGroup *group,
504 const gchar *key);
505 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
506 GKeyFileGroup *group,
507 const gchar *key);
509 static void g_key_file_remove_group_node (GKeyFile *key_file,
510 GList *group_node);
511 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
512 GKeyFileGroup *group,
513 GList *pair_node);
515 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
516 GKeyFileGroup *group,
517 GKeyFileKeyValuePair *pair);
518 static void g_key_file_add_key (GKeyFile *key_file,
519 GKeyFileGroup *group,
520 const gchar *key,
521 const gchar *value);
522 static void g_key_file_add_group (GKeyFile *key_file,
523 const gchar *group_name);
524 static gboolean g_key_file_is_group_name (const gchar *name);
525 static gboolean g_key_file_is_key_name (const gchar *name);
526 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
527 static gboolean g_key_file_line_is_comment (const gchar *line);
528 static gboolean g_key_file_line_is_group (const gchar *line);
529 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
530 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
531 const gchar *value,
532 GSList **separators,
533 GError **error);
534 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
535 const gchar *string,
536 gboolean escape_separator);
537 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
538 const gchar *value,
539 GError **error);
540 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
541 gint value);
542 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
543 const gchar *value,
544 GError **error);
545 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
546 const gchar *value,
547 GError **error);
548 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
549 gboolean value);
550 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
551 const gchar *value);
552 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
553 const gchar *comment);
554 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
555 const gchar *line,
556 gsize length,
557 GError **error);
558 static void g_key_file_parse_comment (GKeyFile *key_file,
559 const gchar *line,
560 gsize length,
561 GError **error);
562 static void g_key_file_parse_group (GKeyFile *key_file,
563 const gchar *line,
564 gsize length,
565 GError **error);
566 static gchar *key_get_locale (const gchar *key);
567 static void g_key_file_parse_data (GKeyFile *key_file,
568 const gchar *data,
569 gsize length,
570 GError **error);
571 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
572 GError **error);
574 G_DEFINE_QUARK (g-key-file-error-quark, g_key_file_error)
576 static void
577 g_key_file_init (GKeyFile *key_file)
579 key_file->current_group = g_slice_new0 (GKeyFileGroup);
580 key_file->groups = g_list_prepend (NULL, key_file->current_group);
581 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
582 key_file->start_group = NULL;
583 key_file->parse_buffer = g_string_sized_new (128);
584 key_file->list_separator = ';';
585 key_file->flags = 0;
586 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
589 static void
590 g_key_file_clear (GKeyFile *key_file)
592 GList *tmp, *group_node;
594 if (key_file->locales)
596 g_strfreev (key_file->locales);
597 key_file->locales = NULL;
600 if (key_file->parse_buffer)
602 g_string_free (key_file->parse_buffer, TRUE);
603 key_file->parse_buffer = NULL;
606 tmp = key_file->groups;
607 while (tmp != NULL)
609 group_node = tmp;
610 tmp = tmp->next;
611 g_key_file_remove_group_node (key_file, group_node);
614 if (key_file->group_hash != NULL)
616 g_hash_table_destroy (key_file->group_hash);
617 key_file->group_hash = NULL;
620 g_warn_if_fail (key_file->groups == NULL);
625 * g_key_file_new:
627 * Creates a new empty #GKeyFile object. Use
628 * g_key_file_load_from_file(), g_key_file_load_from_data(),
629 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
630 * read an existing key file.
632 * Returns: (transfer full): an empty #GKeyFile.
634 * Since: 2.6
636 GKeyFile *
637 g_key_file_new (void)
639 GKeyFile *key_file;
641 key_file = g_slice_new0 (GKeyFile);
642 key_file->ref_count = 1;
643 g_key_file_init (key_file);
645 return key_file;
649 * g_key_file_set_list_separator:
650 * @key_file: a #GKeyFile
651 * @separator: the separator
653 * Sets the character which is used to separate
654 * values in lists. Typically ';' or ',' are used
655 * as separators. The default list separator is ';'.
657 * Since: 2.6
659 void
660 g_key_file_set_list_separator (GKeyFile *key_file,
661 gchar separator)
663 g_return_if_fail (key_file != NULL);
665 key_file->list_separator = separator;
669 /* Iterates through all the directories in *dirs trying to
670 * open file. When it successfully locates and opens a file it
671 * returns the file descriptor to the open file. It also
672 * outputs the absolute path of the file in output_file.
674 static gint
675 find_file_in_data_dirs (const gchar *file,
676 const gchar **dirs,
677 gchar **output_file,
678 GError **error)
680 const gchar **data_dirs, *data_dir;
681 gchar *path;
682 gint fd;
684 path = NULL;
685 fd = -1;
687 if (dirs == NULL)
688 return fd;
690 data_dirs = dirs;
692 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
694 gchar *candidate_file, *sub_dir;
696 candidate_file = (gchar *) file;
697 sub_dir = g_strdup ("");
698 while (candidate_file != NULL && fd == -1)
700 gchar *p;
702 path = g_build_filename (data_dir, sub_dir,
703 candidate_file, NULL);
705 fd = g_open (path, O_RDONLY, 0);
707 if (fd == -1)
709 g_free (path);
710 path = NULL;
713 candidate_file = strchr (candidate_file, '-');
715 if (candidate_file == NULL)
716 break;
718 candidate_file++;
720 g_free (sub_dir);
721 sub_dir = g_strndup (file, candidate_file - file - 1);
723 for (p = sub_dir; *p != '\0'; p++)
725 if (*p == '-')
726 *p = G_DIR_SEPARATOR;
729 g_free (sub_dir);
730 data_dirs++;
733 if (fd == -1)
735 g_set_error_literal (error, G_KEY_FILE_ERROR,
736 G_KEY_FILE_ERROR_NOT_FOUND,
737 _("Valid key file could not be "
738 "found in search dirs"));
741 if (output_file != NULL && fd > 0)
742 *output_file = g_strdup (path);
744 g_free (path);
746 return fd;
749 static gboolean
750 g_key_file_load_from_fd (GKeyFile *key_file,
751 gint fd,
752 GKeyFileFlags flags,
753 GError **error)
755 GError *key_file_error = NULL;
756 gssize bytes_read;
757 struct stat stat_buf;
758 gchar read_buf[4096];
759 gchar list_separator;
761 if (fstat (fd, &stat_buf) < 0)
763 g_set_error_literal (error, G_FILE_ERROR,
764 g_file_error_from_errno (errno),
765 g_strerror (errno));
766 return FALSE;
769 if (!S_ISREG (stat_buf.st_mode))
771 g_set_error_literal (error, G_KEY_FILE_ERROR,
772 G_KEY_FILE_ERROR_PARSE,
773 _("Not a regular file"));
774 return FALSE;
777 list_separator = key_file->list_separator;
778 g_key_file_clear (key_file);
779 g_key_file_init (key_file);
780 key_file->list_separator = list_separator;
781 key_file->flags = flags;
785 bytes_read = read (fd, read_buf, 4096);
787 if (bytes_read == 0) /* End of File */
788 break;
790 if (bytes_read < 0)
792 if (errno == EINTR || errno == EAGAIN)
793 continue;
795 g_set_error_literal (error, G_FILE_ERROR,
796 g_file_error_from_errno (errno),
797 g_strerror (errno));
798 return FALSE;
801 g_key_file_parse_data (key_file,
802 read_buf, bytes_read,
803 &key_file_error);
805 while (!key_file_error);
807 if (key_file_error)
809 g_propagate_error (error, key_file_error);
810 return FALSE;
813 g_key_file_flush_parse_buffer (key_file, &key_file_error);
815 if (key_file_error)
817 g_propagate_error (error, key_file_error);
818 return FALSE;
821 return TRUE;
825 * g_key_file_load_from_file:
826 * @key_file: an empty #GKeyFile struct
827 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
828 * @flags: flags from #GKeyFileFlags
829 * @error: return location for a #GError, or %NULL
831 * Loads a key file into an empty #GKeyFile structure.
832 * If the file could not be loaded then @error is set to
833 * either a #GFileError or #GKeyFileError.
835 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
837 * Since: 2.6
839 gboolean
840 g_key_file_load_from_file (GKeyFile *key_file,
841 const gchar *file,
842 GKeyFileFlags flags,
843 GError **error)
845 GError *key_file_error = NULL;
846 gint fd;
848 g_return_val_if_fail (key_file != NULL, FALSE);
849 g_return_val_if_fail (file != NULL, FALSE);
851 fd = g_open (file, O_RDONLY, 0);
853 if (fd == -1)
855 g_set_error_literal (error, G_FILE_ERROR,
856 g_file_error_from_errno (errno),
857 g_strerror (errno));
858 return FALSE;
861 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
862 close (fd);
864 if (key_file_error)
866 g_propagate_error (error, key_file_error);
867 return FALSE;
870 return TRUE;
874 * g_key_file_load_from_data:
875 * @key_file: an empty #GKeyFile struct
876 * @data: key file loaded in memory
877 * @length: the length of @data in bytes (or (gsize)-1 if data is nul-terminated)
878 * @flags: flags from #GKeyFileFlags
879 * @error: return location for a #GError, or %NULL
881 * Loads a key file from memory into an empty #GKeyFile structure.
882 * If the object cannot be created then %error is set to a #GKeyFileError.
884 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
886 * Since: 2.6
888 gboolean
889 g_key_file_load_from_data (GKeyFile *key_file,
890 const gchar *data,
891 gsize length,
892 GKeyFileFlags flags,
893 GError **error)
895 GError *key_file_error = NULL;
896 gchar list_separator;
898 g_return_val_if_fail (key_file != NULL, FALSE);
899 g_return_val_if_fail (data != NULL || length == 0, FALSE);
901 if (length == (gsize)-1)
902 length = strlen (data);
904 list_separator = key_file->list_separator;
905 g_key_file_clear (key_file);
906 g_key_file_init (key_file);
907 key_file->list_separator = list_separator;
908 key_file->flags = flags;
910 g_key_file_parse_data (key_file, data, length, &key_file_error);
912 if (key_file_error)
914 g_propagate_error (error, key_file_error);
915 return FALSE;
918 g_key_file_flush_parse_buffer (key_file, &key_file_error);
920 if (key_file_error)
922 g_propagate_error (error, key_file_error);
923 return FALSE;
926 return TRUE;
930 * g_key_file_load_from_dirs:
931 * @key_file: an empty #GKeyFile struct
932 * @file: (type filename): a relative path to a filename to open and parse
933 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
934 * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
935 * of the file, or %NULL
936 * @flags: flags from #GKeyFileFlags
937 * @error: return location for a #GError, or %NULL
939 * This function looks for a key file named @file in the paths
940 * specified in @search_dirs, loads the file into @key_file and
941 * returns the file's full path in @full_path. If the file could not
942 * be loaded then an %error is set to either a #GFileError or
943 * #GKeyFileError.
945 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
947 * Since: 2.14
949 gboolean
950 g_key_file_load_from_dirs (GKeyFile *key_file,
951 const gchar *file,
952 const gchar **search_dirs,
953 gchar **full_path,
954 GKeyFileFlags flags,
955 GError **error)
957 GError *key_file_error = NULL;
958 const gchar **data_dirs;
959 gchar *output_path;
960 gint fd;
961 gboolean found_file;
963 g_return_val_if_fail (key_file != NULL, FALSE);
964 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
965 g_return_val_if_fail (search_dirs != NULL, FALSE);
967 found_file = FALSE;
968 data_dirs = search_dirs;
969 output_path = NULL;
970 while (*data_dirs != NULL && !found_file)
972 g_free (output_path);
974 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
975 &key_file_error);
977 if (fd == -1)
979 if (key_file_error)
980 g_propagate_error (error, key_file_error);
981 break;
984 found_file = g_key_file_load_from_fd (key_file, fd, flags,
985 &key_file_error);
986 close (fd);
988 if (key_file_error)
990 g_propagate_error (error, key_file_error);
991 break;
995 if (found_file && full_path)
996 *full_path = output_path;
997 else
998 g_free (output_path);
1000 return found_file;
1004 * g_key_file_load_from_data_dirs:
1005 * @key_file: an empty #GKeyFile struct
1006 * @file: (type filename): a relative path to a filename to open and parse
1007 * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
1008 * of the file, or %NULL
1009 * @flags: flags from #GKeyFileFlags
1010 * @error: return location for a #GError, or %NULL
1012 * This function looks for a key file named @file in the paths
1013 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1014 * loads the file into @key_file and returns the file's full path in
1015 * @full_path. If the file could not be loaded then an %error is
1016 * set to either a #GFileError or #GKeyFileError.
1018 * Returns: %TRUE if a key file could be loaded, %FALSE othewise
1019 * Since: 2.6
1021 gboolean
1022 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1023 const gchar *file,
1024 gchar **full_path,
1025 GKeyFileFlags flags,
1026 GError **error)
1028 gchar **all_data_dirs;
1029 const gchar * user_data_dir;
1030 const gchar * const * system_data_dirs;
1031 gsize i, j;
1032 gboolean found_file;
1034 g_return_val_if_fail (key_file != NULL, FALSE);
1035 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1037 user_data_dir = g_get_user_data_dir ();
1038 system_data_dirs = g_get_system_data_dirs ();
1039 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1041 i = 0;
1042 all_data_dirs[i++] = g_strdup (user_data_dir);
1044 j = 0;
1045 while (system_data_dirs[j] != NULL)
1046 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1047 all_data_dirs[i] = NULL;
1049 found_file = g_key_file_load_from_dirs (key_file,
1050 file,
1051 (const gchar **)all_data_dirs,
1052 full_path,
1053 flags,
1054 error);
1056 g_strfreev (all_data_dirs);
1058 return found_file;
1062 * g_key_file_ref: (skip)
1063 * @key_file: a #GKeyFile
1065 * Increases the reference count of @key_file.
1067 * Returns: the same @key_file.
1069 * Since: 2.32
1071 GKeyFile *
1072 g_key_file_ref (GKeyFile *key_file)
1074 g_return_val_if_fail (key_file != NULL, NULL);
1076 g_atomic_int_inc (&key_file->ref_count);
1078 return key_file;
1082 * g_key_file_free: (skip)
1083 * @key_file: a #GKeyFile
1085 * Clears all keys and groups from @key_file, and decreases the
1086 * reference count by 1. If the reference count reaches zero,
1087 * frees the key file and all its allocated memory.
1089 * Since: 2.6
1091 void
1092 g_key_file_free (GKeyFile *key_file)
1094 g_return_if_fail (key_file != NULL);
1096 g_key_file_clear (key_file);
1097 g_key_file_unref (key_file);
1101 * g_key_file_unref:
1102 * @key_file: a #GKeyFile
1104 * Decreases the reference count of @key_file by 1. If the reference count
1105 * reaches zero, frees the key file and all its allocated memory.
1107 * Since: 2.32
1109 void
1110 g_key_file_unref (GKeyFile *key_file)
1112 g_return_if_fail (key_file != NULL);
1114 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1116 g_key_file_clear (key_file);
1117 g_slice_free (GKeyFile, key_file);
1121 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1122 * true for locales that match those in g_get_language_names().
1124 static gboolean
1125 g_key_file_locale_is_interesting (GKeyFile *key_file,
1126 const gchar *locale)
1128 gsize i;
1130 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1131 return TRUE;
1133 for (i = 0; key_file->locales[i] != NULL; i++)
1135 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
1136 return TRUE;
1139 return FALSE;
1142 static void
1143 g_key_file_parse_line (GKeyFile *key_file,
1144 const gchar *line,
1145 gsize length,
1146 GError **error)
1148 GError *parse_error = NULL;
1149 gchar *line_start;
1151 g_return_if_fail (key_file != NULL);
1152 g_return_if_fail (line != NULL);
1154 line_start = (gchar *) line;
1155 while (g_ascii_isspace (*line_start))
1156 line_start++;
1158 if (g_key_file_line_is_comment (line_start))
1159 g_key_file_parse_comment (key_file, line, length, &parse_error);
1160 else if (g_key_file_line_is_group (line_start))
1161 g_key_file_parse_group (key_file, line_start,
1162 length - (line_start - line),
1163 &parse_error);
1164 else if (g_key_file_line_is_key_value_pair (line_start))
1165 g_key_file_parse_key_value_pair (key_file, line_start,
1166 length - (line_start - line),
1167 &parse_error);
1168 else
1170 gchar *line_utf8 = _g_utf8_make_valid (line);
1171 g_set_error (error, G_KEY_FILE_ERROR,
1172 G_KEY_FILE_ERROR_PARSE,
1173 _("Key file contains line '%s' which is not "
1174 "a key-value pair, group, or comment"),
1175 line_utf8);
1176 g_free (line_utf8);
1178 return;
1181 if (parse_error)
1182 g_propagate_error (error, parse_error);
1185 static void
1186 g_key_file_parse_comment (GKeyFile *key_file,
1187 const gchar *line,
1188 gsize length,
1189 GError **error)
1191 GKeyFileKeyValuePair *pair;
1193 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1194 return;
1196 g_warn_if_fail (key_file->current_group != NULL);
1198 pair = g_slice_new (GKeyFileKeyValuePair);
1199 pair->key = NULL;
1200 pair->value = g_strndup (line, length);
1202 key_file->current_group->key_value_pairs =
1203 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1206 static void
1207 g_key_file_parse_group (GKeyFile *key_file,
1208 const gchar *line,
1209 gsize length,
1210 GError **error)
1212 gchar *group_name;
1213 const gchar *group_name_start, *group_name_end;
1215 /* advance past opening '['
1217 group_name_start = line + 1;
1218 group_name_end = line + length - 1;
1220 while (*group_name_end != ']')
1221 group_name_end--;
1223 group_name = g_strndup (group_name_start,
1224 group_name_end - group_name_start);
1226 if (!g_key_file_is_group_name (group_name))
1228 g_set_error (error, G_KEY_FILE_ERROR,
1229 G_KEY_FILE_ERROR_PARSE,
1230 _("Invalid group name: %s"), group_name);
1231 g_free (group_name);
1232 return;
1235 g_key_file_add_group (key_file, group_name);
1236 g_free (group_name);
1239 static void
1240 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1241 const gchar *line,
1242 gsize length,
1243 GError **error)
1245 gchar *key, *value, *key_end, *value_start, *locale;
1246 gsize key_len, value_len;
1248 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1250 g_set_error_literal (error, G_KEY_FILE_ERROR,
1251 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1252 _("Key file does not start with a group"));
1253 return;
1256 key_end = value_start = strchr (line, '=');
1258 g_warn_if_fail (key_end != NULL);
1260 key_end--;
1261 value_start++;
1263 /* Pull the key name from the line (chomping trailing whitespace)
1265 while (g_ascii_isspace (*key_end))
1266 key_end--;
1268 key_len = key_end - line + 2;
1270 g_warn_if_fail (key_len <= length);
1272 key = g_strndup (line, key_len - 1);
1274 if (!g_key_file_is_key_name (key))
1276 g_set_error (error, G_KEY_FILE_ERROR,
1277 G_KEY_FILE_ERROR_PARSE,
1278 _("Invalid key name: %s"), key);
1279 g_free (key);
1280 return;
1283 /* Pull the value from the line (chugging leading whitespace)
1285 while (g_ascii_isspace (*value_start))
1286 value_start++;
1288 value_len = line + length - value_start + 1;
1290 value = g_strndup (value_start, value_len);
1292 g_warn_if_fail (key_file->start_group != NULL);
1294 if (key_file->current_group
1295 && key_file->current_group->name
1296 && strcmp (key_file->start_group->name,
1297 key_file->current_group->name) == 0
1298 && strcmp (key, "Encoding") == 0)
1300 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
1302 gchar *value_utf8 = _g_utf8_make_valid (value);
1303 g_set_error (error, G_KEY_FILE_ERROR,
1304 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1305 _("Key file contains unsupported "
1306 "encoding '%s'"), value_utf8);
1307 g_free (value_utf8);
1309 g_free (key);
1310 g_free (value);
1311 return;
1315 /* Is this key a translation? If so, is it one that we care about?
1317 locale = key_get_locale (key);
1319 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
1321 GKeyFileKeyValuePair *pair;
1323 pair = g_slice_new (GKeyFileKeyValuePair);
1324 pair->key = key;
1325 pair->value = value;
1327 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1329 else
1331 g_free (key);
1332 g_free (value);
1335 g_free (locale);
1338 static gchar *
1339 key_get_locale (const gchar *key)
1341 gchar *locale;
1343 locale = g_strrstr (key, "[");
1345 if (locale && strlen (locale) <= 2)
1346 locale = NULL;
1348 if (locale)
1349 locale = g_strndup (locale + 1, strlen (locale) - 2);
1351 return locale;
1354 static void
1355 g_key_file_parse_data (GKeyFile *key_file,
1356 const gchar *data,
1357 gsize length,
1358 GError **error)
1360 GError *parse_error;
1361 gsize i;
1363 g_return_if_fail (key_file != NULL);
1364 g_return_if_fail (data != NULL || length == 0);
1366 parse_error = NULL;
1368 i = 0;
1369 while (i < length)
1371 if (data[i] == '\n')
1373 if (key_file->parse_buffer->len > 0
1374 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1375 == '\r'))
1376 g_string_erase (key_file->parse_buffer,
1377 key_file->parse_buffer->len - 1,
1380 /* When a newline is encountered flush the parse buffer so that the
1381 * line can be parsed. Note that completely blank lines won't show
1382 * up in the parse buffer, so they get parsed directly.
1384 if (key_file->parse_buffer->len > 0)
1385 g_key_file_flush_parse_buffer (key_file, &parse_error);
1386 else
1387 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1389 if (parse_error)
1391 g_propagate_error (error, parse_error);
1392 return;
1394 i++;
1396 else
1398 const gchar *start_of_line;
1399 const gchar *end_of_line;
1400 gsize line_length;
1402 start_of_line = data + i;
1403 end_of_line = memchr (start_of_line, '\n', length - i);
1405 if (end_of_line == NULL)
1406 end_of_line = data + length;
1408 line_length = end_of_line - start_of_line;
1410 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1411 i += line_length;
1416 static void
1417 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1418 GError **error)
1420 GError *file_error = NULL;
1422 g_return_if_fail (key_file != NULL);
1424 file_error = NULL;
1426 if (key_file->parse_buffer->len > 0)
1428 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1429 key_file->parse_buffer->len,
1430 &file_error);
1431 g_string_erase (key_file->parse_buffer, 0, -1);
1433 if (file_error)
1435 g_propagate_error (error, file_error);
1436 return;
1442 * g_key_file_to_data:
1443 * @key_file: a #GKeyFile
1444 * @length: (out) (allow-none): return location for the length of the
1445 * returned string, or %NULL
1446 * @error: return location for a #GError, or %NULL
1448 * This function outputs @key_file as a string.
1450 * Note that this function never reports an error,
1451 * so it is safe to pass %NULL as @error.
1453 * Returns: a newly allocated string holding
1454 * the contents of the #GKeyFile
1456 * Since: 2.6
1458 gchar *
1459 g_key_file_to_data (GKeyFile *key_file,
1460 gsize *length,
1461 GError **error)
1463 GString *data_string;
1464 GList *group_node, *key_file_node;
1466 g_return_val_if_fail (key_file != NULL, NULL);
1468 data_string = g_string_new (NULL);
1470 for (group_node = g_list_last (key_file->groups);
1471 group_node != NULL;
1472 group_node = group_node->prev)
1474 GKeyFileGroup *group;
1476 group = (GKeyFileGroup *) group_node->data;
1478 /* separate groups by at least an empty line */
1479 if (data_string->len >= 2 &&
1480 data_string->str[data_string->len - 2] != '\n')
1481 g_string_append_c (data_string, '\n');
1483 if (group->comment != NULL)
1484 g_string_append_printf (data_string, "%s\n", group->comment->value);
1486 if (group->name != NULL)
1487 g_string_append_printf (data_string, "[%s]\n", group->name);
1489 for (key_file_node = g_list_last (group->key_value_pairs);
1490 key_file_node != NULL;
1491 key_file_node = key_file_node->prev)
1493 GKeyFileKeyValuePair *pair;
1495 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1497 if (pair->key != NULL)
1498 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1499 else
1500 g_string_append_printf (data_string, "%s\n", pair->value);
1504 if (length)
1505 *length = data_string->len;
1507 return g_string_free (data_string, FALSE);
1511 * g_key_file_get_keys:
1512 * @key_file: a #GKeyFile
1513 * @group_name: a group name
1514 * @length: (out) (allow-none): return location for the number of keys returned, or %NULL
1515 * @error: return location for a #GError, or %NULL
1517 * Returns all keys for the group name @group_name. The array of
1518 * returned keys will be %NULL-terminated, so @length may
1519 * optionally be %NULL. In the event that the @group_name cannot
1520 * be found, %NULL is returned and @error is set to
1521 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1523 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1524 * Use g_strfreev() to free it.
1526 * Since: 2.6
1528 gchar **
1529 g_key_file_get_keys (GKeyFile *key_file,
1530 const gchar *group_name,
1531 gsize *length,
1532 GError **error)
1534 GKeyFileGroup *group;
1535 GList *tmp;
1536 gchar **keys;
1537 gsize i, num_keys;
1539 g_return_val_if_fail (key_file != NULL, NULL);
1540 g_return_val_if_fail (group_name != NULL, NULL);
1542 group = g_key_file_lookup_group (key_file, group_name);
1544 if (!group)
1546 g_set_error (error, G_KEY_FILE_ERROR,
1547 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1548 _("Key file does not have group '%s'"),
1549 group_name);
1550 return NULL;
1553 num_keys = 0;
1554 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1556 GKeyFileKeyValuePair *pair;
1558 pair = (GKeyFileKeyValuePair *) tmp->data;
1560 if (pair->key)
1561 num_keys++;
1564 keys = g_new (gchar *, num_keys + 1);
1566 i = num_keys - 1;
1567 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1569 GKeyFileKeyValuePair *pair;
1571 pair = (GKeyFileKeyValuePair *) tmp->data;
1573 if (pair->key)
1575 keys[i] = g_strdup (pair->key);
1576 i--;
1580 keys[num_keys] = NULL;
1582 if (length)
1583 *length = num_keys;
1585 return keys;
1589 * g_key_file_get_start_group:
1590 * @key_file: a #GKeyFile
1592 * Returns the name of the start group of the file.
1594 * Returns: The start group of the key file.
1596 * Since: 2.6
1598 gchar *
1599 g_key_file_get_start_group (GKeyFile *key_file)
1601 g_return_val_if_fail (key_file != NULL, NULL);
1603 if (key_file->start_group)
1604 return g_strdup (key_file->start_group->name);
1606 return NULL;
1610 * g_key_file_get_groups:
1611 * @key_file: a #GKeyFile
1612 * @length: (out) (allow-none): return location for the number of returned groups, or %NULL
1614 * Returns all groups in the key file loaded with @key_file.
1615 * The array of returned groups will be %NULL-terminated, so
1616 * @length may optionally be %NULL.
1618 * Returns: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1619 * Use g_strfreev() to free it.
1620 * Since: 2.6
1622 gchar **
1623 g_key_file_get_groups (GKeyFile *key_file,
1624 gsize *length)
1626 GList *group_node;
1627 gchar **groups;
1628 gsize i, num_groups;
1630 g_return_val_if_fail (key_file != NULL, NULL);
1632 num_groups = g_list_length (key_file->groups);
1634 g_return_val_if_fail (num_groups > 0, NULL);
1636 group_node = g_list_last (key_file->groups);
1638 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1640 /* Only need num_groups instead of num_groups + 1
1641 * because the first group of the file (last in the
1642 * list) is always the comment group at the top,
1643 * which we skip
1645 groups = g_new (gchar *, num_groups);
1648 i = 0;
1649 for (group_node = group_node->prev;
1650 group_node != NULL;
1651 group_node = group_node->prev)
1653 GKeyFileGroup *group;
1655 group = (GKeyFileGroup *) group_node->data;
1657 g_warn_if_fail (group->name != NULL);
1659 groups[i++] = g_strdup (group->name);
1661 groups[i] = NULL;
1663 if (length)
1664 *length = i;
1666 return groups;
1669 static void
1670 set_not_found_key_error (const char *group_name,
1671 const char *key,
1672 GError **error)
1674 g_set_error (error, G_KEY_FILE_ERROR,
1675 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1676 _("Key file does not have key '%s' in group '%s'"),
1677 key, group_name);
1681 * g_key_file_get_value:
1682 * @key_file: a #GKeyFile
1683 * @group_name: a group name
1684 * @key: a key
1685 * @error: return location for a #GError, or %NULL
1687 * Returns the raw value associated with @key under @group_name.
1688 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1690 * In the event the key cannot be found, %NULL is returned and
1691 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1692 * event that the @group_name cannot be found, %NULL is returned
1693 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1696 * Returns: a newly allocated string or %NULL if the specified
1697 * key cannot be found.
1699 * Since: 2.6
1701 gchar *
1702 g_key_file_get_value (GKeyFile *key_file,
1703 const gchar *group_name,
1704 const gchar *key,
1705 GError **error)
1707 GKeyFileGroup *group;
1708 GKeyFileKeyValuePair *pair;
1709 gchar *value = NULL;
1711 g_return_val_if_fail (key_file != NULL, NULL);
1712 g_return_val_if_fail (group_name != NULL, NULL);
1713 g_return_val_if_fail (key != NULL, NULL);
1715 group = g_key_file_lookup_group (key_file, group_name);
1717 if (!group)
1719 g_set_error (error, G_KEY_FILE_ERROR,
1720 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1721 _("Key file does not have group '%s'"),
1722 group_name);
1723 return NULL;
1726 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1728 if (pair)
1729 value = g_strdup (pair->value);
1730 else
1731 set_not_found_key_error (group_name, key, error);
1733 return value;
1737 * g_key_file_set_value:
1738 * @key_file: a #GKeyFile
1739 * @group_name: a group name
1740 * @key: a key
1741 * @value: a string
1743 * Associates a new value with @key under @group_name.
1745 * If @key cannot be found then it is created. If @group_name cannot
1746 * be found then it is created. To set an UTF-8 string which may contain
1747 * characters that need escaping (such as newlines or spaces), use
1748 * g_key_file_set_string().
1750 * Since: 2.6
1752 void
1753 g_key_file_set_value (GKeyFile *key_file,
1754 const gchar *group_name,
1755 const gchar *key,
1756 const gchar *value)
1758 GKeyFileGroup *group;
1759 GKeyFileKeyValuePair *pair;
1761 g_return_if_fail (key_file != NULL);
1762 g_return_if_fail (g_key_file_is_group_name (group_name));
1763 g_return_if_fail (g_key_file_is_key_name (key));
1764 g_return_if_fail (value != NULL);
1766 group = g_key_file_lookup_group (key_file, group_name);
1768 if (!group)
1770 g_key_file_add_group (key_file, group_name);
1771 group = (GKeyFileGroup *) key_file->groups->data;
1773 g_key_file_add_key (key_file, group, key, value);
1775 else
1777 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1779 if (!pair)
1780 g_key_file_add_key (key_file, group, key, value);
1781 else
1783 g_free (pair->value);
1784 pair->value = g_strdup (value);
1790 * g_key_file_get_string:
1791 * @key_file: a #GKeyFile
1792 * @group_name: a group name
1793 * @key: a key
1794 * @error: return location for a #GError, or %NULL
1796 * Returns the string value associated with @key under @group_name.
1797 * Unlike g_key_file_get_value(), this function handles escape sequences
1798 * like \s.
1800 * In the event the key cannot be found, %NULL is returned and
1801 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1802 * event that the @group_name cannot be found, %NULL is returned
1803 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1805 * Returns: a newly allocated string or %NULL if the specified
1806 * key cannot be found.
1808 * Since: 2.6
1810 gchar *
1811 g_key_file_get_string (GKeyFile *key_file,
1812 const gchar *group_name,
1813 const gchar *key,
1814 GError **error)
1816 gchar *value, *string_value;
1817 GError *key_file_error;
1819 g_return_val_if_fail (key_file != NULL, NULL);
1820 g_return_val_if_fail (group_name != NULL, NULL);
1821 g_return_val_if_fail (key != NULL, NULL);
1823 key_file_error = NULL;
1825 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1827 if (key_file_error)
1829 g_propagate_error (error, key_file_error);
1830 return NULL;
1833 if (!g_utf8_validate (value, -1, NULL))
1835 gchar *value_utf8 = _g_utf8_make_valid (value);
1836 g_set_error (error, G_KEY_FILE_ERROR,
1837 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1838 _("Key file contains key '%s' with value '%s' "
1839 "which is not UTF-8"), key, value_utf8);
1840 g_free (value_utf8);
1841 g_free (value);
1843 return NULL;
1846 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1847 &key_file_error);
1848 g_free (value);
1850 if (key_file_error)
1852 if (g_error_matches (key_file_error,
1853 G_KEY_FILE_ERROR,
1854 G_KEY_FILE_ERROR_INVALID_VALUE))
1856 g_set_error (error, G_KEY_FILE_ERROR,
1857 G_KEY_FILE_ERROR_INVALID_VALUE,
1858 _("Key file contains key '%s' "
1859 "which has a value that cannot be interpreted."),
1860 key);
1861 g_error_free (key_file_error);
1863 else
1864 g_propagate_error (error, key_file_error);
1867 return string_value;
1871 * g_key_file_set_string:
1872 * @key_file: a #GKeyFile
1873 * @group_name: a group name
1874 * @key: a key
1875 * @string: a string
1877 * Associates a new string value with @key under @group_name.
1878 * If @key cannot be found then it is created.
1879 * If @group_name cannot be found then it is created.
1880 * Unlike g_key_file_set_value(), this function handles characters
1881 * that need escaping, such as newlines.
1883 * Since: 2.6
1885 void
1886 g_key_file_set_string (GKeyFile *key_file,
1887 const gchar *group_name,
1888 const gchar *key,
1889 const gchar *string)
1891 gchar *value;
1893 g_return_if_fail (key_file != NULL);
1894 g_return_if_fail (string != NULL);
1896 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1897 g_key_file_set_value (key_file, group_name, key, value);
1898 g_free (value);
1902 * g_key_file_get_string_list:
1903 * @key_file: a #GKeyFile
1904 * @group_name: a group name
1905 * @key: a key
1906 * @length: (out) (allow-none): return location for the number of returned strings, or %NULL
1907 * @error: return location for a #GError, or %NULL
1909 * Returns the values associated with @key under @group_name.
1911 * In the event the key cannot be found, %NULL is returned and
1912 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1913 * event that the @group_name cannot be found, %NULL is returned
1914 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1916 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
1917 * a %NULL-terminated string array or %NULL if the specified
1918 * key cannot be found. The array should be freed with g_strfreev().
1920 * Since: 2.6
1922 gchar **
1923 g_key_file_get_string_list (GKeyFile *key_file,
1924 const gchar *group_name,
1925 const gchar *key,
1926 gsize *length,
1927 GError **error)
1929 GError *key_file_error = NULL;
1930 gchar *value, *string_value, **values;
1931 gint i, len;
1932 GSList *p, *pieces = NULL;
1934 g_return_val_if_fail (key_file != NULL, NULL);
1935 g_return_val_if_fail (group_name != NULL, NULL);
1936 g_return_val_if_fail (key != NULL, NULL);
1938 if (length)
1939 *length = 0;
1941 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1943 if (key_file_error)
1945 g_propagate_error (error, key_file_error);
1946 return NULL;
1949 if (!g_utf8_validate (value, -1, NULL))
1951 gchar *value_utf8 = _g_utf8_make_valid (value);
1952 g_set_error (error, G_KEY_FILE_ERROR,
1953 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1954 _("Key file contains key '%s' with value '%s' "
1955 "which is not UTF-8"), key, value_utf8);
1956 g_free (value_utf8);
1957 g_free (value);
1959 return NULL;
1962 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1963 g_free (value);
1964 g_free (string_value);
1966 if (key_file_error)
1968 if (g_error_matches (key_file_error,
1969 G_KEY_FILE_ERROR,
1970 G_KEY_FILE_ERROR_INVALID_VALUE))
1972 g_set_error (error, G_KEY_FILE_ERROR,
1973 G_KEY_FILE_ERROR_INVALID_VALUE,
1974 _("Key file contains key '%s' "
1975 "which has a value that cannot be interpreted."),
1976 key);
1977 g_error_free (key_file_error);
1979 else
1980 g_propagate_error (error, key_file_error);
1982 g_slist_free_full (pieces, g_free);
1983 return NULL;
1986 len = g_slist_length (pieces);
1987 values = g_new (gchar *, len + 1);
1988 for (p = pieces, i = 0; p; p = p->next)
1989 values[i++] = p->data;
1990 values[len] = NULL;
1992 g_slist_free (pieces);
1994 if (length)
1995 *length = len;
1997 return values;
2001 * g_key_file_set_string_list:
2002 * @key_file: a #GKeyFile
2003 * @group_name: a group name
2004 * @key: a key
2005 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
2006 * @length: number of string values in @list
2008 * Associates a list of string values for @key under @group_name.
2009 * If @key cannot be found then it is created.
2010 * If @group_name cannot be found then it is created.
2012 * Since: 2.6
2014 void
2015 g_key_file_set_string_list (GKeyFile *key_file,
2016 const gchar *group_name,
2017 const gchar *key,
2018 const gchar * const list[],
2019 gsize length)
2021 GString *value_list;
2022 gsize i;
2024 g_return_if_fail (key_file != NULL);
2025 g_return_if_fail (list != NULL || length == 0);
2027 value_list = g_string_sized_new (length * 128);
2028 for (i = 0; i < length && list[i] != NULL; i++)
2030 gchar *value;
2032 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2033 g_string_append (value_list, value);
2034 g_string_append_c (value_list, key_file->list_separator);
2036 g_free (value);
2039 g_key_file_set_value (key_file, group_name, key, value_list->str);
2040 g_string_free (value_list, TRUE);
2044 * g_key_file_set_locale_string:
2045 * @key_file: a #GKeyFile
2046 * @group_name: a group name
2047 * @key: a key
2048 * @locale: a locale identifier
2049 * @string: a string
2051 * Associates a string value for @key and @locale under @group_name.
2052 * If the translation for @key cannot be found then it is created.
2054 * Since: 2.6
2056 void
2057 g_key_file_set_locale_string (GKeyFile *key_file,
2058 const gchar *group_name,
2059 const gchar *key,
2060 const gchar *locale,
2061 const gchar *string)
2063 gchar *full_key, *value;
2065 g_return_if_fail (key_file != NULL);
2066 g_return_if_fail (key != NULL);
2067 g_return_if_fail (locale != NULL);
2068 g_return_if_fail (string != NULL);
2070 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2071 full_key = g_strdup_printf ("%s[%s]", key, locale);
2072 g_key_file_set_value (key_file, group_name, full_key, value);
2073 g_free (full_key);
2074 g_free (value);
2078 * g_key_file_get_locale_string:
2079 * @key_file: a #GKeyFile
2080 * @group_name: a group name
2081 * @key: a key
2082 * @locale: (allow-none): a locale identifier or %NULL
2083 * @error: return location for a #GError, or %NULL
2085 * Returns the value associated with @key under @group_name
2086 * translated in the given @locale if available. If @locale is
2087 * %NULL then the current locale is assumed.
2089 * If @key cannot be found then %NULL is returned and @error is set
2090 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2091 * with @key cannot be interpreted or no suitable translation can
2092 * be found then the untranslated value is returned.
2094 * Returns: a newly allocated string or %NULL if the specified
2095 * key cannot be found.
2097 * Since: 2.6
2099 gchar *
2100 g_key_file_get_locale_string (GKeyFile *key_file,
2101 const gchar *group_name,
2102 const gchar *key,
2103 const gchar *locale,
2104 GError **error)
2106 gchar *candidate_key, *translated_value;
2107 GError *key_file_error;
2108 gchar **languages;
2109 gboolean free_languages = FALSE;
2110 gint i;
2112 g_return_val_if_fail (key_file != NULL, NULL);
2113 g_return_val_if_fail (group_name != NULL, NULL);
2114 g_return_val_if_fail (key != NULL, NULL);
2116 candidate_key = NULL;
2117 translated_value = NULL;
2118 key_file_error = NULL;
2120 if (locale)
2122 languages = g_get_locale_variants (locale);
2123 free_languages = TRUE;
2125 else
2127 languages = (gchar **) g_get_language_names ();
2128 free_languages = FALSE;
2131 for (i = 0; languages[i]; i++)
2133 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2135 translated_value = g_key_file_get_string (key_file,
2136 group_name,
2137 candidate_key, NULL);
2138 g_free (candidate_key);
2140 if (translated_value)
2141 break;
2143 g_free (translated_value);
2144 translated_value = NULL;
2147 /* Fallback to untranslated key
2149 if (!translated_value)
2151 translated_value = g_key_file_get_string (key_file, group_name, key,
2152 &key_file_error);
2154 if (!translated_value)
2155 g_propagate_error (error, key_file_error);
2158 if (free_languages)
2159 g_strfreev (languages);
2161 return translated_value;
2165 * g_key_file_get_locale_string_list:
2166 * @key_file: a #GKeyFile
2167 * @group_name: a group name
2168 * @key: a key
2169 * @locale: (allow-none): a locale identifier or %NULL
2170 * @length: (out) (allow-none): return location for the number of returned strings or %NULL
2171 * @error: return location for a #GError or %NULL
2173 * Returns the values associated with @key under @group_name
2174 * translated in the given @locale if available. If @locale is
2175 * %NULL then the current locale is assumed.
2177 * If @key cannot be found then %NULL is returned and @error is set
2178 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2179 * with @key cannot be interpreted or no suitable translations
2180 * can be found then the untranslated values are returned. The
2181 * returned array is %NULL-terminated, so @length may optionally
2182 * be %NULL.
2184 * Returns: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2185 * or %NULL if the key isn't found. The string array should be freed
2186 * with g_strfreev().
2188 * Since: 2.6
2190 gchar **
2191 g_key_file_get_locale_string_list (GKeyFile *key_file,
2192 const gchar *group_name,
2193 const gchar *key,
2194 const gchar *locale,
2195 gsize *length,
2196 GError **error)
2198 GError *key_file_error;
2199 gchar **values, *value;
2200 char list_separator[2];
2201 gsize len;
2203 g_return_val_if_fail (key_file != NULL, NULL);
2204 g_return_val_if_fail (group_name != NULL, NULL);
2205 g_return_val_if_fail (key != NULL, NULL);
2207 key_file_error = NULL;
2209 value = g_key_file_get_locale_string (key_file, group_name,
2210 key, locale,
2211 &key_file_error);
2213 if (key_file_error)
2214 g_propagate_error (error, key_file_error);
2216 if (!value)
2218 if (length)
2219 *length = 0;
2220 return NULL;
2223 len = strlen (value);
2224 if (value[len - 1] == key_file->list_separator)
2225 value[len - 1] = '\0';
2227 list_separator[0] = key_file->list_separator;
2228 list_separator[1] = '\0';
2229 values = g_strsplit (value, list_separator, 0);
2231 g_free (value);
2233 if (length)
2234 *length = g_strv_length (values);
2236 return values;
2240 * g_key_file_set_locale_string_list:
2241 * @key_file: a #GKeyFile
2242 * @group_name: a group name
2243 * @key: a key
2244 * @locale: a locale identifier
2245 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2246 * @length: the length of @list
2248 * Associates a list of string values for @key and @locale under
2249 * @group_name. If the translation for @key cannot be found then
2250 * it is created.
2252 * Since: 2.6
2254 void
2255 g_key_file_set_locale_string_list (GKeyFile *key_file,
2256 const gchar *group_name,
2257 const gchar *key,
2258 const gchar *locale,
2259 const gchar * const list[],
2260 gsize length)
2262 GString *value_list;
2263 gchar *full_key;
2264 gsize i;
2266 g_return_if_fail (key_file != NULL);
2267 g_return_if_fail (key != NULL);
2268 g_return_if_fail (locale != NULL);
2269 g_return_if_fail (length != 0);
2271 value_list = g_string_sized_new (length * 128);
2272 for (i = 0; i < length && list[i] != NULL; i++)
2274 gchar *value;
2276 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2277 g_string_append (value_list, value);
2278 g_string_append_c (value_list, key_file->list_separator);
2280 g_free (value);
2283 full_key = g_strdup_printf ("%s[%s]", key, locale);
2284 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2285 g_free (full_key);
2286 g_string_free (value_list, TRUE);
2290 * g_key_file_get_boolean:
2291 * @key_file: a #GKeyFile
2292 * @group_name: a group name
2293 * @key: a key
2294 * @error: return location for a #GError
2296 * Returns the value associated with @key under @group_name as a
2297 * boolean.
2299 * If @key cannot be found then %FALSE is returned and @error is set
2300 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2301 * associated with @key cannot be interpreted as a boolean then %FALSE
2302 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2304 * Returns: the value associated with the key as a boolean,
2305 * or %FALSE if the key was not found or could not be parsed.
2307 * Since: 2.6
2309 gboolean
2310 g_key_file_get_boolean (GKeyFile *key_file,
2311 const gchar *group_name,
2312 const gchar *key,
2313 GError **error)
2315 GError *key_file_error = NULL;
2316 gchar *value;
2317 gboolean bool_value;
2319 g_return_val_if_fail (key_file != NULL, FALSE);
2320 g_return_val_if_fail (group_name != NULL, FALSE);
2321 g_return_val_if_fail (key != NULL, FALSE);
2323 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2325 if (!value)
2327 g_propagate_error (error, key_file_error);
2328 return FALSE;
2331 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2332 &key_file_error);
2333 g_free (value);
2335 if (key_file_error)
2337 if (g_error_matches (key_file_error,
2338 G_KEY_FILE_ERROR,
2339 G_KEY_FILE_ERROR_INVALID_VALUE))
2341 g_set_error (error, G_KEY_FILE_ERROR,
2342 G_KEY_FILE_ERROR_INVALID_VALUE,
2343 _("Key file contains key '%s' "
2344 "which has a value that cannot be interpreted."),
2345 key);
2346 g_error_free (key_file_error);
2348 else
2349 g_propagate_error (error, key_file_error);
2352 return bool_value;
2356 * g_key_file_set_boolean:
2357 * @key_file: a #GKeyFile
2358 * @group_name: a group name
2359 * @key: a key
2360 * @value: %TRUE or %FALSE
2362 * Associates a new boolean value with @key under @group_name.
2363 * If @key cannot be found then it is created.
2365 * Since: 2.6
2367 void
2368 g_key_file_set_boolean (GKeyFile *key_file,
2369 const gchar *group_name,
2370 const gchar *key,
2371 gboolean value)
2373 gchar *result;
2375 g_return_if_fail (key_file != NULL);
2377 result = g_key_file_parse_boolean_as_value (key_file, value);
2378 g_key_file_set_value (key_file, group_name, key, result);
2379 g_free (result);
2383 * g_key_file_get_boolean_list:
2384 * @key_file: a #GKeyFile
2385 * @group_name: a group name
2386 * @key: a key
2387 * @length: (out): the number of booleans returned
2388 * @error: return location for a #GError
2390 * Returns the values associated with @key under @group_name as
2391 * booleans.
2393 * If @key cannot be found then %NULL is returned and @error is set to
2394 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2395 * with @key cannot be interpreted as booleans then %NULL is returned
2396 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2398 * Returns: (array length=length) (element-type gboolean) (transfer container):
2399 * the values associated with the key as a list of booleans, or %NULL if the
2400 * key was not found or could not be parsed. The returned list of booleans
2401 * should be freed with g_free() when no longer needed.
2403 * Since: 2.6
2405 gboolean *
2406 g_key_file_get_boolean_list (GKeyFile *key_file,
2407 const gchar *group_name,
2408 const gchar *key,
2409 gsize *length,
2410 GError **error)
2412 GError *key_file_error;
2413 gchar **values;
2414 gboolean *bool_values;
2415 gsize i, num_bools;
2417 g_return_val_if_fail (key_file != NULL, NULL);
2418 g_return_val_if_fail (group_name != NULL, NULL);
2419 g_return_val_if_fail (key != NULL, NULL);
2421 if (length)
2422 *length = 0;
2424 key_file_error = NULL;
2426 values = g_key_file_get_string_list (key_file, group_name, key,
2427 &num_bools, &key_file_error);
2429 if (key_file_error)
2430 g_propagate_error (error, key_file_error);
2432 if (!values)
2433 return NULL;
2435 bool_values = g_new (gboolean, num_bools);
2437 for (i = 0; i < num_bools; i++)
2439 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2440 values[i],
2441 &key_file_error);
2443 if (key_file_error)
2445 g_propagate_error (error, key_file_error);
2446 g_strfreev (values);
2447 g_free (bool_values);
2449 return NULL;
2452 g_strfreev (values);
2454 if (length)
2455 *length = num_bools;
2457 return bool_values;
2461 * g_key_file_set_boolean_list:
2462 * @key_file: a #GKeyFile
2463 * @group_name: a group name
2464 * @key: a key
2465 * @list: (array length=length): an array of boolean values
2466 * @length: length of @list
2468 * Associates a list of boolean values with @key under @group_name.
2469 * If @key cannot be found then it is created.
2470 * If @group_name is %NULL, the start_group is used.
2472 * Since: 2.6
2474 void
2475 g_key_file_set_boolean_list (GKeyFile *key_file,
2476 const gchar *group_name,
2477 const gchar *key,
2478 gboolean list[],
2479 gsize length)
2481 GString *value_list;
2482 gsize i;
2484 g_return_if_fail (key_file != NULL);
2485 g_return_if_fail (list != NULL);
2487 value_list = g_string_sized_new (length * 8);
2488 for (i = 0; i < length; i++)
2490 gchar *value;
2492 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2494 g_string_append (value_list, value);
2495 g_string_append_c (value_list, key_file->list_separator);
2497 g_free (value);
2500 g_key_file_set_value (key_file, group_name, key, value_list->str);
2501 g_string_free (value_list, TRUE);
2505 * g_key_file_get_integer:
2506 * @key_file: a #GKeyFile
2507 * @group_name: a group name
2508 * @key: a key
2509 * @error: return location for a #GError
2511 * Returns the value associated with @key under @group_name as an
2512 * integer.
2514 * If @key cannot be found then 0 is returned and @error is set to
2515 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2516 * with @key cannot be interpreted as an integer then 0 is returned
2517 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2519 * Returns: the value associated with the key as an integer, or
2520 * 0 if the key was not found or could not be parsed.
2522 * Since: 2.6
2524 gint
2525 g_key_file_get_integer (GKeyFile *key_file,
2526 const gchar *group_name,
2527 const gchar *key,
2528 GError **error)
2530 GError *key_file_error;
2531 gchar *value;
2532 gint int_value;
2534 g_return_val_if_fail (key_file != NULL, -1);
2535 g_return_val_if_fail (group_name != NULL, -1);
2536 g_return_val_if_fail (key != NULL, -1);
2538 key_file_error = NULL;
2540 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2542 if (key_file_error)
2544 g_propagate_error (error, key_file_error);
2545 return 0;
2548 int_value = g_key_file_parse_value_as_integer (key_file, value,
2549 &key_file_error);
2550 g_free (value);
2552 if (key_file_error)
2554 if (g_error_matches (key_file_error,
2555 G_KEY_FILE_ERROR,
2556 G_KEY_FILE_ERROR_INVALID_VALUE))
2558 g_set_error (error, G_KEY_FILE_ERROR,
2559 G_KEY_FILE_ERROR_INVALID_VALUE,
2560 _("Key file contains key '%s' in group '%s' "
2561 "which has a value that cannot be interpreted."),
2562 key, group_name);
2563 g_error_free (key_file_error);
2565 else
2566 g_propagate_error (error, key_file_error);
2569 return int_value;
2573 * g_key_file_set_integer:
2574 * @key_file: a #GKeyFile
2575 * @group_name: a group name
2576 * @key: a key
2577 * @value: an integer value
2579 * Associates a new integer value with @key under @group_name.
2580 * If @key cannot be found then it is created.
2582 * Since: 2.6
2584 void
2585 g_key_file_set_integer (GKeyFile *key_file,
2586 const gchar *group_name,
2587 const gchar *key,
2588 gint value)
2590 gchar *result;
2592 g_return_if_fail (key_file != NULL);
2594 result = g_key_file_parse_integer_as_value (key_file, value);
2595 g_key_file_set_value (key_file, group_name, key, result);
2596 g_free (result);
2600 * g_key_file_get_int64:
2601 * @key_file: a non-%NULL #GKeyFile
2602 * @group_name: a non-%NULL group name
2603 * @key: a non-%NULL key
2604 * @error: return location for a #GError
2606 * Returns the value associated with @key under @group_name as a signed
2607 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2608 * 64-bit results without truncation.
2610 * Returns: the value associated with the key as a signed 64-bit integer, or
2611 * 0 if the key was not found or could not be parsed.
2613 * Since: 2.26
2615 gint64
2616 g_key_file_get_int64 (GKeyFile *key_file,
2617 const gchar *group_name,
2618 const gchar *key,
2619 GError **error)
2621 gchar *s, *end;
2622 gint64 v;
2624 g_return_val_if_fail (key_file != NULL, -1);
2625 g_return_val_if_fail (group_name != NULL, -1);
2626 g_return_val_if_fail (key != NULL, -1);
2628 s = g_key_file_get_value (key_file, group_name, key, error);
2630 if (s == NULL)
2631 return 0;
2633 v = g_ascii_strtoll (s, &end, 10);
2635 if (*s == '\0' || *end != '\0')
2637 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2638 _("Key '%s' in group '%s' has value '%s' "
2639 "where %s was expected"),
2640 key, group_name, s, "int64");
2641 g_free (s);
2642 return 0;
2645 g_free (s);
2646 return v;
2650 * g_key_file_set_int64:
2651 * @key_file: a #GKeyFile
2652 * @group_name: a group name
2653 * @key: a key
2654 * @value: an integer value
2656 * Associates a new integer value with @key under @group_name.
2657 * If @key cannot be found then it is created.
2659 * Since: 2.26
2661 void
2662 g_key_file_set_int64 (GKeyFile *key_file,
2663 const gchar *group_name,
2664 const gchar *key,
2665 gint64 value)
2667 gchar *result;
2669 g_return_if_fail (key_file != NULL);
2671 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2672 g_key_file_set_value (key_file, group_name, key, result);
2673 g_free (result);
2677 * g_key_file_get_uint64:
2678 * @key_file: a non-%NULL #GKeyFile
2679 * @group_name: a non-%NULL group name
2680 * @key: a non-%NULL key
2681 * @error: return location for a #GError
2683 * Returns the value associated with @key under @group_name as an unsigned
2684 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2685 * large positive results without truncation.
2687 * Returns: the value associated with the key as an unsigned 64-bit integer,
2688 * or 0 if the key was not found or could not be parsed.
2690 * Since: 2.26
2692 guint64
2693 g_key_file_get_uint64 (GKeyFile *key_file,
2694 const gchar *group_name,
2695 const gchar *key,
2696 GError **error)
2698 gchar *s, *end;
2699 guint64 v;
2701 g_return_val_if_fail (key_file != NULL, -1);
2702 g_return_val_if_fail (group_name != NULL, -1);
2703 g_return_val_if_fail (key != NULL, -1);
2705 s = g_key_file_get_value (key_file, group_name, key, error);
2707 if (s == NULL)
2708 return 0;
2710 v = g_ascii_strtoull (s, &end, 10);
2712 if (*s == '\0' || *end != '\0')
2714 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2715 _("Key '%s' in group '%s' has value '%s' "
2716 "where %s was expected"),
2717 key, group_name, s, "uint64");
2718 g_free (s);
2719 return 0;
2722 g_free (s);
2723 return v;
2727 * g_key_file_set_uint64:
2728 * @key_file: a #GKeyFile
2729 * @group_name: a group name
2730 * @key: a key
2731 * @value: an integer value
2733 * Associates a new integer value with @key under @group_name.
2734 * If @key cannot be found then it is created.
2736 * Since: 2.26
2738 void
2739 g_key_file_set_uint64 (GKeyFile *key_file,
2740 const gchar *group_name,
2741 const gchar *key,
2742 guint64 value)
2744 gchar *result;
2746 g_return_if_fail (key_file != NULL);
2748 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2749 g_key_file_set_value (key_file, group_name, key, result);
2750 g_free (result);
2754 * g_key_file_get_integer_list:
2755 * @key_file: a #GKeyFile
2756 * @group_name: a group name
2757 * @key: a key
2758 * @length: (out): the number of integers returned
2759 * @error: return location for a #GError
2761 * Returns the values associated with @key under @group_name as
2762 * integers.
2764 * If @key cannot be found then %NULL is returned and @error is set to
2765 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2766 * with @key cannot be interpreted as integers then %NULL is returned
2767 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2769 * Returns: (array length=length) (element-type gint) (transfer container):
2770 * the values associated with the key as a list of integers, or %NULL if
2771 * the key was not found or could not be parsed. The returned list of
2772 * integers should be freed with g_free() when no longer needed.
2774 * Since: 2.6
2776 gint *
2777 g_key_file_get_integer_list (GKeyFile *key_file,
2778 const gchar *group_name,
2779 const gchar *key,
2780 gsize *length,
2781 GError **error)
2783 GError *key_file_error = NULL;
2784 gchar **values;
2785 gint *int_values;
2786 gsize i, num_ints;
2788 g_return_val_if_fail (key_file != NULL, NULL);
2789 g_return_val_if_fail (group_name != NULL, NULL);
2790 g_return_val_if_fail (key != NULL, NULL);
2792 if (length)
2793 *length = 0;
2795 values = g_key_file_get_string_list (key_file, group_name, key,
2796 &num_ints, &key_file_error);
2798 if (key_file_error)
2799 g_propagate_error (error, key_file_error);
2801 if (!values)
2802 return NULL;
2804 int_values = g_new (gint, num_ints);
2806 for (i = 0; i < num_ints; i++)
2808 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2809 values[i],
2810 &key_file_error);
2812 if (key_file_error)
2814 g_propagate_error (error, key_file_error);
2815 g_strfreev (values);
2816 g_free (int_values);
2818 return NULL;
2821 g_strfreev (values);
2823 if (length)
2824 *length = num_ints;
2826 return int_values;
2830 * g_key_file_set_integer_list:
2831 * @key_file: a #GKeyFile
2832 * @group_name: a group name
2833 * @key: a key
2834 * @list: (array length=length): an array of integer values
2835 * @length: number of integer values in @list
2837 * Associates a list of integer values with @key under @group_name.
2838 * If @key cannot be found then it is created.
2840 * Since: 2.6
2842 void
2843 g_key_file_set_integer_list (GKeyFile *key_file,
2844 const gchar *group_name,
2845 const gchar *key,
2846 gint list[],
2847 gsize length)
2849 GString *values;
2850 gsize i;
2852 g_return_if_fail (key_file != NULL);
2853 g_return_if_fail (list != NULL);
2855 values = g_string_sized_new (length * 16);
2856 for (i = 0; i < length; i++)
2858 gchar *value;
2860 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2862 g_string_append (values, value);
2863 g_string_append_c (values, key_file->list_separator);
2865 g_free (value);
2868 g_key_file_set_value (key_file, group_name, key, values->str);
2869 g_string_free (values, TRUE);
2873 * g_key_file_get_double:
2874 * @key_file: a #GKeyFile
2875 * @group_name: a group name
2876 * @key: a key
2877 * @error: return location for a #GError
2879 * Returns the value associated with @key under @group_name as a
2880 * double. If @group_name is %NULL, the start_group is used.
2882 * If @key cannot be found then 0.0 is returned and @error is set to
2883 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2884 * with @key cannot be interpreted as a double then 0.0 is returned
2885 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2887 * Returns: the value associated with the key as a double, or
2888 * 0.0 if the key was not found or could not be parsed.
2890 * Since: 2.12
2892 gdouble
2893 g_key_file_get_double (GKeyFile *key_file,
2894 const gchar *group_name,
2895 const gchar *key,
2896 GError **error)
2898 GError *key_file_error;
2899 gchar *value;
2900 gdouble double_value;
2902 g_return_val_if_fail (key_file != NULL, -1);
2903 g_return_val_if_fail (group_name != NULL, -1);
2904 g_return_val_if_fail (key != NULL, -1);
2906 key_file_error = NULL;
2908 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2910 if (key_file_error)
2912 g_propagate_error (error, key_file_error);
2913 return 0;
2916 double_value = g_key_file_parse_value_as_double (key_file, value,
2917 &key_file_error);
2918 g_free (value);
2920 if (key_file_error)
2922 if (g_error_matches (key_file_error,
2923 G_KEY_FILE_ERROR,
2924 G_KEY_FILE_ERROR_INVALID_VALUE))
2926 g_set_error (error, G_KEY_FILE_ERROR,
2927 G_KEY_FILE_ERROR_INVALID_VALUE,
2928 _("Key file contains key '%s' in group '%s' "
2929 "which has a value that cannot be interpreted."),
2930 key, group_name);
2931 g_error_free (key_file_error);
2933 else
2934 g_propagate_error (error, key_file_error);
2937 return double_value;
2941 * g_key_file_set_double:
2942 * @key_file: a #GKeyFile
2943 * @group_name: a group name
2944 * @key: a key
2945 * @value: an double value
2947 * Associates a new double value with @key under @group_name.
2948 * If @key cannot be found then it is created.
2950 * Since: 2.12
2952 void
2953 g_key_file_set_double (GKeyFile *key_file,
2954 const gchar *group_name,
2955 const gchar *key,
2956 gdouble value)
2958 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2960 g_return_if_fail (key_file != NULL);
2962 g_ascii_dtostr (result, sizeof (result), value);
2963 g_key_file_set_value (key_file, group_name, key, result);
2967 * g_key_file_get_double_list:
2968 * @key_file: a #GKeyFile
2969 * @group_name: a group name
2970 * @key: a key
2971 * @length: (out): the number of doubles returned
2972 * @error: return location for a #GError
2974 * Returns the values associated with @key under @group_name as
2975 * doubles.
2977 * If @key cannot be found then %NULL is returned and @error is set to
2978 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2979 * with @key cannot be interpreted as doubles then %NULL is returned
2980 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2982 * Returns: (array length=length) (element-type gdouble) (transfer container):
2983 * the values associated with the key as a list of doubles, or %NULL if the
2984 * key was not found or could not be parsed. The returned list of doubles
2985 * should be freed with g_free() when no longer needed.
2987 * Since: 2.12
2989 gdouble *
2990 g_key_file_get_double_list (GKeyFile *key_file,
2991 const gchar *group_name,
2992 const gchar *key,
2993 gsize *length,
2994 GError **error)
2996 GError *key_file_error = NULL;
2997 gchar **values;
2998 gdouble *double_values;
2999 gsize i, num_doubles;
3001 g_return_val_if_fail (key_file != NULL, NULL);
3002 g_return_val_if_fail (group_name != NULL, NULL);
3003 g_return_val_if_fail (key != NULL, NULL);
3005 if (length)
3006 *length = 0;
3008 values = g_key_file_get_string_list (key_file, group_name, key,
3009 &num_doubles, &key_file_error);
3011 if (key_file_error)
3012 g_propagate_error (error, key_file_error);
3014 if (!values)
3015 return NULL;
3017 double_values = g_new (gdouble, num_doubles);
3019 for (i = 0; i < num_doubles; i++)
3021 double_values[i] = g_key_file_parse_value_as_double (key_file,
3022 values[i],
3023 &key_file_error);
3025 if (key_file_error)
3027 g_propagate_error (error, key_file_error);
3028 g_strfreev (values);
3029 g_free (double_values);
3031 return NULL;
3034 g_strfreev (values);
3036 if (length)
3037 *length = num_doubles;
3039 return double_values;
3043 * g_key_file_set_double_list:
3044 * @key_file: a #GKeyFile
3045 * @group_name: a group name
3046 * @key: a key
3047 * @list: (array length=length): an array of double values
3048 * @length: number of double values in @list
3050 * Associates a list of double values with @key under
3051 * @group_name. If @key cannot be found then it is created.
3053 * Since: 2.12
3055 void
3056 g_key_file_set_double_list (GKeyFile *key_file,
3057 const gchar *group_name,
3058 const gchar *key,
3059 gdouble list[],
3060 gsize length)
3062 GString *values;
3063 gsize i;
3065 g_return_if_fail (key_file != NULL);
3066 g_return_if_fail (list != NULL);
3068 values = g_string_sized_new (length * 16);
3069 for (i = 0; i < length; i++)
3071 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3073 g_ascii_dtostr( result, sizeof (result), list[i] );
3075 g_string_append (values, result);
3076 g_string_append_c (values, key_file->list_separator);
3079 g_key_file_set_value (key_file, group_name, key, values->str);
3080 g_string_free (values, TRUE);
3083 static gboolean
3084 g_key_file_set_key_comment (GKeyFile *key_file,
3085 const gchar *group_name,
3086 const gchar *key,
3087 const gchar *comment,
3088 GError **error)
3090 GKeyFileGroup *group;
3091 GKeyFileKeyValuePair *pair;
3092 GList *key_node, *comment_node, *tmp;
3094 group = g_key_file_lookup_group (key_file, group_name);
3095 if (!group)
3097 g_set_error (error, G_KEY_FILE_ERROR,
3098 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3099 _("Key file does not have group '%s'"),
3100 group_name ? group_name : "(null)");
3102 return FALSE;
3105 /* First find the key the comments are supposed to be
3106 * associated with
3108 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3110 if (key_node == NULL)
3112 set_not_found_key_error (group->name, key, error);
3113 return FALSE;
3116 /* Then find all the comments already associated with the
3117 * key and free them
3119 tmp = key_node->next;
3120 while (tmp != NULL)
3122 pair = (GKeyFileKeyValuePair *) tmp->data;
3124 if (pair->key != NULL)
3125 break;
3127 comment_node = tmp;
3128 tmp = tmp->next;
3129 g_key_file_remove_key_value_pair_node (key_file, group,
3130 comment_node);
3133 if (comment == NULL)
3134 return TRUE;
3136 /* Now we can add our new comment
3138 pair = g_slice_new (GKeyFileKeyValuePair);
3139 pair->key = NULL;
3140 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3142 key_node = g_list_insert (key_node, pair, 1);
3144 return TRUE;
3147 static gboolean
3148 g_key_file_set_group_comment (GKeyFile *key_file,
3149 const gchar *group_name,
3150 const gchar *comment,
3151 GError **error)
3153 GKeyFileGroup *group;
3155 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3157 group = g_key_file_lookup_group (key_file, group_name);
3158 if (!group)
3160 g_set_error (error, G_KEY_FILE_ERROR,
3161 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3162 _("Key file does not have group '%s'"),
3163 group_name ? group_name : "(null)");
3165 return FALSE;
3168 /* First remove any existing comment
3170 if (group->comment)
3172 g_key_file_key_value_pair_free (group->comment);
3173 group->comment = NULL;
3176 if (comment == NULL)
3177 return TRUE;
3179 /* Now we can add our new comment
3181 group->comment = g_slice_new (GKeyFileKeyValuePair);
3182 group->comment->key = NULL;
3183 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3185 return TRUE;
3188 static gboolean
3189 g_key_file_set_top_comment (GKeyFile *key_file,
3190 const gchar *comment,
3191 GError **error)
3193 GList *group_node;
3194 GKeyFileGroup *group;
3195 GKeyFileKeyValuePair *pair;
3197 /* The last group in the list should be the top (comments only)
3198 * group in the file
3200 g_warn_if_fail (key_file->groups != NULL);
3201 group_node = g_list_last (key_file->groups);
3202 group = (GKeyFileGroup *) group_node->data;
3203 g_warn_if_fail (group->name == NULL);
3205 /* Note all keys must be comments at the top of
3206 * the file, so we can just free it all.
3208 g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3209 group->key_value_pairs = NULL;
3211 if (comment == NULL)
3212 return TRUE;
3214 pair = g_slice_new (GKeyFileKeyValuePair);
3215 pair->key = NULL;
3216 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3218 group->key_value_pairs =
3219 g_list_prepend (group->key_value_pairs, pair);
3221 return TRUE;
3225 * g_key_file_set_comment:
3226 * @key_file: a #GKeyFile
3227 * @group_name: (allow-none): a group name, or %NULL
3228 * @key: (allow-none): a key
3229 * @comment: a comment
3230 * @error: return location for a #GError
3232 * Places a comment above @key from @group_name.
3234 * If @key is %NULL then @comment will be written above @group_name.
3235 * If both @key and @group_name are %NULL, then @comment will be
3236 * written above the first group in the file.
3238 * Note that this function prepends a '#' comment marker to
3239 * each line of @comment.
3241 * Returns: %TRUE if the comment was written, %FALSE otherwise
3243 * Since: 2.6
3245 gboolean
3246 g_key_file_set_comment (GKeyFile *key_file,
3247 const gchar *group_name,
3248 const gchar *key,
3249 const gchar *comment,
3250 GError **error)
3252 g_return_val_if_fail (key_file != NULL, FALSE);
3254 if (group_name != NULL && key != NULL)
3256 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3257 return FALSE;
3259 else if (group_name != NULL)
3261 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3262 return FALSE;
3264 else
3266 if (!g_key_file_set_top_comment (key_file, comment, error))
3267 return FALSE;
3270 return TRUE;
3273 static gchar *
3274 g_key_file_get_key_comment (GKeyFile *key_file,
3275 const gchar *group_name,
3276 const gchar *key,
3277 GError **error)
3279 GKeyFileGroup *group;
3280 GKeyFileKeyValuePair *pair;
3281 GList *key_node, *tmp;
3282 GString *string;
3283 gchar *comment;
3285 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3287 group = g_key_file_lookup_group (key_file, group_name);
3288 if (!group)
3290 g_set_error (error, G_KEY_FILE_ERROR,
3291 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3292 _("Key file does not have group '%s'"),
3293 group_name ? group_name : "(null)");
3295 return NULL;
3298 /* First find the key the comments are supposed to be
3299 * associated with
3301 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3303 if (key_node == NULL)
3305 set_not_found_key_error (group->name, key, error);
3306 return NULL;
3309 string = NULL;
3311 /* Then find all the comments already associated with the
3312 * key and concatentate them.
3314 tmp = key_node->next;
3315 if (!key_node->next)
3316 return NULL;
3318 pair = (GKeyFileKeyValuePair *) tmp->data;
3319 if (pair->key != NULL)
3320 return NULL;
3322 while (tmp->next)
3324 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3326 if (pair->key != NULL)
3327 break;
3329 tmp = tmp->next;
3332 while (tmp != key_node)
3334 pair = (GKeyFileKeyValuePair *) tmp->data;
3336 if (string == NULL)
3337 string = g_string_sized_new (512);
3339 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3340 g_string_append (string, comment);
3341 g_free (comment);
3343 tmp = tmp->prev;
3346 if (string != NULL)
3348 comment = string->str;
3349 g_string_free (string, FALSE);
3351 else
3352 comment = NULL;
3354 return comment;
3357 static gchar *
3358 get_group_comment (GKeyFile *key_file,
3359 GKeyFileGroup *group,
3360 GError **error)
3362 GString *string;
3363 GList *tmp;
3364 gchar *comment;
3366 string = NULL;
3368 tmp = group->key_value_pairs;
3369 while (tmp)
3371 GKeyFileKeyValuePair *pair;
3373 pair = (GKeyFileKeyValuePair *) tmp->data;
3375 if (pair->key != NULL)
3377 tmp = tmp->prev;
3378 break;
3381 if (tmp->next == NULL)
3382 break;
3384 tmp = tmp->next;
3387 while (tmp != NULL)
3389 GKeyFileKeyValuePair *pair;
3391 pair = (GKeyFileKeyValuePair *) tmp->data;
3393 if (string == NULL)
3394 string = g_string_sized_new (512);
3396 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3397 g_string_append (string, comment);
3398 g_free (comment);
3400 tmp = tmp->prev;
3403 if (string != NULL)
3404 return g_string_free (string, FALSE);
3406 return NULL;
3409 static gchar *
3410 g_key_file_get_group_comment (GKeyFile *key_file,
3411 const gchar *group_name,
3412 GError **error)
3414 GList *group_node;
3415 GKeyFileGroup *group;
3417 group = g_key_file_lookup_group (key_file, group_name);
3418 if (!group)
3420 g_set_error (error, G_KEY_FILE_ERROR,
3421 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3422 _("Key file does not have group '%s'"),
3423 group_name ? group_name : "(null)");
3425 return NULL;
3428 if (group->comment)
3429 return g_strdup (group->comment->value);
3431 group_node = g_key_file_lookup_group_node (key_file, group_name);
3432 group_node = group_node->next;
3433 group = (GKeyFileGroup *)group_node->data;
3434 return get_group_comment (key_file, group, error);
3437 static gchar *
3438 g_key_file_get_top_comment (GKeyFile *key_file,
3439 GError **error)
3441 GList *group_node;
3442 GKeyFileGroup *group;
3444 /* The last group in the list should be the top (comments only)
3445 * group in the file
3447 g_warn_if_fail (key_file->groups != NULL);
3448 group_node = g_list_last (key_file->groups);
3449 group = (GKeyFileGroup *) group_node->data;
3450 g_warn_if_fail (group->name == NULL);
3452 return get_group_comment (key_file, group, error);
3456 * g_key_file_get_comment:
3457 * @key_file: a #GKeyFile
3458 * @group_name: (allow-none): a group name, or %NULL
3459 * @key: a key
3460 * @error: return location for a #GError
3462 * Retrieves a comment above @key from @group_name.
3463 * If @key is %NULL then @comment will be read from above
3464 * @group_name. If both @key and @group_name are %NULL, then
3465 * @comment will be read from above the first group in the file.
3467 * Note that the returned string includes the '#' comment markers.
3469 * Returns: a comment that should be freed with g_free()
3471 * Since: 2.6
3473 gchar *
3474 g_key_file_get_comment (GKeyFile *key_file,
3475 const gchar *group_name,
3476 const gchar *key,
3477 GError **error)
3479 g_return_val_if_fail (key_file != NULL, NULL);
3481 if (group_name != NULL && key != NULL)
3482 return g_key_file_get_key_comment (key_file, group_name, key, error);
3483 else if (group_name != NULL)
3484 return g_key_file_get_group_comment (key_file, group_name, error);
3485 else
3486 return g_key_file_get_top_comment (key_file, error);
3490 * g_key_file_remove_comment:
3491 * @key_file: a #GKeyFile
3492 * @group_name: (allow-none): a group name, or %NULL
3493 * @key: (allow-none): a key
3494 * @error: return location for a #GError
3496 * Removes a comment above @key from @group_name.
3497 * If @key is %NULL then @comment will be removed above @group_name.
3498 * If both @key and @group_name are %NULL, then @comment will
3499 * be removed above the first group in the file.
3501 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3503 * Since: 2.6
3506 gboolean
3507 g_key_file_remove_comment (GKeyFile *key_file,
3508 const gchar *group_name,
3509 const gchar *key,
3510 GError **error)
3512 g_return_val_if_fail (key_file != NULL, FALSE);
3514 if (group_name != NULL && key != NULL)
3515 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3516 else if (group_name != NULL)
3517 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3518 else
3519 return g_key_file_set_top_comment (key_file, NULL, error);
3523 * g_key_file_has_group:
3524 * @key_file: a #GKeyFile
3525 * @group_name: a group name
3527 * Looks whether the key file has the group @group_name.
3529 * Returns: %TRUE if @group_name is a part of @key_file, %FALSE
3530 * otherwise.
3531 * Since: 2.6
3533 gboolean
3534 g_key_file_has_group (GKeyFile *key_file,
3535 const gchar *group_name)
3537 g_return_val_if_fail (key_file != NULL, FALSE);
3538 g_return_val_if_fail (group_name != NULL, FALSE);
3540 return g_key_file_lookup_group (key_file, group_name) != NULL;
3543 /* This code remains from a historical attempt to add a new public API
3544 * which respects the GError rules.
3546 static gboolean
3547 g_key_file_has_key_full (GKeyFile *key_file,
3548 const gchar *group_name,
3549 const gchar *key,
3550 gboolean *has_key,
3551 GError **error)
3553 GKeyFileKeyValuePair *pair;
3554 GKeyFileGroup *group;
3556 g_return_val_if_fail (key_file != NULL, FALSE);
3557 g_return_val_if_fail (group_name != NULL, FALSE);
3558 g_return_val_if_fail (key != NULL, FALSE);
3560 group = g_key_file_lookup_group (key_file, group_name);
3562 if (!group)
3564 g_set_error (error, G_KEY_FILE_ERROR,
3565 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3566 _("Key file does not have group '%s'"),
3567 group_name);
3569 return FALSE;
3572 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3574 if (has_key)
3575 *has_key = pair != NULL;
3576 return TRUE;
3580 * g_key_file_has_key: (skip)
3581 * @key_file: a #GKeyFile
3582 * @group_name: a group name
3583 * @key: a key name
3584 * @error: return location for a #GError
3586 * Looks whether the key file has the key @key in the group
3587 * @group_name.
3589 * Note that this function does not follow the rules for #GError strictly;
3590 * the return value both carries meaning and signals an error. To use
3591 * this function, you must pass a #GError pointer in @error, and check
3592 * whether it is not %NULL to see if an error occurred.
3594 * Language bindings should use g_key_file_get_value() to test whether
3595 * or not a key exists.
3597 * Returns: %TRUE if @key is a part of @group_name, %FALSE otherwise
3599 * Since: 2.6
3601 gboolean
3602 g_key_file_has_key (GKeyFile *key_file,
3603 const gchar *group_name,
3604 const gchar *key,
3605 GError **error)
3607 GError *temp_error = NULL;
3608 gboolean has_key;
3610 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3612 return has_key;
3614 else
3616 g_propagate_error (error, temp_error);
3617 return FALSE;
3621 static void
3622 g_key_file_add_group (GKeyFile *key_file,
3623 const gchar *group_name)
3625 GKeyFileGroup *group;
3627 g_return_if_fail (key_file != NULL);
3628 g_return_if_fail (g_key_file_is_group_name (group_name));
3630 group = g_key_file_lookup_group (key_file, group_name);
3631 if (group != NULL)
3633 key_file->current_group = group;
3634 return;
3637 group = g_slice_new0 (GKeyFileGroup);
3638 group->name = g_strdup (group_name);
3639 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3640 key_file->groups = g_list_prepend (key_file->groups, group);
3641 key_file->current_group = group;
3643 if (key_file->start_group == NULL)
3644 key_file->start_group = group;
3646 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3649 static void
3650 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3652 if (pair != NULL)
3654 g_free (pair->key);
3655 g_free (pair->value);
3656 g_slice_free (GKeyFileKeyValuePair, pair);
3660 /* Be careful not to call this function on a node with data in the
3661 * lookup map without removing it from the lookup map, first.
3663 * Some current cases where this warning is not a concern are
3664 * when:
3665 * - the node being removed is a comment node
3666 * - the entire lookup map is getting destroyed soon after
3667 * anyway.
3669 static void
3670 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3671 GKeyFileGroup *group,
3672 GList *pair_node)
3675 GKeyFileKeyValuePair *pair;
3677 pair = (GKeyFileKeyValuePair *) pair_node->data;
3679 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3681 g_warn_if_fail (pair->value != NULL);
3683 g_key_file_key_value_pair_free (pair);
3685 g_list_free_1 (pair_node);
3688 static void
3689 g_key_file_remove_group_node (GKeyFile *key_file,
3690 GList *group_node)
3692 GKeyFileGroup *group;
3693 GList *tmp;
3695 group = (GKeyFileGroup *) group_node->data;
3697 if (group->name)
3698 g_hash_table_remove (key_file->group_hash, group->name);
3700 /* If the current group gets deleted make the current group the last
3701 * added group.
3703 if (key_file->current_group == group)
3705 /* groups should always contain at least the top comment group,
3706 * unless g_key_file_clear has been called
3708 if (key_file->groups)
3709 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3710 else
3711 key_file->current_group = NULL;
3714 /* If the start group gets deleted make the start group the first
3715 * added group.
3717 if (key_file->start_group == group)
3719 tmp = g_list_last (key_file->groups);
3720 while (tmp != NULL)
3722 if (tmp != group_node &&
3723 ((GKeyFileGroup *) tmp->data)->name != NULL)
3724 break;
3726 tmp = tmp->prev;
3729 if (tmp)
3730 key_file->start_group = (GKeyFileGroup *) tmp->data;
3731 else
3732 key_file->start_group = NULL;
3735 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3737 tmp = group->key_value_pairs;
3738 while (tmp != NULL)
3740 GList *pair_node;
3742 pair_node = tmp;
3743 tmp = tmp->next;
3744 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3747 g_warn_if_fail (group->key_value_pairs == NULL);
3749 if (group->comment)
3751 g_key_file_key_value_pair_free (group->comment);
3752 group->comment = NULL;
3755 if (group->lookup_map)
3757 g_hash_table_destroy (group->lookup_map);
3758 group->lookup_map = NULL;
3761 g_free ((gchar *) group->name);
3762 g_slice_free (GKeyFileGroup, group);
3763 g_list_free_1 (group_node);
3767 * g_key_file_remove_group:
3768 * @key_file: a #GKeyFile
3769 * @group_name: a group name
3770 * @error: return location for a #GError or %NULL
3772 * Removes the specified group, @group_name,
3773 * from the key file.
3775 * Returns: %TRUE if the group was removed, %FALSE otherwise
3777 * Since: 2.6
3779 gboolean
3780 g_key_file_remove_group (GKeyFile *key_file,
3781 const gchar *group_name,
3782 GError **error)
3784 GList *group_node;
3786 g_return_val_if_fail (key_file != NULL, FALSE);
3787 g_return_val_if_fail (group_name != NULL, FALSE);
3789 group_node = g_key_file_lookup_group_node (key_file, group_name);
3791 if (!group_node)
3793 g_set_error (error, G_KEY_FILE_ERROR,
3794 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3795 _("Key file does not have group '%s'"),
3796 group_name);
3797 return FALSE;
3800 g_key_file_remove_group_node (key_file, group_node);
3802 return TRUE;
3805 static void
3806 g_key_file_add_key_value_pair (GKeyFile *key_file,
3807 GKeyFileGroup *group,
3808 GKeyFileKeyValuePair *pair)
3810 g_hash_table_replace (group->lookup_map, pair->key, pair);
3811 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3814 static void
3815 g_key_file_add_key (GKeyFile *key_file,
3816 GKeyFileGroup *group,
3817 const gchar *key,
3818 const gchar *value)
3820 GKeyFileKeyValuePair *pair;
3822 pair = g_slice_new (GKeyFileKeyValuePair);
3823 pair->key = g_strdup (key);
3824 pair->value = g_strdup (value);
3826 g_key_file_add_key_value_pair (key_file, group, pair);
3830 * g_key_file_remove_key:
3831 * @key_file: a #GKeyFile
3832 * @group_name: a group name
3833 * @key: a key name to remove
3834 * @error: return location for a #GError or %NULL
3836 * Removes @key in @group_name from the key file.
3838 * Returns: %TRUE if the key was removed, %FALSE otherwise
3840 * Since: 2.6
3842 gboolean
3843 g_key_file_remove_key (GKeyFile *key_file,
3844 const gchar *group_name,
3845 const gchar *key,
3846 GError **error)
3848 GKeyFileGroup *group;
3849 GKeyFileKeyValuePair *pair;
3851 g_return_val_if_fail (key_file != NULL, FALSE);
3852 g_return_val_if_fail (group_name != NULL, FALSE);
3853 g_return_val_if_fail (key != NULL, FALSE);
3855 pair = NULL;
3857 group = g_key_file_lookup_group (key_file, group_name);
3858 if (!group)
3860 g_set_error (error, G_KEY_FILE_ERROR,
3861 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3862 _("Key file does not have group '%s'"),
3863 group_name);
3864 return FALSE;
3867 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3869 if (!pair)
3871 set_not_found_key_error (group->name, key, error);
3872 return FALSE;
3875 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3876 g_hash_table_remove (group->lookup_map, pair->key);
3877 g_key_file_key_value_pair_free (pair);
3879 return TRUE;
3882 static GList *
3883 g_key_file_lookup_group_node (GKeyFile *key_file,
3884 const gchar *group_name)
3886 GKeyFileGroup *group;
3887 GList *tmp;
3889 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3891 group = (GKeyFileGroup *) tmp->data;
3893 if (group && group->name && strcmp (group->name, group_name) == 0)
3894 break;
3897 return tmp;
3900 static GKeyFileGroup *
3901 g_key_file_lookup_group (GKeyFile *key_file,
3902 const gchar *group_name)
3904 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3907 static GList *
3908 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3909 GKeyFileGroup *group,
3910 const gchar *key)
3912 GList *key_node;
3914 for (key_node = group->key_value_pairs;
3915 key_node != NULL;
3916 key_node = key_node->next)
3918 GKeyFileKeyValuePair *pair;
3920 pair = (GKeyFileKeyValuePair *) key_node->data;
3922 if (pair->key && strcmp (pair->key, key) == 0)
3923 break;
3926 return key_node;
3929 static GKeyFileKeyValuePair *
3930 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3931 GKeyFileGroup *group,
3932 const gchar *key)
3934 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3937 /* Lines starting with # or consisting entirely of whitespace are merely
3938 * recorded, not parsed. This function assumes all leading whitespace
3939 * has been stripped.
3941 static gboolean
3942 g_key_file_line_is_comment (const gchar *line)
3944 return (*line == '#' || *line == '\0' || *line == '\n');
3947 static gboolean
3948 g_key_file_is_group_name (const gchar *name)
3950 gchar *p, *q;
3952 if (name == NULL)
3953 return FALSE;
3955 p = q = (gchar *) name;
3956 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3957 q = g_utf8_find_next_char (q, NULL);
3959 if (*q != '\0' || q == p)
3960 return FALSE;
3962 return TRUE;
3965 static gboolean
3966 g_key_file_is_key_name (const gchar *name)
3968 gchar *p, *q;
3970 if (name == NULL)
3971 return FALSE;
3973 p = q = (gchar *) name;
3974 /* We accept a little more than the desktop entry spec says,
3975 * since gnome-vfs uses mime-types as keys in its cache.
3977 while (*q && *q != '=' && *q != '[' && *q != ']')
3978 q = g_utf8_find_next_char (q, NULL);
3980 /* No empty keys, please */
3981 if (q == p)
3982 return FALSE;
3984 /* We accept spaces in the middle of keys to not break
3985 * existing apps, but we don't tolerate initial or final
3986 * spaces, which would lead to silent corruption when
3987 * rereading the file.
3989 if (*p == ' ' || q[-1] == ' ')
3990 return FALSE;
3992 if (*q == '[')
3994 q++;
3995 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3996 q = g_utf8_find_next_char (q, NULL);
3998 if (*q != ']')
3999 return FALSE;
4001 q++;
4004 if (*q != '\0')
4005 return FALSE;
4007 return TRUE;
4010 /* A group in a key file is made up of a starting '[' followed by one
4011 * or more letters making up the group name followed by ']'.
4013 static gboolean
4014 g_key_file_line_is_group (const gchar *line)
4016 gchar *p;
4018 p = (gchar *) line;
4019 if (*p != '[')
4020 return FALSE;
4022 p++;
4024 while (*p && *p != ']')
4025 p = g_utf8_find_next_char (p, NULL);
4027 if (*p != ']')
4028 return FALSE;
4030 /* silently accept whitespace after the ] */
4031 p = g_utf8_find_next_char (p, NULL);
4032 while (*p == ' ' || *p == '\t')
4033 p = g_utf8_find_next_char (p, NULL);
4035 if (*p)
4036 return FALSE;
4038 return TRUE;
4041 static gboolean
4042 g_key_file_line_is_key_value_pair (const gchar *line)
4044 gchar *p;
4046 p = (gchar *) g_utf8_strchr (line, -1, '=');
4048 if (!p)
4049 return FALSE;
4051 /* Key must be non-empty
4053 if (*p == line[0])
4054 return FALSE;
4056 return TRUE;
4059 static gchar *
4060 g_key_file_parse_value_as_string (GKeyFile *key_file,
4061 const gchar *value,
4062 GSList **pieces,
4063 GError **error)
4065 gchar *string_value, *p, *q0, *q;
4067 string_value = g_new (gchar, strlen (value) + 1);
4069 p = (gchar *) value;
4070 q0 = q = string_value;
4071 while (*p)
4073 if (*p == '\\')
4075 p++;
4077 switch (*p)
4079 case 's':
4080 *q = ' ';
4081 break;
4083 case 'n':
4084 *q = '\n';
4085 break;
4087 case 't':
4088 *q = '\t';
4089 break;
4091 case 'r':
4092 *q = '\r';
4093 break;
4095 case '\\':
4096 *q = '\\';
4097 break;
4099 case '\0':
4100 g_set_error_literal (error, G_KEY_FILE_ERROR,
4101 G_KEY_FILE_ERROR_INVALID_VALUE,
4102 _("Key file contains escape character "
4103 "at end of line"));
4104 break;
4106 default:
4107 if (pieces && *p == key_file->list_separator)
4108 *q = key_file->list_separator;
4109 else
4111 *q++ = '\\';
4112 *q = *p;
4114 if (*error == NULL)
4116 gchar sequence[3];
4118 sequence[0] = '\\';
4119 sequence[1] = *p;
4120 sequence[2] = '\0';
4122 g_set_error (error, G_KEY_FILE_ERROR,
4123 G_KEY_FILE_ERROR_INVALID_VALUE,
4124 _("Key file contains invalid escape "
4125 "sequence '%s'"), sequence);
4128 break;
4131 else
4133 *q = *p;
4134 if (pieces && (*p == key_file->list_separator))
4136 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4137 q0 = q + 1;
4141 if (*p == '\0')
4142 break;
4144 q++;
4145 p++;
4148 *q = '\0';
4149 if (pieces)
4151 if (q0 < q)
4152 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4153 *pieces = g_slist_reverse (*pieces);
4156 return string_value;
4159 static gchar *
4160 g_key_file_parse_string_as_value (GKeyFile *key_file,
4161 const gchar *string,
4162 gboolean escape_separator)
4164 gchar *value, *p, *q;
4165 gsize length;
4166 gboolean parsing_leading_space;
4168 length = strlen (string) + 1;
4170 /* Worst case would be that every character needs to be escaped.
4171 * In other words every character turns to two characters
4173 value = g_new (gchar, 2 * length);
4175 p = (gchar *) string;
4176 q = value;
4177 parsing_leading_space = TRUE;
4178 while (p < (string + length - 1))
4180 gchar escaped_character[3] = { '\\', 0, 0 };
4182 switch (*p)
4184 case ' ':
4185 if (parsing_leading_space)
4187 escaped_character[1] = 's';
4188 strcpy (q, escaped_character);
4189 q += 2;
4191 else
4193 *q = *p;
4194 q++;
4196 break;
4197 case '\t':
4198 if (parsing_leading_space)
4200 escaped_character[1] = 't';
4201 strcpy (q, escaped_character);
4202 q += 2;
4204 else
4206 *q = *p;
4207 q++;
4209 break;
4210 case '\n':
4211 escaped_character[1] = 'n';
4212 strcpy (q, escaped_character);
4213 q += 2;
4214 break;
4215 case '\r':
4216 escaped_character[1] = 'r';
4217 strcpy (q, escaped_character);
4218 q += 2;
4219 break;
4220 case '\\':
4221 escaped_character[1] = '\\';
4222 strcpy (q, escaped_character);
4223 q += 2;
4224 parsing_leading_space = FALSE;
4225 break;
4226 default:
4227 if (escape_separator && *p == key_file->list_separator)
4229 escaped_character[1] = key_file->list_separator;
4230 strcpy (q, escaped_character);
4231 q += 2;
4232 parsing_leading_space = TRUE;
4234 else
4236 *q = *p;
4237 q++;
4238 parsing_leading_space = FALSE;
4240 break;
4242 p++;
4244 *q = '\0';
4246 return value;
4249 static gint
4250 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4251 const gchar *value,
4252 GError **error)
4254 gchar *eof_int;
4255 glong long_value;
4256 gint int_value;
4258 errno = 0;
4259 long_value = strtol (value, &eof_int, 10);
4261 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4263 gchar *value_utf8 = _g_utf8_make_valid (value);
4264 g_set_error (error, G_KEY_FILE_ERROR,
4265 G_KEY_FILE_ERROR_INVALID_VALUE,
4266 _("Value '%s' cannot be interpreted "
4267 "as a number."), value_utf8);
4268 g_free (value_utf8);
4270 return 0;
4273 int_value = long_value;
4274 if (int_value != long_value || errno == ERANGE)
4276 gchar *value_utf8 = _g_utf8_make_valid (value);
4277 g_set_error (error,
4278 G_KEY_FILE_ERROR,
4279 G_KEY_FILE_ERROR_INVALID_VALUE,
4280 _("Integer value '%s' out of range"),
4281 value_utf8);
4282 g_free (value_utf8);
4284 return 0;
4287 return int_value;
4290 static gchar *
4291 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4292 gint value)
4295 return g_strdup_printf ("%d", value);
4298 static gdouble
4299 g_key_file_parse_value_as_double (GKeyFile *key_file,
4300 const gchar *value,
4301 GError **error)
4303 gchar *end_of_valid_d;
4304 gdouble double_value = 0;
4306 double_value = g_ascii_strtod (value, &end_of_valid_d);
4308 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4310 gchar *value_utf8 = _g_utf8_make_valid (value);
4311 g_set_error (error, G_KEY_FILE_ERROR,
4312 G_KEY_FILE_ERROR_INVALID_VALUE,
4313 _("Value '%s' cannot be interpreted "
4314 "as a float number."),
4315 value_utf8);
4316 g_free (value_utf8);
4319 return double_value;
4322 static gint
4323 strcmp_sized (const gchar *s1, size_t len1, const gchar *s2)
4325 size_t len2 = strlen (s2);
4326 return strncmp (s1, s2, MAX (len1, len2));
4329 static gboolean
4330 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4331 const gchar *value,
4332 GError **error)
4334 gchar *value_utf8;
4335 gint i, length = 0;
4337 /* Count the number of non-whitespace characters */
4338 for (i = 0; value[i]; i++)
4339 if (!g_ascii_isspace (value[i]))
4340 length = i + 1;
4342 if (strcmp_sized (value, length, "true") == 0 || strcmp_sized (value, length, "1") == 0)
4343 return TRUE;
4344 else if (strcmp_sized (value, length, "false") == 0 || strcmp_sized (value, length, "0") == 0)
4345 return FALSE;
4347 value_utf8 = _g_utf8_make_valid (value);
4348 g_set_error (error, G_KEY_FILE_ERROR,
4349 G_KEY_FILE_ERROR_INVALID_VALUE,
4350 _("Value '%s' cannot be interpreted "
4351 "as a boolean."), value_utf8);
4352 g_free (value_utf8);
4354 return FALSE;
4357 static gchar *
4358 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4359 gboolean value)
4361 if (value)
4362 return g_strdup ("true");
4363 else
4364 return g_strdup ("false");
4367 static gchar *
4368 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4369 const gchar *value)
4371 GString *string;
4372 gchar **lines;
4373 gsize i;
4375 string = g_string_sized_new (512);
4377 lines = g_strsplit (value, "\n", 0);
4379 for (i = 0; lines[i] != NULL; i++)
4381 if (lines[i][0] != '#')
4382 g_string_append_printf (string, "%s\n", lines[i]);
4383 else
4384 g_string_append_printf (string, "%s\n", lines[i] + 1);
4386 g_strfreev (lines);
4388 return g_string_free (string, FALSE);
4391 static gchar *
4392 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4393 const gchar *comment)
4395 GString *string;
4396 gchar **lines;
4397 gsize i;
4399 string = g_string_sized_new (512);
4401 lines = g_strsplit (comment, "\n", 0);
4403 for (i = 0; lines[i] != NULL; i++)
4404 g_string_append_printf (string, "#%s%s", lines[i],
4405 lines[i + 1] == NULL? "" : "\n");
4406 g_strfreev (lines);
4408 return g_string_free (string, FALSE);
4412 * g_key_file_save_to_file:
4413 * @key_file: a #GKeyFile
4414 * @filename: the name of the file to write to
4415 * @error: a pointer to a %NULL #GError, or %NULL
4417 * Writes the contents of @key_file to @filename using
4418 * g_file_set_contents().
4420 * This function can fail for any of the reasons that
4421 * g_file_set_contents() may fail.
4423 * Returns: %TRUE if successful, else %FALSE with @error set
4425 * Since: 2.40
4427 gboolean
4428 g_key_file_save_to_file (GKeyFile *key_file,
4429 const gchar *filename,
4430 GError **error)
4432 gchar *contents;
4433 gboolean success;
4434 gsize length;
4436 g_return_val_if_fail (key_file != NULL, FALSE);
4437 g_return_val_if_fail (filename != NULL, FALSE);
4438 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4440 contents = g_key_file_to_data (key_file, &length, NULL);
4441 g_assert (contents != NULL);
4443 success = g_file_set_contents (filename, contents, length, error);
4444 g_free (contents);
4446 return success;