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/>.
36 #include <sys/types.h>
45 #define fstat(a,b) _fstati64(a,b)
50 #define S_ISREG(mode) ((mode)&_S_IFREG)
53 #endif /* G_OS_WIN23 */
58 #include "gfileutils.h"
64 #include "gmessages.h"
67 #include "gstrfuncs.h"
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.
91 * # this is just an example
92 * # there can be comments before the first group
96 * Name=Key File Example\tthis value shows\nescaping
98 * # localized strings are stored in multiple key-value pairs
101 * Welcome[fr_FR]=Bonjour
103 * Welcome[be@latin]=Hello
107 * Numbers=2;20;-200;0
109 * Booleans=true;false;true;true
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:
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>
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.
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.
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.
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
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
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.
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.
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.
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.
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.
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.
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.
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
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.
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
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
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.
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.
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.
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.
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.
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.
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>.
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.
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.
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.
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.
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.
424 typedef struct _GKeyFileGroup GKeyFileGroup
;
429 * The GKeyFile struct contains only private data
430 * and should not be accessed directly.
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
;
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 */
473 static gint
find_file_in_data_dirs (const gchar
*file
,
474 const gchar
**data_dirs
,
477 static gboolean
g_key_file_load_from_fd (GKeyFile
*key_file
,
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
,
489 static GKeyFileKeyValuePair
*g_key_file_lookup_key_value_pair (GKeyFile
*key_file
,
490 GKeyFileGroup
*group
,
493 static void g_key_file_remove_group_node (GKeyFile
*key_file
,
495 static void g_key_file_remove_key_value_pair_node (GKeyFile
*key_file
,
496 GKeyFileGroup
*group
,
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
,
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
,
518 static gchar
*g_key_file_parse_string_as_value (GKeyFile
*key_file
,
520 gboolean escape_separator
);
521 static gint
g_key_file_parse_value_as_integer (GKeyFile
*key_file
,
524 static gchar
*g_key_file_parse_integer_as_value (GKeyFile
*key_file
,
526 static gdouble
g_key_file_parse_value_as_double (GKeyFile
*key_file
,
529 static gboolean
g_key_file_parse_value_as_boolean (GKeyFile
*key_file
,
532 static gchar
*g_key_file_parse_boolean_as_value (GKeyFile
*key_file
,
534 static gchar
*g_key_file_parse_value_as_comment (GKeyFile
*key_file
,
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
,
542 static void g_key_file_parse_comment (GKeyFile
*key_file
,
546 static void g_key_file_parse_group (GKeyFile
*key_file
,
550 static gchar
*key_get_locale (const gchar
*key
);
551 static void g_key_file_parse_data (GKeyFile
*key_file
,
555 static void g_key_file_flush_parse_buffer (GKeyFile
*key_file
,
558 G_DEFINE_QUARK (g
-key
-file
-error
-quark
, g_key_file_error
)
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
= ';';
570 key_file
->locales
= g_strdupv ((gchar
**)g_get_language_names ());
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
;
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
);
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.
621 g_key_file_new (void)
625 key_file
= g_slice_new0 (GKeyFile
);
626 key_file
->ref_count
= 1;
627 g_key_file_init (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 ';'.
644 g_key_file_set_list_separator (GKeyFile
*key_file
,
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.
659 find_file_in_data_dirs (const gchar
*file
,
664 const gchar
**data_dirs
, *data_dir
;
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)
686 path
= g_build_filename (data_dir
, sub_dir
,
687 candidate_file
, NULL
);
689 fd
= g_open (path
, O_RDONLY
, 0);
697 candidate_file
= strchr (candidate_file
, '-');
699 if (candidate_file
== NULL
)
705 sub_dir
= g_strndup (file
, candidate_file
- file
- 1);
707 for (p
= sub_dir
; *p
!= '\0'; p
++)
710 *p
= G_DIR_SEPARATOR
;
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
);
734 g_key_file_load_from_fd (GKeyFile
*key_file
,
739 GError
*key_file_error
= NULL
;
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
),
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"));
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 */
776 if (errno
== EINTR
|| errno
== EAGAIN
)
779 g_set_error_literal (error
, G_FILE_ERROR
,
780 g_file_error_from_errno (errno
),
785 g_key_file_parse_data (key_file
,
786 read_buf
, bytes_read
,
789 while (!key_file_error
);
793 g_propagate_error (error
, key_file_error
);
797 g_key_file_flush_parse_buffer (key_file
, &key_file_error
);
801 g_propagate_error (error
, key_file_error
);
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
824 g_key_file_load_from_file (GKeyFile
*key_file
,
829 GError
*key_file_error
= NULL
;
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);
839 g_set_error_literal (error
, G_FILE_ERROR
,
840 g_file_error_from_errno (errno
),
845 g_key_file_load_from_fd (key_file
, fd
, flags
, &key_file_error
);
850 g_propagate_error (error
, key_file_error
);
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
873 g_key_file_load_from_data (GKeyFile
*key_file
,
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
);
898 g_propagate_error (error
, key_file_error
);
902 g_key_file_flush_parse_buffer (key_file
, &key_file_error
);
906 g_propagate_error (error
, key_file_error
);
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
929 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
934 g_key_file_load_from_dirs (GKeyFile
*key_file
,
936 const gchar
**search_dirs
,
941 GError
*key_file_error
= NULL
;
942 const gchar
**data_dirs
;
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
);
952 data_dirs
= search_dirs
;
954 while (*data_dirs
!= NULL
&& !found_file
)
956 g_free (output_path
);
958 fd
= find_file_in_data_dirs (file
, data_dirs
, &output_path
,
964 g_propagate_error (error
, key_file_error
);
968 found_file
= g_key_file_load_from_fd (key_file
, fd
, flags
,
974 g_propagate_error (error
, key_file_error
);
979 if (found_file
&& full_path
)
980 *full_path
= output_path
;
982 g_free (output_path
);
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
1006 g_key_file_load_from_data_dirs (GKeyFile
*key_file
,
1009 GKeyFileFlags flags
,
1012 gchar
**all_data_dirs
;
1013 const gchar
* user_data_dir
;
1014 const gchar
* const * system_data_dirs
;
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);
1026 all_data_dirs
[i
++] = g_strdup (user_data_dir
);
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
,
1035 (const gchar
**)all_data_dirs
,
1040 g_strfreev (all_data_dirs
);
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.
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
);
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.
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
);
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.
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().
1109 g_key_file_locale_is_interesting (GKeyFile
*key_file
,
1110 const gchar
*locale
)
1114 if (key_file
->flags
& G_KEY_FILE_KEEP_TRANSLATIONS
)
1117 for (i
= 0; key_file
->locales
[i
] != NULL
; i
++)
1119 if (g_ascii_strcasecmp (key_file
->locales
[i
], locale
) == 0)
1127 g_key_file_parse_line (GKeyFile
*key_file
,
1132 GError
*parse_error
= NULL
;
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
))
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
),
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
),
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"),
1166 g_propagate_error (error
, parse_error
);
1170 g_key_file_parse_comment (GKeyFile
*key_file
,
1175 GKeyFileKeyValuePair
*pair
;
1177 if (!(key_file
->flags
& G_KEY_FILE_KEEP_COMMENTS
))
1180 g_warn_if_fail (key_file
->current_group
!= NULL
);
1182 pair
= g_slice_new (GKeyFileKeyValuePair
);
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
);
1191 g_key_file_parse_group (GKeyFile
*key_file
,
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
!= ']')
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
);
1219 g_key_file_add_group (key_file
, group_name
);
1220 g_free (group_name
);
1224 g_key_file_parse_key_value_pair (GKeyFile
*key_file
,
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"));
1240 key_end
= value_start
= strchr (line
, '=');
1242 g_warn_if_fail (key_end
!= NULL
);
1247 /* Pull the key name from the line (chomping trailing whitespace)
1249 while (g_ascii_isspace (*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
);
1267 /* Pull the value from the line (chugging leading whitespace)
1269 while (g_ascii_isspace (*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
);
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
);
1309 pair
->value
= value
;
1311 g_key_file_add_key_value_pair (key_file
, key_file
->current_group
, pair
);
1323 key_get_locale (const gchar
*key
)
1327 locale
= g_strrstr (key
, "[");
1329 if (locale
&& strlen (locale
) <= 2)
1333 locale
= g_strndup (locale
+ 1, strlen (locale
) - 2);
1339 g_key_file_parse_data (GKeyFile
*key_file
,
1344 GError
*parse_error
;
1347 g_return_if_fail (key_file
!= NULL
);
1348 g_return_if_fail (data
!= NULL
|| length
== 0);
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]
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
);
1371 g_key_file_parse_comment (key_file
, "", 1, &parse_error
);
1375 g_propagate_error (error
, parse_error
);
1382 const gchar
*start_of_line
;
1383 const gchar
*end_of_line
;
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
);
1401 g_key_file_flush_parse_buffer (GKeyFile
*key_file
,
1404 GError
*file_error
= NULL
;
1406 g_return_if_fail (key_file
!= 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
,
1415 g_string_erase (key_file
->parse_buffer
, 0, -1);
1419 g_propagate_error (error
, file_error
);
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
1443 g_key_file_to_data (GKeyFile
*key_file
,
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
);
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
);
1484 g_string_append_printf (data_string
, "%s\n", pair
->value
);
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.
1513 g_key_file_get_keys (GKeyFile
*key_file
,
1514 const gchar
*group_name
,
1518 GKeyFileGroup
*group
;
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
);
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)");
1538 for (tmp
= group
->key_value_pairs
; tmp
; tmp
= tmp
->next
)
1540 GKeyFileKeyValuePair
*pair
;
1542 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
1548 keys
= g_new (gchar
*, num_keys
+ 1);
1551 for (tmp
= group
->key_value_pairs
; tmp
; tmp
= tmp
->next
)
1553 GKeyFileKeyValuePair
*pair
;
1555 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
1559 keys
[i
] = g_strdup (pair
->key
);
1564 keys
[num_keys
] = NULL
;
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.
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
);
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.
1607 g_key_file_get_groups (GKeyFile
*key_file
,
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,
1629 groups
= g_new (gchar
*, num_groups
);
1633 for (group_node
= group_node
->prev
;
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
);
1654 * g_key_file_get_value:
1655 * @key_file: a #GKeyFile
1656 * @group_name: a group name
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.
1675 g_key_file_get_value (GKeyFile
*key_file
,
1676 const gchar
*group_name
,
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
);
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)");
1699 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
1702 value
= g_strdup (pair
->value
);
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
);
1712 * g_key_file_set_value:
1713 * @key_file: a #GKeyFile
1714 * @group_name: a group name
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().
1728 g_key_file_set_value (GKeyFile
*key_file
,
1729 const gchar
*group_name
,
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
);
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
);
1752 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
1755 g_key_file_add_key (key_file
, group
, key
, value
);
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
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
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.
1786 g_key_file_get_string (GKeyFile
*key_file
,
1787 const gchar
*group_name
,
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
);
1804 g_propagate_error (error
, key_file_error
);
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
);
1821 string_value
= g_key_file_parse_value_as_string (key_file
, value
, NULL
,
1827 if (g_error_matches (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."),
1836 g_error_free (key_file_error
);
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
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.
1861 g_key_file_set_string (GKeyFile
*key_file
,
1862 const gchar
*group_name
,
1864 const gchar
*string
)
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
);
1877 * g_key_file_get_string_list:
1878 * @key_file: a #GKeyFile
1879 * @group_name: a group name
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().
1898 g_key_file_get_string_list (GKeyFile
*key_file
,
1899 const gchar
*group_name
,
1904 GError
*key_file_error
= NULL
;
1905 gchar
*value
, *string_value
, **values
;
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
);
1916 value
= g_key_file_get_value (key_file
, group_name
, key
, &key_file_error
);
1920 g_propagate_error (error
, key_file_error
);
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
);
1937 string_value
= g_key_file_parse_value_as_string (key_file
, value
, &pieces
, &key_file_error
);
1939 g_free (string_value
);
1943 if (g_error_matches (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."),
1952 g_error_free (key_file_error
);
1955 g_propagate_error (error
, key_file_error
);
1957 g_slist_free_full (pieces
, g_free
);
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
;
1967 g_slist_free (pieces
);
1976 * g_key_file_set_string_list:
1977 * @key_file: a #GKeyFile
1978 * @group_name: a group name
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.
1990 g_key_file_set_string_list (GKeyFile
*key_file
,
1991 const gchar
*group_name
,
1993 const gchar
* const list
[],
1996 GString
*value_list
;
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
++)
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
);
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
2023 * @locale: a locale identifier
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.
2032 g_key_file_set_locale_string (GKeyFile
*key_file
,
2033 const gchar
*group_name
,
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
);
2053 * g_key_file_get_locale_string:
2054 * @key_file: a #GKeyFile
2055 * @group_name: a group name
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.
2075 g_key_file_get_locale_string (GKeyFile
*key_file
,
2076 const gchar
*group_name
,
2078 const gchar
*locale
,
2081 gchar
*candidate_key
, *translated_value
;
2082 GError
*key_file_error
;
2084 gboolean free_languages
= FALSE
;
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
;
2097 languages
= g_get_locale_variants (locale
);
2098 free_languages
= TRUE
;
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
,
2112 candidate_key
, NULL
);
2113 g_free (candidate_key
);
2115 if (translated_value
)
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
,
2129 if (!translated_value
)
2130 g_propagate_error (error
, key_file_error
);
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
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
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().
2166 g_key_file_get_locale_string_list (GKeyFile
*key_file
,
2167 const gchar
*group_name
,
2169 const gchar
*locale
,
2173 GError
*key_file_error
;
2174 gchar
**values
, *value
;
2175 char list_separator
[2];
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
,
2189 g_propagate_error (error
, key_file_error
);
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);
2209 *length
= g_strv_length (values
);
2215 * g_key_file_set_locale_string_list:
2216 * @key_file: a #GKeyFile
2217 * @group_name: a group name
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
2230 g_key_file_set_locale_string_list (GKeyFile
*key_file
,
2231 const gchar
*group_name
,
2233 const gchar
*locale
,
2234 const gchar
* const list
[],
2237 GString
*value_list
;
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
++)
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
);
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
);
2261 g_string_free (value_list
, TRUE
);
2265 * g_key_file_get_boolean:
2266 * @key_file: a #GKeyFile
2267 * @group_name: a group name
2269 * @error: return location for a #GError
2271 * Returns the value associated with @key under @group_name as a
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.
2285 g_key_file_get_boolean (GKeyFile
*key_file
,
2286 const gchar
*group_name
,
2290 GError
*key_file_error
= NULL
;
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
);
2302 g_propagate_error (error
, key_file_error
);
2306 bool_value
= g_key_file_parse_value_as_boolean (key_file
, value
,
2312 if (g_error_matches (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."),
2321 g_error_free (key_file_error
);
2324 g_propagate_error (error
, key_file_error
);
2331 * g_key_file_set_boolean:
2332 * @key_file: a #GKeyFile
2333 * @group_name: a group name
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.
2343 g_key_file_set_boolean (GKeyFile
*key_file
,
2344 const gchar
*group_name
,
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
);
2358 * g_key_file_get_boolean_list:
2359 * @key_file: a #GKeyFile
2360 * @group_name: a group name
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
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.
2381 g_key_file_get_boolean_list (GKeyFile
*key_file
,
2382 const gchar
*group_name
,
2387 GError
*key_file_error
;
2389 gboolean
*bool_values
;
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
);
2399 key_file_error
= NULL
;
2401 values
= g_key_file_get_string_list (key_file
, group_name
, key
,
2402 &num_bools
, &key_file_error
);
2405 g_propagate_error (error
, key_file_error
);
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
,
2420 g_propagate_error (error
, key_file_error
);
2421 g_strfreev (values
);
2422 g_free (bool_values
);
2427 g_strfreev (values
);
2430 *length
= num_bools
;
2436 * g_key_file_set_boolean_list:
2437 * @key_file: a #GKeyFile
2438 * @group_name: a group name
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.
2450 g_key_file_set_boolean_list (GKeyFile
*key_file
,
2451 const gchar
*group_name
,
2456 GString
*value_list
;
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
++)
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
);
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
2484 * @error: return location for a #GError
2486 * Returns the value associated with @key under @group_name as an
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.
2500 g_key_file_get_integer (GKeyFile
*key_file
,
2501 const gchar
*group_name
,
2505 GError
*key_file_error
;
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
);
2519 g_propagate_error (error
, key_file_error
);
2523 int_value
= g_key_file_parse_value_as_integer (key_file
, value
,
2529 if (g_error_matches (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."),
2538 g_error_free (key_file_error
);
2541 g_propagate_error (error
, key_file_error
);
2548 * g_key_file_set_integer:
2549 * @key_file: a #GKeyFile
2550 * @group_name: a group name
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.
2560 g_key_file_set_integer (GKeyFile
*key_file
,
2561 const gchar
*group_name
,
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
);
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.
2591 g_key_file_get_int64 (GKeyFile
*key_file
,
2592 const gchar
*group_name
,
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
);
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");
2625 * g_key_file_set_int64:
2626 * @key_file: a #GKeyFile
2627 * @group_name: a group name
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.
2637 g_key_file_set_int64 (GKeyFile
*key_file
,
2638 const gchar
*group_name
,
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
);
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.
2668 g_key_file_get_uint64 (GKeyFile
*key_file
,
2669 const gchar
*group_name
,
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
);
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");
2702 * g_key_file_set_uint64:
2703 * @key_file: a #GKeyFile
2704 * @group_name: a group name
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.
2714 g_key_file_set_uint64 (GKeyFile
*key_file
,
2715 const gchar
*group_name
,
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
);
2729 * g_key_file_get_integer_list:
2730 * @key_file: a #GKeyFile
2731 * @group_name: a group name
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
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.
2752 g_key_file_get_integer_list (GKeyFile
*key_file
,
2753 const gchar
*group_name
,
2758 GError
*key_file_error
= NULL
;
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
);
2770 values
= g_key_file_get_string_list (key_file
, group_name
, key
,
2771 &num_ints
, &key_file_error
);
2774 g_propagate_error (error
, key_file_error
);
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
,
2789 g_propagate_error (error
, key_file_error
);
2790 g_strfreev (values
);
2791 g_free (int_values
);
2796 g_strfreev (values
);
2805 * g_key_file_set_integer_list:
2806 * @key_file: a #GKeyFile
2807 * @group_name: a group name
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.
2818 g_key_file_set_integer_list (GKeyFile
*key_file
,
2819 const gchar
*group_name
,
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
++)
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
);
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
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.
2868 g_key_file_get_double (GKeyFile
*key_file
,
2869 const gchar
*group_name
,
2873 GError
*key_file_error
;
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
);
2887 g_propagate_error (error
, key_file_error
);
2891 double_value
= g_key_file_parse_value_as_double (key_file
, value
,
2897 if (g_error_matches (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."),
2906 g_error_free (key_file_error
);
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
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.
2928 g_key_file_set_double (GKeyFile
*key_file
,
2929 const gchar
*group_name
,
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
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
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.
2965 g_key_file_get_double_list (GKeyFile
*key_file
,
2966 const gchar
*group_name
,
2971 GError
*key_file_error
= NULL
;
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
);
2983 values
= g_key_file_get_string_list (key_file
, group_name
, key
,
2984 &num_doubles
, &key_file_error
);
2987 g_propagate_error (error
, key_file_error
);
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
,
3002 g_propagate_error (error
, key_file_error
);
3003 g_strfreev (values
);
3004 g_free (double_values
);
3009 g_strfreev (values
);
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
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.
3031 g_key_file_set_double_list (GKeyFile
*key_file
,
3032 const gchar
*group_name
,
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
);
3059 g_key_file_set_key_comment (GKeyFile
*key_file
,
3060 const gchar
*group_name
,
3062 const gchar
*comment
,
3065 GKeyFileGroup
*group
;
3066 GKeyFileKeyValuePair
*pair
;
3067 GList
*key_node
, *comment_node
, *tmp
;
3069 group
= g_key_file_lookup_group (key_file
, group_name
);
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)");
3080 /* First find the key the comments are supposed to be
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'"),
3094 /* Then find all the comments already associated with the
3097 tmp
= key_node
->next
;
3100 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3102 if (pair
->key
!= NULL
)
3107 g_key_file_remove_key_value_pair_node (key_file
, group
,
3111 if (comment
== NULL
)
3114 /* Now we can add our new comment
3116 pair
= g_slice_new (GKeyFileKeyValuePair
);
3118 pair
->value
= g_key_file_parse_comment_as_value (key_file
, comment
);
3120 key_node
= g_list_insert (key_node
, pair
, 1);
3126 g_key_file_set_group_comment (GKeyFile
*key_file
,
3127 const gchar
*group_name
,
3128 const gchar
*comment
,
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
);
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)");
3146 /* First remove any existing comment
3150 g_key_file_key_value_pair_free (group
->comment
);
3151 group
->comment
= NULL
;
3154 if (comment
== NULL
)
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
);
3167 g_key_file_set_top_comment (GKeyFile
*key_file
,
3168 const gchar
*comment
,
3172 GKeyFileGroup
*group
;
3173 GKeyFileKeyValuePair
*pair
;
3175 /* The last group in the list should be the top (comments only)
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
)
3192 pair
= g_slice_new (GKeyFileKeyValuePair
);
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
);
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
3220 g_key_file_set_comment (GKeyFile
*key_file
,
3221 const gchar
*group_name
,
3223 const gchar
*comment
,
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
))
3233 else if (group_name
!= NULL
)
3235 if (!g_key_file_set_group_comment (key_file
, group_name
, comment
, error
))
3240 if (!g_key_file_set_top_comment (key_file
, comment
, error
))
3248 g_key_file_get_key_comment (GKeyFile
*key_file
,
3249 const gchar
*group_name
,
3253 GKeyFileGroup
*group
;
3254 GKeyFileKeyValuePair
*pair
;
3255 GList
*key_node
, *tmp
;
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
);
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)");
3272 /* First find the key the comments are supposed to be
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'"),
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
)
3295 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3296 if (pair
->key
!= NULL
)
3301 pair
= (GKeyFileKeyValuePair
*) tmp
->next
->data
;
3303 if (pair
->key
!= NULL
)
3309 while (tmp
!= key_node
)
3311 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
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
);
3325 comment
= string
->str
;
3326 g_string_free (string
, FALSE
);
3335 get_group_comment (GKeyFile
*key_file
,
3336 GKeyFileGroup
*group
,
3345 tmp
= group
->key_value_pairs
;
3348 GKeyFileKeyValuePair
*pair
;
3350 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
3352 if (pair
->key
!= NULL
)
3358 if (tmp
->next
== NULL
)
3366 GKeyFileKeyValuePair
*pair
;
3368 pair
= (GKeyFileKeyValuePair
*) tmp
->data
;
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
);
3381 return g_string_free (string
, FALSE
);
3387 g_key_file_get_group_comment (GKeyFile
*key_file
,
3388 const gchar
*group_name
,
3392 GKeyFileGroup
*group
;
3394 group
= g_key_file_lookup_group (key_file
, group_name
);
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)");
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
);
3415 g_key_file_get_top_comment (GKeyFile
*key_file
,
3419 GKeyFileGroup
*group
;
3421 /* The last group in the list should be the top (comments only)
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
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()
3449 g_key_file_get_comment (GKeyFile
*key_file
,
3450 const gchar
*group_name
,
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
);
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
3482 g_key_file_remove_comment (GKeyFile
*key_file
,
3483 const gchar
*group_name
,
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
);
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
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.
3522 g_key_file_has_key_full (GKeyFile
*key_file
,
3523 const gchar
*group_name
,
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
);
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)");
3547 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
3550 *has_key
= pair
!= NULL
;
3555 * g_key_file_has_key: (skip)
3556 * @key_file: a #GKeyFile
3557 * @group_name: a group name
3559 * @error: return location for a #GError
3561 * Looks whether the key file has the key @key in the group
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
3577 g_key_file_has_key (GKeyFile
*key_file
,
3578 const gchar
*group_name
,
3582 GError
*temp_error
= NULL
;
3585 if (g_key_file_has_key_full (key_file
, group_name
, key
, &has_key
, &temp_error
))
3591 g_propagate_error (error
, temp_error
);
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
);
3608 key_file
->current_group
= group
;
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
);
3625 g_key_file_key_value_pair_free (GKeyFileKeyValuePair
*pair
)
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
3640 * - the node being removed is a comment node
3641 * - the entire lookup map is getting destroyed soon after
3645 g_key_file_remove_key_value_pair_node (GKeyFile
*key_file
,
3646 GKeyFileGroup
*group
,
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
);
3664 g_key_file_remove_group_node (GKeyFile
*key_file
,
3667 GKeyFileGroup
*group
;
3670 group
= (GKeyFileGroup
*) group_node
->data
;
3673 g_hash_table_remove (key_file
->group_hash
, group
->name
);
3675 /* If the current group gets deleted make the current group the last
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
;
3686 key_file
->current_group
= NULL
;
3689 /* If the start group gets deleted make the start group the first
3692 if (key_file
->start_group
== group
)
3694 tmp
= g_list_last (key_file
->groups
);
3697 if (tmp
!= group_node
&&
3698 ((GKeyFileGroup
*) tmp
->data
)->name
!= NULL
)
3705 key_file
->start_group
= (GKeyFileGroup
*) tmp
->data
;
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
;
3719 g_key_file_remove_key_value_pair_node (key_file
, group
, pair_node
);
3722 g_warn_if_fail (group
->key_value_pairs
== NULL
);
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
3755 g_key_file_remove_group (GKeyFile
*key_file
,
3756 const gchar
*group_name
,
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
);
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'"),
3775 g_key_file_remove_group_node (key_file
, group_node
);
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
);
3790 g_key_file_add_key (GKeyFile
*key_file
,
3791 GKeyFileGroup
*group
,
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
3818 g_key_file_remove_key (GKeyFile
*key_file
,
3819 const gchar
*group_name
,
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
);
3832 group
= g_key_file_lookup_group (key_file
, group_name
);
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)");
3842 pair
= g_key_file_lookup_key_value_pair (key_file
, group
, key
);
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'"),
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
);
3861 g_key_file_lookup_group_node (GKeyFile
*key_file
,
3862 const gchar
*group_name
)
3864 GKeyFileGroup
*group
;
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)
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
);
3886 g_key_file_lookup_key_value_pair_node (GKeyFile
*key_file
,
3887 GKeyFileGroup
*group
,
3892 for (key_node
= group
->key_value_pairs
;
3894 key_node
= key_node
->next
)
3896 GKeyFileKeyValuePair
*pair
;
3898 pair
= (GKeyFileKeyValuePair
*) key_node
->data
;
3900 if (pair
->key
&& strcmp (pair
->key
, key
) == 0)
3907 static GKeyFileKeyValuePair
*
3908 g_key_file_lookup_key_value_pair (GKeyFile
*key_file
,
3909 GKeyFileGroup
*group
,
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.
3920 g_key_file_line_is_comment (const gchar
*line
)
3922 return (*line
== '#' || *line
== '\0' || *line
== '\n');
3926 g_key_file_is_group_name (const gchar
*name
)
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
)
3944 g_key_file_is_key_name (const gchar
*name
)
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 */
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] == ' ')
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
);
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 ']'.
3992 g_key_file_line_is_group (const gchar
*line
)
4002 while (*p
&& *p
!= ']')
4003 p
= g_utf8_find_next_char (p
, NULL
);
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
);
4020 g_key_file_line_is_key_value_pair (const gchar
*line
)
4024 p
= (gchar
*) g_utf8_strchr (line
, -1, '=');
4029 /* Key must be non-empty
4038 g_key_file_parse_value_as_string (GKeyFile
*key_file
,
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
;
4078 g_set_error_literal (error
, G_KEY_FILE_ERROR
,
4079 G_KEY_FILE_ERROR_INVALID_VALUE
,
4080 _("Key file contains escape character "
4085 if (pieces
&& *p
== key_file
->list_separator
)
4086 *q
= key_file
->list_separator
;
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
);
4112 if (pieces
&& (*p
== key_file
->list_separator
))
4114 *pieces
= g_slist_prepend (*pieces
, g_strndup (q0
, q
- q0
));
4130 *pieces
= g_slist_prepend (*pieces
, g_strndup (q0
, q
- q0
));
4131 *pieces
= g_slist_reverse (*pieces
);
4134 return string_value
;
4138 g_key_file_parse_string_as_value (GKeyFile
*key_file
,
4139 const gchar
*string
,
4140 gboolean escape_separator
)
4142 gchar
*value
, *p
, *q
;
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
;
4155 parsing_leading_space
= TRUE
;
4156 while (p
< (string
+ length
- 1))
4158 gchar escaped_character
[3] = { '\\', 0, 0 };
4163 if (parsing_leading_space
)
4165 escaped_character
[1] = 's';
4166 strcpy (q
, escaped_character
);
4176 if (parsing_leading_space
)
4178 escaped_character
[1] = 't';
4179 strcpy (q
, escaped_character
);
4189 escaped_character
[1] = 'n';
4190 strcpy (q
, escaped_character
);
4194 escaped_character
[1] = 'r';
4195 strcpy (q
, escaped_character
);
4199 escaped_character
[1] = '\\';
4200 strcpy (q
, escaped_character
);
4202 parsing_leading_space
= FALSE
;
4205 if (escape_separator
&& *p
== key_file
->list_separator
)
4207 escaped_character
[1] = key_file
->list_separator
;
4208 strcpy (q
, escaped_character
);
4210 parsing_leading_space
= TRUE
;
4216 parsing_leading_space
= FALSE
;
4228 g_key_file_parse_value_as_integer (GKeyFile
*key_file
,
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
);
4251 int_value
= long_value
;
4252 if (int_value
!= long_value
|| errno
== ERANGE
)
4254 gchar
*value_utf8
= _g_utf8_make_valid (value
);
4257 G_KEY_FILE_ERROR_INVALID_VALUE
,
4258 _("Integer value '%s' out of range"),
4260 g_free (value_utf8
);
4269 g_key_file_parse_integer_as_value (GKeyFile
*key_file
,
4273 return g_strdup_printf ("%d", value
);
4277 g_key_file_parse_value_as_double (GKeyFile
*key_file
,
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."),
4294 g_free (value_utf8
);
4297 return double_value
;
4301 g_key_file_parse_value_as_boolean (GKeyFile
*key_file
,
4307 if (strcmp (value
, "true") == 0 || strcmp (value
, "1") == 0)
4309 else if (strcmp (value
, "false") == 0 || strcmp (value
, "0") == 0)
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
);
4323 g_key_file_parse_boolean_as_value (GKeyFile
*key_file
,
4327 return g_strdup ("true");
4329 return g_strdup ("false");
4333 g_key_file_parse_value_as_comment (GKeyFile
*key_file
,
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
]);
4349 g_string_append_printf (string
, "%s\n", lines
[i
] + 1);
4353 return g_string_free (string
, FALSE
);
4357 g_key_file_parse_comment_as_value (GKeyFile
*key_file
,
4358 const gchar
*comment
)
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");
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
4393 g_key_file_save_to_file (GKeyFile
*key_file
,
4394 const gchar
*filename
,
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
);