GSimpleProxyResolver: convert docs to markdown
[glib.git] / glib / gkeyfile.c
blobd8c01bbff60626f54adf6479303e602054053277
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 <firstterm>key files</firstterm> for
78 * lack of a better name. Several freedesktop.org specifications use
79 * key files now, e.g the
80 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
81 * Entry Specification</ulink> and the
82 * <ulink url="http://freedesktop.org/Standards/icon-theme-spec">Icon
83 * Theme Specification</ulink>.
85 * The syntax of key files is described in detail in the
86 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
87 * Entry Specification</ulink>, here is a quick summary: Key files
88 * consists of groups of key-value pairs, interspersed with comments.
90 * |[
91 * # this is just an example
92 * # there can be comments before the first group
94 * [First Group]
96 * Name=Key File Example\tthis value shows\nescaping
98 * # localized strings are stored in multiple key-value pairs
99 * Welcome=Hello
100 * Welcome[de]=Hallo
101 * Welcome[fr_FR]=Bonjour
102 * Welcome[it]=Ciao
103 * Welcome[be@latin]=Hello
105 * [Another Group]
107 * Numbers=2;20;-200;0
109 * Booleans=true;false;true;true
110 * ]|
112 * Lines beginning with a '#' and blank lines are considered comments.
114 * Groups are started by a header line containing the group name enclosed
115 * in '[' and ']', and ended implicitly by the start of the next group or
116 * the end of the file. Each key-value pair must be contained in a group.
118 * Key-value pairs generally have the form <literal>key=value</literal>,
119 * with the exception of localized strings, which have the form
120 * <literal>key[locale]=value</literal>, with a locale identifier of the
121 * form <literal>lang_COUNTRY@MODIFIER</literal> where
122 * <literal>COUNTRY</literal> and <literal>MODIFIER</literal> are optional.
123 * Space before and after the '=' character are ignored. Newline, tab,
124 * carriage return and backslash characters in value are escaped as \n,
125 * \t, \r, and \\, respectively. To preserve leading spaces in values,
126 * these can also be escaped as \s.
128 * Key files can store strings (possibly with localized variants), integers,
129 * booleans and lists of these. Lists are separated by a separator character,
130 * typically ';' or ','. To use the list separator character in a value in
131 * a list, it has to be escaped by prefixing it with a backslash.
133 * This syntax is obviously inspired by the .ini files commonly met
134 * on Windows, but there are some important differences:
135 * <itemizedlist>
136 * <listitem>.ini files use the ';' character to begin comments,
137 * key files use the '#' character.</listitem>
138 * <listitem>Key files do not allow for ungrouped keys meaning only
139 * comments can precede the first group.</listitem>
140 * <listitem>Key files are always encoded in UTF-8.</listitem>
141 * <listitem>Key and Group names are case-sensitive. For example, a
142 * group called <literal>[GROUP]</literal> is a different from
143 * <literal>[group]</literal>.</listitem>
144 * <listitem>.ini files don't have a strongly typed boolean entry type,
145 * they only have GetProfileInt(). In key files, only
146 * <literal>true</literal> and <literal>false</literal> (in lower case)
147 * are allowed.</listitem>
148 * </itemizedlist>
150 * Note that in contrast to the
151 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
152 * Entry Specification</ulink>, groups in key files may contain the same
153 * key multiple times; the last entry wins. Key files may also contain
154 * multiple groups with the same name; they are merged together.
155 * Another difference is that keys and group names in key files are not
156 * restricted to ASCII characters.
160 * G_KEY_FILE_ERROR:
162 * Error domain for key file parsing. Errors in this domain will
163 * be from the #GKeyFileError enumeration.
165 * See #GError for information on error domains.
169 * GKeyFileError:
170 * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
171 * an unknown encoding
172 * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
173 * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
174 * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
175 * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
176 * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
178 * Error codes returned by key file parsing.
182 * GKeyFileFlags:
183 * @G_KEY_FILE_NONE: No flags, default behaviour
184 * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
185 * (possibly modified) contents of the key file back to a file;
186 * otherwise all comments will be lost when the key file is
187 * written back.
188 * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
189 * (possibly modified) contents of the key file back to a file;
190 * otherwise only the translations for the current language will be
191 * written back.
193 * Flags which influence the parsing.
197 * G_KEY_FILE_DESKTOP_GROUP:
199 * The name of the main group of a desktop entry file, as defined in the
200 * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
201 * Entry Specification</ulink>. Consult the specification for more
202 * details about the meanings of the keys below.
204 * Since: 2.14
208 * G_KEY_FILE_DESKTOP_KEY_TYPE:
210 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
211 * giving the type of the desktop entry. Usually
212 * #G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
213 * #G_KEY_FILE_DESKTOP_TYPE_LINK, or
214 * #G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
216 * Since: 2.14
220 * G_KEY_FILE_DESKTOP_KEY_VERSION:
222 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
223 * giving the version of the Desktop Entry Specification used for
224 * the desktop entry file.
226 * Since: 2.14
230 * G_KEY_FILE_DESKTOP_KEY_NAME:
232 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
233 * string giving the specific name of the desktop entry.
235 * Since: 2.14
239 * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
241 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
242 * string giving the generic name of the desktop entry.
244 * Since: 2.14
248 * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
250 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
251 * stating whether the desktop entry should be shown in menus.
253 * Since: 2.14
257 * G_KEY_FILE_DESKTOP_KEY_COMMENT:
259 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
260 * string giving the tooltip for the desktop entry.
262 * Since: 2.14
266 * G_KEY_FILE_DESKTOP_KEY_ICON:
268 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
269 * string giving the name of the icon to be displayed for the desktop
270 * entry.
272 * Since: 2.14
276 * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
278 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
279 * stating whether the desktop entry has been deleted by the user.
281 * Since: 2.14
285 * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
287 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
288 * strings identifying the environments that should display the
289 * desktop entry.
291 * Since: 2.14
295 * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
297 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
298 * strings identifying the environments that should not display the
299 * desktop entry.
301 * Since: 2.14
305 * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
307 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
308 * giving the file name of a binary on disk used to determine if the
309 * program is actually installed. It is only valid for desktop entries
310 * with the <literal>Application</literal> type.
312 * Since: 2.14
316 * G_KEY_FILE_DESKTOP_KEY_EXEC:
318 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
319 * giving the command line to execute. It is only valid for desktop
320 * entries with the <literal>Application</literal> type.
322 * Since: 2.14
326 * G_KEY_FILE_DESKTOP_KEY_PATH:
328 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
329 * containing the working directory to run the program in. It is only
330 * valid for desktop entries with the <literal>Application</literal> type.
332 * Since: 2.14
336 * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
338 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
339 * stating whether the program should be run in a terminal window.
340 * It is only valid for desktop entries with the
341 * <literal>Application</literal> type.
343 * Since: 2.14
347 * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
349 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
350 * of strings giving the MIME types supported by this desktop entry.
352 * Since: 2.14
356 * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
358 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
359 * of strings giving the categories in which the desktop entry
360 * should be shown in a menu.
362 * Since: 2.14
366 * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
368 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
369 * stating whether the application supports the <ulink
370 * url="http://www.freedesktop.org/Standards/startup-notification-spec">Startup
371 * Notification Protocol Specification</ulink>.
373 * Since: 2.14
377 * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
379 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is string
380 * identifying the WM class or name hint of a window that the application
381 * will create, which can be used to emulate Startup Notification with
382 * older applications.
384 * Since: 2.14
388 * G_KEY_FILE_DESKTOP_KEY_URL :
390 * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
391 * giving the URL to access. It is only valid for desktop entries
392 * with the <literal>Link</literal> type.
394 * Since: 2.14
398 * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
400 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
401 * entries representing applications.
403 * Since: 2.14
407 * G_KEY_FILE_DESKTOP_TYPE_LINK:
409 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
410 * entries representing links to documents.
412 * Since: 2.14
416 * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
418 * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
419 * entries representing directories.
421 * Since: 2.14
424 typedef struct _GKeyFileGroup GKeyFileGroup;
427 * GKeyFile:
429 * The GKeyFile struct contains only private data
430 * and should not be accessed directly.
432 struct _GKeyFile
434 GList *groups;
435 GHashTable *group_hash;
437 GKeyFileGroup *start_group;
438 GKeyFileGroup *current_group;
440 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
442 gchar list_separator;
444 GKeyFileFlags flags;
446 gchar **locales;
448 volatile gint ref_count;
451 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
453 struct _GKeyFileGroup
455 const gchar *name; /* NULL for above first group (which will be comments) */
457 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
459 GList *key_value_pairs;
461 /* Used in parallel with key_value_pairs for
462 * increased lookup performance
464 GHashTable *lookup_map;
467 struct _GKeyFileKeyValuePair
469 gchar *key; /* NULL for comments */
470 gchar *value;
473 static gint find_file_in_data_dirs (const gchar *file,
474 const gchar **data_dirs,
475 gchar **output_file,
476 GError **error);
477 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
478 gint fd,
479 GKeyFileFlags flags,
480 GError **error);
481 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
482 const gchar *group_name);
483 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
484 const gchar *group_name);
486 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
487 GKeyFileGroup *group,
488 const gchar *key);
489 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
490 GKeyFileGroup *group,
491 const gchar *key);
493 static void g_key_file_remove_group_node (GKeyFile *key_file,
494 GList *group_node);
495 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
496 GKeyFileGroup *group,
497 GList *pair_node);
499 static void g_key_file_add_key_value_pair (GKeyFile *key_file,
500 GKeyFileGroup *group,
501 GKeyFileKeyValuePair *pair);
502 static void g_key_file_add_key (GKeyFile *key_file,
503 GKeyFileGroup *group,
504 const gchar *key,
505 const gchar *value);
506 static void g_key_file_add_group (GKeyFile *key_file,
507 const gchar *group_name);
508 static gboolean g_key_file_is_group_name (const gchar *name);
509 static gboolean g_key_file_is_key_name (const gchar *name);
510 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
511 static gboolean g_key_file_line_is_comment (const gchar *line);
512 static gboolean g_key_file_line_is_group (const gchar *line);
513 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
514 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
515 const gchar *value,
516 GSList **separators,
517 GError **error);
518 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
519 const gchar *string,
520 gboolean escape_separator);
521 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
522 const gchar *value,
523 GError **error);
524 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
525 gint value);
526 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
527 const gchar *value,
528 GError **error);
529 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
530 const gchar *value,
531 GError **error);
532 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
533 gboolean value);
534 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
535 const gchar *value);
536 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
537 const gchar *comment);
538 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
539 const gchar *line,
540 gsize length,
541 GError **error);
542 static void g_key_file_parse_comment (GKeyFile *key_file,
543 const gchar *line,
544 gsize length,
545 GError **error);
546 static void g_key_file_parse_group (GKeyFile *key_file,
547 const gchar *line,
548 gsize length,
549 GError **error);
550 static gchar *key_get_locale (const gchar *key);
551 static void g_key_file_parse_data (GKeyFile *key_file,
552 const gchar *data,
553 gsize length,
554 GError **error);
555 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
556 GError **error);
558 G_DEFINE_QUARK (g-key-file-error-quark, g_key_file_error)
560 static void
561 g_key_file_init (GKeyFile *key_file)
563 key_file->current_group = g_slice_new0 (GKeyFileGroup);
564 key_file->groups = g_list_prepend (NULL, key_file->current_group);
565 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
566 key_file->start_group = NULL;
567 key_file->parse_buffer = g_string_sized_new (128);
568 key_file->list_separator = ';';
569 key_file->flags = 0;
570 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
573 static void
574 g_key_file_clear (GKeyFile *key_file)
576 GList *tmp, *group_node;
578 if (key_file->locales)
580 g_strfreev (key_file->locales);
581 key_file->locales = NULL;
584 if (key_file->parse_buffer)
586 g_string_free (key_file->parse_buffer, TRUE);
587 key_file->parse_buffer = NULL;
590 tmp = key_file->groups;
591 while (tmp != NULL)
593 group_node = tmp;
594 tmp = tmp->next;
595 g_key_file_remove_group_node (key_file, group_node);
598 if (key_file->group_hash != NULL)
600 g_hash_table_destroy (key_file->group_hash);
601 key_file->group_hash = NULL;
604 g_warn_if_fail (key_file->groups == NULL);
609 * g_key_file_new:
611 * Creates a new empty #GKeyFile object. Use
612 * g_key_file_load_from_file(), g_key_file_load_from_data(),
613 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
614 * read an existing key file.
616 * Return value: (transfer full): an empty #GKeyFile.
618 * Since: 2.6
620 GKeyFile *
621 g_key_file_new (void)
623 GKeyFile *key_file;
625 key_file = g_slice_new0 (GKeyFile);
626 key_file->ref_count = 1;
627 g_key_file_init (key_file);
629 return key_file;
633 * g_key_file_set_list_separator:
634 * @key_file: a #GKeyFile
635 * @separator: the separator
637 * Sets the character which is used to separate
638 * values in lists. Typically ';' or ',' are used
639 * as separators. The default list separator is ';'.
641 * Since: 2.6
643 void
644 g_key_file_set_list_separator (GKeyFile *key_file,
645 gchar separator)
647 g_return_if_fail (key_file != NULL);
649 key_file->list_separator = separator;
653 /* Iterates through all the directories in *dirs trying to
654 * open file. When it successfully locates and opens a file it
655 * returns the file descriptor to the open file. It also
656 * outputs the absolute path of the file in output_file.
658 static gint
659 find_file_in_data_dirs (const gchar *file,
660 const gchar **dirs,
661 gchar **output_file,
662 GError **error)
664 const gchar **data_dirs, *data_dir;
665 gchar *path;
666 gint fd;
668 path = NULL;
669 fd = -1;
671 if (dirs == NULL)
672 return fd;
674 data_dirs = dirs;
676 while (data_dirs && (data_dir = *data_dirs) && fd == -1)
678 gchar *candidate_file, *sub_dir;
680 candidate_file = (gchar *) file;
681 sub_dir = g_strdup ("");
682 while (candidate_file != NULL && fd == -1)
684 gchar *p;
686 path = g_build_filename (data_dir, sub_dir,
687 candidate_file, NULL);
689 fd = g_open (path, O_RDONLY, 0);
691 if (fd == -1)
693 g_free (path);
694 path = NULL;
697 candidate_file = strchr (candidate_file, '-');
699 if (candidate_file == NULL)
700 break;
702 candidate_file++;
704 g_free (sub_dir);
705 sub_dir = g_strndup (file, candidate_file - file - 1);
707 for (p = sub_dir; *p != '\0'; p++)
709 if (*p == '-')
710 *p = G_DIR_SEPARATOR;
713 g_free (sub_dir);
714 data_dirs++;
717 if (fd == -1)
719 g_set_error_literal (error, G_KEY_FILE_ERROR,
720 G_KEY_FILE_ERROR_NOT_FOUND,
721 _("Valid key file could not be "
722 "found in search dirs"));
725 if (output_file != NULL && fd > 0)
726 *output_file = g_strdup (path);
728 g_free (path);
730 return fd;
733 static gboolean
734 g_key_file_load_from_fd (GKeyFile *key_file,
735 gint fd,
736 GKeyFileFlags flags,
737 GError **error)
739 GError *key_file_error = NULL;
740 gssize bytes_read;
741 struct stat stat_buf;
742 gchar read_buf[4096];
743 gchar list_separator;
745 if (fstat (fd, &stat_buf) < 0)
747 g_set_error_literal (error, G_FILE_ERROR,
748 g_file_error_from_errno (errno),
749 g_strerror (errno));
750 return FALSE;
753 if (!S_ISREG (stat_buf.st_mode))
755 g_set_error_literal (error, G_KEY_FILE_ERROR,
756 G_KEY_FILE_ERROR_PARSE,
757 _("Not a regular file"));
758 return FALSE;
761 list_separator = key_file->list_separator;
762 g_key_file_clear (key_file);
763 g_key_file_init (key_file);
764 key_file->list_separator = list_separator;
765 key_file->flags = flags;
769 bytes_read = read (fd, read_buf, 4096);
771 if (bytes_read == 0) /* End of File */
772 break;
774 if (bytes_read < 0)
776 if (errno == EINTR || errno == EAGAIN)
777 continue;
779 g_set_error_literal (error, G_FILE_ERROR,
780 g_file_error_from_errno (errno),
781 g_strerror (errno));
782 return FALSE;
785 g_key_file_parse_data (key_file,
786 read_buf, bytes_read,
787 &key_file_error);
789 while (!key_file_error);
791 if (key_file_error)
793 g_propagate_error (error, key_file_error);
794 return FALSE;
797 g_key_file_flush_parse_buffer (key_file, &key_file_error);
799 if (key_file_error)
801 g_propagate_error (error, key_file_error);
802 return FALSE;
805 return TRUE;
809 * g_key_file_load_from_file:
810 * @key_file: an empty #GKeyFile struct
811 * @file: (type filename): the path of a filename to load, in the GLib filename encoding
812 * @flags: flags from #GKeyFileFlags
813 * @error: return location for a #GError, or %NULL
815 * Loads a key file into an empty #GKeyFile structure.
816 * If the file could not be loaded then @error is set to
817 * either a #GFileError or #GKeyFileError.
819 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
821 * Since: 2.6
823 gboolean
824 g_key_file_load_from_file (GKeyFile *key_file,
825 const gchar *file,
826 GKeyFileFlags flags,
827 GError **error)
829 GError *key_file_error = NULL;
830 gint fd;
832 g_return_val_if_fail (key_file != NULL, FALSE);
833 g_return_val_if_fail (file != NULL, FALSE);
835 fd = g_open (file, O_RDONLY, 0);
837 if (fd == -1)
839 g_set_error_literal (error, G_FILE_ERROR,
840 g_file_error_from_errno (errno),
841 g_strerror (errno));
842 return FALSE;
845 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
846 close (fd);
848 if (key_file_error)
850 g_propagate_error (error, key_file_error);
851 return FALSE;
854 return TRUE;
858 * g_key_file_load_from_data:
859 * @key_file: an empty #GKeyFile struct
860 * @data: key file loaded in memory
861 * @length: the length of @data in bytes (or (gsize)-1 if data is nul-terminated)
862 * @flags: flags from #GKeyFileFlags
863 * @error: return location for a #GError, or %NULL
865 * Loads a key file from memory into an empty #GKeyFile structure.
866 * If the object cannot be created then %error is set to a #GKeyFileError.
868 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
870 * Since: 2.6
872 gboolean
873 g_key_file_load_from_data (GKeyFile *key_file,
874 const gchar *data,
875 gsize length,
876 GKeyFileFlags flags,
877 GError **error)
879 GError *key_file_error = NULL;
880 gchar list_separator;
882 g_return_val_if_fail (key_file != NULL, FALSE);
883 g_return_val_if_fail (data != NULL || length == 0, FALSE);
885 if (length == (gsize)-1)
886 length = strlen (data);
888 list_separator = key_file->list_separator;
889 g_key_file_clear (key_file);
890 g_key_file_init (key_file);
891 key_file->list_separator = list_separator;
892 key_file->flags = flags;
894 g_key_file_parse_data (key_file, data, length, &key_file_error);
896 if (key_file_error)
898 g_propagate_error (error, key_file_error);
899 return FALSE;
902 g_key_file_flush_parse_buffer (key_file, &key_file_error);
904 if (key_file_error)
906 g_propagate_error (error, key_file_error);
907 return FALSE;
910 return TRUE;
914 * g_key_file_load_from_dirs:
915 * @key_file: an empty #GKeyFile struct
916 * @file: (type filename): a relative path to a filename to open and parse
917 * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
918 * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
919 * of the file, or %NULL
920 * @flags: flags from #GKeyFileFlags
921 * @error: return location for a #GError, or %NULL
923 * This function looks for a key file named @file in the paths
924 * specified in @search_dirs, loads the file into @key_file and
925 * returns the file's full path in @full_path. If the file could not
926 * be loaded then an %error is set to either a #GFileError or
927 * #GKeyFileError.
929 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
931 * Since: 2.14
933 gboolean
934 g_key_file_load_from_dirs (GKeyFile *key_file,
935 const gchar *file,
936 const gchar **search_dirs,
937 gchar **full_path,
938 GKeyFileFlags flags,
939 GError **error)
941 GError *key_file_error = NULL;
942 const gchar **data_dirs;
943 gchar *output_path;
944 gint fd;
945 gboolean found_file;
947 g_return_val_if_fail (key_file != NULL, FALSE);
948 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
949 g_return_val_if_fail (search_dirs != NULL, FALSE);
951 found_file = FALSE;
952 data_dirs = search_dirs;
953 output_path = NULL;
954 while (*data_dirs != NULL && !found_file)
956 g_free (output_path);
958 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
959 &key_file_error);
961 if (fd == -1)
963 if (key_file_error)
964 g_propagate_error (error, key_file_error);
965 break;
968 found_file = g_key_file_load_from_fd (key_file, fd, flags,
969 &key_file_error);
970 close (fd);
972 if (key_file_error)
974 g_propagate_error (error, key_file_error);
975 break;
979 if (found_file && full_path)
980 *full_path = output_path;
981 else
982 g_free (output_path);
984 return found_file;
988 * g_key_file_load_from_data_dirs:
989 * @key_file: an empty #GKeyFile struct
990 * @file: (type filename): a relative path to a filename to open and parse
991 * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
992 * of the file, or %NULL
993 * @flags: flags from #GKeyFileFlags
994 * @error: return location for a #GError, or %NULL
996 * This function looks for a key file named @file in the paths
997 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
998 * loads the file into @key_file and returns the file's full path in
999 * @full_path. If the file could not be loaded then an %error is
1000 * set to either a #GFileError or #GKeyFileError.
1002 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1003 * Since: 2.6
1005 gboolean
1006 g_key_file_load_from_data_dirs (GKeyFile *key_file,
1007 const gchar *file,
1008 gchar **full_path,
1009 GKeyFileFlags flags,
1010 GError **error)
1012 gchar **all_data_dirs;
1013 const gchar * user_data_dir;
1014 const gchar * const * system_data_dirs;
1015 gsize i, j;
1016 gboolean found_file;
1018 g_return_val_if_fail (key_file != NULL, FALSE);
1019 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1021 user_data_dir = g_get_user_data_dir ();
1022 system_data_dirs = g_get_system_data_dirs ();
1023 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1025 i = 0;
1026 all_data_dirs[i++] = g_strdup (user_data_dir);
1028 j = 0;
1029 while (system_data_dirs[j] != NULL)
1030 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1031 all_data_dirs[i] = NULL;
1033 found_file = g_key_file_load_from_dirs (key_file,
1034 file,
1035 (const gchar **)all_data_dirs,
1036 full_path,
1037 flags,
1038 error);
1040 g_strfreev (all_data_dirs);
1042 return found_file;
1046 * g_key_file_ref: (skip)
1047 * @key_file: a #GKeyFile
1049 * Increases the reference count of @key_file.
1051 * Returns: the same @key_file.
1053 * Since: 2.32
1055 GKeyFile *
1056 g_key_file_ref (GKeyFile *key_file)
1058 g_return_val_if_fail (key_file != NULL, NULL);
1060 g_atomic_int_inc (&key_file->ref_count);
1062 return key_file;
1066 * g_key_file_free: (skip)
1067 * @key_file: a #GKeyFile
1069 * Clears all keys and groups from @key_file, and decreases the
1070 * reference count by 1. If the reference count reaches zero,
1071 * frees the key file and all its allocated memory.
1073 * Since: 2.6
1075 void
1076 g_key_file_free (GKeyFile *key_file)
1078 g_return_if_fail (key_file != NULL);
1080 g_key_file_clear (key_file);
1081 g_key_file_unref (key_file);
1085 * g_key_file_unref:
1086 * @key_file: a #GKeyFile
1088 * Decreases the reference count of @key_file by 1. If the reference count
1089 * reaches zero, frees the key file and all its allocated memory.
1091 * Since: 2.32
1093 void
1094 g_key_file_unref (GKeyFile *key_file)
1096 g_return_if_fail (key_file != NULL);
1098 if (g_atomic_int_dec_and_test (&key_file->ref_count))
1100 g_key_file_clear (key_file);
1101 g_slice_free (GKeyFile, key_file);
1105 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1106 * true for locales that match those in g_get_language_names().
1108 static gboolean
1109 g_key_file_locale_is_interesting (GKeyFile *key_file,
1110 const gchar *locale)
1112 gsize i;
1114 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1115 return TRUE;
1117 for (i = 0; key_file->locales[i] != NULL; i++)
1119 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
1120 return TRUE;
1123 return FALSE;
1126 static void
1127 g_key_file_parse_line (GKeyFile *key_file,
1128 const gchar *line,
1129 gsize length,
1130 GError **error)
1132 GError *parse_error = NULL;
1133 gchar *line_start;
1135 g_return_if_fail (key_file != NULL);
1136 g_return_if_fail (line != NULL);
1138 line_start = (gchar *) line;
1139 while (g_ascii_isspace (*line_start))
1140 line_start++;
1142 if (g_key_file_line_is_comment (line_start))
1143 g_key_file_parse_comment (key_file, line, length, &parse_error);
1144 else if (g_key_file_line_is_group (line_start))
1145 g_key_file_parse_group (key_file, line_start,
1146 length - (line_start - line),
1147 &parse_error);
1148 else if (g_key_file_line_is_key_value_pair (line_start))
1149 g_key_file_parse_key_value_pair (key_file, line_start,
1150 length - (line_start - line),
1151 &parse_error);
1152 else
1154 gchar *line_utf8 = _g_utf8_make_valid (line);
1155 g_set_error (error, G_KEY_FILE_ERROR,
1156 G_KEY_FILE_ERROR_PARSE,
1157 _("Key file contains line '%s' which is not "
1158 "a key-value pair, group, or comment"),
1159 line_utf8);
1160 g_free (line_utf8);
1162 return;
1165 if (parse_error)
1166 g_propagate_error (error, parse_error);
1169 static void
1170 g_key_file_parse_comment (GKeyFile *key_file,
1171 const gchar *line,
1172 gsize length,
1173 GError **error)
1175 GKeyFileKeyValuePair *pair;
1177 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1178 return;
1180 g_warn_if_fail (key_file->current_group != NULL);
1182 pair = g_slice_new (GKeyFileKeyValuePair);
1183 pair->key = NULL;
1184 pair->value = g_strndup (line, length);
1186 key_file->current_group->key_value_pairs =
1187 g_list_prepend (key_file->current_group->key_value_pairs, pair);
1190 static void
1191 g_key_file_parse_group (GKeyFile *key_file,
1192 const gchar *line,
1193 gsize length,
1194 GError **error)
1196 gchar *group_name;
1197 const gchar *group_name_start, *group_name_end;
1199 /* advance past opening '['
1201 group_name_start = line + 1;
1202 group_name_end = line + length - 1;
1204 while (*group_name_end != ']')
1205 group_name_end--;
1207 group_name = g_strndup (group_name_start,
1208 group_name_end - group_name_start);
1210 if (!g_key_file_is_group_name (group_name))
1212 g_set_error (error, G_KEY_FILE_ERROR,
1213 G_KEY_FILE_ERROR_PARSE,
1214 _("Invalid group name: %s"), group_name);
1215 g_free (group_name);
1216 return;
1219 g_key_file_add_group (key_file, group_name);
1220 g_free (group_name);
1223 static void
1224 g_key_file_parse_key_value_pair (GKeyFile *key_file,
1225 const gchar *line,
1226 gsize length,
1227 GError **error)
1229 gchar *key, *value, *key_end, *value_start, *locale;
1230 gsize key_len, value_len;
1232 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1234 g_set_error_literal (error, G_KEY_FILE_ERROR,
1235 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1236 _("Key file does not start with a group"));
1237 return;
1240 key_end = value_start = strchr (line, '=');
1242 g_warn_if_fail (key_end != NULL);
1244 key_end--;
1245 value_start++;
1247 /* Pull the key name from the line (chomping trailing whitespace)
1249 while (g_ascii_isspace (*key_end))
1250 key_end--;
1252 key_len = key_end - line + 2;
1254 g_warn_if_fail (key_len <= length);
1256 key = g_strndup (line, key_len - 1);
1258 if (!g_key_file_is_key_name (key))
1260 g_set_error (error, G_KEY_FILE_ERROR,
1261 G_KEY_FILE_ERROR_PARSE,
1262 _("Invalid key name: %s"), key);
1263 g_free (key);
1264 return;
1267 /* Pull the value from the line (chugging leading whitespace)
1269 while (g_ascii_isspace (*value_start))
1270 value_start++;
1272 value_len = line + length - value_start + 1;
1274 value = g_strndup (value_start, value_len);
1276 g_warn_if_fail (key_file->start_group != NULL);
1278 if (key_file->current_group
1279 && key_file->current_group->name
1280 && strcmp (key_file->start_group->name,
1281 key_file->current_group->name) == 0
1282 && strcmp (key, "Encoding") == 0)
1284 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
1286 gchar *value_utf8 = _g_utf8_make_valid (value);
1287 g_set_error (error, G_KEY_FILE_ERROR,
1288 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1289 _("Key file contains unsupported "
1290 "encoding '%s'"), value_utf8);
1291 g_free (value_utf8);
1293 g_free (key);
1294 g_free (value);
1295 return;
1299 /* Is this key a translation? If so, is it one that we care about?
1301 locale = key_get_locale (key);
1303 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
1305 GKeyFileKeyValuePair *pair;
1307 pair = g_slice_new (GKeyFileKeyValuePair);
1308 pair->key = key;
1309 pair->value = value;
1311 g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1313 else
1315 g_free (key);
1316 g_free (value);
1319 g_free (locale);
1322 static gchar *
1323 key_get_locale (const gchar *key)
1325 gchar *locale;
1327 locale = g_strrstr (key, "[");
1329 if (locale && strlen (locale) <= 2)
1330 locale = NULL;
1332 if (locale)
1333 locale = g_strndup (locale + 1, strlen (locale) - 2);
1335 return locale;
1338 static void
1339 g_key_file_parse_data (GKeyFile *key_file,
1340 const gchar *data,
1341 gsize length,
1342 GError **error)
1344 GError *parse_error;
1345 gsize i;
1347 g_return_if_fail (key_file != NULL);
1348 g_return_if_fail (data != NULL || length == 0);
1350 parse_error = NULL;
1352 i = 0;
1353 while (i < length)
1355 if (data[i] == '\n')
1357 if (key_file->parse_buffer->len > 0
1358 && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1359 == '\r'))
1360 g_string_erase (key_file->parse_buffer,
1361 key_file->parse_buffer->len - 1,
1364 /* When a newline is encountered flush the parse buffer so that the
1365 * line can be parsed. Note that completely blank lines won't show
1366 * up in the parse buffer, so they get parsed directly.
1368 if (key_file->parse_buffer->len > 0)
1369 g_key_file_flush_parse_buffer (key_file, &parse_error);
1370 else
1371 g_key_file_parse_comment (key_file, "", 1, &parse_error);
1373 if (parse_error)
1375 g_propagate_error (error, parse_error);
1376 return;
1378 i++;
1380 else
1382 const gchar *start_of_line;
1383 const gchar *end_of_line;
1384 gsize line_length;
1386 start_of_line = data + i;
1387 end_of_line = memchr (start_of_line, '\n', length - i);
1389 if (end_of_line == NULL)
1390 end_of_line = data + length;
1392 line_length = end_of_line - start_of_line;
1394 g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1395 i += line_length;
1400 static void
1401 g_key_file_flush_parse_buffer (GKeyFile *key_file,
1402 GError **error)
1404 GError *file_error = NULL;
1406 g_return_if_fail (key_file != NULL);
1408 file_error = NULL;
1410 if (key_file->parse_buffer->len > 0)
1412 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1413 key_file->parse_buffer->len,
1414 &file_error);
1415 g_string_erase (key_file->parse_buffer, 0, -1);
1417 if (file_error)
1419 g_propagate_error (error, file_error);
1420 return;
1426 * g_key_file_to_data:
1427 * @key_file: a #GKeyFile
1428 * @length: (out) (allow-none): return location for the length of the
1429 * returned string, or %NULL
1430 * @error: return location for a #GError, or %NULL
1432 * This function outputs @key_file as a string.
1434 * Note that this function never reports an error,
1435 * so it is safe to pass %NULL as @error.
1437 * Return value: a newly allocated string holding
1438 * the contents of the #GKeyFile
1440 * Since: 2.6
1442 gchar *
1443 g_key_file_to_data (GKeyFile *key_file,
1444 gsize *length,
1445 GError **error)
1447 GString *data_string;
1448 GList *group_node, *key_file_node;
1450 g_return_val_if_fail (key_file != NULL, NULL);
1452 data_string = g_string_new (NULL);
1454 for (group_node = g_list_last (key_file->groups);
1455 group_node != NULL;
1456 group_node = group_node->prev)
1458 GKeyFileGroup *group;
1460 group = (GKeyFileGroup *) group_node->data;
1462 /* separate groups by at least an empty line */
1463 if (data_string->len >= 2 &&
1464 data_string->str[data_string->len - 2] != '\n')
1465 g_string_append_c (data_string, '\n');
1467 if (group->comment != NULL)
1468 g_string_append_printf (data_string, "%s\n", group->comment->value);
1470 if (group->name != NULL)
1471 g_string_append_printf (data_string, "[%s]\n", group->name);
1473 for (key_file_node = g_list_last (group->key_value_pairs);
1474 key_file_node != NULL;
1475 key_file_node = key_file_node->prev)
1477 GKeyFileKeyValuePair *pair;
1479 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1481 if (pair->key != NULL)
1482 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1483 else
1484 g_string_append_printf (data_string, "%s\n", pair->value);
1488 if (length)
1489 *length = data_string->len;
1491 return g_string_free (data_string, FALSE);
1495 * g_key_file_get_keys:
1496 * @key_file: a #GKeyFile
1497 * @group_name: a group name
1498 * @length: (out) (allow-none): return location for the number of keys returned, or %NULL
1499 * @error: return location for a #GError, or %NULL
1501 * Returns all keys for the group name @group_name. The array of
1502 * returned keys will be %NULL-terminated, so @length may
1503 * optionally be %NULL. In the event that the @group_name cannot
1504 * be found, %NULL is returned and @error is set to
1505 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1507 * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1508 * Use g_strfreev() to free it.
1510 * Since: 2.6
1512 gchar **
1513 g_key_file_get_keys (GKeyFile *key_file,
1514 const gchar *group_name,
1515 gsize *length,
1516 GError **error)
1518 GKeyFileGroup *group;
1519 GList *tmp;
1520 gchar **keys;
1521 gsize i, num_keys;
1523 g_return_val_if_fail (key_file != NULL, NULL);
1524 g_return_val_if_fail (group_name != NULL, NULL);
1526 group = g_key_file_lookup_group (key_file, group_name);
1528 if (!group)
1530 g_set_error (error, G_KEY_FILE_ERROR,
1531 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1532 _("Key file does not have group '%s'"),
1533 group_name ? group_name : "(null)");
1534 return NULL;
1537 num_keys = 0;
1538 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1540 GKeyFileKeyValuePair *pair;
1542 pair = (GKeyFileKeyValuePair *) tmp->data;
1544 if (pair->key)
1545 num_keys++;
1548 keys = g_new (gchar *, num_keys + 1);
1550 i = num_keys - 1;
1551 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1553 GKeyFileKeyValuePair *pair;
1555 pair = (GKeyFileKeyValuePair *) tmp->data;
1557 if (pair->key)
1559 keys[i] = g_strdup (pair->key);
1560 i--;
1564 keys[num_keys] = NULL;
1566 if (length)
1567 *length = num_keys;
1569 return keys;
1573 * g_key_file_get_start_group:
1574 * @key_file: a #GKeyFile
1576 * Returns the name of the start group of the file.
1578 * Return value: The start group of the key file.
1580 * Since: 2.6
1582 gchar *
1583 g_key_file_get_start_group (GKeyFile *key_file)
1585 g_return_val_if_fail (key_file != NULL, NULL);
1587 if (key_file->start_group)
1588 return g_strdup (key_file->start_group->name);
1590 return NULL;
1594 * g_key_file_get_groups:
1595 * @key_file: a #GKeyFile
1596 * @length: (out) (allow-none): return location for the number of returned groups, or %NULL
1598 * Returns all groups in the key file loaded with @key_file.
1599 * The array of returned groups will be %NULL-terminated, so
1600 * @length may optionally be %NULL.
1602 * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1603 * Use g_strfreev() to free it.
1604 * Since: 2.6
1606 gchar **
1607 g_key_file_get_groups (GKeyFile *key_file,
1608 gsize *length)
1610 GList *group_node;
1611 gchar **groups;
1612 gsize i, num_groups;
1614 g_return_val_if_fail (key_file != NULL, NULL);
1616 num_groups = g_list_length (key_file->groups);
1618 g_return_val_if_fail (num_groups > 0, NULL);
1620 group_node = g_list_last (key_file->groups);
1622 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1624 /* Only need num_groups instead of num_groups + 1
1625 * because the first group of the file (last in the
1626 * list) is always the comment group at the top,
1627 * which we skip
1629 groups = g_new (gchar *, num_groups);
1632 i = 0;
1633 for (group_node = group_node->prev;
1634 group_node != NULL;
1635 group_node = group_node->prev)
1637 GKeyFileGroup *group;
1639 group = (GKeyFileGroup *) group_node->data;
1641 g_warn_if_fail (group->name != NULL);
1643 groups[i++] = g_strdup (group->name);
1645 groups[i] = NULL;
1647 if (length)
1648 *length = i;
1650 return groups;
1654 * g_key_file_get_value:
1655 * @key_file: a #GKeyFile
1656 * @group_name: a group name
1657 * @key: a key
1658 * @error: return location for a #GError, or %NULL
1660 * Returns the raw value associated with @key under @group_name.
1661 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1663 * In the event the key cannot be found, %NULL is returned and
1664 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1665 * event that the @group_name cannot be found, %NULL is returned
1666 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1669 * Return value: a newly allocated string or %NULL if the specified
1670 * key cannot be found.
1672 * Since: 2.6
1674 gchar *
1675 g_key_file_get_value (GKeyFile *key_file,
1676 const gchar *group_name,
1677 const gchar *key,
1678 GError **error)
1680 GKeyFileGroup *group;
1681 GKeyFileKeyValuePair *pair;
1682 gchar *value = NULL;
1684 g_return_val_if_fail (key_file != NULL, NULL);
1685 g_return_val_if_fail (group_name != NULL, NULL);
1686 g_return_val_if_fail (key != NULL, NULL);
1688 group = g_key_file_lookup_group (key_file, group_name);
1690 if (!group)
1692 g_set_error (error, G_KEY_FILE_ERROR,
1693 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1694 _("Key file does not have group '%s'"),
1695 group_name ? group_name : "(null)");
1696 return NULL;
1699 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1701 if (pair)
1702 value = g_strdup (pair->value);
1703 else
1704 g_set_error (error, G_KEY_FILE_ERROR,
1705 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1706 _("Key file does not have key '%s'"), key);
1708 return value;
1712 * g_key_file_set_value:
1713 * @key_file: a #GKeyFile
1714 * @group_name: a group name
1715 * @key: a key
1716 * @value: a string
1718 * Associates a new value with @key under @group_name.
1720 * If @key cannot be found then it is created. If @group_name cannot
1721 * be found then it is created. To set an UTF-8 string which may contain
1722 * characters that need escaping (such as newlines or spaces), use
1723 * g_key_file_set_string().
1725 * Since: 2.6
1727 void
1728 g_key_file_set_value (GKeyFile *key_file,
1729 const gchar *group_name,
1730 const gchar *key,
1731 const gchar *value)
1733 GKeyFileGroup *group;
1734 GKeyFileKeyValuePair *pair;
1736 g_return_if_fail (key_file != NULL);
1737 g_return_if_fail (g_key_file_is_group_name (group_name));
1738 g_return_if_fail (g_key_file_is_key_name (key));
1739 g_return_if_fail (value != NULL);
1741 group = g_key_file_lookup_group (key_file, group_name);
1743 if (!group)
1745 g_key_file_add_group (key_file, group_name);
1746 group = (GKeyFileGroup *) key_file->groups->data;
1748 g_key_file_add_key (key_file, group, key, value);
1750 else
1752 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1754 if (!pair)
1755 g_key_file_add_key (key_file, group, key, value);
1756 else
1758 g_free (pair->value);
1759 pair->value = g_strdup (value);
1765 * g_key_file_get_string:
1766 * @key_file: a #GKeyFile
1767 * @group_name: a group name
1768 * @key: a key
1769 * @error: return location for a #GError, or %NULL
1771 * Returns the string value associated with @key under @group_name.
1772 * Unlike g_key_file_get_value(), this function handles escape sequences
1773 * like \s.
1775 * In the event the key cannot be found, %NULL is returned and
1776 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1777 * event that the @group_name cannot be found, %NULL is returned
1778 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1780 * Return value: a newly allocated string or %NULL if the specified
1781 * key cannot be found.
1783 * Since: 2.6
1785 gchar *
1786 g_key_file_get_string (GKeyFile *key_file,
1787 const gchar *group_name,
1788 const gchar *key,
1789 GError **error)
1791 gchar *value, *string_value;
1792 GError *key_file_error;
1794 g_return_val_if_fail (key_file != NULL, NULL);
1795 g_return_val_if_fail (group_name != NULL, NULL);
1796 g_return_val_if_fail (key != NULL, NULL);
1798 key_file_error = NULL;
1800 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1802 if (key_file_error)
1804 g_propagate_error (error, key_file_error);
1805 return NULL;
1808 if (!g_utf8_validate (value, -1, NULL))
1810 gchar *value_utf8 = _g_utf8_make_valid (value);
1811 g_set_error (error, G_KEY_FILE_ERROR,
1812 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1813 _("Key file contains key '%s' with value '%s' "
1814 "which is not UTF-8"), key, value_utf8);
1815 g_free (value_utf8);
1816 g_free (value);
1818 return NULL;
1821 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1822 &key_file_error);
1823 g_free (value);
1825 if (key_file_error)
1827 if (g_error_matches (key_file_error,
1828 G_KEY_FILE_ERROR,
1829 G_KEY_FILE_ERROR_INVALID_VALUE))
1831 g_set_error (error, G_KEY_FILE_ERROR,
1832 G_KEY_FILE_ERROR_INVALID_VALUE,
1833 _("Key file contains key '%s' "
1834 "which has a value that cannot be interpreted."),
1835 key);
1836 g_error_free (key_file_error);
1838 else
1839 g_propagate_error (error, key_file_error);
1842 return string_value;
1846 * g_key_file_set_string:
1847 * @key_file: a #GKeyFile
1848 * @group_name: a group name
1849 * @key: a key
1850 * @string: a string
1852 * Associates a new string value with @key under @group_name.
1853 * If @key cannot be found then it is created.
1854 * If @group_name cannot be found then it is created.
1855 * Unlike g_key_file_set_value(), this function handles characters
1856 * that need escaping, such as newlines.
1858 * Since: 2.6
1860 void
1861 g_key_file_set_string (GKeyFile *key_file,
1862 const gchar *group_name,
1863 const gchar *key,
1864 const gchar *string)
1866 gchar *value;
1868 g_return_if_fail (key_file != NULL);
1869 g_return_if_fail (string != NULL);
1871 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1872 g_key_file_set_value (key_file, group_name, key, value);
1873 g_free (value);
1877 * g_key_file_get_string_list:
1878 * @key_file: a #GKeyFile
1879 * @group_name: a group name
1880 * @key: a key
1881 * @length: (out) (allow-none): return location for the number of returned strings, or %NULL
1882 * @error: return location for a #GError, or %NULL
1884 * Returns the values associated with @key under @group_name.
1886 * In the event the key cannot be found, %NULL is returned and
1887 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1888 * event that the @group_name cannot be found, %NULL is returned
1889 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1891 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full):
1892 * a %NULL-terminated string array or %NULL if the specified
1893 * key cannot be found. The array should be freed with g_strfreev().
1895 * Since: 2.6
1897 gchar **
1898 g_key_file_get_string_list (GKeyFile *key_file,
1899 const gchar *group_name,
1900 const gchar *key,
1901 gsize *length,
1902 GError **error)
1904 GError *key_file_error = NULL;
1905 gchar *value, *string_value, **values;
1906 gint i, len;
1907 GSList *p, *pieces = NULL;
1909 g_return_val_if_fail (key_file != NULL, NULL);
1910 g_return_val_if_fail (group_name != NULL, NULL);
1911 g_return_val_if_fail (key != NULL, NULL);
1913 if (length)
1914 *length = 0;
1916 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1918 if (key_file_error)
1920 g_propagate_error (error, key_file_error);
1921 return NULL;
1924 if (!g_utf8_validate (value, -1, NULL))
1926 gchar *value_utf8 = _g_utf8_make_valid (value);
1927 g_set_error (error, G_KEY_FILE_ERROR,
1928 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1929 _("Key file contains key '%s' with value '%s' "
1930 "which is not UTF-8"), key, value_utf8);
1931 g_free (value_utf8);
1932 g_free (value);
1934 return NULL;
1937 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1938 g_free (value);
1939 g_free (string_value);
1941 if (key_file_error)
1943 if (g_error_matches (key_file_error,
1944 G_KEY_FILE_ERROR,
1945 G_KEY_FILE_ERROR_INVALID_VALUE))
1947 g_set_error (error, G_KEY_FILE_ERROR,
1948 G_KEY_FILE_ERROR_INVALID_VALUE,
1949 _("Key file contains key '%s' "
1950 "which has a value that cannot be interpreted."),
1951 key);
1952 g_error_free (key_file_error);
1954 else
1955 g_propagate_error (error, key_file_error);
1957 g_slist_free_full (pieces, g_free);
1958 return NULL;
1961 len = g_slist_length (pieces);
1962 values = g_new (gchar *, len + 1);
1963 for (p = pieces, i = 0; p; p = p->next)
1964 values[i++] = p->data;
1965 values[len] = NULL;
1967 g_slist_free (pieces);
1969 if (length)
1970 *length = len;
1972 return values;
1976 * g_key_file_set_string_list:
1977 * @key_file: a #GKeyFile
1978 * @group_name: a group name
1979 * @key: a key
1980 * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
1981 * @length: number of string values in @list
1983 * Associates a list of string values for @key under @group_name.
1984 * If @key cannot be found then it is created.
1985 * If @group_name cannot be found then it is created.
1987 * Since: 2.6
1989 void
1990 g_key_file_set_string_list (GKeyFile *key_file,
1991 const gchar *group_name,
1992 const gchar *key,
1993 const gchar * const list[],
1994 gsize length)
1996 GString *value_list;
1997 gsize i;
1999 g_return_if_fail (key_file != NULL);
2000 g_return_if_fail (list != NULL || length == 0);
2002 value_list = g_string_sized_new (length * 128);
2003 for (i = 0; i < length && list[i] != NULL; i++)
2005 gchar *value;
2007 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2008 g_string_append (value_list, value);
2009 g_string_append_c (value_list, key_file->list_separator);
2011 g_free (value);
2014 g_key_file_set_value (key_file, group_name, key, value_list->str);
2015 g_string_free (value_list, TRUE);
2019 * g_key_file_set_locale_string:
2020 * @key_file: a #GKeyFile
2021 * @group_name: a group name
2022 * @key: a key
2023 * @locale: a locale identifier
2024 * @string: a string
2026 * Associates a string value for @key and @locale under @group_name.
2027 * If the translation for @key cannot be found then it is created.
2029 * Since: 2.6
2031 void
2032 g_key_file_set_locale_string (GKeyFile *key_file,
2033 const gchar *group_name,
2034 const gchar *key,
2035 const gchar *locale,
2036 const gchar *string)
2038 gchar *full_key, *value;
2040 g_return_if_fail (key_file != NULL);
2041 g_return_if_fail (key != NULL);
2042 g_return_if_fail (locale != NULL);
2043 g_return_if_fail (string != NULL);
2045 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2046 full_key = g_strdup_printf ("%s[%s]", key, locale);
2047 g_key_file_set_value (key_file, group_name, full_key, value);
2048 g_free (full_key);
2049 g_free (value);
2053 * g_key_file_get_locale_string:
2054 * @key_file: a #GKeyFile
2055 * @group_name: a group name
2056 * @key: a key
2057 * @locale: (allow-none): a locale identifier or %NULL
2058 * @error: return location for a #GError, or %NULL
2060 * Returns the value associated with @key under @group_name
2061 * translated in the given @locale if available. If @locale is
2062 * %NULL then the current locale is assumed.
2064 * If @key cannot be found then %NULL is returned and @error is set
2065 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2066 * with @key cannot be interpreted or no suitable translation can
2067 * be found then the untranslated value is returned.
2069 * Return value: a newly allocated string or %NULL if the specified
2070 * key cannot be found.
2072 * Since: 2.6
2074 gchar *
2075 g_key_file_get_locale_string (GKeyFile *key_file,
2076 const gchar *group_name,
2077 const gchar *key,
2078 const gchar *locale,
2079 GError **error)
2081 gchar *candidate_key, *translated_value;
2082 GError *key_file_error;
2083 gchar **languages;
2084 gboolean free_languages = FALSE;
2085 gint i;
2087 g_return_val_if_fail (key_file != NULL, NULL);
2088 g_return_val_if_fail (group_name != NULL, NULL);
2089 g_return_val_if_fail (key != NULL, NULL);
2091 candidate_key = NULL;
2092 translated_value = NULL;
2093 key_file_error = NULL;
2095 if (locale)
2097 languages = g_get_locale_variants (locale);
2098 free_languages = TRUE;
2100 else
2102 languages = (gchar **) g_get_language_names ();
2103 free_languages = FALSE;
2106 for (i = 0; languages[i]; i++)
2108 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2110 translated_value = g_key_file_get_string (key_file,
2111 group_name,
2112 candidate_key, NULL);
2113 g_free (candidate_key);
2115 if (translated_value)
2116 break;
2118 g_free (translated_value);
2119 translated_value = NULL;
2122 /* Fallback to untranslated key
2124 if (!translated_value)
2126 translated_value = g_key_file_get_string (key_file, group_name, key,
2127 &key_file_error);
2129 if (!translated_value)
2130 g_propagate_error (error, key_file_error);
2133 if (free_languages)
2134 g_strfreev (languages);
2136 return translated_value;
2140 * g_key_file_get_locale_string_list:
2141 * @key_file: a #GKeyFile
2142 * @group_name: a group name
2143 * @key: a key
2144 * @locale: (allow-none): a locale identifier or %NULL
2145 * @length: (out) (allow-none): return location for the number of returned strings or %NULL
2146 * @error: return location for a #GError or %NULL
2148 * Returns the values associated with @key under @group_name
2149 * translated in the given @locale if available. If @locale is
2150 * %NULL then the current locale is assumed.
2152 * If @key cannot be found then %NULL is returned and @error is set
2153 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2154 * with @key cannot be interpreted or no suitable translations
2155 * can be found then the untranslated values are returned. The
2156 * returned array is %NULL-terminated, so @length may optionally
2157 * be %NULL.
2159 * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2160 * or %NULL if the key isn't found. The string array should be freed
2161 * with g_strfreev().
2163 * Since: 2.6
2165 gchar **
2166 g_key_file_get_locale_string_list (GKeyFile *key_file,
2167 const gchar *group_name,
2168 const gchar *key,
2169 const gchar *locale,
2170 gsize *length,
2171 GError **error)
2173 GError *key_file_error;
2174 gchar **values, *value;
2175 char list_separator[2];
2176 gsize len;
2178 g_return_val_if_fail (key_file != NULL, NULL);
2179 g_return_val_if_fail (group_name != NULL, NULL);
2180 g_return_val_if_fail (key != NULL, NULL);
2182 key_file_error = NULL;
2184 value = g_key_file_get_locale_string (key_file, group_name,
2185 key, locale,
2186 &key_file_error);
2188 if (key_file_error)
2189 g_propagate_error (error, key_file_error);
2191 if (!value)
2193 if (length)
2194 *length = 0;
2195 return NULL;
2198 len = strlen (value);
2199 if (value[len - 1] == key_file->list_separator)
2200 value[len - 1] = '\0';
2202 list_separator[0] = key_file->list_separator;
2203 list_separator[1] = '\0';
2204 values = g_strsplit (value, list_separator, 0);
2206 g_free (value);
2208 if (length)
2209 *length = g_strv_length (values);
2211 return values;
2215 * g_key_file_set_locale_string_list:
2216 * @key_file: a #GKeyFile
2217 * @group_name: a group name
2218 * @key: a key
2219 * @locale: a locale identifier
2220 * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2221 * @length: the length of @list
2223 * Associates a list of string values for @key and @locale under
2224 * @group_name. If the translation for @key cannot be found then
2225 * it is created.
2227 * Since: 2.6
2229 void
2230 g_key_file_set_locale_string_list (GKeyFile *key_file,
2231 const gchar *group_name,
2232 const gchar *key,
2233 const gchar *locale,
2234 const gchar * const list[],
2235 gsize length)
2237 GString *value_list;
2238 gchar *full_key;
2239 gsize i;
2241 g_return_if_fail (key_file != NULL);
2242 g_return_if_fail (key != NULL);
2243 g_return_if_fail (locale != NULL);
2244 g_return_if_fail (length != 0);
2246 value_list = g_string_sized_new (length * 128);
2247 for (i = 0; i < length && list[i] != NULL; i++)
2249 gchar *value;
2251 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2252 g_string_append (value_list, value);
2253 g_string_append_c (value_list, key_file->list_separator);
2255 g_free (value);
2258 full_key = g_strdup_printf ("%s[%s]", key, locale);
2259 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2260 g_free (full_key);
2261 g_string_free (value_list, TRUE);
2265 * g_key_file_get_boolean:
2266 * @key_file: a #GKeyFile
2267 * @group_name: a group name
2268 * @key: a key
2269 * @error: return location for a #GError
2271 * Returns the value associated with @key under @group_name as a
2272 * boolean.
2274 * If @key cannot be found then %FALSE is returned and @error is set
2275 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2276 * associated with @key cannot be interpreted as a boolean then %FALSE
2277 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2279 * Return value: the value associated with the key as a boolean,
2280 * or %FALSE if the key was not found or could not be parsed.
2282 * Since: 2.6
2284 gboolean
2285 g_key_file_get_boolean (GKeyFile *key_file,
2286 const gchar *group_name,
2287 const gchar *key,
2288 GError **error)
2290 GError *key_file_error = NULL;
2291 gchar *value;
2292 gboolean bool_value;
2294 g_return_val_if_fail (key_file != NULL, FALSE);
2295 g_return_val_if_fail (group_name != NULL, FALSE);
2296 g_return_val_if_fail (key != NULL, FALSE);
2298 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2300 if (!value)
2302 g_propagate_error (error, key_file_error);
2303 return FALSE;
2306 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2307 &key_file_error);
2308 g_free (value);
2310 if (key_file_error)
2312 if (g_error_matches (key_file_error,
2313 G_KEY_FILE_ERROR,
2314 G_KEY_FILE_ERROR_INVALID_VALUE))
2316 g_set_error (error, G_KEY_FILE_ERROR,
2317 G_KEY_FILE_ERROR_INVALID_VALUE,
2318 _("Key file contains key '%s' "
2319 "which has a value that cannot be interpreted."),
2320 key);
2321 g_error_free (key_file_error);
2323 else
2324 g_propagate_error (error, key_file_error);
2327 return bool_value;
2331 * g_key_file_set_boolean:
2332 * @key_file: a #GKeyFile
2333 * @group_name: a group name
2334 * @key: a key
2335 * @value: %TRUE or %FALSE
2337 * Associates a new boolean value with @key under @group_name.
2338 * If @key cannot be found then it is created.
2340 * Since: 2.6
2342 void
2343 g_key_file_set_boolean (GKeyFile *key_file,
2344 const gchar *group_name,
2345 const gchar *key,
2346 gboolean value)
2348 gchar *result;
2350 g_return_if_fail (key_file != NULL);
2352 result = g_key_file_parse_boolean_as_value (key_file, value);
2353 g_key_file_set_value (key_file, group_name, key, result);
2354 g_free (result);
2358 * g_key_file_get_boolean_list:
2359 * @key_file: a #GKeyFile
2360 * @group_name: a group name
2361 * @key: a key
2362 * @length: (out): the number of booleans returned
2363 * @error: return location for a #GError
2365 * Returns the values associated with @key under @group_name as
2366 * booleans.
2368 * If @key cannot be found then %NULL is returned and @error is set to
2369 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2370 * with @key cannot be interpreted as booleans then %NULL is returned
2371 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2373 * Return value: (array length=length) (element-type gboolean) (transfer container):
2374 * the values associated with the key as a list of booleans, or %NULL if the
2375 * key was not found or could not be parsed. The returned list of booleans
2376 * should be freed with g_free() when no longer needed.
2378 * Since: 2.6
2380 gboolean *
2381 g_key_file_get_boolean_list (GKeyFile *key_file,
2382 const gchar *group_name,
2383 const gchar *key,
2384 gsize *length,
2385 GError **error)
2387 GError *key_file_error;
2388 gchar **values;
2389 gboolean *bool_values;
2390 gsize i, num_bools;
2392 g_return_val_if_fail (key_file != NULL, NULL);
2393 g_return_val_if_fail (group_name != NULL, NULL);
2394 g_return_val_if_fail (key != NULL, NULL);
2396 if (length)
2397 *length = 0;
2399 key_file_error = NULL;
2401 values = g_key_file_get_string_list (key_file, group_name, key,
2402 &num_bools, &key_file_error);
2404 if (key_file_error)
2405 g_propagate_error (error, key_file_error);
2407 if (!values)
2408 return NULL;
2410 bool_values = g_new (gboolean, num_bools);
2412 for (i = 0; i < num_bools; i++)
2414 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2415 values[i],
2416 &key_file_error);
2418 if (key_file_error)
2420 g_propagate_error (error, key_file_error);
2421 g_strfreev (values);
2422 g_free (bool_values);
2424 return NULL;
2427 g_strfreev (values);
2429 if (length)
2430 *length = num_bools;
2432 return bool_values;
2436 * g_key_file_set_boolean_list:
2437 * @key_file: a #GKeyFile
2438 * @group_name: a group name
2439 * @key: a key
2440 * @list: (array length=length): an array of boolean values
2441 * @length: length of @list
2443 * Associates a list of boolean values with @key under @group_name.
2444 * If @key cannot be found then it is created.
2445 * If @group_name is %NULL, the start_group is used.
2447 * Since: 2.6
2449 void
2450 g_key_file_set_boolean_list (GKeyFile *key_file,
2451 const gchar *group_name,
2452 const gchar *key,
2453 gboolean list[],
2454 gsize length)
2456 GString *value_list;
2457 gsize i;
2459 g_return_if_fail (key_file != NULL);
2460 g_return_if_fail (list != NULL);
2462 value_list = g_string_sized_new (length * 8);
2463 for (i = 0; i < length; i++)
2465 gchar *value;
2467 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2469 g_string_append (value_list, value);
2470 g_string_append_c (value_list, key_file->list_separator);
2472 g_free (value);
2475 g_key_file_set_value (key_file, group_name, key, value_list->str);
2476 g_string_free (value_list, TRUE);
2480 * g_key_file_get_integer:
2481 * @key_file: a #GKeyFile
2482 * @group_name: a group name
2483 * @key: a key
2484 * @error: return location for a #GError
2486 * Returns the value associated with @key under @group_name as an
2487 * integer.
2489 * If @key cannot be found then 0 is returned and @error is set to
2490 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2491 * with @key cannot be interpreted as an integer then 0 is returned
2492 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2494 * Return value: the value associated with the key as an integer, or
2495 * 0 if the key was not found or could not be parsed.
2497 * Since: 2.6
2499 gint
2500 g_key_file_get_integer (GKeyFile *key_file,
2501 const gchar *group_name,
2502 const gchar *key,
2503 GError **error)
2505 GError *key_file_error;
2506 gchar *value;
2507 gint int_value;
2509 g_return_val_if_fail (key_file != NULL, -1);
2510 g_return_val_if_fail (group_name != NULL, -1);
2511 g_return_val_if_fail (key != NULL, -1);
2513 key_file_error = NULL;
2515 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2517 if (key_file_error)
2519 g_propagate_error (error, key_file_error);
2520 return 0;
2523 int_value = g_key_file_parse_value_as_integer (key_file, value,
2524 &key_file_error);
2525 g_free (value);
2527 if (key_file_error)
2529 if (g_error_matches (key_file_error,
2530 G_KEY_FILE_ERROR,
2531 G_KEY_FILE_ERROR_INVALID_VALUE))
2533 g_set_error (error, G_KEY_FILE_ERROR,
2534 G_KEY_FILE_ERROR_INVALID_VALUE,
2535 _("Key file contains key '%s' in group '%s' "
2536 "which has a value that cannot be interpreted."),
2537 key, group_name);
2538 g_error_free (key_file_error);
2540 else
2541 g_propagate_error (error, key_file_error);
2544 return int_value;
2548 * g_key_file_set_integer:
2549 * @key_file: a #GKeyFile
2550 * @group_name: a group name
2551 * @key: a key
2552 * @value: an integer value
2554 * Associates a new integer value with @key under @group_name.
2555 * If @key cannot be found then it is created.
2557 * Since: 2.6
2559 void
2560 g_key_file_set_integer (GKeyFile *key_file,
2561 const gchar *group_name,
2562 const gchar *key,
2563 gint value)
2565 gchar *result;
2567 g_return_if_fail (key_file != NULL);
2569 result = g_key_file_parse_integer_as_value (key_file, value);
2570 g_key_file_set_value (key_file, group_name, key, result);
2571 g_free (result);
2575 * g_key_file_get_int64:
2576 * @key_file: a non-%NULL #GKeyFile
2577 * @group_name: a non-%NULL group name
2578 * @key: a non-%NULL key
2579 * @error: return location for a #GError
2581 * Returns the value associated with @key under @group_name as a signed
2582 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2583 * 64-bit results without truncation.
2585 * Returns: the value associated with the key as a signed 64-bit integer, or
2586 * 0 if the key was not found or could not be parsed.
2588 * Since: 2.26
2590 gint64
2591 g_key_file_get_int64 (GKeyFile *key_file,
2592 const gchar *group_name,
2593 const gchar *key,
2594 GError **error)
2596 gchar *s, *end;
2597 gint64 v;
2599 g_return_val_if_fail (key_file != NULL, -1);
2600 g_return_val_if_fail (group_name != NULL, -1);
2601 g_return_val_if_fail (key != NULL, -1);
2603 s = g_key_file_get_value (key_file, group_name, key, error);
2605 if (s == NULL)
2606 return 0;
2608 v = g_ascii_strtoll (s, &end, 10);
2610 if (*s == '\0' || *end != '\0')
2612 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2613 _("Key '%s' in group '%s' has value '%s' "
2614 "where %s was expected"),
2615 key, group_name, s, "int64");
2616 g_free (s);
2617 return 0;
2620 g_free (s);
2621 return v;
2625 * g_key_file_set_int64:
2626 * @key_file: a #GKeyFile
2627 * @group_name: a group name
2628 * @key: a key
2629 * @value: an integer value
2631 * Associates a new integer value with @key under @group_name.
2632 * If @key cannot be found then it is created.
2634 * Since: 2.26
2636 void
2637 g_key_file_set_int64 (GKeyFile *key_file,
2638 const gchar *group_name,
2639 const gchar *key,
2640 gint64 value)
2642 gchar *result;
2644 g_return_if_fail (key_file != NULL);
2646 result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2647 g_key_file_set_value (key_file, group_name, key, result);
2648 g_free (result);
2652 * g_key_file_get_uint64:
2653 * @key_file: a non-%NULL #GKeyFile
2654 * @group_name: a non-%NULL group name
2655 * @key: a non-%NULL key
2656 * @error: return location for a #GError
2658 * Returns the value associated with @key under @group_name as an unsigned
2659 * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2660 * large positive results without truncation.
2662 * Returns: the value associated with the key as an unsigned 64-bit integer,
2663 * or 0 if the key was not found or could not be parsed.
2665 * Since: 2.26
2667 guint64
2668 g_key_file_get_uint64 (GKeyFile *key_file,
2669 const gchar *group_name,
2670 const gchar *key,
2671 GError **error)
2673 gchar *s, *end;
2674 guint64 v;
2676 g_return_val_if_fail (key_file != NULL, -1);
2677 g_return_val_if_fail (group_name != NULL, -1);
2678 g_return_val_if_fail (key != NULL, -1);
2680 s = g_key_file_get_value (key_file, group_name, key, error);
2682 if (s == NULL)
2683 return 0;
2685 v = g_ascii_strtoull (s, &end, 10);
2687 if (*s == '\0' || *end != '\0')
2689 g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2690 _("Key '%s' in group '%s' has value '%s' "
2691 "where %s was expected"),
2692 key, group_name, s, "uint64");
2693 g_free (s);
2694 return 0;
2697 g_free (s);
2698 return v;
2702 * g_key_file_set_uint64:
2703 * @key_file: a #GKeyFile
2704 * @group_name: a group name
2705 * @key: a key
2706 * @value: an integer value
2708 * Associates a new integer value with @key under @group_name.
2709 * If @key cannot be found then it is created.
2711 * Since: 2.26
2713 void
2714 g_key_file_set_uint64 (GKeyFile *key_file,
2715 const gchar *group_name,
2716 const gchar *key,
2717 guint64 value)
2719 gchar *result;
2721 g_return_if_fail (key_file != NULL);
2723 result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2724 g_key_file_set_value (key_file, group_name, key, result);
2725 g_free (result);
2729 * g_key_file_get_integer_list:
2730 * @key_file: a #GKeyFile
2731 * @group_name: a group name
2732 * @key: a key
2733 * @length: (out): the number of integers returned
2734 * @error: return location for a #GError
2736 * Returns the values associated with @key under @group_name as
2737 * integers.
2739 * If @key cannot be found then %NULL is returned and @error is set to
2740 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2741 * with @key cannot be interpreted as integers then %NULL is returned
2742 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2744 * Return value: (array length=length) (element-type gint) (transfer container):
2745 * the values associated with the key as a list of integers, or %NULL if
2746 * the key was not found or could not be parsed. The returned list of
2747 * integers should be freed with g_free() when no longer needed.
2749 * Since: 2.6
2751 gint *
2752 g_key_file_get_integer_list (GKeyFile *key_file,
2753 const gchar *group_name,
2754 const gchar *key,
2755 gsize *length,
2756 GError **error)
2758 GError *key_file_error = NULL;
2759 gchar **values;
2760 gint *int_values;
2761 gsize i, num_ints;
2763 g_return_val_if_fail (key_file != NULL, NULL);
2764 g_return_val_if_fail (group_name != NULL, NULL);
2765 g_return_val_if_fail (key != NULL, NULL);
2767 if (length)
2768 *length = 0;
2770 values = g_key_file_get_string_list (key_file, group_name, key,
2771 &num_ints, &key_file_error);
2773 if (key_file_error)
2774 g_propagate_error (error, key_file_error);
2776 if (!values)
2777 return NULL;
2779 int_values = g_new (gint, num_ints);
2781 for (i = 0; i < num_ints; i++)
2783 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2784 values[i],
2785 &key_file_error);
2787 if (key_file_error)
2789 g_propagate_error (error, key_file_error);
2790 g_strfreev (values);
2791 g_free (int_values);
2793 return NULL;
2796 g_strfreev (values);
2798 if (length)
2799 *length = num_ints;
2801 return int_values;
2805 * g_key_file_set_integer_list:
2806 * @key_file: a #GKeyFile
2807 * @group_name: a group name
2808 * @key: a key
2809 * @list: (array length=length): an array of integer values
2810 * @length: number of integer values in @list
2812 * Associates a list of integer values with @key under @group_name.
2813 * If @key cannot be found then it is created.
2815 * Since: 2.6
2817 void
2818 g_key_file_set_integer_list (GKeyFile *key_file,
2819 const gchar *group_name,
2820 const gchar *key,
2821 gint list[],
2822 gsize length)
2824 GString *values;
2825 gsize i;
2827 g_return_if_fail (key_file != NULL);
2828 g_return_if_fail (list != NULL);
2830 values = g_string_sized_new (length * 16);
2831 for (i = 0; i < length; i++)
2833 gchar *value;
2835 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2837 g_string_append (values, value);
2838 g_string_append_c (values, key_file->list_separator);
2840 g_free (value);
2843 g_key_file_set_value (key_file, group_name, key, values->str);
2844 g_string_free (values, TRUE);
2848 * g_key_file_get_double:
2849 * @key_file: a #GKeyFile
2850 * @group_name: a group name
2851 * @key: a key
2852 * @error: return location for a #GError
2854 * Returns the value associated with @key under @group_name as a
2855 * double. If @group_name is %NULL, the start_group is used.
2857 * If @key cannot be found then 0.0 is returned and @error is set to
2858 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2859 * with @key cannot be interpreted as a double then 0.0 is returned
2860 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2862 * Return value: the value associated with the key as a double, or
2863 * 0.0 if the key was not found or could not be parsed.
2865 * Since: 2.12
2867 gdouble
2868 g_key_file_get_double (GKeyFile *key_file,
2869 const gchar *group_name,
2870 const gchar *key,
2871 GError **error)
2873 GError *key_file_error;
2874 gchar *value;
2875 gdouble double_value;
2877 g_return_val_if_fail (key_file != NULL, -1);
2878 g_return_val_if_fail (group_name != NULL, -1);
2879 g_return_val_if_fail (key != NULL, -1);
2881 key_file_error = NULL;
2883 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2885 if (key_file_error)
2887 g_propagate_error (error, key_file_error);
2888 return 0;
2891 double_value = g_key_file_parse_value_as_double (key_file, value,
2892 &key_file_error);
2893 g_free (value);
2895 if (key_file_error)
2897 if (g_error_matches (key_file_error,
2898 G_KEY_FILE_ERROR,
2899 G_KEY_FILE_ERROR_INVALID_VALUE))
2901 g_set_error (error, G_KEY_FILE_ERROR,
2902 G_KEY_FILE_ERROR_INVALID_VALUE,
2903 _("Key file contains key '%s' in group '%s' "
2904 "which has a value that cannot be interpreted."),
2905 key, group_name);
2906 g_error_free (key_file_error);
2908 else
2909 g_propagate_error (error, key_file_error);
2912 return double_value;
2916 * g_key_file_set_double:
2917 * @key_file: a #GKeyFile
2918 * @group_name: a group name
2919 * @key: a key
2920 * @value: an double value
2922 * Associates a new double value with @key under @group_name.
2923 * If @key cannot be found then it is created.
2925 * Since: 2.12
2927 void
2928 g_key_file_set_double (GKeyFile *key_file,
2929 const gchar *group_name,
2930 const gchar *key,
2931 gdouble value)
2933 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2935 g_return_if_fail (key_file != NULL);
2937 g_ascii_dtostr (result, sizeof (result), value);
2938 g_key_file_set_value (key_file, group_name, key, result);
2942 * g_key_file_get_double_list:
2943 * @key_file: a #GKeyFile
2944 * @group_name: a group name
2945 * @key: a key
2946 * @length: (out): the number of doubles returned
2947 * @error: return location for a #GError
2949 * Returns the values associated with @key under @group_name as
2950 * doubles.
2952 * If @key cannot be found then %NULL is returned and @error is set to
2953 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2954 * with @key cannot be interpreted as doubles then %NULL is returned
2955 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2957 * Return value: (array length=length) (element-type gdouble) (transfer container):
2958 * the values associated with the key as a list of doubles, or %NULL if the
2959 * key was not found or could not be parsed. The returned list of doubles
2960 * should be freed with g_free() when no longer needed.
2962 * Since: 2.12
2964 gdouble *
2965 g_key_file_get_double_list (GKeyFile *key_file,
2966 const gchar *group_name,
2967 const gchar *key,
2968 gsize *length,
2969 GError **error)
2971 GError *key_file_error = NULL;
2972 gchar **values;
2973 gdouble *double_values;
2974 gsize i, num_doubles;
2976 g_return_val_if_fail (key_file != NULL, NULL);
2977 g_return_val_if_fail (group_name != NULL, NULL);
2978 g_return_val_if_fail (key != NULL, NULL);
2980 if (length)
2981 *length = 0;
2983 values = g_key_file_get_string_list (key_file, group_name, key,
2984 &num_doubles, &key_file_error);
2986 if (key_file_error)
2987 g_propagate_error (error, key_file_error);
2989 if (!values)
2990 return NULL;
2992 double_values = g_new (gdouble, num_doubles);
2994 for (i = 0; i < num_doubles; i++)
2996 double_values[i] = g_key_file_parse_value_as_double (key_file,
2997 values[i],
2998 &key_file_error);
3000 if (key_file_error)
3002 g_propagate_error (error, key_file_error);
3003 g_strfreev (values);
3004 g_free (double_values);
3006 return NULL;
3009 g_strfreev (values);
3011 if (length)
3012 *length = num_doubles;
3014 return double_values;
3018 * g_key_file_set_double_list:
3019 * @key_file: a #GKeyFile
3020 * @group_name: a group name
3021 * @key: a key
3022 * @list: (array length=length): an array of double values
3023 * @length: number of double values in @list
3025 * Associates a list of double values with @key under
3026 * @group_name. If @key cannot be found then it is created.
3028 * Since: 2.12
3030 void
3031 g_key_file_set_double_list (GKeyFile *key_file,
3032 const gchar *group_name,
3033 const gchar *key,
3034 gdouble list[],
3035 gsize length)
3037 GString *values;
3038 gsize i;
3040 g_return_if_fail (key_file != NULL);
3041 g_return_if_fail (list != NULL);
3043 values = g_string_sized_new (length * 16);
3044 for (i = 0; i < length; i++)
3046 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3048 g_ascii_dtostr( result, sizeof (result), list[i] );
3050 g_string_append (values, result);
3051 g_string_append_c (values, key_file->list_separator);
3054 g_key_file_set_value (key_file, group_name, key, values->str);
3055 g_string_free (values, TRUE);
3058 static gboolean
3059 g_key_file_set_key_comment (GKeyFile *key_file,
3060 const gchar *group_name,
3061 const gchar *key,
3062 const gchar *comment,
3063 GError **error)
3065 GKeyFileGroup *group;
3066 GKeyFileKeyValuePair *pair;
3067 GList *key_node, *comment_node, *tmp;
3069 group = g_key_file_lookup_group (key_file, group_name);
3070 if (!group)
3072 g_set_error (error, G_KEY_FILE_ERROR,
3073 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3074 _("Key file does not have group '%s'"),
3075 group_name ? group_name : "(null)");
3077 return FALSE;
3080 /* First find the key the comments are supposed to be
3081 * associated with
3083 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3085 if (key_node == NULL)
3087 g_set_error (error, G_KEY_FILE_ERROR,
3088 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3089 _("Key file does not have key '%s' in group '%s'"),
3090 key, group->name);
3091 return FALSE;
3094 /* Then find all the comments already associated with the
3095 * key and free them
3097 tmp = key_node->next;
3098 while (tmp != NULL)
3100 pair = (GKeyFileKeyValuePair *) tmp->data;
3102 if (pair->key != NULL)
3103 break;
3105 comment_node = tmp;
3106 tmp = tmp->next;
3107 g_key_file_remove_key_value_pair_node (key_file, group,
3108 comment_node);
3111 if (comment == NULL)
3112 return TRUE;
3114 /* Now we can add our new comment
3116 pair = g_slice_new (GKeyFileKeyValuePair);
3117 pair->key = NULL;
3118 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3120 key_node = g_list_insert (key_node, pair, 1);
3122 return TRUE;
3125 static gboolean
3126 g_key_file_set_group_comment (GKeyFile *key_file,
3127 const gchar *group_name,
3128 const gchar *comment,
3129 GError **error)
3131 GKeyFileGroup *group;
3133 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3135 group = g_key_file_lookup_group (key_file, group_name);
3136 if (!group)
3138 g_set_error (error, G_KEY_FILE_ERROR,
3139 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3140 _("Key file does not have group '%s'"),
3141 group_name ? group_name : "(null)");
3143 return FALSE;
3146 /* First remove any existing comment
3148 if (group->comment)
3150 g_key_file_key_value_pair_free (group->comment);
3151 group->comment = NULL;
3154 if (comment == NULL)
3155 return TRUE;
3157 /* Now we can add our new comment
3159 group->comment = g_slice_new (GKeyFileKeyValuePair);
3160 group->comment->key = NULL;
3161 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3163 return TRUE;
3166 static gboolean
3167 g_key_file_set_top_comment (GKeyFile *key_file,
3168 const gchar *comment,
3169 GError **error)
3171 GList *group_node;
3172 GKeyFileGroup *group;
3173 GKeyFileKeyValuePair *pair;
3175 /* The last group in the list should be the top (comments only)
3176 * group in the file
3178 g_warn_if_fail (key_file->groups != NULL);
3179 group_node = g_list_last (key_file->groups);
3180 group = (GKeyFileGroup *) group_node->data;
3181 g_warn_if_fail (group->name == NULL);
3183 /* Note all keys must be comments at the top of
3184 * the file, so we can just free it all.
3186 g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3187 group->key_value_pairs = NULL;
3189 if (comment == NULL)
3190 return TRUE;
3192 pair = g_slice_new (GKeyFileKeyValuePair);
3193 pair->key = NULL;
3194 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3196 group->key_value_pairs =
3197 g_list_prepend (group->key_value_pairs, pair);
3199 return TRUE;
3203 * g_key_file_set_comment:
3204 * @key_file: a #GKeyFile
3205 * @group_name: (allow-none): a group name, or %NULL
3206 * @key: (allow-none): a key
3207 * @comment: a comment
3208 * @error: return location for a #GError
3210 * Places a comment above @key from @group_name.
3211 * If @key is %NULL then @comment will be written above @group_name.
3212 * If both @key and @group_name are %NULL, then @comment will be
3213 * written above the first group in the file.
3215 * Returns: %TRUE if the comment was written, %FALSE otherwise
3217 * Since: 2.6
3219 gboolean
3220 g_key_file_set_comment (GKeyFile *key_file,
3221 const gchar *group_name,
3222 const gchar *key,
3223 const gchar *comment,
3224 GError **error)
3226 g_return_val_if_fail (key_file != NULL, FALSE);
3228 if (group_name != NULL && key != NULL)
3230 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3231 return FALSE;
3233 else if (group_name != NULL)
3235 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3236 return FALSE;
3238 else
3240 if (!g_key_file_set_top_comment (key_file, comment, error))
3241 return FALSE;
3244 return TRUE;
3247 static gchar *
3248 g_key_file_get_key_comment (GKeyFile *key_file,
3249 const gchar *group_name,
3250 const gchar *key,
3251 GError **error)
3253 GKeyFileGroup *group;
3254 GKeyFileKeyValuePair *pair;
3255 GList *key_node, *tmp;
3256 GString *string;
3257 gchar *comment;
3259 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3261 group = g_key_file_lookup_group (key_file, group_name);
3262 if (!group)
3264 g_set_error (error, G_KEY_FILE_ERROR,
3265 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3266 _("Key file does not have group '%s'"),
3267 group_name ? group_name : "(null)");
3269 return NULL;
3272 /* First find the key the comments are supposed to be
3273 * associated with
3275 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3277 if (key_node == NULL)
3279 g_set_error (error, G_KEY_FILE_ERROR,
3280 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3281 _("Key file does not have key '%s' in group '%s'"),
3282 key, group->name);
3283 return NULL;
3286 string = NULL;
3288 /* Then find all the comments already associated with the
3289 * key and concatentate them.
3291 tmp = key_node->next;
3292 if (!key_node->next)
3293 return NULL;
3295 pair = (GKeyFileKeyValuePair *) tmp->data;
3296 if (pair->key != NULL)
3297 return NULL;
3299 while (tmp->next)
3301 pair = (GKeyFileKeyValuePair *) tmp->next->data;
3303 if (pair->key != NULL)
3304 break;
3306 tmp = tmp->next;
3309 while (tmp != key_node)
3311 pair = (GKeyFileKeyValuePair *) tmp->data;
3313 if (string == NULL)
3314 string = g_string_sized_new (512);
3316 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3317 g_string_append (string, comment);
3318 g_free (comment);
3320 tmp = tmp->prev;
3323 if (string != NULL)
3325 comment = string->str;
3326 g_string_free (string, FALSE);
3328 else
3329 comment = NULL;
3331 return comment;
3334 static gchar *
3335 get_group_comment (GKeyFile *key_file,
3336 GKeyFileGroup *group,
3337 GError **error)
3339 GString *string;
3340 GList *tmp;
3341 gchar *comment;
3343 string = NULL;
3345 tmp = group->key_value_pairs;
3346 while (tmp)
3348 GKeyFileKeyValuePair *pair;
3350 pair = (GKeyFileKeyValuePair *) tmp->data;
3352 if (pair->key != NULL)
3354 tmp = tmp->prev;
3355 break;
3358 if (tmp->next == NULL)
3359 break;
3361 tmp = tmp->next;
3364 while (tmp != NULL)
3366 GKeyFileKeyValuePair *pair;
3368 pair = (GKeyFileKeyValuePair *) tmp->data;
3370 if (string == NULL)
3371 string = g_string_sized_new (512);
3373 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3374 g_string_append (string, comment);
3375 g_free (comment);
3377 tmp = tmp->prev;
3380 if (string != NULL)
3381 return g_string_free (string, FALSE);
3383 return NULL;
3386 static gchar *
3387 g_key_file_get_group_comment (GKeyFile *key_file,
3388 const gchar *group_name,
3389 GError **error)
3391 GList *group_node;
3392 GKeyFileGroup *group;
3394 group = g_key_file_lookup_group (key_file, group_name);
3395 if (!group)
3397 g_set_error (error, G_KEY_FILE_ERROR,
3398 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3399 _("Key file does not have group '%s'"),
3400 group_name ? group_name : "(null)");
3402 return NULL;
3405 if (group->comment)
3406 return g_strdup (group->comment->value);
3408 group_node = g_key_file_lookup_group_node (key_file, group_name);
3409 group_node = group_node->next;
3410 group = (GKeyFileGroup *)group_node->data;
3411 return get_group_comment (key_file, group, error);
3414 static gchar *
3415 g_key_file_get_top_comment (GKeyFile *key_file,
3416 GError **error)
3418 GList *group_node;
3419 GKeyFileGroup *group;
3421 /* The last group in the list should be the top (comments only)
3422 * group in the file
3424 g_warn_if_fail (key_file->groups != NULL);
3425 group_node = g_list_last (key_file->groups);
3426 group = (GKeyFileGroup *) group_node->data;
3427 g_warn_if_fail (group->name == NULL);
3429 return get_group_comment (key_file, group, error);
3433 * g_key_file_get_comment:
3434 * @key_file: a #GKeyFile
3435 * @group_name: (allow-none): a group name, or %NULL
3436 * @key: a key
3437 * @error: return location for a #GError
3439 * Retrieves a comment above @key from @group_name.
3440 * If @key is %NULL then @comment will be read from above
3441 * @group_name. If both @key and @group_name are %NULL, then
3442 * @comment will be read from above the first group in the file.
3444 * Returns: a comment that should be freed with g_free()
3446 * Since: 2.6
3448 gchar *
3449 g_key_file_get_comment (GKeyFile *key_file,
3450 const gchar *group_name,
3451 const gchar *key,
3452 GError **error)
3454 g_return_val_if_fail (key_file != NULL, NULL);
3456 if (group_name != NULL && key != NULL)
3457 return g_key_file_get_key_comment (key_file, group_name, key, error);
3458 else if (group_name != NULL)
3459 return g_key_file_get_group_comment (key_file, group_name, error);
3460 else
3461 return g_key_file_get_top_comment (key_file, error);
3465 * g_key_file_remove_comment:
3466 * @key_file: a #GKeyFile
3467 * @group_name: (allow-none): a group name, or %NULL
3468 * @key: (allow-none): a key
3469 * @error: return location for a #GError
3471 * Removes a comment above @key from @group_name.
3472 * If @key is %NULL then @comment will be removed above @group_name.
3473 * If both @key and @group_name are %NULL, then @comment will
3474 * be removed above the first group in the file.
3476 * Returns: %TRUE if the comment was removed, %FALSE otherwise
3478 * Since: 2.6
3481 gboolean
3482 g_key_file_remove_comment (GKeyFile *key_file,
3483 const gchar *group_name,
3484 const gchar *key,
3485 GError **error)
3487 g_return_val_if_fail (key_file != NULL, FALSE);
3489 if (group_name != NULL && key != NULL)
3490 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3491 else if (group_name != NULL)
3492 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3493 else
3494 return g_key_file_set_top_comment (key_file, NULL, error);
3498 * g_key_file_has_group:
3499 * @key_file: a #GKeyFile
3500 * @group_name: a group name
3502 * Looks whether the key file has the group @group_name.
3504 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3505 * otherwise.
3506 * Since: 2.6
3508 gboolean
3509 g_key_file_has_group (GKeyFile *key_file,
3510 const gchar *group_name)
3512 g_return_val_if_fail (key_file != NULL, FALSE);
3513 g_return_val_if_fail (group_name != NULL, FALSE);
3515 return g_key_file_lookup_group (key_file, group_name) != NULL;
3518 /* This code remains from a historical attempt to add a new public API
3519 * which respects the GError rules.
3521 static gboolean
3522 g_key_file_has_key_full (GKeyFile *key_file,
3523 const gchar *group_name,
3524 const gchar *key,
3525 gboolean *has_key,
3526 GError **error)
3528 GKeyFileKeyValuePair *pair;
3529 GKeyFileGroup *group;
3531 g_return_val_if_fail (key_file != NULL, FALSE);
3532 g_return_val_if_fail (group_name != NULL, FALSE);
3533 g_return_val_if_fail (key != NULL, FALSE);
3535 group = g_key_file_lookup_group (key_file, group_name);
3537 if (!group)
3539 g_set_error (error, G_KEY_FILE_ERROR,
3540 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3541 _("Key file does not have group '%s'"),
3542 group_name ? group_name : "(null)");
3544 return FALSE;
3547 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3549 if (has_key)
3550 *has_key = pair != NULL;
3551 return TRUE;
3555 * g_key_file_has_key: (skip)
3556 * @key_file: a #GKeyFile
3557 * @group_name: a group name
3558 * @key: a key name
3559 * @error: return location for a #GError
3561 * Looks whether the key file has the key @key in the group
3562 * @group_name.
3564 * Note that this function does not follow the rules for #GError strictly;
3565 * the return value both carries meaning and signals an error. To use
3566 * this function, you must pass a #GError pointer in @error, and check
3567 * whether it is not %NULL to see if an error occurred.
3569 * Language bindings should use g_key_file_get_value() to test whether
3570 * or not a key exists.
3572 * Return value: %TRUE if @key is a part of @group_name, %FALSE otherwise
3574 * Since: 2.6
3576 gboolean
3577 g_key_file_has_key (GKeyFile *key_file,
3578 const gchar *group_name,
3579 const gchar *key,
3580 GError **error)
3582 GError *temp_error = NULL;
3583 gboolean has_key;
3585 if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3587 return has_key;
3589 else
3591 g_propagate_error (error, temp_error);
3592 return FALSE;
3596 static void
3597 g_key_file_add_group (GKeyFile *key_file,
3598 const gchar *group_name)
3600 GKeyFileGroup *group;
3602 g_return_if_fail (key_file != NULL);
3603 g_return_if_fail (g_key_file_is_group_name (group_name));
3605 group = g_key_file_lookup_group (key_file, group_name);
3606 if (group != NULL)
3608 key_file->current_group = group;
3609 return;
3612 group = g_slice_new0 (GKeyFileGroup);
3613 group->name = g_strdup (group_name);
3614 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3615 key_file->groups = g_list_prepend (key_file->groups, group);
3616 key_file->current_group = group;
3618 if (key_file->start_group == NULL)
3619 key_file->start_group = group;
3621 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3624 static void
3625 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3627 if (pair != NULL)
3629 g_free (pair->key);
3630 g_free (pair->value);
3631 g_slice_free (GKeyFileKeyValuePair, pair);
3635 /* Be careful not to call this function on a node with data in the
3636 * lookup map without removing it from the lookup map, first.
3638 * Some current cases where this warning is not a concern are
3639 * when:
3640 * - the node being removed is a comment node
3641 * - the entire lookup map is getting destroyed soon after
3642 * anyway.
3644 static void
3645 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3646 GKeyFileGroup *group,
3647 GList *pair_node)
3650 GKeyFileKeyValuePair *pair;
3652 pair = (GKeyFileKeyValuePair *) pair_node->data;
3654 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3656 g_warn_if_fail (pair->value != NULL);
3658 g_key_file_key_value_pair_free (pair);
3660 g_list_free_1 (pair_node);
3663 static void
3664 g_key_file_remove_group_node (GKeyFile *key_file,
3665 GList *group_node)
3667 GKeyFileGroup *group;
3668 GList *tmp;
3670 group = (GKeyFileGroup *) group_node->data;
3672 if (group->name)
3673 g_hash_table_remove (key_file->group_hash, group->name);
3675 /* If the current group gets deleted make the current group the last
3676 * added group.
3678 if (key_file->current_group == group)
3680 /* groups should always contain at least the top comment group,
3681 * unless g_key_file_clear has been called
3683 if (key_file->groups)
3684 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3685 else
3686 key_file->current_group = NULL;
3689 /* If the start group gets deleted make the start group the first
3690 * added group.
3692 if (key_file->start_group == group)
3694 tmp = g_list_last (key_file->groups);
3695 while (tmp != NULL)
3697 if (tmp != group_node &&
3698 ((GKeyFileGroup *) tmp->data)->name != NULL)
3699 break;
3701 tmp = tmp->prev;
3704 if (tmp)
3705 key_file->start_group = (GKeyFileGroup *) tmp->data;
3706 else
3707 key_file->start_group = NULL;
3710 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3712 tmp = group->key_value_pairs;
3713 while (tmp != NULL)
3715 GList *pair_node;
3717 pair_node = tmp;
3718 tmp = tmp->next;
3719 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3722 g_warn_if_fail (group->key_value_pairs == NULL);
3724 if (group->comment)
3726 g_key_file_key_value_pair_free (group->comment);
3727 group->comment = NULL;
3730 if (group->lookup_map)
3732 g_hash_table_destroy (group->lookup_map);
3733 group->lookup_map = NULL;
3736 g_free ((gchar *) group->name);
3737 g_slice_free (GKeyFileGroup, group);
3738 g_list_free_1 (group_node);
3742 * g_key_file_remove_group:
3743 * @key_file: a #GKeyFile
3744 * @group_name: a group name
3745 * @error: return location for a #GError or %NULL
3747 * Removes the specified group, @group_name,
3748 * from the key file.
3750 * Returns: %TRUE if the group was removed, %FALSE otherwise
3752 * Since: 2.6
3754 gboolean
3755 g_key_file_remove_group (GKeyFile *key_file,
3756 const gchar *group_name,
3757 GError **error)
3759 GList *group_node;
3761 g_return_val_if_fail (key_file != NULL, FALSE);
3762 g_return_val_if_fail (group_name != NULL, FALSE);
3764 group_node = g_key_file_lookup_group_node (key_file, group_name);
3766 if (!group_node)
3768 g_set_error (error, G_KEY_FILE_ERROR,
3769 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3770 _("Key file does not have group '%s'"),
3771 group_name);
3772 return FALSE;
3775 g_key_file_remove_group_node (key_file, group_node);
3777 return TRUE;
3780 static void
3781 g_key_file_add_key_value_pair (GKeyFile *key_file,
3782 GKeyFileGroup *group,
3783 GKeyFileKeyValuePair *pair)
3785 g_hash_table_replace (group->lookup_map, pair->key, pair);
3786 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3789 static void
3790 g_key_file_add_key (GKeyFile *key_file,
3791 GKeyFileGroup *group,
3792 const gchar *key,
3793 const gchar *value)
3795 GKeyFileKeyValuePair *pair;
3797 pair = g_slice_new (GKeyFileKeyValuePair);
3798 pair->key = g_strdup (key);
3799 pair->value = g_strdup (value);
3801 g_key_file_add_key_value_pair (key_file, group, pair);
3805 * g_key_file_remove_key:
3806 * @key_file: a #GKeyFile
3807 * @group_name: a group name
3808 * @key: a key name to remove
3809 * @error: return location for a #GError or %NULL
3811 * Removes @key in @group_name from the key file.
3813 * Returns: %TRUE if the key was removed, %FALSE otherwise
3815 * Since: 2.6
3817 gboolean
3818 g_key_file_remove_key (GKeyFile *key_file,
3819 const gchar *group_name,
3820 const gchar *key,
3821 GError **error)
3823 GKeyFileGroup *group;
3824 GKeyFileKeyValuePair *pair;
3826 g_return_val_if_fail (key_file != NULL, FALSE);
3827 g_return_val_if_fail (group_name != NULL, FALSE);
3828 g_return_val_if_fail (key != NULL, FALSE);
3830 pair = NULL;
3832 group = g_key_file_lookup_group (key_file, group_name);
3833 if (!group)
3835 g_set_error (error, G_KEY_FILE_ERROR,
3836 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3837 _("Key file does not have group '%s'"),
3838 group_name ? group_name : "(null)");
3839 return FALSE;
3842 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3844 if (!pair)
3846 g_set_error (error, G_KEY_FILE_ERROR,
3847 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3848 _("Key file does not have key '%s' in group '%s'"),
3849 key, group->name);
3850 return FALSE;
3853 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3854 g_hash_table_remove (group->lookup_map, pair->key);
3855 g_key_file_key_value_pair_free (pair);
3857 return TRUE;
3860 static GList *
3861 g_key_file_lookup_group_node (GKeyFile *key_file,
3862 const gchar *group_name)
3864 GKeyFileGroup *group;
3865 GList *tmp;
3867 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3869 group = (GKeyFileGroup *) tmp->data;
3871 if (group && group->name && strcmp (group->name, group_name) == 0)
3872 break;
3875 return tmp;
3878 static GKeyFileGroup *
3879 g_key_file_lookup_group (GKeyFile *key_file,
3880 const gchar *group_name)
3882 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3885 static GList *
3886 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3887 GKeyFileGroup *group,
3888 const gchar *key)
3890 GList *key_node;
3892 for (key_node = group->key_value_pairs;
3893 key_node != NULL;
3894 key_node = key_node->next)
3896 GKeyFileKeyValuePair *pair;
3898 pair = (GKeyFileKeyValuePair *) key_node->data;
3900 if (pair->key && strcmp (pair->key, key) == 0)
3901 break;
3904 return key_node;
3907 static GKeyFileKeyValuePair *
3908 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3909 GKeyFileGroup *group,
3910 const gchar *key)
3912 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3915 /* Lines starting with # or consisting entirely of whitespace are merely
3916 * recorded, not parsed. This function assumes all leading whitespace
3917 * has been stripped.
3919 static gboolean
3920 g_key_file_line_is_comment (const gchar *line)
3922 return (*line == '#' || *line == '\0' || *line == '\n');
3925 static gboolean
3926 g_key_file_is_group_name (const gchar *name)
3928 gchar *p, *q;
3930 if (name == NULL)
3931 return FALSE;
3933 p = q = (gchar *) name;
3934 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3935 q = g_utf8_find_next_char (q, NULL);
3937 if (*q != '\0' || q == p)
3938 return FALSE;
3940 return TRUE;
3943 static gboolean
3944 g_key_file_is_key_name (const gchar *name)
3946 gchar *p, *q;
3948 if (name == NULL)
3949 return FALSE;
3951 p = q = (gchar *) name;
3952 /* We accept a little more than the desktop entry spec says,
3953 * since gnome-vfs uses mime-types as keys in its cache.
3955 while (*q && *q != '=' && *q != '[' && *q != ']')
3956 q = g_utf8_find_next_char (q, NULL);
3958 /* No empty keys, please */
3959 if (q == p)
3960 return FALSE;
3962 /* We accept spaces in the middle of keys to not break
3963 * existing apps, but we don't tolerate initial or final
3964 * spaces, which would lead to silent corruption when
3965 * rereading the file.
3967 if (*p == ' ' || q[-1] == ' ')
3968 return FALSE;
3970 if (*q == '[')
3972 q++;
3973 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3974 q = g_utf8_find_next_char (q, NULL);
3976 if (*q != ']')
3977 return FALSE;
3979 q++;
3982 if (*q != '\0')
3983 return FALSE;
3985 return TRUE;
3988 /* A group in a key file is made up of a starting '[' followed by one
3989 * or more letters making up the group name followed by ']'.
3991 static gboolean
3992 g_key_file_line_is_group (const gchar *line)
3994 gchar *p;
3996 p = (gchar *) line;
3997 if (*p != '[')
3998 return FALSE;
4000 p++;
4002 while (*p && *p != ']')
4003 p = g_utf8_find_next_char (p, NULL);
4005 if (*p != ']')
4006 return FALSE;
4008 /* silently accept whitespace after the ] */
4009 p = g_utf8_find_next_char (p, NULL);
4010 while (*p == ' ' || *p == '\t')
4011 p = g_utf8_find_next_char (p, NULL);
4013 if (*p)
4014 return FALSE;
4016 return TRUE;
4019 static gboolean
4020 g_key_file_line_is_key_value_pair (const gchar *line)
4022 gchar *p;
4024 p = (gchar *) g_utf8_strchr (line, -1, '=');
4026 if (!p)
4027 return FALSE;
4029 /* Key must be non-empty
4031 if (*p == line[0])
4032 return FALSE;
4034 return TRUE;
4037 static gchar *
4038 g_key_file_parse_value_as_string (GKeyFile *key_file,
4039 const gchar *value,
4040 GSList **pieces,
4041 GError **error)
4043 gchar *string_value, *p, *q0, *q;
4045 string_value = g_new (gchar, strlen (value) + 1);
4047 p = (gchar *) value;
4048 q0 = q = string_value;
4049 while (*p)
4051 if (*p == '\\')
4053 p++;
4055 switch (*p)
4057 case 's':
4058 *q = ' ';
4059 break;
4061 case 'n':
4062 *q = '\n';
4063 break;
4065 case 't':
4066 *q = '\t';
4067 break;
4069 case 'r':
4070 *q = '\r';
4071 break;
4073 case '\\':
4074 *q = '\\';
4075 break;
4077 case '\0':
4078 g_set_error_literal (error, G_KEY_FILE_ERROR,
4079 G_KEY_FILE_ERROR_INVALID_VALUE,
4080 _("Key file contains escape character "
4081 "at end of line"));
4082 break;
4084 default:
4085 if (pieces && *p == key_file->list_separator)
4086 *q = key_file->list_separator;
4087 else
4089 *q++ = '\\';
4090 *q = *p;
4092 if (*error == NULL)
4094 gchar sequence[3];
4096 sequence[0] = '\\';
4097 sequence[1] = *p;
4098 sequence[2] = '\0';
4100 g_set_error (error, G_KEY_FILE_ERROR,
4101 G_KEY_FILE_ERROR_INVALID_VALUE,
4102 _("Key file contains invalid escape "
4103 "sequence '%s'"), sequence);
4106 break;
4109 else
4111 *q = *p;
4112 if (pieces && (*p == key_file->list_separator))
4114 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4115 q0 = q + 1;
4119 if (*p == '\0')
4120 break;
4122 q++;
4123 p++;
4126 *q = '\0';
4127 if (pieces)
4129 if (q0 < q)
4130 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4131 *pieces = g_slist_reverse (*pieces);
4134 return string_value;
4137 static gchar *
4138 g_key_file_parse_string_as_value (GKeyFile *key_file,
4139 const gchar *string,
4140 gboolean escape_separator)
4142 gchar *value, *p, *q;
4143 gsize length;
4144 gboolean parsing_leading_space;
4146 length = strlen (string) + 1;
4148 /* Worst case would be that every character needs to be escaped.
4149 * In other words every character turns to two characters
4151 value = g_new (gchar, 2 * length);
4153 p = (gchar *) string;
4154 q = value;
4155 parsing_leading_space = TRUE;
4156 while (p < (string + length - 1))
4158 gchar escaped_character[3] = { '\\', 0, 0 };
4160 switch (*p)
4162 case ' ':
4163 if (parsing_leading_space)
4165 escaped_character[1] = 's';
4166 strcpy (q, escaped_character);
4167 q += 2;
4169 else
4171 *q = *p;
4172 q++;
4174 break;
4175 case '\t':
4176 if (parsing_leading_space)
4178 escaped_character[1] = 't';
4179 strcpy (q, escaped_character);
4180 q += 2;
4182 else
4184 *q = *p;
4185 q++;
4187 break;
4188 case '\n':
4189 escaped_character[1] = 'n';
4190 strcpy (q, escaped_character);
4191 q += 2;
4192 break;
4193 case '\r':
4194 escaped_character[1] = 'r';
4195 strcpy (q, escaped_character);
4196 q += 2;
4197 break;
4198 case '\\':
4199 escaped_character[1] = '\\';
4200 strcpy (q, escaped_character);
4201 q += 2;
4202 parsing_leading_space = FALSE;
4203 break;
4204 default:
4205 if (escape_separator && *p == key_file->list_separator)
4207 escaped_character[1] = key_file->list_separator;
4208 strcpy (q, escaped_character);
4209 q += 2;
4210 parsing_leading_space = TRUE;
4212 else
4214 *q = *p;
4215 q++;
4216 parsing_leading_space = FALSE;
4218 break;
4220 p++;
4222 *q = '\0';
4224 return value;
4227 static gint
4228 g_key_file_parse_value_as_integer (GKeyFile *key_file,
4229 const gchar *value,
4230 GError **error)
4232 gchar *eof_int;
4233 glong long_value;
4234 gint int_value;
4236 errno = 0;
4237 long_value = strtol (value, &eof_int, 10);
4239 if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4241 gchar *value_utf8 = _g_utf8_make_valid (value);
4242 g_set_error (error, G_KEY_FILE_ERROR,
4243 G_KEY_FILE_ERROR_INVALID_VALUE,
4244 _("Value '%s' cannot be interpreted "
4245 "as a number."), value_utf8);
4246 g_free (value_utf8);
4248 return 0;
4251 int_value = long_value;
4252 if (int_value != long_value || errno == ERANGE)
4254 gchar *value_utf8 = _g_utf8_make_valid (value);
4255 g_set_error (error,
4256 G_KEY_FILE_ERROR,
4257 G_KEY_FILE_ERROR_INVALID_VALUE,
4258 _("Integer value '%s' out of range"),
4259 value_utf8);
4260 g_free (value_utf8);
4262 return 0;
4265 return int_value;
4268 static gchar *
4269 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4270 gint value)
4273 return g_strdup_printf ("%d", value);
4276 static gdouble
4277 g_key_file_parse_value_as_double (GKeyFile *key_file,
4278 const gchar *value,
4279 GError **error)
4281 gchar *end_of_valid_d;
4282 gdouble double_value = 0;
4284 double_value = g_ascii_strtod (value, &end_of_valid_d);
4286 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4288 gchar *value_utf8 = _g_utf8_make_valid (value);
4289 g_set_error (error, G_KEY_FILE_ERROR,
4290 G_KEY_FILE_ERROR_INVALID_VALUE,
4291 _("Value '%s' cannot be interpreted "
4292 "as a float number."),
4293 value_utf8);
4294 g_free (value_utf8);
4297 return double_value;
4300 static gboolean
4301 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
4302 const gchar *value,
4303 GError **error)
4305 gchar *value_utf8;
4307 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
4308 return TRUE;
4309 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
4310 return FALSE;
4312 value_utf8 = _g_utf8_make_valid (value);
4313 g_set_error (error, G_KEY_FILE_ERROR,
4314 G_KEY_FILE_ERROR_INVALID_VALUE,
4315 _("Value '%s' cannot be interpreted "
4316 "as a boolean."), value_utf8);
4317 g_free (value_utf8);
4319 return FALSE;
4322 static gchar *
4323 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4324 gboolean value)
4326 if (value)
4327 return g_strdup ("true");
4328 else
4329 return g_strdup ("false");
4332 static gchar *
4333 g_key_file_parse_value_as_comment (GKeyFile *key_file,
4334 const gchar *value)
4336 GString *string;
4337 gchar **lines;
4338 gsize i;
4340 string = g_string_sized_new (512);
4342 lines = g_strsplit (value, "\n", 0);
4344 for (i = 0; lines[i] != NULL; i++)
4346 if (lines[i][0] != '#')
4347 g_string_append_printf (string, "%s\n", lines[i]);
4348 else
4349 g_string_append_printf (string, "%s\n", lines[i] + 1);
4351 g_strfreev (lines);
4353 return g_string_free (string, FALSE);
4356 static gchar *
4357 g_key_file_parse_comment_as_value (GKeyFile *key_file,
4358 const gchar *comment)
4360 GString *string;
4361 gchar **lines;
4362 gsize i;
4364 string = g_string_sized_new (512);
4366 lines = g_strsplit (comment, "\n", 0);
4368 for (i = 0; lines[i] != NULL; i++)
4369 g_string_append_printf (string, "#%s%s", lines[i],
4370 lines[i + 1] == NULL? "" : "\n");
4371 g_strfreev (lines);
4373 return g_string_free (string, FALSE);
4377 * g_key_file_save_to_file:
4378 * @key_file: a #GKeyFile
4379 * @filename: the name of the file to write to
4380 * @error: a pointer to a %NULL #GError, or %NULL
4382 * Writes the contents of @key_file to @filename using
4383 * g_file_set_contents().
4385 * This function can fail for any of the reasons that
4386 * g_file_set_contents() may fail.
4388 * Returns: %TRUE if successful, else %FALSE with @error set
4390 * Since: 2.40
4392 gboolean
4393 g_key_file_save_to_file (GKeyFile *key_file,
4394 const gchar *filename,
4395 GError **error)
4397 gchar *contents;
4398 gboolean success;
4399 gsize length;
4401 g_return_val_if_fail (key_file != NULL, FALSE);
4402 g_return_val_if_fail (filename != NULL, FALSE);
4403 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4405 contents = g_key_file_to_data (key_file, &length, NULL);
4406 g_assert (contents != NULL);
4408 success = g_file_set_contents (filename, contents, length, error);
4409 g_free (contents);
4411 return success;