GKeyFile: Convert docs to markdown
[platform/upstream/glib.git] / glib / gkeyfile.c
1 /* gkeyfile.c - key file parser
2  *
3  *  Copyright 2004  Red Hat, Inc.  
4  *  Copyright 2009-2010  Collabora Ltd.
5  *  Copyright 2009  Nokia Corporation
6  *
7  * Written by Ray Strode <rstrode@redhat.com>
8  *            Matthias Clasen <mclasen@redhat.com>
9  *
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.
14  *
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.
19  *
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/>.
23  */
24
25 #include "config.h"
26
27 #include "gkeyfile.h"
28 #include "gutils.h"
29
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <locale.h>
33 #include <string.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #ifdef G_OS_UNIX
39 #include <unistd.h>
40 #endif
41 #ifdef G_OS_WIN32
42 #include <io.h>
43
44 #undef fstat
45 #define fstat(a,b) _fstati64(a,b)
46 #undef stat
47 #define stat _stati64
48
49 #ifndef S_ISREG
50 #define S_ISREG(mode) ((mode)&_S_IFREG)
51 #endif
52
53 #endif  /* G_OS_WIN23 */
54
55 #include "gconvert.h"
56 #include "gdataset.h"
57 #include "gerror.h"
58 #include "gfileutils.h"
59 #include "ghash.h"
60 #include "glibintl.h"
61 #include "glist.h"
62 #include "gslist.h"
63 #include "gmem.h"
64 #include "gmessages.h"
65 #include "gstdio.h"
66 #include "gstring.h"
67 #include "gstrfuncs.h"
68 #include "gutils.h"
69
70
71 /**
72  * SECTION:keyfile
73  * @title: Key-value file parser
74  * @short_description: parses .ini-like config files
75  *
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>.
84  *
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.
89  *
90  * |[
91  * # this is just an example
92  * # there can be comments before the first group
93  *
94  * [First Group]
95  *
96  * Name=Key File Example\tthis value shows\nescaping
97  *
98  * # localized strings are stored in multiple key-value pairs
99  * Welcome=Hello
100  * Welcome[de]=Hallo
101  * Welcome[fr_FR]=Bonjour
102  * Welcome[it]=Ciao
103  * Welcome[be@latin]=Hello
104  *
105  * [Another Group]
106  *
107  * Numbers=2;20;-200;0
108  *
109  * Booleans=true;false;true;true
110  * ]|
111  *
112  * Lines beginning with a '#' and blank lines are considered comments.
113  *
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.
117  *
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.
127  *
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.
132  *
133  * This syntax is obviously inspired by the .ini files commonly met
134  * on Windows, but there are some important differences:
135  *
136  * - .ini files use the ';' character to begin comments,
137  *   key files use the '#' character.
138  *
139  * - Key files do not allow for ungrouped keys meaning only
140  *   comments can precede the first group.
141  *
142  * - Key files are always encoded in UTF-8.
143  *
144  * - Key and Group names are case-sensitive. For example, a group called
145  *   [GROUP] is a different from [group].
146  *
147  * - .ini files don't have a strongly typed boolean entry type,
148  *    they only have GetProfileInt(). In key files, only
149  *    true and false (in lower case) are allowed.
150  *
151  * Note that in contrast to the
152  * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
153  * Entry Specification</ulink>, groups in key files may contain the same
154  * key multiple times; the last entry wins. Key files may also contain
155  * multiple groups with the same name; they are merged together.
156  * Another difference is that keys and group names in key files are not
157  * restricted to ASCII characters.
158  */
159
160 /**
161  * G_KEY_FILE_ERROR:
162  *
163  * Error domain for key file parsing. Errors in this domain will
164  * be from the #GKeyFileError enumeration.
165  *
166  * See #GError for information on error domains.
167  */
168
169 /**
170  * GKeyFileError:
171  * @G_KEY_FILE_ERROR_UNKNOWN_ENCODING: the text being parsed was in
172  *     an unknown encoding
173  * @G_KEY_FILE_ERROR_PARSE: document was ill-formed
174  * @G_KEY_FILE_ERROR_NOT_FOUND: the file was not found
175  * @G_KEY_FILE_ERROR_KEY_NOT_FOUND: a requested key was not found
176  * @G_KEY_FILE_ERROR_GROUP_NOT_FOUND: a requested group was not found
177  * @G_KEY_FILE_ERROR_INVALID_VALUE: a value could not be parsed
178  *
179  * Error codes returned by key file parsing.
180  */
181
182 /**
183  * GKeyFileFlags:
184  * @G_KEY_FILE_NONE: No flags, default behaviour
185  * @G_KEY_FILE_KEEP_COMMENTS: Use this flag if you plan to write the
186  *     (possibly modified) contents of the key file back to a file;
187  *     otherwise all comments will be lost when the key file is
188  *     written back.
189  * @G_KEY_FILE_KEEP_TRANSLATIONS: Use this flag if you plan to write the
190  *     (possibly modified) contents of the key file back to a file;
191  *     otherwise only the translations for the current language will be
192  *     written back.
193  *
194  * Flags which influence the parsing.
195  */
196
197 /**
198  * G_KEY_FILE_DESKTOP_GROUP:
199  *
200  * The name of the main group of a desktop entry file, as defined in the
201  * <ulink url="http://freedesktop.org/Standards/desktop-entry-spec">Desktop
202  * Entry Specification</ulink>. Consult the specification for more
203  * details about the meanings of the keys below.
204  *
205  * Since: 2.14
206  */
207
208 /**
209  * G_KEY_FILE_DESKTOP_KEY_TYPE:
210  *
211  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
212  * giving the type of the desktop entry. Usually
213  * #G_KEY_FILE_DESKTOP_TYPE_APPLICATION,
214  * #G_KEY_FILE_DESKTOP_TYPE_LINK, or
215  * #G_KEY_FILE_DESKTOP_TYPE_DIRECTORY.
216  *
217  * Since: 2.14
218  */
219
220 /**
221  * G_KEY_FILE_DESKTOP_KEY_VERSION:
222  *
223  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
224  * giving the version of the Desktop Entry Specification used for
225  * the desktop entry file.
226  *
227  * Since: 2.14
228  */
229
230 /**
231  * G_KEY_FILE_DESKTOP_KEY_NAME:
232  *
233  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
234  * string giving the specific name of the desktop entry.
235  *
236  * Since: 2.14
237  */
238
239 /**
240  * G_KEY_FILE_DESKTOP_KEY_GENERIC_NAME:
241  *
242  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
243  * string giving the generic name of the desktop entry.
244  *
245  * Since: 2.14
246  */
247
248 /**
249  * G_KEY_FILE_DESKTOP_KEY_NO_DISPLAY:
250  *
251  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
252  * stating whether the desktop entry should be shown in menus.
253  *
254  * Since: 2.14
255  */
256
257 /**
258  * G_KEY_FILE_DESKTOP_KEY_COMMENT:
259  *
260  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
261  * string giving the tooltip for the desktop entry.
262  *
263  * Since: 2.14
264  */
265
266 /**
267  * G_KEY_FILE_DESKTOP_KEY_ICON:
268  *
269  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a localized
270  * string giving the name of the icon to be displayed for the desktop
271  * entry.
272  *
273  * Since: 2.14
274  */
275
276 /**
277  * G_KEY_FILE_DESKTOP_KEY_HIDDEN:
278  *
279  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
280  * stating whether the desktop entry has been deleted by the user.
281  *
282  * Since: 2.14
283  */
284
285 /**
286  * G_KEY_FILE_DESKTOP_KEY_ONLY_SHOW_IN:
287  *
288  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
289  * strings identifying the environments that should display the
290  * desktop entry.
291  *
292  * Since: 2.14
293  */
294
295 /**
296  * G_KEY_FILE_DESKTOP_KEY_NOT_SHOW_IN:
297  *
298  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list of
299  * strings identifying the environments that should not display the
300  * desktop entry.
301  *
302  * Since: 2.14
303  */
304
305 /**
306  * G_KEY_FILE_DESKTOP_KEY_TRY_EXEC:
307  *
308  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
309  * giving the file name of a binary on disk used to determine if the
310  * program is actually installed. It is only valid for desktop entries
311  * with the <literal>Application</literal> type.
312  *
313  * Since: 2.14
314  */
315
316 /**
317  * G_KEY_FILE_DESKTOP_KEY_EXEC:
318  *
319  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
320  * giving the command line to execute. It is only valid for desktop
321  * entries with the <literal>Application</literal> type.
322  *
323  * Since: 2.14
324  */
325
326  /**
327   * G_KEY_FILE_DESKTOP_KEY_PATH:
328   *
329   * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
330   * containing the working directory to run the program in. It is only
331   * valid for desktop entries with the <literal>Application</literal> type.
332   *
333   * Since: 2.14
334   */
335
336 /**
337  * G_KEY_FILE_DESKTOP_KEY_TERMINAL:
338  *
339  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
340  * stating whether the program should be run in a terminal window.
341  * It is only valid for desktop entries with the
342  * <literal>Application</literal> type.
343  *
344  * Since: 2.14
345  */
346
347 /**
348  * G_KEY_FILE_DESKTOP_KEY_MIME_TYPE:
349  *
350  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
351  * of strings giving the MIME types supported by this desktop entry.
352  *
353  * Since: 2.14
354  */
355
356 /**
357  * G_KEY_FILE_DESKTOP_KEY_CATEGORIES:
358  *
359  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a list
360  * of strings giving the categories in which the desktop entry
361  * should be shown in a menu.
362  *
363  * Since: 2.14
364  */
365
366 /**
367  * G_KEY_FILE_DESKTOP_KEY_STARTUP_NOTIFY:
368  *
369  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a boolean
370  * stating whether the application supports the <ulink
371  * url="http://www.freedesktop.org/Standards/startup-notification-spec">Startup
372  * Notification Protocol Specification</ulink>.
373  *
374  * Since: 2.14
375  */
376
377 /**
378  * G_KEY_FILE_DESKTOP_KEY_STARTUP_WM_CLASS:
379  *
380  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is string
381  * identifying the WM class or name hint of a window that the application
382  * will create, which can be used to emulate Startup Notification with
383  * older applications.
384  *
385  * Since: 2.14
386  */
387
388 /**
389  * G_KEY_FILE_DESKTOP_KEY_URL :
390  *
391  * A key under #G_KEY_FILE_DESKTOP_GROUP, whose value is a string
392  * giving the URL to access. It is only valid for desktop entries
393  * with the <literal>Link</literal> type.
394  *
395  * Since: 2.14
396  */
397
398 /**
399  * G_KEY_FILE_DESKTOP_TYPE_APPLICATION:
400  *
401  * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
402  * entries representing applications.
403  *
404  * Since: 2.14
405  */
406
407 /**
408  * G_KEY_FILE_DESKTOP_TYPE_LINK:
409  *
410  * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
411  * entries representing links to documents.
412  *
413  * Since: 2.14
414  */
415
416 /**
417  * G_KEY_FILE_DESKTOP_TYPE_DIRECTORY:
418  *
419  * The value of the #G_KEY_FILE_DESKTOP_KEY_TYPE, key for desktop
420  * entries representing directories.
421  *
422  * Since: 2.14
423  */
424
425 typedef struct _GKeyFileGroup GKeyFileGroup;
426
427 /**
428  * GKeyFile:
429  *
430  * The GKeyFile struct contains only private data
431  * and should not be accessed directly.
432  */
433 struct _GKeyFile
434 {
435   GList *groups;
436   GHashTable *group_hash;
437
438   GKeyFileGroup *start_group;
439   GKeyFileGroup *current_group;
440
441   GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
442
443   gchar list_separator;
444
445   GKeyFileFlags flags;
446
447   gchar **locales;
448
449   volatile gint ref_count;
450 };
451
452 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
453
454 struct _GKeyFileGroup
455 {
456   const gchar *name;  /* NULL for above first group (which will be comments) */
457
458   GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
459
460   GList *key_value_pairs;
461
462   /* Used in parallel with key_value_pairs for
463    * increased lookup performance
464    */
465   GHashTable *lookup_map;
466 };
467
468 struct _GKeyFileKeyValuePair
469 {
470   gchar *key;  /* NULL for comments */
471   gchar *value;
472 };
473
474 static gint                  find_file_in_data_dirs            (const gchar            *file,
475                                                                 const gchar           **data_dirs,
476                                                                 gchar                 **output_file,
477                                                                 GError                **error);
478 static gboolean              g_key_file_load_from_fd           (GKeyFile               *key_file,
479                                                                 gint                    fd,
480                                                                 GKeyFileFlags           flags,
481                                                                 GError                **error);
482 static GList                *g_key_file_lookup_group_node      (GKeyFile               *key_file,
483                                                                 const gchar            *group_name);
484 static GKeyFileGroup        *g_key_file_lookup_group           (GKeyFile               *key_file,
485                                                                 const gchar            *group_name);
486
487 static GList                *g_key_file_lookup_key_value_pair_node  (GKeyFile       *key_file,
488                                                                      GKeyFileGroup  *group,
489                                                                      const gchar    *key);
490 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair       (GKeyFile       *key_file,
491                                                                      GKeyFileGroup  *group,
492                                                                      const gchar    *key);
493
494 static void                  g_key_file_remove_group_node          (GKeyFile      *key_file,
495                                                                     GList         *group_node);
496 static void                  g_key_file_remove_key_value_pair_node (GKeyFile      *key_file,
497                                                                     GKeyFileGroup *group,
498                                                                     GList         *pair_node);
499
500 static void                  g_key_file_add_key_value_pair     (GKeyFile               *key_file,
501                                                                 GKeyFileGroup          *group,
502                                                                 GKeyFileKeyValuePair   *pair);
503 static void                  g_key_file_add_key                (GKeyFile               *key_file,
504                                                                 GKeyFileGroup          *group,
505                                                                 const gchar            *key,
506                                                                 const gchar            *value);
507 static void                  g_key_file_add_group              (GKeyFile               *key_file,
508                                                                 const gchar            *group_name);
509 static gboolean              g_key_file_is_group_name          (const gchar *name);
510 static gboolean              g_key_file_is_key_name            (const gchar *name);
511 static void                  g_key_file_key_value_pair_free    (GKeyFileKeyValuePair   *pair);
512 static gboolean              g_key_file_line_is_comment        (const gchar            *line);
513 static gboolean              g_key_file_line_is_group          (const gchar            *line);
514 static gboolean              g_key_file_line_is_key_value_pair (const gchar            *line);
515 static gchar                *g_key_file_parse_value_as_string  (GKeyFile               *key_file,
516                                                                 const gchar            *value,
517                                                                 GSList                **separators,
518                                                                 GError                **error);
519 static gchar                *g_key_file_parse_string_as_value  (GKeyFile               *key_file,
520                                                                 const gchar            *string,
521                                                                 gboolean                escape_separator);
522 static gint                  g_key_file_parse_value_as_integer (GKeyFile               *key_file,
523                                                                 const gchar            *value,
524                                                                 GError                **error);
525 static gchar                *g_key_file_parse_integer_as_value (GKeyFile               *key_file,
526                                                                 gint                    value);
527 static gdouble               g_key_file_parse_value_as_double  (GKeyFile               *key_file,
528                                                                 const gchar            *value,
529                                                                 GError                **error);
530 static gboolean              g_key_file_parse_value_as_boolean (GKeyFile               *key_file,
531                                                                 const gchar            *value,
532                                                                 GError                **error);
533 static gchar                *g_key_file_parse_boolean_as_value (GKeyFile               *key_file,
534                                                                 gboolean                value);
535 static gchar                *g_key_file_parse_value_as_comment (GKeyFile               *key_file,
536                                                                 const gchar            *value);
537 static gchar                *g_key_file_parse_comment_as_value (GKeyFile               *key_file,
538                                                                 const gchar            *comment);
539 static void                  g_key_file_parse_key_value_pair   (GKeyFile               *key_file,
540                                                                 const gchar            *line,
541                                                                 gsize                   length,
542                                                                 GError                **error);
543 static void                  g_key_file_parse_comment          (GKeyFile               *key_file,
544                                                                 const gchar            *line,
545                                                                 gsize                   length,
546                                                                 GError                **error);
547 static void                  g_key_file_parse_group            (GKeyFile               *key_file,
548                                                                 const gchar            *line,
549                                                                 gsize                   length,
550                                                                 GError                **error);
551 static gchar                *key_get_locale                    (const gchar            *key);
552 static void                  g_key_file_parse_data             (GKeyFile               *key_file,
553                                                                 const gchar            *data,
554                                                                 gsize                   length,
555                                                                 GError                **error);
556 static void                  g_key_file_flush_parse_buffer     (GKeyFile               *key_file,
557                                                                 GError                **error);
558
559 G_DEFINE_QUARK (g-key-file-error-quark, g_key_file_error)
560
561 static void
562 g_key_file_init (GKeyFile *key_file)
563 {  
564   key_file->current_group = g_slice_new0 (GKeyFileGroup);
565   key_file->groups = g_list_prepend (NULL, key_file->current_group);
566   key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
567   key_file->start_group = NULL;
568   key_file->parse_buffer = g_string_sized_new (128);
569   key_file->list_separator = ';';
570   key_file->flags = 0;
571   key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
572 }
573
574 static void
575 g_key_file_clear (GKeyFile *key_file)
576 {
577   GList *tmp, *group_node;
578
579   if (key_file->locales) 
580     {
581       g_strfreev (key_file->locales);
582       key_file->locales = NULL;
583     }
584
585   if (key_file->parse_buffer)
586     {
587       g_string_free (key_file->parse_buffer, TRUE);
588       key_file->parse_buffer = NULL;
589     }
590
591   tmp = key_file->groups;
592   while (tmp != NULL)
593     {
594       group_node = tmp;
595       tmp = tmp->next;
596       g_key_file_remove_group_node (key_file, group_node);
597     }
598
599   if (key_file->group_hash != NULL)
600     {
601       g_hash_table_destroy (key_file->group_hash);
602       key_file->group_hash = NULL;
603     }
604
605   g_warn_if_fail (key_file->groups == NULL);
606 }
607
608
609 /**
610  * g_key_file_new:
611  *
612  * Creates a new empty #GKeyFile object. Use
613  * g_key_file_load_from_file(), g_key_file_load_from_data(),
614  * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
615  * read an existing key file.
616  *
617  * Return value: (transfer full): an empty #GKeyFile.
618  *
619  * Since: 2.6
620  **/
621 GKeyFile *
622 g_key_file_new (void)
623 {
624   GKeyFile *key_file;
625
626   key_file = g_slice_new0 (GKeyFile);
627   key_file->ref_count = 1;
628   g_key_file_init (key_file);
629
630   return key_file;
631 }
632
633 /**
634  * g_key_file_set_list_separator:
635  * @key_file: a #GKeyFile 
636  * @separator: the separator
637  *
638  * Sets the character which is used to separate
639  * values in lists. Typically ';' or ',' are used
640  * as separators. The default list separator is ';'.
641  *
642  * Since: 2.6
643  */
644 void
645 g_key_file_set_list_separator (GKeyFile *key_file,
646                                gchar     separator)
647 {
648   g_return_if_fail (key_file != NULL);
649
650   key_file->list_separator = separator;
651 }
652
653
654 /* Iterates through all the directories in *dirs trying to
655  * open file.  When it successfully locates and opens a file it
656  * returns the file descriptor to the open file.  It also
657  * outputs the absolute path of the file in output_file.
658  */
659 static gint
660 find_file_in_data_dirs (const gchar   *file,
661                         const gchar  **dirs,
662                         gchar        **output_file,
663                         GError       **error)
664 {
665   const gchar **data_dirs, *data_dir;
666   gchar *path;
667   gint fd;
668
669   path = NULL;
670   fd = -1;
671
672   if (dirs == NULL)
673     return fd;
674
675   data_dirs = dirs;
676
677   while (data_dirs && (data_dir = *data_dirs) && fd == -1)
678     {
679       gchar *candidate_file, *sub_dir;
680
681       candidate_file = (gchar *) file;
682       sub_dir = g_strdup ("");
683       while (candidate_file != NULL && fd == -1)
684         {
685           gchar *p;
686
687           path = g_build_filename (data_dir, sub_dir,
688                                    candidate_file, NULL);
689
690           fd = g_open (path, O_RDONLY, 0);
691
692           if (fd == -1)
693             {
694               g_free (path);
695               path = NULL;
696             }
697
698           candidate_file = strchr (candidate_file, '-');
699
700           if (candidate_file == NULL)
701             break;
702
703           candidate_file++;
704
705           g_free (sub_dir);
706           sub_dir = g_strndup (file, candidate_file - file - 1);
707
708           for (p = sub_dir; *p != '\0'; p++)
709             {
710               if (*p == '-')
711                 *p = G_DIR_SEPARATOR;
712             }
713         }
714       g_free (sub_dir);
715       data_dirs++;
716     }
717
718   if (fd == -1)
719     {
720       g_set_error_literal (error, G_KEY_FILE_ERROR,
721                            G_KEY_FILE_ERROR_NOT_FOUND,
722                            _("Valid key file could not be "
723                              "found in search dirs"));
724     }
725
726   if (output_file != NULL && fd > 0)
727     *output_file = g_strdup (path);
728
729   g_free (path);
730
731   return fd;
732 }
733
734 static gboolean
735 g_key_file_load_from_fd (GKeyFile       *key_file,
736                          gint            fd,
737                          GKeyFileFlags   flags,
738                          GError        **error)
739 {
740   GError *key_file_error = NULL;
741   gssize bytes_read;
742   struct stat stat_buf;
743   gchar read_buf[4096];
744   gchar list_separator;
745
746   if (fstat (fd, &stat_buf) < 0)
747     {
748       g_set_error_literal (error, G_FILE_ERROR,
749                            g_file_error_from_errno (errno),
750                            g_strerror (errno));
751       return FALSE;
752     }
753
754   if (!S_ISREG (stat_buf.st_mode))
755     {
756       g_set_error_literal (error, G_KEY_FILE_ERROR,
757                            G_KEY_FILE_ERROR_PARSE,
758                            _("Not a regular file"));
759       return FALSE;
760     }
761
762   list_separator = key_file->list_separator;
763   g_key_file_clear (key_file);
764   g_key_file_init (key_file);
765   key_file->list_separator = list_separator;
766   key_file->flags = flags;
767
768   do
769     {
770       bytes_read = read (fd, read_buf, 4096);
771
772       if (bytes_read == 0)  /* End of File */
773         break;
774
775       if (bytes_read < 0)
776         {
777           if (errno == EINTR || errno == EAGAIN)
778             continue;
779
780           g_set_error_literal (error, G_FILE_ERROR,
781                                g_file_error_from_errno (errno),
782                                g_strerror (errno));
783           return FALSE;
784         }
785
786       g_key_file_parse_data (key_file,
787                              read_buf, bytes_read,
788                              &key_file_error);
789     }
790   while (!key_file_error);
791
792   if (key_file_error)
793     {
794       g_propagate_error (error, key_file_error);
795       return FALSE;
796     }
797
798   g_key_file_flush_parse_buffer (key_file, &key_file_error);
799
800   if (key_file_error)
801     {
802       g_propagate_error (error, key_file_error);
803       return FALSE;
804     }
805
806   return TRUE;
807 }
808
809 /**
810  * g_key_file_load_from_file:
811  * @key_file: an empty #GKeyFile struct
812  * @file: (type filename): the path of a filename to load, in the GLib filename encoding
813  * @flags: flags from #GKeyFileFlags
814  * @error: return location for a #GError, or %NULL
815  *
816  * Loads a key file into an empty #GKeyFile structure.
817  * If the file could not be loaded then @error is set to
818  * either a #GFileError or #GKeyFileError.
819  *
820  * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
821  *
822  * Since: 2.6
823  **/
824 gboolean
825 g_key_file_load_from_file (GKeyFile       *key_file,
826                            const gchar    *file,
827                            GKeyFileFlags   flags,
828                            GError        **error)
829 {
830   GError *key_file_error = NULL;
831   gint fd;
832
833   g_return_val_if_fail (key_file != NULL, FALSE);
834   g_return_val_if_fail (file != NULL, FALSE);
835
836   fd = g_open (file, O_RDONLY, 0);
837
838   if (fd == -1)
839     {
840       g_set_error_literal (error, G_FILE_ERROR,
841                            g_file_error_from_errno (errno),
842                            g_strerror (errno));
843       return FALSE;
844     }
845
846   g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
847   close (fd);
848
849   if (key_file_error)
850     {
851       g_propagate_error (error, key_file_error);
852       return FALSE;
853     }
854
855   return TRUE;
856 }
857
858 /**
859  * g_key_file_load_from_data:
860  * @key_file: an empty #GKeyFile struct
861  * @data: key file loaded in memory
862  * @length: the length of @data in bytes (or (gsize)-1 if data is nul-terminated)
863  * @flags: flags from #GKeyFileFlags
864  * @error: return location for a #GError, or %NULL
865  *
866  * Loads a key file from memory into an empty #GKeyFile structure.  
867  * If the object cannot be created then %error is set to a #GKeyFileError. 
868  *
869  * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
870  *
871  * Since: 2.6
872  **/
873 gboolean
874 g_key_file_load_from_data (GKeyFile       *key_file,
875                            const gchar    *data,
876                            gsize           length,
877                            GKeyFileFlags   flags,
878                            GError        **error)
879 {
880   GError *key_file_error = NULL;
881   gchar list_separator;
882
883   g_return_val_if_fail (key_file != NULL, FALSE);
884   g_return_val_if_fail (data != NULL || length == 0, FALSE);
885
886   if (length == (gsize)-1)
887     length = strlen (data);
888
889   list_separator = key_file->list_separator;
890   g_key_file_clear (key_file);
891   g_key_file_init (key_file);
892   key_file->list_separator = list_separator;
893   key_file->flags = flags;
894
895   g_key_file_parse_data (key_file, data, length, &key_file_error);
896   
897   if (key_file_error)
898     {
899       g_propagate_error (error, key_file_error);
900       return FALSE;
901     }
902
903   g_key_file_flush_parse_buffer (key_file, &key_file_error);
904   
905   if (key_file_error)
906     {
907       g_propagate_error (error, key_file_error);
908       return FALSE;
909     }
910
911   return TRUE;
912 }
913
914 /**
915  * g_key_file_load_from_dirs:
916  * @key_file: an empty #GKeyFile struct
917  * @file: (type filename): a relative path to a filename to open and parse
918  * @search_dirs: (array zero-terminated=1) (element-type filename): %NULL-terminated array of directories to search
919  * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
920  *   of the file, or %NULL
921  * @flags: flags from #GKeyFileFlags
922  * @error: return location for a #GError, or %NULL
923  *
924  * This function looks for a key file named @file in the paths
925  * specified in @search_dirs, loads the file into @key_file and
926  * returns the file's full path in @full_path.  If the file could not
927  * be loaded then an %error is set to either a #GFileError or
928  * #GKeyFileError.
929  *
930  * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
931  *
932  * Since: 2.14
933  **/
934 gboolean
935 g_key_file_load_from_dirs (GKeyFile       *key_file,
936                            const gchar    *file,
937                            const gchar   **search_dirs,
938                            gchar         **full_path,
939                            GKeyFileFlags   flags,
940                            GError        **error)
941 {
942   GError *key_file_error = NULL;
943   const gchar **data_dirs;
944   gchar *output_path;
945   gint fd;
946   gboolean found_file;
947
948   g_return_val_if_fail (key_file != NULL, FALSE);
949   g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
950   g_return_val_if_fail (search_dirs != NULL, FALSE);
951
952   found_file = FALSE;
953   data_dirs = search_dirs;
954   output_path = NULL;
955   while (*data_dirs != NULL && !found_file)
956     {
957       g_free (output_path);
958
959       fd = find_file_in_data_dirs (file, data_dirs, &output_path,
960                                    &key_file_error);
961
962       if (fd == -1)
963         {
964           if (key_file_error)
965             g_propagate_error (error, key_file_error);
966           break;
967         }
968
969       found_file = g_key_file_load_from_fd (key_file, fd, flags,
970                                             &key_file_error);
971       close (fd);
972
973       if (key_file_error)
974         {
975           g_propagate_error (error, key_file_error);
976           break;
977         }
978     }
979
980   if (found_file && full_path)
981     *full_path = output_path;
982   else
983     g_free (output_path);
984
985   return found_file;
986 }
987
988 /**
989  * g_key_file_load_from_data_dirs:
990  * @key_file: an empty #GKeyFile struct
991  * @file: (type filename): a relative path to a filename to open and parse
992  * @full_path: (out) (type filename) (allow-none): return location for a string containing the full path
993  *   of the file, or %NULL
994  * @flags: flags from #GKeyFileFlags 
995  * @error: return location for a #GError, or %NULL
996  *
997  * This function looks for a key file named @file in the paths 
998  * returned from g_get_user_data_dir() and g_get_system_data_dirs(), 
999  * loads the file into @key_file and returns the file's full path in 
1000  * @full_path.  If the file could not be loaded then an %error is
1001  * set to either a #GFileError or #GKeyFileError.
1002  *
1003  * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1004  * Since: 2.6
1005  **/
1006 gboolean
1007 g_key_file_load_from_data_dirs (GKeyFile       *key_file,
1008                                 const gchar    *file,
1009                                 gchar         **full_path,
1010                                 GKeyFileFlags   flags,
1011                                 GError        **error)
1012 {
1013   gchar **all_data_dirs;
1014   const gchar * user_data_dir;
1015   const gchar * const * system_data_dirs;
1016   gsize i, j;
1017   gboolean found_file;
1018
1019   g_return_val_if_fail (key_file != NULL, FALSE);
1020   g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1021
1022   user_data_dir = g_get_user_data_dir ();
1023   system_data_dirs = g_get_system_data_dirs ();
1024   all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1025
1026   i = 0;
1027   all_data_dirs[i++] = g_strdup (user_data_dir);
1028
1029   j = 0;
1030   while (system_data_dirs[j] != NULL)
1031     all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1032   all_data_dirs[i] = NULL;
1033
1034   found_file = g_key_file_load_from_dirs (key_file,
1035                                           file,
1036                                           (const gchar **)all_data_dirs,
1037                                           full_path,
1038                                           flags,
1039                                           error);
1040
1041   g_strfreev (all_data_dirs);
1042
1043   return found_file;
1044 }
1045
1046 /**
1047  * g_key_file_ref: (skip)
1048  * @key_file: a #GKeyFile
1049  *
1050  * Increases the reference count of @key_file.
1051  *
1052  * Returns: the same @key_file.
1053  *
1054  * Since: 2.32
1055  **/
1056 GKeyFile *
1057 g_key_file_ref (GKeyFile *key_file)
1058 {
1059   g_return_val_if_fail (key_file != NULL, NULL);
1060
1061   g_atomic_int_inc (&key_file->ref_count);
1062
1063   return key_file;
1064 }
1065
1066 /**
1067  * g_key_file_free: (skip)
1068  * @key_file: a #GKeyFile
1069  *
1070  * Clears all keys and groups from @key_file, and decreases the
1071  * reference count by 1. If the reference count reaches zero,
1072  * frees the key file and all its allocated memory.
1073  *
1074  * Since: 2.6
1075  **/
1076 void
1077 g_key_file_free (GKeyFile *key_file)
1078 {
1079   g_return_if_fail (key_file != NULL);
1080
1081   g_key_file_clear (key_file);
1082   g_key_file_unref (key_file);
1083 }
1084
1085 /**
1086  * g_key_file_unref:
1087  * @key_file: a #GKeyFile
1088  *
1089  * Decreases the reference count of @key_file by 1. If the reference count
1090  * reaches zero, frees the key file and all its allocated memory.
1091  *
1092  * Since: 2.32
1093  **/
1094 void
1095 g_key_file_unref (GKeyFile *key_file)
1096 {
1097   g_return_if_fail (key_file != NULL);
1098
1099   if (g_atomic_int_dec_and_test (&key_file->ref_count))
1100     {
1101       g_key_file_clear (key_file);
1102       g_slice_free (GKeyFile, key_file);
1103     }
1104 }
1105
1106 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
1107  * true for locales that match those in g_get_language_names().
1108  */
1109 static gboolean
1110 g_key_file_locale_is_interesting (GKeyFile    *key_file,
1111                                   const gchar *locale)
1112 {
1113   gsize i;
1114
1115   if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
1116     return TRUE;
1117
1118   for (i = 0; key_file->locales[i] != NULL; i++)
1119     {
1120       if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
1121         return TRUE;
1122     }
1123
1124   return FALSE;
1125 }
1126
1127 static void
1128 g_key_file_parse_line (GKeyFile     *key_file,
1129                        const gchar  *line,
1130                        gsize         length,
1131                        GError      **error)
1132 {
1133   GError *parse_error = NULL;
1134   gchar *line_start;
1135
1136   g_return_if_fail (key_file != NULL);
1137   g_return_if_fail (line != NULL);
1138
1139   line_start = (gchar *) line;
1140   while (g_ascii_isspace (*line_start))
1141     line_start++;
1142
1143   if (g_key_file_line_is_comment (line_start))
1144     g_key_file_parse_comment (key_file, line, length, &parse_error);
1145   else if (g_key_file_line_is_group (line_start))
1146     g_key_file_parse_group (key_file, line_start,
1147                             length - (line_start - line),
1148                             &parse_error);
1149   else if (g_key_file_line_is_key_value_pair (line_start))
1150     g_key_file_parse_key_value_pair (key_file, line_start,
1151                                      length - (line_start - line),
1152                                      &parse_error);
1153   else
1154     {
1155       gchar *line_utf8 = _g_utf8_make_valid (line);
1156       g_set_error (error, G_KEY_FILE_ERROR,
1157                    G_KEY_FILE_ERROR_PARSE,
1158                    _("Key file contains line '%s' which is not "
1159                      "a key-value pair, group, or comment"),
1160                    line_utf8);
1161       g_free (line_utf8);
1162
1163       return;
1164     }
1165
1166   if (parse_error)
1167     g_propagate_error (error, parse_error);
1168 }
1169
1170 static void
1171 g_key_file_parse_comment (GKeyFile     *key_file,
1172                           const gchar  *line,
1173                           gsize         length,
1174                           GError      **error)
1175 {
1176   GKeyFileKeyValuePair *pair;
1177   
1178   if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
1179     return;
1180   
1181   g_warn_if_fail (key_file->current_group != NULL);
1182
1183   pair = g_slice_new (GKeyFileKeyValuePair);
1184   pair->key = NULL;
1185   pair->value = g_strndup (line, length);
1186   
1187   key_file->current_group->key_value_pairs =
1188     g_list_prepend (key_file->current_group->key_value_pairs, pair);
1189 }
1190
1191 static void
1192 g_key_file_parse_group (GKeyFile     *key_file,
1193                         const gchar  *line,
1194                         gsize         length,
1195                         GError      **error)
1196 {
1197   gchar *group_name;
1198   const gchar *group_name_start, *group_name_end;
1199   
1200   /* advance past opening '['
1201    */
1202   group_name_start = line + 1;
1203   group_name_end = line + length - 1;
1204   
1205   while (*group_name_end != ']')
1206     group_name_end--;
1207
1208   group_name = g_strndup (group_name_start, 
1209                           group_name_end - group_name_start);
1210   
1211   if (!g_key_file_is_group_name (group_name))
1212     {
1213       g_set_error (error, G_KEY_FILE_ERROR,
1214                    G_KEY_FILE_ERROR_PARSE,
1215                    _("Invalid group name: %s"), group_name);
1216       g_free (group_name);
1217       return;
1218     }
1219
1220   g_key_file_add_group (key_file, group_name);
1221   g_free (group_name);
1222 }
1223
1224 static void
1225 g_key_file_parse_key_value_pair (GKeyFile     *key_file,
1226                                  const gchar  *line,
1227                                  gsize         length,
1228                                  GError      **error)
1229 {
1230   gchar *key, *value, *key_end, *value_start, *locale;
1231   gsize key_len, value_len;
1232
1233   if (key_file->current_group == NULL || key_file->current_group->name == NULL)
1234     {
1235       g_set_error_literal (error, G_KEY_FILE_ERROR,
1236                            G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1237                            _("Key file does not start with a group"));
1238       return;
1239     }
1240
1241   key_end = value_start = strchr (line, '=');
1242
1243   g_warn_if_fail (key_end != NULL);
1244
1245   key_end--;
1246   value_start++;
1247
1248   /* Pull the key name from the line (chomping trailing whitespace)
1249    */
1250   while (g_ascii_isspace (*key_end))
1251     key_end--;
1252
1253   key_len = key_end - line + 2;
1254
1255   g_warn_if_fail (key_len <= length);
1256
1257   key = g_strndup (line, key_len - 1);
1258
1259   if (!g_key_file_is_key_name (key))
1260     {
1261       g_set_error (error, G_KEY_FILE_ERROR,
1262                    G_KEY_FILE_ERROR_PARSE,
1263                    _("Invalid key name: %s"), key);
1264       g_free (key);
1265       return; 
1266     }
1267
1268   /* Pull the value from the line (chugging leading whitespace)
1269    */
1270   while (g_ascii_isspace (*value_start))
1271     value_start++;
1272
1273   value_len = line + length - value_start + 1;
1274
1275   value = g_strndup (value_start, value_len);
1276
1277   g_warn_if_fail (key_file->start_group != NULL);
1278
1279   if (key_file->current_group
1280       && key_file->current_group->name
1281       && strcmp (key_file->start_group->name,
1282                  key_file->current_group->name) == 0
1283       && strcmp (key, "Encoding") == 0)
1284     {
1285       if (g_ascii_strcasecmp (value, "UTF-8") != 0)
1286         {
1287           gchar *value_utf8 = _g_utf8_make_valid (value);
1288           g_set_error (error, G_KEY_FILE_ERROR,
1289                        G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1290                        _("Key file contains unsupported "
1291                          "encoding '%s'"), value_utf8);
1292           g_free (value_utf8);
1293
1294           g_free (key);
1295           g_free (value);
1296           return;
1297         }
1298     }
1299
1300   /* Is this key a translation? If so, is it one that we care about?
1301    */
1302   locale = key_get_locale (key);
1303
1304   if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
1305     {
1306       GKeyFileKeyValuePair *pair;
1307
1308       pair = g_slice_new (GKeyFileKeyValuePair);
1309       pair->key = key;
1310       pair->value = value;
1311
1312       g_key_file_add_key_value_pair (key_file, key_file->current_group, pair);
1313     }
1314   else
1315     {
1316       g_free (key);
1317       g_free (value);
1318     }
1319
1320   g_free (locale);
1321 }
1322
1323 static gchar *
1324 key_get_locale (const gchar *key)
1325 {
1326   gchar *locale;
1327
1328   locale = g_strrstr (key, "[");
1329
1330   if (locale && strlen (locale) <= 2)
1331     locale = NULL;
1332
1333   if (locale)
1334     locale = g_strndup (locale + 1, strlen (locale) - 2);
1335
1336   return locale;
1337 }
1338
1339 static void
1340 g_key_file_parse_data (GKeyFile     *key_file,
1341                        const gchar  *data,
1342                        gsize         length,
1343                        GError      **error)
1344 {
1345   GError *parse_error;
1346   gsize i;
1347
1348   g_return_if_fail (key_file != NULL);
1349   g_return_if_fail (data != NULL || length == 0);
1350
1351   parse_error = NULL;
1352
1353   i = 0;
1354   while (i < length)
1355     {
1356       if (data[i] == '\n')
1357         {
1358           if (key_file->parse_buffer->len > 0
1359               && (key_file->parse_buffer->str[key_file->parse_buffer->len - 1]
1360                   == '\r'))
1361             g_string_erase (key_file->parse_buffer,
1362                             key_file->parse_buffer->len - 1,
1363                             1);
1364             
1365           /* When a newline is encountered flush the parse buffer so that the
1366            * line can be parsed.  Note that completely blank lines won't show
1367            * up in the parse buffer, so they get parsed directly.
1368            */
1369           if (key_file->parse_buffer->len > 0)
1370             g_key_file_flush_parse_buffer (key_file, &parse_error);
1371           else
1372             g_key_file_parse_comment (key_file, "", 1, &parse_error);
1373
1374           if (parse_error)
1375             {
1376               g_propagate_error (error, parse_error);
1377               return;
1378             }
1379           i++;
1380         }
1381       else
1382         {
1383           const gchar *start_of_line;
1384           const gchar *end_of_line;
1385           gsize line_length;
1386
1387           start_of_line = data + i;
1388           end_of_line = memchr (start_of_line, '\n', length - i);
1389
1390           if (end_of_line == NULL)
1391             end_of_line = data + length;
1392
1393           line_length = end_of_line - start_of_line;
1394
1395           g_string_append_len (key_file->parse_buffer, start_of_line, line_length);
1396           i += line_length;
1397         }
1398     }
1399 }
1400
1401 static void
1402 g_key_file_flush_parse_buffer (GKeyFile  *key_file,
1403                                GError   **error)
1404 {
1405   GError *file_error = NULL;
1406
1407   g_return_if_fail (key_file != NULL);
1408
1409   file_error = NULL;
1410
1411   if (key_file->parse_buffer->len > 0)
1412     {
1413       g_key_file_parse_line (key_file, key_file->parse_buffer->str,
1414                              key_file->parse_buffer->len,
1415                              &file_error);
1416       g_string_erase (key_file->parse_buffer, 0, -1);
1417
1418       if (file_error)
1419         {
1420           g_propagate_error (error, file_error);
1421           return;
1422         }
1423     }
1424 }
1425
1426 /**
1427  * g_key_file_to_data:
1428  * @key_file: a #GKeyFile
1429  * @length: (out) (allow-none): return location for the length of the
1430  *   returned string, or %NULL
1431  * @error: return location for a #GError, or %NULL
1432  *
1433  * This function outputs @key_file as a string.  
1434  *
1435  * Note that this function never reports an error,
1436  * so it is safe to pass %NULL as @error.
1437  *
1438  * Return value: a newly allocated string holding
1439  *   the contents of the #GKeyFile 
1440  *
1441  * Since: 2.6
1442  **/
1443 gchar *
1444 g_key_file_to_data (GKeyFile  *key_file,
1445                     gsize     *length,
1446                     GError   **error)
1447 {
1448   GString *data_string;
1449   GList *group_node, *key_file_node;
1450
1451   g_return_val_if_fail (key_file != NULL, NULL);
1452
1453   data_string = g_string_new (NULL);
1454
1455   for (group_node = g_list_last (key_file->groups);
1456        group_node != NULL;
1457        group_node = group_node->prev)
1458     {
1459       GKeyFileGroup *group;
1460
1461       group = (GKeyFileGroup *) group_node->data;
1462
1463       /* separate groups by at least an empty line */
1464       if (data_string->len >= 2 &&
1465           data_string->str[data_string->len - 2] != '\n')
1466         g_string_append_c (data_string, '\n');
1467
1468       if (group->comment != NULL)
1469         g_string_append_printf (data_string, "%s\n", group->comment->value);
1470
1471       if (group->name != NULL)
1472         g_string_append_printf (data_string, "[%s]\n", group->name);
1473
1474       for (key_file_node = g_list_last (group->key_value_pairs);
1475            key_file_node != NULL;
1476            key_file_node = key_file_node->prev)
1477         {
1478           GKeyFileKeyValuePair *pair;
1479
1480           pair = (GKeyFileKeyValuePair *) key_file_node->data;
1481
1482           if (pair->key != NULL)
1483             g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1484           else
1485             g_string_append_printf (data_string, "%s\n", pair->value);
1486         }
1487     }
1488
1489   if (length)
1490     *length = data_string->len;
1491
1492   return g_string_free (data_string, FALSE);
1493 }
1494
1495 /**
1496  * g_key_file_get_keys:
1497  * @key_file: a #GKeyFile
1498  * @group_name: a group name
1499  * @length: (out) (allow-none): return location for the number of keys returned, or %NULL
1500  * @error: return location for a #GError, or %NULL
1501  *
1502  * Returns all keys for the group name @group_name.  The array of
1503  * returned keys will be %NULL-terminated, so @length may
1504  * optionally be %NULL. In the event that the @group_name cannot
1505  * be found, %NULL is returned and @error is set to
1506  * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1507  *
1508  * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1509  *     Use g_strfreev() to free it.
1510  *
1511  * Since: 2.6
1512  **/
1513 gchar **
1514 g_key_file_get_keys (GKeyFile     *key_file,
1515                      const gchar  *group_name,
1516                      gsize        *length,
1517                      GError      **error)
1518 {
1519   GKeyFileGroup *group;
1520   GList *tmp;
1521   gchar **keys;
1522   gsize i, num_keys;
1523   
1524   g_return_val_if_fail (key_file != NULL, NULL);
1525   g_return_val_if_fail (group_name != NULL, NULL);
1526   
1527   group = g_key_file_lookup_group (key_file, group_name);
1528   
1529   if (!group)
1530     {
1531       g_set_error (error, G_KEY_FILE_ERROR,
1532                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1533                    _("Key file does not have group '%s'"),
1534                    group_name ? group_name : "(null)");
1535       return NULL;
1536     }
1537
1538   num_keys = 0;
1539   for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1540     {
1541       GKeyFileKeyValuePair *pair;
1542
1543       pair = (GKeyFileKeyValuePair *) tmp->data;
1544
1545       if (pair->key)
1546         num_keys++;
1547     }
1548   
1549   keys = g_new (gchar *, num_keys + 1);
1550
1551   i = num_keys - 1;
1552   for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1553     {
1554       GKeyFileKeyValuePair *pair;
1555
1556       pair = (GKeyFileKeyValuePair *) tmp->data;
1557
1558       if (pair->key)
1559         {
1560           keys[i] = g_strdup (pair->key);
1561           i--;
1562         }
1563     }
1564
1565   keys[num_keys] = NULL;
1566
1567   if (length)
1568     *length = num_keys;
1569
1570   return keys;
1571 }
1572
1573 /**
1574  * g_key_file_get_start_group:
1575  * @key_file: a #GKeyFile
1576  *
1577  * Returns the name of the start group of the file. 
1578  *
1579  * Return value: The start group of the key file.
1580  *
1581  * Since: 2.6
1582  **/
1583 gchar *
1584 g_key_file_get_start_group (GKeyFile *key_file)
1585 {
1586   g_return_val_if_fail (key_file != NULL, NULL);
1587
1588   if (key_file->start_group)
1589     return g_strdup (key_file->start_group->name);
1590
1591   return NULL;
1592 }
1593
1594 /**
1595  * g_key_file_get_groups:
1596  * @key_file: a #GKeyFile
1597  * @length: (out) (allow-none): return location for the number of returned groups, or %NULL
1598  *
1599  * Returns all groups in the key file loaded with @key_file.  
1600  * The array of returned groups will be %NULL-terminated, so 
1601  * @length may optionally be %NULL.
1602  *
1603  * Return value: (array zero-terminated=1) (transfer full): a newly-allocated %NULL-terminated array of strings.
1604  *   Use g_strfreev() to free it.
1605  * Since: 2.6
1606  **/
1607 gchar **
1608 g_key_file_get_groups (GKeyFile *key_file,
1609                        gsize    *length)
1610 {
1611   GList *group_node;
1612   gchar **groups;
1613   gsize i, num_groups;
1614
1615   g_return_val_if_fail (key_file != NULL, NULL);
1616
1617   num_groups = g_list_length (key_file->groups);
1618
1619   g_return_val_if_fail (num_groups > 0, NULL);
1620
1621   group_node = g_list_last (key_file->groups);
1622   
1623   g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1624
1625   /* Only need num_groups instead of num_groups + 1
1626    * because the first group of the file (last in the
1627    * list) is always the comment group at the top,
1628    * which we skip
1629    */
1630   groups = g_new (gchar *, num_groups);
1631
1632
1633   i = 0;
1634   for (group_node = group_node->prev;
1635        group_node != NULL;
1636        group_node = group_node->prev)
1637     {
1638       GKeyFileGroup *group;
1639
1640       group = (GKeyFileGroup *) group_node->data;
1641
1642       g_warn_if_fail (group->name != NULL);
1643
1644       groups[i++] = g_strdup (group->name);
1645     }
1646   groups[i] = NULL;
1647
1648   if (length)
1649     *length = i;
1650
1651   return groups;
1652 }
1653
1654 /**
1655  * g_key_file_get_value:
1656  * @key_file: a #GKeyFile
1657  * @group_name: a group name
1658  * @key: a key
1659  * @error: return location for a #GError, or %NULL
1660  *
1661  * Returns the raw value associated with @key under @group_name. 
1662  * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string. 
1663  *
1664  * In the event the key cannot be found, %NULL is returned and 
1665  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND.  In the 
1666  * event that the @group_name cannot be found, %NULL is returned 
1667  * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1668  *
1669  *
1670  * Return value: a newly allocated string or %NULL if the specified 
1671  *  key cannot be found.
1672  *
1673  * Since: 2.6
1674  **/
1675 gchar *
1676 g_key_file_get_value (GKeyFile     *key_file,
1677                       const gchar  *group_name,
1678                       const gchar  *key,
1679                       GError      **error)
1680 {
1681   GKeyFileGroup *group;
1682   GKeyFileKeyValuePair *pair;
1683   gchar *value = NULL;
1684
1685   g_return_val_if_fail (key_file != NULL, NULL);
1686   g_return_val_if_fail (group_name != NULL, NULL);
1687   g_return_val_if_fail (key != NULL, NULL);
1688   
1689   group = g_key_file_lookup_group (key_file, group_name);
1690
1691   if (!group)
1692     {
1693       g_set_error (error, G_KEY_FILE_ERROR,
1694                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1695                    _("Key file does not have group '%s'"),
1696                    group_name ? group_name : "(null)");
1697       return NULL;
1698     }
1699
1700   pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1701
1702   if (pair)
1703     value = g_strdup (pair->value);
1704   else
1705     g_set_error (error, G_KEY_FILE_ERROR,
1706                  G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1707                  _("Key file does not have key '%s'"), key);
1708
1709   return value;
1710 }
1711
1712 /**
1713  * g_key_file_set_value:
1714  * @key_file: a #GKeyFile
1715  * @group_name: a group name
1716  * @key: a key
1717  * @value: a string
1718  *
1719  * Associates a new value with @key under @group_name.  
1720  *
1721  * If @key cannot be found then it is created. If @group_name cannot 
1722  * be found then it is created. To set an UTF-8 string which may contain 
1723  * characters that need escaping (such as newlines or spaces), use 
1724  * g_key_file_set_string().
1725  *
1726  * Since: 2.6
1727  **/
1728 void
1729 g_key_file_set_value (GKeyFile    *key_file,
1730                       const gchar *group_name,
1731                       const gchar *key,
1732                       const gchar *value)
1733 {
1734   GKeyFileGroup *group;
1735   GKeyFileKeyValuePair *pair;
1736
1737   g_return_if_fail (key_file != NULL);
1738   g_return_if_fail (g_key_file_is_group_name (group_name));
1739   g_return_if_fail (g_key_file_is_key_name (key));
1740   g_return_if_fail (value != NULL);
1741
1742   group = g_key_file_lookup_group (key_file, group_name);
1743
1744   if (!group)
1745     {
1746       g_key_file_add_group (key_file, group_name);
1747       group = (GKeyFileGroup *) key_file->groups->data;
1748
1749       g_key_file_add_key (key_file, group, key, value);
1750     }
1751   else
1752     {
1753       pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1754
1755       if (!pair)
1756         g_key_file_add_key (key_file, group, key, value);
1757       else
1758         {
1759           g_free (pair->value);
1760           pair->value = g_strdup (value);
1761         }
1762     }
1763 }
1764
1765 /**
1766  * g_key_file_get_string:
1767  * @key_file: a #GKeyFile
1768  * @group_name: a group name
1769  * @key: a key
1770  * @error: return location for a #GError, or %NULL
1771  *
1772  * Returns the string value associated with @key under @group_name.
1773  * Unlike g_key_file_get_value(), this function handles escape sequences
1774  * like \s.
1775  *
1776  * In the event the key cannot be found, %NULL is returned and 
1777  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND.  In the 
1778  * event that the @group_name cannot be found, %NULL is returned 
1779  * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1780  *
1781  * Return value: a newly allocated string or %NULL if the specified 
1782  *   key cannot be found.
1783  *
1784  * Since: 2.6
1785  **/
1786 gchar *
1787 g_key_file_get_string (GKeyFile     *key_file,
1788                        const gchar  *group_name,
1789                        const gchar  *key,
1790                        GError      **error)
1791 {
1792   gchar *value, *string_value;
1793   GError *key_file_error;
1794
1795   g_return_val_if_fail (key_file != NULL, NULL);
1796   g_return_val_if_fail (group_name != NULL, NULL);
1797   g_return_val_if_fail (key != NULL, NULL);
1798
1799   key_file_error = NULL;
1800
1801   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1802
1803   if (key_file_error)
1804     {
1805       g_propagate_error (error, key_file_error);
1806       return NULL;
1807     }
1808
1809   if (!g_utf8_validate (value, -1, NULL))
1810     {
1811       gchar *value_utf8 = _g_utf8_make_valid (value);
1812       g_set_error (error, G_KEY_FILE_ERROR,
1813                    G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1814                    _("Key file contains key '%s' with value '%s' "
1815                      "which is not UTF-8"), key, value_utf8);
1816       g_free (value_utf8);
1817       g_free (value);
1818
1819       return NULL;
1820     }
1821
1822   string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1823                                                    &key_file_error);
1824   g_free (value);
1825
1826   if (key_file_error)
1827     {
1828       if (g_error_matches (key_file_error,
1829                            G_KEY_FILE_ERROR,
1830                            G_KEY_FILE_ERROR_INVALID_VALUE))
1831         {
1832           g_set_error (error, G_KEY_FILE_ERROR,
1833                        G_KEY_FILE_ERROR_INVALID_VALUE,
1834                        _("Key file contains key '%s' "
1835                          "which has a value that cannot be interpreted."),
1836                        key);
1837           g_error_free (key_file_error);
1838         }
1839       else
1840         g_propagate_error (error, key_file_error);
1841     }
1842
1843   return string_value;
1844 }
1845
1846 /**
1847  * g_key_file_set_string:
1848  * @key_file: a #GKeyFile
1849  * @group_name: a group name
1850  * @key: a key
1851  * @string: a string
1852  *
1853  * Associates a new string value with @key under @group_name.  
1854  * If @key cannot be found then it is created.  
1855  * If @group_name cannot be found then it is created.
1856  * Unlike g_key_file_set_value(), this function handles characters
1857  * that need escaping, such as newlines.
1858  *
1859  * Since: 2.6
1860  **/
1861 void
1862 g_key_file_set_string (GKeyFile    *key_file,
1863                        const gchar *group_name,
1864                        const gchar *key,
1865                        const gchar *string)
1866 {
1867   gchar *value;
1868
1869   g_return_if_fail (key_file != NULL);
1870   g_return_if_fail (string != NULL);
1871
1872   value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1873   g_key_file_set_value (key_file, group_name, key, value);
1874   g_free (value);
1875 }
1876
1877 /**
1878  * g_key_file_get_string_list:
1879  * @key_file: a #GKeyFile
1880  * @group_name: a group name
1881  * @key: a key
1882  * @length: (out) (allow-none): return location for the number of returned strings, or %NULL
1883  * @error: return location for a #GError, or %NULL
1884  *
1885  * Returns the values associated with @key under @group_name.
1886  *
1887  * In the event the key cannot be found, %NULL is returned and
1888  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND.  In the
1889  * event that the @group_name cannot be found, %NULL is returned
1890  * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1891  *
1892  * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): 
1893  *  a %NULL-terminated string array or %NULL if the specified 
1894  *  key cannot be found. The array should be freed with g_strfreev().
1895  *
1896  * Since: 2.6
1897  **/
1898 gchar **
1899 g_key_file_get_string_list (GKeyFile     *key_file,
1900                             const gchar  *group_name,
1901                             const gchar  *key,
1902                             gsize        *length,
1903                             GError      **error)
1904 {
1905   GError *key_file_error = NULL;
1906   gchar *value, *string_value, **values;
1907   gint i, len;
1908   GSList *p, *pieces = NULL;
1909
1910   g_return_val_if_fail (key_file != NULL, NULL);
1911   g_return_val_if_fail (group_name != NULL, NULL);
1912   g_return_val_if_fail (key != NULL, NULL);
1913
1914   if (length)
1915     *length = 0;
1916
1917   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1918
1919   if (key_file_error)
1920     {
1921       g_propagate_error (error, key_file_error);
1922       return NULL;
1923     }
1924
1925   if (!g_utf8_validate (value, -1, NULL))
1926     {
1927       gchar *value_utf8 = _g_utf8_make_valid (value);
1928       g_set_error (error, G_KEY_FILE_ERROR,
1929                    G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1930                    _("Key file contains key '%s' with value '%s' "
1931                      "which is not UTF-8"), key, value_utf8);
1932       g_free (value_utf8);
1933       g_free (value);
1934
1935       return NULL;
1936     }
1937
1938   string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1939   g_free (value);
1940   g_free (string_value);
1941
1942   if (key_file_error)
1943     {
1944       if (g_error_matches (key_file_error,
1945                            G_KEY_FILE_ERROR,
1946                            G_KEY_FILE_ERROR_INVALID_VALUE))
1947         {
1948           g_set_error (error, G_KEY_FILE_ERROR,
1949                        G_KEY_FILE_ERROR_INVALID_VALUE,
1950                        _("Key file contains key '%s' "
1951                          "which has a value that cannot be interpreted."),
1952                        key);
1953           g_error_free (key_file_error);
1954         }
1955       else
1956         g_propagate_error (error, key_file_error);
1957
1958       g_slist_free_full (pieces, g_free);
1959       return NULL;
1960     }
1961
1962   len = g_slist_length (pieces);
1963   values = g_new (gchar *, len + 1);
1964   for (p = pieces, i = 0; p; p = p->next)
1965     values[i++] = p->data;
1966   values[len] = NULL;
1967
1968   g_slist_free (pieces);
1969
1970   if (length)
1971     *length = len;
1972
1973   return values;
1974 }
1975
1976 /**
1977  * g_key_file_set_string_list:
1978  * @key_file: a #GKeyFile
1979  * @group_name: a group name
1980  * @key: a key
1981  * @list: (array zero-terminated=1 length=length) (element-type utf8): an array of string values
1982  * @length: number of string values in @list
1983  *
1984  * Associates a list of string values for @key under @group_name.
1985  * If @key cannot be found then it is created.
1986  * If @group_name cannot be found then it is created.
1987  *
1988  * Since: 2.6
1989  **/
1990 void
1991 g_key_file_set_string_list (GKeyFile            *key_file,
1992                             const gchar         *group_name,
1993                             const gchar         *key,
1994                             const gchar * const  list[],
1995                             gsize                length)
1996 {
1997   GString *value_list;
1998   gsize i;
1999
2000   g_return_if_fail (key_file != NULL);
2001   g_return_if_fail (list != NULL || length == 0);
2002
2003   value_list = g_string_sized_new (length * 128);
2004   for (i = 0; i < length && list[i] != NULL; i++)
2005     {
2006       gchar *value;
2007
2008       value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2009       g_string_append (value_list, value);
2010       g_string_append_c (value_list, key_file->list_separator);
2011
2012       g_free (value);
2013     }
2014
2015   g_key_file_set_value (key_file, group_name, key, value_list->str);
2016   g_string_free (value_list, TRUE);
2017 }
2018
2019 /**
2020  * g_key_file_set_locale_string:
2021  * @key_file: a #GKeyFile
2022  * @group_name: a group name
2023  * @key: a key
2024  * @locale: a locale identifier
2025  * @string: a string
2026  *
2027  * Associates a string value for @key and @locale under @group_name.
2028  * If the translation for @key cannot be found then it is created.
2029  *
2030  * Since: 2.6
2031  **/
2032 void
2033 g_key_file_set_locale_string (GKeyFile     *key_file,
2034                               const gchar  *group_name,
2035                               const gchar  *key,
2036                               const gchar  *locale,
2037                               const gchar  *string)
2038 {
2039   gchar *full_key, *value;
2040
2041   g_return_if_fail (key_file != NULL);
2042   g_return_if_fail (key != NULL);
2043   g_return_if_fail (locale != NULL);
2044   g_return_if_fail (string != NULL);
2045
2046   value = g_key_file_parse_string_as_value (key_file, string, FALSE);
2047   full_key = g_strdup_printf ("%s[%s]", key, locale);
2048   g_key_file_set_value (key_file, group_name, full_key, value);
2049   g_free (full_key);
2050   g_free (value);
2051 }
2052
2053 /**
2054  * g_key_file_get_locale_string:
2055  * @key_file: a #GKeyFile
2056  * @group_name: a group name
2057  * @key: a key
2058  * @locale: (allow-none): a locale identifier or %NULL
2059  * @error: return location for a #GError, or %NULL
2060  *
2061  * Returns the value associated with @key under @group_name
2062  * translated in the given @locale if available.  If @locale is
2063  * %NULL then the current locale is assumed. 
2064  *
2065  * If @key cannot be found then %NULL is returned and @error is set 
2066  * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
2067  * with @key cannot be interpreted or no suitable translation can
2068  * be found then the untranslated value is returned.
2069  *
2070  * Return value: a newly allocated string or %NULL if the specified 
2071  *   key cannot be found.
2072  *
2073  * Since: 2.6
2074  **/
2075 gchar *
2076 g_key_file_get_locale_string (GKeyFile     *key_file,
2077                               const gchar  *group_name,
2078                               const gchar  *key,
2079                               const gchar  *locale,
2080                               GError      **error)
2081 {
2082   gchar *candidate_key, *translated_value;
2083   GError *key_file_error;
2084   gchar **languages;
2085   gboolean free_languages = FALSE;
2086   gint i;
2087
2088   g_return_val_if_fail (key_file != NULL, NULL);
2089   g_return_val_if_fail (group_name != NULL, NULL);
2090   g_return_val_if_fail (key != NULL, NULL);
2091
2092   candidate_key = NULL;
2093   translated_value = NULL;
2094   key_file_error = NULL;
2095
2096   if (locale)
2097     {
2098       languages = g_get_locale_variants (locale);
2099       free_languages = TRUE;
2100     }
2101   else
2102     {
2103       languages = (gchar **) g_get_language_names ();
2104       free_languages = FALSE;
2105     }
2106   
2107   for (i = 0; languages[i]; i++)
2108     {
2109       candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
2110       
2111       translated_value = g_key_file_get_string (key_file,
2112                                                 group_name,
2113                                                 candidate_key, NULL);
2114       g_free (candidate_key);
2115
2116       if (translated_value)
2117         break;
2118
2119       g_free (translated_value);
2120       translated_value = NULL;
2121    }
2122
2123   /* Fallback to untranslated key
2124    */
2125   if (!translated_value)
2126     {
2127       translated_value = g_key_file_get_string (key_file, group_name, key,
2128                                                 &key_file_error);
2129       
2130       if (!translated_value)
2131         g_propagate_error (error, key_file_error);
2132     }
2133
2134   if (free_languages)
2135     g_strfreev (languages);
2136
2137   return translated_value;
2138 }
2139
2140 /**
2141  * g_key_file_get_locale_string_list:
2142  * @key_file: a #GKeyFile
2143  * @group_name: a group name
2144  * @key: a key
2145  * @locale: (allow-none): a locale identifier or %NULL
2146  * @length: (out) (allow-none): return location for the number of returned strings or %NULL
2147  * @error: return location for a #GError or %NULL
2148  *
2149  * Returns the values associated with @key under @group_name
2150  * translated in the given @locale if available.  If @locale is
2151  * %NULL then the current locale is assumed.
2152
2153  * If @key cannot be found then %NULL is returned and @error is set 
2154  * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
2155  * with @key cannot be interpreted or no suitable translations
2156  * can be found then the untranslated values are returned. The 
2157  * returned array is %NULL-terminated, so @length may optionally 
2158  * be %NULL.
2159  *
2160  * Return value: (array zero-terminated=1 length=length) (element-type utf8) (transfer full): a newly allocated %NULL-terminated string array
2161  *   or %NULL if the key isn't found. The string array should be freed
2162  *   with g_strfreev().
2163  *
2164  * Since: 2.6
2165  **/
2166 gchar **
2167 g_key_file_get_locale_string_list (GKeyFile     *key_file,
2168                                    const gchar  *group_name,
2169                                    const gchar  *key,
2170                                    const gchar  *locale,
2171                                    gsize        *length,
2172                                    GError      **error)
2173 {
2174   GError *key_file_error;
2175   gchar **values, *value;
2176   char list_separator[2];
2177   gsize len;
2178
2179   g_return_val_if_fail (key_file != NULL, NULL);
2180   g_return_val_if_fail (group_name != NULL, NULL);
2181   g_return_val_if_fail (key != NULL, NULL);
2182
2183   key_file_error = NULL;
2184
2185   value = g_key_file_get_locale_string (key_file, group_name, 
2186                                         key, locale,
2187                                         &key_file_error);
2188   
2189   if (key_file_error)
2190     g_propagate_error (error, key_file_error);
2191   
2192   if (!value)
2193     {
2194       if (length)
2195         *length = 0;
2196       return NULL;
2197     }
2198
2199   len = strlen (value);
2200   if (value[len - 1] == key_file->list_separator)
2201     value[len - 1] = '\0';
2202
2203   list_separator[0] = key_file->list_separator;
2204   list_separator[1] = '\0';
2205   values = g_strsplit (value, list_separator, 0);
2206
2207   g_free (value);
2208
2209   if (length)
2210     *length = g_strv_length (values);
2211
2212   return values;
2213 }
2214
2215 /**
2216  * g_key_file_set_locale_string_list:
2217  * @key_file: a #GKeyFile
2218  * @group_name: a group name
2219  * @key: a key
2220  * @locale: a locale identifier
2221  * @list: (array zero-terminated=1 length=length): a %NULL-terminated array of locale string values
2222  * @length: the length of @list
2223  *
2224  * Associates a list of string values for @key and @locale under
2225  * @group_name.  If the translation for @key cannot be found then
2226  * it is created. 
2227  *
2228  * Since: 2.6
2229  **/
2230 void
2231 g_key_file_set_locale_string_list (GKeyFile            *key_file,
2232                                    const gchar         *group_name,
2233                                    const gchar         *key,
2234                                    const gchar         *locale,
2235                                    const gchar * const  list[],
2236                                    gsize                length)
2237 {
2238   GString *value_list;
2239   gchar *full_key;
2240   gsize i;
2241
2242   g_return_if_fail (key_file != NULL);
2243   g_return_if_fail (key != NULL);
2244   g_return_if_fail (locale != NULL);
2245   g_return_if_fail (length != 0);
2246
2247   value_list = g_string_sized_new (length * 128);
2248   for (i = 0; i < length && list[i] != NULL; i++)
2249     {
2250       gchar *value;
2251       
2252       value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
2253       g_string_append (value_list, value);
2254       g_string_append_c (value_list, key_file->list_separator);
2255
2256       g_free (value);
2257     }
2258
2259   full_key = g_strdup_printf ("%s[%s]", key, locale);
2260   g_key_file_set_value (key_file, group_name, full_key, value_list->str);
2261   g_free (full_key);
2262   g_string_free (value_list, TRUE);
2263 }
2264
2265 /**
2266  * g_key_file_get_boolean:
2267  * @key_file: a #GKeyFile
2268  * @group_name: a group name
2269  * @key: a key
2270  * @error: return location for a #GError
2271  *
2272  * Returns the value associated with @key under @group_name as a
2273  * boolean. 
2274  *
2275  * If @key cannot be found then %FALSE is returned and @error is set
2276  * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
2277  * associated with @key cannot be interpreted as a boolean then %FALSE
2278  * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2279  *
2280  * Return value: the value associated with the key as a boolean, 
2281  *    or %FALSE if the key was not found or could not be parsed.
2282  *
2283  * Since: 2.6
2284  **/
2285 gboolean
2286 g_key_file_get_boolean (GKeyFile     *key_file,
2287                         const gchar  *group_name,
2288                         const gchar  *key,
2289                         GError      **error)
2290 {
2291   GError *key_file_error = NULL;
2292   gchar *value;
2293   gboolean bool_value;
2294
2295   g_return_val_if_fail (key_file != NULL, FALSE);
2296   g_return_val_if_fail (group_name != NULL, FALSE);
2297   g_return_val_if_fail (key != NULL, FALSE);
2298
2299   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2300
2301   if (!value)
2302     {
2303       g_propagate_error (error, key_file_error);
2304       return FALSE;
2305     }
2306
2307   bool_value = g_key_file_parse_value_as_boolean (key_file, value,
2308                                                   &key_file_error);
2309   g_free (value);
2310
2311   if (key_file_error)
2312     {
2313       if (g_error_matches (key_file_error,
2314                            G_KEY_FILE_ERROR,
2315                            G_KEY_FILE_ERROR_INVALID_VALUE))
2316         {
2317           g_set_error (error, G_KEY_FILE_ERROR,
2318                        G_KEY_FILE_ERROR_INVALID_VALUE,
2319                        _("Key file contains key '%s' "
2320                          "which has a value that cannot be interpreted."),
2321                        key);
2322           g_error_free (key_file_error);
2323         }
2324       else
2325         g_propagate_error (error, key_file_error);
2326     }
2327
2328   return bool_value;
2329 }
2330
2331 /**
2332  * g_key_file_set_boolean:
2333  * @key_file: a #GKeyFile
2334  * @group_name: a group name
2335  * @key: a key
2336  * @value: %TRUE or %FALSE
2337  *
2338  * Associates a new boolean value with @key under @group_name.
2339  * If @key cannot be found then it is created. 
2340  *
2341  * Since: 2.6
2342  **/
2343 void
2344 g_key_file_set_boolean (GKeyFile    *key_file,
2345                         const gchar *group_name,
2346                         const gchar *key,
2347                         gboolean     value)
2348 {
2349   gchar *result;
2350
2351   g_return_if_fail (key_file != NULL);
2352
2353   result = g_key_file_parse_boolean_as_value (key_file, value);
2354   g_key_file_set_value (key_file, group_name, key, result);
2355   g_free (result);
2356 }
2357
2358 /**
2359  * g_key_file_get_boolean_list:
2360  * @key_file: a #GKeyFile
2361  * @group_name: a group name
2362  * @key: a key
2363  * @length: (out): the number of booleans returned
2364  * @error: return location for a #GError
2365  *
2366  * Returns the values associated with @key under @group_name as
2367  * booleans. 
2368  *
2369  * If @key cannot be found then %NULL is returned and @error is set to
2370  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2371  * with @key cannot be interpreted as booleans then %NULL is returned
2372  * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2373  *
2374  * Return value: (array length=length) (element-type gboolean) (transfer container):
2375  *    the values associated with the key as a list of booleans, or %NULL if the
2376  *    key was not found or could not be parsed. The returned list of booleans
2377  *    should be freed with g_free() when no longer needed.
2378  * 
2379  * Since: 2.6
2380  **/
2381 gboolean *
2382 g_key_file_get_boolean_list (GKeyFile     *key_file,
2383                              const gchar  *group_name,
2384                              const gchar  *key,
2385                              gsize        *length,
2386                              GError      **error)
2387 {
2388   GError *key_file_error;
2389   gchar **values;
2390   gboolean *bool_values;
2391   gsize i, num_bools;
2392
2393   g_return_val_if_fail (key_file != NULL, NULL);
2394   g_return_val_if_fail (group_name != NULL, NULL);
2395   g_return_val_if_fail (key != NULL, NULL);
2396
2397   if (length)
2398     *length = 0;
2399
2400   key_file_error = NULL;
2401
2402   values = g_key_file_get_string_list (key_file, group_name, key,
2403                                        &num_bools, &key_file_error);
2404
2405   if (key_file_error)
2406     g_propagate_error (error, key_file_error);
2407
2408   if (!values)
2409     return NULL;
2410
2411   bool_values = g_new (gboolean, num_bools);
2412
2413   for (i = 0; i < num_bools; i++)
2414     {
2415       bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2416                                                           values[i],
2417                                                           &key_file_error);
2418
2419       if (key_file_error)
2420         {
2421           g_propagate_error (error, key_file_error);
2422           g_strfreev (values);
2423           g_free (bool_values);
2424
2425           return NULL;
2426         }
2427     }
2428   g_strfreev (values);
2429
2430   if (length)
2431     *length = num_bools;
2432
2433   return bool_values;
2434 }
2435
2436 /**
2437  * g_key_file_set_boolean_list:
2438  * @key_file: a #GKeyFile
2439  * @group_name: a group name
2440  * @key: a key
2441  * @list: (array length=length): an array of boolean values
2442  * @length: length of @list
2443  *
2444  * Associates a list of boolean values with @key under @group_name.  
2445  * If @key cannot be found then it is created.
2446  * If @group_name is %NULL, the start_group is used.
2447  *
2448  * Since: 2.6
2449  **/
2450 void
2451 g_key_file_set_boolean_list (GKeyFile    *key_file,
2452                              const gchar *group_name,
2453                              const gchar *key,
2454                              gboolean     list[],
2455                              gsize        length)
2456 {
2457   GString *value_list;
2458   gsize i;
2459
2460   g_return_if_fail (key_file != NULL);
2461   g_return_if_fail (list != NULL);
2462
2463   value_list = g_string_sized_new (length * 8);
2464   for (i = 0; i < length; i++)
2465     {
2466       gchar *value;
2467
2468       value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2469
2470       g_string_append (value_list, value);
2471       g_string_append_c (value_list, key_file->list_separator);
2472
2473       g_free (value);
2474     }
2475
2476   g_key_file_set_value (key_file, group_name, key, value_list->str);
2477   g_string_free (value_list, TRUE);
2478 }
2479
2480 /**
2481  * g_key_file_get_integer:
2482  * @key_file: a #GKeyFile
2483  * @group_name: a group name
2484  * @key: a key
2485  * @error: return location for a #GError
2486  *
2487  * Returns the value associated with @key under @group_name as an
2488  * integer. 
2489  *
2490  * If @key cannot be found then 0 is returned and @error is set to
2491  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2492  * with @key cannot be interpreted as an integer then 0 is returned
2493  * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2494  *
2495  * Return value: the value associated with the key as an integer, or
2496  *     0 if the key was not found or could not be parsed.
2497  *
2498  * Since: 2.6
2499  **/
2500 gint
2501 g_key_file_get_integer (GKeyFile     *key_file,
2502                         const gchar  *group_name,
2503                         const gchar  *key,
2504                         GError      **error)
2505 {
2506   GError *key_file_error;
2507   gchar *value;
2508   gint int_value;
2509
2510   g_return_val_if_fail (key_file != NULL, -1);
2511   g_return_val_if_fail (group_name != NULL, -1);
2512   g_return_val_if_fail (key != NULL, -1);
2513
2514   key_file_error = NULL;
2515
2516   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2517
2518   if (key_file_error)
2519     {
2520       g_propagate_error (error, key_file_error);
2521       return 0;
2522     }
2523
2524   int_value = g_key_file_parse_value_as_integer (key_file, value,
2525                                                  &key_file_error);
2526   g_free (value);
2527
2528   if (key_file_error)
2529     {
2530       if (g_error_matches (key_file_error,
2531                            G_KEY_FILE_ERROR,
2532                            G_KEY_FILE_ERROR_INVALID_VALUE))
2533         {
2534           g_set_error (error, G_KEY_FILE_ERROR,
2535                        G_KEY_FILE_ERROR_INVALID_VALUE,
2536                        _("Key file contains key '%s' in group '%s' "
2537                          "which has a value that cannot be interpreted."),
2538                          key, group_name);
2539           g_error_free (key_file_error);
2540         }
2541       else
2542         g_propagate_error (error, key_file_error);
2543     }
2544
2545   return int_value;
2546 }
2547
2548 /**
2549  * g_key_file_set_integer:
2550  * @key_file: a #GKeyFile
2551  * @group_name: a group name
2552  * @key: a key
2553  * @value: an integer value
2554  *
2555  * Associates a new integer value with @key under @group_name.
2556  * If @key cannot be found then it is created.
2557  *
2558  * Since: 2.6
2559  **/
2560 void
2561 g_key_file_set_integer (GKeyFile    *key_file,
2562                         const gchar *group_name,
2563                         const gchar *key,
2564                         gint         value)
2565 {
2566   gchar *result;
2567
2568   g_return_if_fail (key_file != NULL);
2569
2570   result = g_key_file_parse_integer_as_value (key_file, value);
2571   g_key_file_set_value (key_file, group_name, key, result);
2572   g_free (result);
2573 }
2574
2575 /**
2576  * g_key_file_get_int64:
2577  * @key_file: a non-%NULL #GKeyFile
2578  * @group_name: a non-%NULL group name
2579  * @key: a non-%NULL key
2580  * @error: return location for a #GError
2581  *
2582  * Returns the value associated with @key under @group_name as a signed
2583  * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2584  * 64-bit results without truncation.
2585  *
2586  * Returns: the value associated with the key as a signed 64-bit integer, or
2587  * 0 if the key was not found or could not be parsed.
2588  *
2589  * Since: 2.26
2590  */
2591 gint64
2592 g_key_file_get_int64 (GKeyFile     *key_file,
2593                       const gchar  *group_name,
2594                       const gchar  *key,
2595                       GError      **error)
2596 {
2597   gchar *s, *end;
2598   gint64 v;
2599
2600   g_return_val_if_fail (key_file != NULL, -1);
2601   g_return_val_if_fail (group_name != NULL, -1);
2602   g_return_val_if_fail (key != NULL, -1);
2603
2604   s = g_key_file_get_value (key_file, group_name, key, error);
2605
2606   if (s == NULL)
2607     return 0;
2608
2609   v = g_ascii_strtoll (s, &end, 10);
2610
2611   if (*s == '\0' || *end != '\0')
2612     {
2613       g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2614                    _("Key '%s' in group '%s' has value '%s' "
2615                      "where %s was expected"),
2616                    key, group_name, s, "int64");
2617       g_free (s);
2618       return 0;
2619     }
2620
2621   g_free (s);
2622   return v;
2623 }
2624
2625 /**
2626  * g_key_file_set_int64:
2627  * @key_file: a #GKeyFile
2628  * @group_name: a group name
2629  * @key: a key
2630  * @value: an integer value
2631  *
2632  * Associates a new integer value with @key under @group_name.
2633  * If @key cannot be found then it is created.
2634  *
2635  * Since: 2.26
2636  **/
2637 void
2638 g_key_file_set_int64 (GKeyFile    *key_file,
2639                       const gchar *group_name,
2640                       const gchar *key,
2641                       gint64       value)
2642 {
2643   gchar *result;
2644
2645   g_return_if_fail (key_file != NULL);
2646
2647   result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
2648   g_key_file_set_value (key_file, group_name, key, result);
2649   g_free (result);
2650 }
2651
2652 /**
2653  * g_key_file_get_uint64:
2654  * @key_file: a non-%NULL #GKeyFile
2655  * @group_name: a non-%NULL group name
2656  * @key: a non-%NULL key
2657  * @error: return location for a #GError
2658  *
2659  * Returns the value associated with @key under @group_name as an unsigned
2660  * 64-bit integer. This is similar to g_key_file_get_integer() but can return
2661  * large positive results without truncation.
2662  *
2663  * Returns: the value associated with the key as an unsigned 64-bit integer,
2664  * or 0 if the key was not found or could not be parsed.
2665  *
2666  * Since: 2.26
2667  */
2668 guint64
2669 g_key_file_get_uint64 (GKeyFile     *key_file,
2670                        const gchar  *group_name,
2671                        const gchar  *key,
2672                        GError      **error)
2673 {
2674   gchar *s, *end;
2675   guint64 v;
2676
2677   g_return_val_if_fail (key_file != NULL, -1);
2678   g_return_val_if_fail (group_name != NULL, -1);
2679   g_return_val_if_fail (key != NULL, -1);
2680
2681   s = g_key_file_get_value (key_file, group_name, key, error);
2682
2683   if (s == NULL)
2684     return 0;
2685
2686   v = g_ascii_strtoull (s, &end, 10);
2687
2688   if (*s == '\0' || *end != '\0')
2689     {
2690       g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
2691                    _("Key '%s' in group '%s' has value '%s' "
2692                      "where %s was expected"),
2693                    key, group_name, s, "uint64");
2694       g_free (s);
2695       return 0;
2696     }
2697
2698   g_free (s);
2699   return v;
2700 }
2701
2702 /**
2703  * g_key_file_set_uint64:
2704  * @key_file: a #GKeyFile
2705  * @group_name: a group name
2706  * @key: a key
2707  * @value: an integer value
2708  *
2709  * Associates a new integer value with @key under @group_name.
2710  * If @key cannot be found then it is created.
2711  *
2712  * Since: 2.26
2713  **/
2714 void
2715 g_key_file_set_uint64 (GKeyFile    *key_file,
2716                        const gchar *group_name,
2717                        const gchar *key,
2718                        guint64      value)
2719 {
2720   gchar *result;
2721
2722   g_return_if_fail (key_file != NULL);
2723
2724   result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
2725   g_key_file_set_value (key_file, group_name, key, result);
2726   g_free (result);
2727 }
2728
2729 /**
2730  * g_key_file_get_integer_list:
2731  * @key_file: a #GKeyFile
2732  * @group_name: a group name
2733  * @key: a key
2734  * @length: (out): the number of integers returned
2735  * @error: return location for a #GError
2736  *
2737  * Returns the values associated with @key under @group_name as
2738  * integers. 
2739  *
2740  * If @key cannot be found then %NULL is returned and @error is set to
2741  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2742  * with @key cannot be interpreted as integers then %NULL is returned
2743  * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2744  *
2745  * Return value: (array length=length) (element-type gint) (transfer container):
2746  *     the values associated with the key as a list of integers, or %NULL if
2747  *     the key was not found or could not be parsed. The returned list of
2748  *     integers should be freed with g_free() when no longer needed.
2749  *
2750  * Since: 2.6
2751  **/
2752 gint *
2753 g_key_file_get_integer_list (GKeyFile     *key_file,
2754                              const gchar  *group_name,
2755                              const gchar  *key,
2756                              gsize        *length,
2757                              GError      **error)
2758 {
2759   GError *key_file_error = NULL;
2760   gchar **values;
2761   gint *int_values;
2762   gsize i, num_ints;
2763
2764   g_return_val_if_fail (key_file != NULL, NULL);
2765   g_return_val_if_fail (group_name != NULL, NULL);
2766   g_return_val_if_fail (key != NULL, NULL);
2767
2768   if (length)
2769     *length = 0;
2770
2771   values = g_key_file_get_string_list (key_file, group_name, key,
2772                                        &num_ints, &key_file_error);
2773
2774   if (key_file_error)
2775     g_propagate_error (error, key_file_error);
2776
2777   if (!values)
2778     return NULL;
2779
2780   int_values = g_new (gint, num_ints);
2781
2782   for (i = 0; i < num_ints; i++)
2783     {
2784       int_values[i] = g_key_file_parse_value_as_integer (key_file,
2785                                                          values[i],
2786                                                          &key_file_error);
2787
2788       if (key_file_error)
2789         {
2790           g_propagate_error (error, key_file_error);
2791           g_strfreev (values);
2792           g_free (int_values);
2793
2794           return NULL;
2795         }
2796     }
2797   g_strfreev (values);
2798
2799   if (length)
2800     *length = num_ints;
2801
2802   return int_values;
2803 }
2804
2805 /**
2806  * g_key_file_set_integer_list:
2807  * @key_file: a #GKeyFile
2808  * @group_name: a group name
2809  * @key: a key
2810  * @list: (array length=length): an array of integer values
2811  * @length: number of integer values in @list
2812  *
2813  * Associates a list of integer values with @key under @group_name.  
2814  * If @key cannot be found then it is created.
2815  *
2816  * Since: 2.6
2817  **/
2818 void
2819 g_key_file_set_integer_list (GKeyFile    *key_file,
2820                              const gchar *group_name,
2821                              const gchar *key,
2822                              gint         list[],
2823                              gsize        length)
2824 {
2825   GString *values;
2826   gsize i;
2827
2828   g_return_if_fail (key_file != NULL);
2829   g_return_if_fail (list != NULL);
2830
2831   values = g_string_sized_new (length * 16);
2832   for (i = 0; i < length; i++)
2833     {
2834       gchar *value;
2835
2836       value = g_key_file_parse_integer_as_value (key_file, list[i]);
2837
2838       g_string_append (values, value);
2839       g_string_append_c (values, key_file->list_separator);
2840
2841       g_free (value);
2842     }
2843
2844   g_key_file_set_value (key_file, group_name, key, values->str);
2845   g_string_free (values, TRUE);
2846 }
2847
2848 /**
2849  * g_key_file_get_double:
2850  * @key_file: a #GKeyFile
2851  * @group_name: a group name
2852  * @key: a key
2853  * @error: return location for a #GError
2854  *
2855  * Returns the value associated with @key under @group_name as a
2856  * double. If @group_name is %NULL, the start_group is used.
2857  *
2858  * If @key cannot be found then 0.0 is returned and @error is set to
2859  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2860  * with @key cannot be interpreted as a double then 0.0 is returned
2861  * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2862  *
2863  * Return value: the value associated with the key as a double, or
2864  *     0.0 if the key was not found or could not be parsed.
2865  *
2866  * Since: 2.12
2867  **/
2868 gdouble
2869 g_key_file_get_double  (GKeyFile     *key_file,
2870                         const gchar  *group_name,
2871                         const gchar  *key,
2872                         GError      **error)
2873 {
2874   GError *key_file_error;
2875   gchar *value;
2876   gdouble double_value;
2877
2878   g_return_val_if_fail (key_file != NULL, -1);
2879   g_return_val_if_fail (group_name != NULL, -1);
2880   g_return_val_if_fail (key != NULL, -1);
2881
2882   key_file_error = NULL;
2883
2884   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2885
2886   if (key_file_error)
2887     {
2888       g_propagate_error (error, key_file_error);
2889       return 0;
2890     }
2891
2892   double_value = g_key_file_parse_value_as_double (key_file, value,
2893                                                   &key_file_error);
2894   g_free (value);
2895
2896   if (key_file_error)
2897     {
2898       if (g_error_matches (key_file_error,
2899                            G_KEY_FILE_ERROR,
2900                            G_KEY_FILE_ERROR_INVALID_VALUE))
2901         {
2902           g_set_error (error, G_KEY_FILE_ERROR,
2903                        G_KEY_FILE_ERROR_INVALID_VALUE,
2904                        _("Key file contains key '%s' in group '%s' "
2905                          "which has a value that cannot be interpreted."),
2906                        key, group_name);
2907           g_error_free (key_file_error);
2908         }
2909       else
2910         g_propagate_error (error, key_file_error);
2911     }
2912
2913   return double_value;
2914 }
2915
2916 /**
2917  * g_key_file_set_double:
2918  * @key_file: a #GKeyFile
2919  * @group_name: a group name
2920  * @key: a key
2921  * @value: an double value
2922  *
2923  * Associates a new double value with @key under @group_name.
2924  * If @key cannot be found then it is created. 
2925  *
2926  * Since: 2.12
2927  **/
2928 void
2929 g_key_file_set_double  (GKeyFile    *key_file,
2930                         const gchar *group_name,
2931                         const gchar *key,
2932                         gdouble      value)
2933 {
2934   gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2935
2936   g_return_if_fail (key_file != NULL);
2937
2938   g_ascii_dtostr (result, sizeof (result), value);
2939   g_key_file_set_value (key_file, group_name, key, result);
2940 }
2941
2942 /**
2943  * g_key_file_get_double_list:
2944  * @key_file: a #GKeyFile
2945  * @group_name: a group name
2946  * @key: a key
2947  * @length: (out): the number of doubles returned
2948  * @error: return location for a #GError
2949  *
2950  * Returns the values associated with @key under @group_name as
2951  * doubles. 
2952  *
2953  * If @key cannot be found then %NULL is returned and @error is set to
2954  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2955  * with @key cannot be interpreted as doubles then %NULL is returned
2956  * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2957  *
2958  * Return value: (array length=length) (element-type gdouble) (transfer container):
2959  *     the values associated with the key as a list of doubles, or %NULL if the
2960  *     key was not found or could not be parsed. The returned list of doubles
2961  *     should be freed with g_free() when no longer needed.
2962  *
2963  * Since: 2.12
2964  **/
2965 gdouble *
2966 g_key_file_get_double_list  (GKeyFile     *key_file,
2967                              const gchar  *group_name,
2968                              const gchar  *key,
2969                              gsize        *length,
2970                              GError      **error)
2971 {
2972   GError *key_file_error = NULL;
2973   gchar **values;
2974   gdouble *double_values;
2975   gsize i, num_doubles;
2976
2977   g_return_val_if_fail (key_file != NULL, NULL);
2978   g_return_val_if_fail (group_name != NULL, NULL);
2979   g_return_val_if_fail (key != NULL, NULL);
2980
2981   if (length)
2982     *length = 0;
2983
2984   values = g_key_file_get_string_list (key_file, group_name, key,
2985                                        &num_doubles, &key_file_error);
2986
2987   if (key_file_error)
2988     g_propagate_error (error, key_file_error);
2989
2990   if (!values)
2991     return NULL;
2992
2993   double_values = g_new (gdouble, num_doubles);
2994
2995   for (i = 0; i < num_doubles; i++)
2996     {
2997       double_values[i] = g_key_file_parse_value_as_double (key_file,
2998                                                            values[i],
2999                                                            &key_file_error);
3000
3001       if (key_file_error)
3002         {
3003           g_propagate_error (error, key_file_error);
3004           g_strfreev (values);
3005           g_free (double_values);
3006
3007           return NULL;
3008         }
3009     }
3010   g_strfreev (values);
3011
3012   if (length)
3013     *length = num_doubles;
3014
3015   return double_values;
3016 }
3017
3018 /**
3019  * g_key_file_set_double_list:
3020  * @key_file: a #GKeyFile
3021  * @group_name: a group name
3022  * @key: a key
3023  * @list: (array length=length): an array of double values
3024  * @length: number of double values in @list
3025  *
3026  * Associates a list of double values with @key under
3027  * @group_name.  If @key cannot be found then it is created.
3028  *
3029  * Since: 2.12
3030  **/
3031 void
3032 g_key_file_set_double_list (GKeyFile    *key_file,
3033                             const gchar *group_name,
3034                             const gchar *key,
3035                             gdouble      list[],
3036                             gsize        length)
3037 {
3038   GString *values;
3039   gsize i;
3040
3041   g_return_if_fail (key_file != NULL);
3042   g_return_if_fail (list != NULL);
3043
3044   values = g_string_sized_new (length * 16);
3045   for (i = 0; i < length; i++)
3046     {
3047       gchar result[G_ASCII_DTOSTR_BUF_SIZE];
3048
3049       g_ascii_dtostr( result, sizeof (result), list[i] );
3050
3051       g_string_append (values, result);
3052       g_string_append_c (values, key_file->list_separator);
3053     }
3054
3055   g_key_file_set_value (key_file, group_name, key, values->str);
3056   g_string_free (values, TRUE);
3057 }
3058
3059 static gboolean
3060 g_key_file_set_key_comment (GKeyFile     *key_file,
3061                             const gchar  *group_name,
3062                             const gchar  *key,
3063                             const gchar  *comment,
3064                             GError      **error)
3065 {
3066   GKeyFileGroup *group;
3067   GKeyFileKeyValuePair *pair;
3068   GList *key_node, *comment_node, *tmp;
3069   
3070   group = g_key_file_lookup_group (key_file, group_name);
3071   if (!group)
3072     {
3073       g_set_error (error, G_KEY_FILE_ERROR,
3074                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3075                    _("Key file does not have group '%s'"),
3076                    group_name ? group_name : "(null)");
3077
3078       return FALSE;
3079     }
3080
3081   /* First find the key the comments are supposed to be
3082    * associated with
3083    */
3084   key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3085
3086   if (key_node == NULL)
3087     {
3088       g_set_error (error, G_KEY_FILE_ERROR,
3089                    G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3090                    _("Key file does not have key '%s' in group '%s'"),
3091                    key, group->name);
3092       return FALSE;
3093     }
3094
3095   /* Then find all the comments already associated with the
3096    * key and free them
3097    */
3098   tmp = key_node->next;
3099   while (tmp != NULL)
3100     {
3101       pair = (GKeyFileKeyValuePair *) tmp->data;
3102
3103       if (pair->key != NULL)
3104         break;
3105
3106       comment_node = tmp;
3107       tmp = tmp->next;
3108       g_key_file_remove_key_value_pair_node (key_file, group,
3109                                              comment_node); 
3110     }
3111
3112   if (comment == NULL)
3113     return TRUE;
3114
3115   /* Now we can add our new comment
3116    */
3117   pair = g_slice_new (GKeyFileKeyValuePair);
3118   pair->key = NULL;
3119   pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3120   
3121   key_node = g_list_insert (key_node, pair, 1);
3122
3123   return TRUE;
3124 }
3125
3126 static gboolean
3127 g_key_file_set_group_comment (GKeyFile     *key_file,
3128                               const gchar  *group_name,
3129                               const gchar  *comment,
3130                               GError      **error)
3131 {
3132   GKeyFileGroup *group;
3133   
3134   g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
3135
3136   group = g_key_file_lookup_group (key_file, group_name);
3137   if (!group)
3138     {
3139       g_set_error (error, G_KEY_FILE_ERROR,
3140                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3141                    _("Key file does not have group '%s'"),
3142                    group_name ? group_name : "(null)");
3143
3144       return FALSE;
3145     }
3146
3147   /* First remove any existing comment
3148    */
3149   if (group->comment)
3150     {
3151       g_key_file_key_value_pair_free (group->comment);
3152       group->comment = NULL;
3153     }
3154
3155   if (comment == NULL)
3156     return TRUE;
3157
3158   /* Now we can add our new comment
3159    */
3160   group->comment = g_slice_new (GKeyFileKeyValuePair);
3161   group->comment->key = NULL;
3162   group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
3163
3164   return TRUE;
3165 }
3166
3167 static gboolean
3168 g_key_file_set_top_comment (GKeyFile     *key_file,
3169                             const gchar  *comment,
3170                             GError      **error)
3171 {
3172   GList *group_node;
3173   GKeyFileGroup *group;
3174   GKeyFileKeyValuePair *pair;
3175
3176   /* The last group in the list should be the top (comments only)
3177    * group in the file
3178    */
3179   g_warn_if_fail (key_file->groups != NULL);
3180   group_node = g_list_last (key_file->groups);
3181   group = (GKeyFileGroup *) group_node->data;
3182   g_warn_if_fail (group->name == NULL);
3183
3184   /* Note all keys must be comments at the top of
3185    * the file, so we can just free it all.
3186    */
3187   g_list_free_full (group->key_value_pairs, (GDestroyNotify) g_key_file_key_value_pair_free);
3188   group->key_value_pairs = NULL;
3189
3190   if (comment == NULL)
3191      return TRUE;
3192
3193   pair = g_slice_new (GKeyFileKeyValuePair);
3194   pair->key = NULL;
3195   pair->value = g_key_file_parse_comment_as_value (key_file, comment);
3196   
3197   group->key_value_pairs =
3198     g_list_prepend (group->key_value_pairs, pair);
3199
3200   return TRUE;
3201 }
3202
3203 /**
3204  * g_key_file_set_comment:
3205  * @key_file: a #GKeyFile
3206  * @group_name: (allow-none): a group name, or %NULL
3207  * @key: (allow-none): a key
3208  * @comment: a comment
3209  * @error: return location for a #GError
3210  *
3211  * Places a comment above @key from @group_name.
3212  * If @key is %NULL then @comment will be written above @group_name.  
3213  * If both @key and @group_name  are %NULL, then @comment will be 
3214  * written above the first group in the file.
3215  *
3216  * Returns: %TRUE if the comment was written, %FALSE otherwise
3217  *
3218  * Since: 2.6
3219  **/
3220 gboolean
3221 g_key_file_set_comment (GKeyFile     *key_file,
3222                         const gchar  *group_name,
3223                         const gchar  *key,
3224                         const gchar  *comment,
3225                         GError      **error)
3226 {
3227   g_return_val_if_fail (key_file != NULL, FALSE);
3228
3229   if (group_name != NULL && key != NULL) 
3230     {
3231       if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
3232         return FALSE;
3233     } 
3234   else if (group_name != NULL) 
3235     {
3236       if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
3237         return FALSE;
3238     } 
3239   else 
3240     {
3241       if (!g_key_file_set_top_comment (key_file, comment, error))
3242         return FALSE;
3243     }
3244
3245   return TRUE;
3246 }
3247
3248 static gchar *
3249 g_key_file_get_key_comment (GKeyFile     *key_file,
3250                             const gchar  *group_name,
3251                             const gchar  *key,
3252                             GError      **error)
3253 {
3254   GKeyFileGroup *group;
3255   GKeyFileKeyValuePair *pair;
3256   GList *key_node, *tmp;
3257   GString *string;
3258   gchar *comment;
3259
3260   g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
3261
3262   group = g_key_file_lookup_group (key_file, group_name);
3263   if (!group)
3264     {
3265       g_set_error (error, G_KEY_FILE_ERROR,
3266                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3267                    _("Key file does not have group '%s'"),
3268                    group_name ? group_name : "(null)");
3269
3270       return NULL;
3271     }
3272
3273   /* First find the key the comments are supposed to be
3274    * associated with
3275    */
3276   key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
3277
3278   if (key_node == NULL)
3279     {
3280       g_set_error (error, G_KEY_FILE_ERROR,
3281                    G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3282                    _("Key file does not have key '%s' in group '%s'"),
3283                    key, group->name);
3284       return NULL;
3285     }
3286
3287   string = NULL;
3288
3289   /* Then find all the comments already associated with the
3290    * key and concatentate them.
3291    */
3292   tmp = key_node->next;
3293   if (!key_node->next)
3294     return NULL;
3295
3296   pair = (GKeyFileKeyValuePair *) tmp->data;
3297   if (pair->key != NULL)
3298     return NULL;
3299
3300   while (tmp->next)
3301     {
3302       pair = (GKeyFileKeyValuePair *) tmp->next->data;
3303       
3304       if (pair->key != NULL)
3305         break;
3306
3307       tmp = tmp->next;
3308     }
3309
3310   while (tmp != key_node)
3311     {
3312       pair = (GKeyFileKeyValuePair *) tmp->data;
3313       
3314       if (string == NULL)
3315         string = g_string_sized_new (512);
3316       
3317       comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3318       g_string_append (string, comment);
3319       g_free (comment);
3320       
3321       tmp = tmp->prev;
3322     }
3323
3324   if (string != NULL)
3325     {
3326       comment = string->str;
3327       g_string_free (string, FALSE);
3328     }
3329   else
3330     comment = NULL;
3331
3332   return comment;
3333 }
3334
3335 static gchar *
3336 get_group_comment (GKeyFile       *key_file,
3337                    GKeyFileGroup  *group,
3338                    GError        **error)
3339 {
3340   GString *string;
3341   GList *tmp;
3342   gchar *comment;
3343
3344   string = NULL;
3345
3346   tmp = group->key_value_pairs;
3347   while (tmp)
3348     {
3349       GKeyFileKeyValuePair *pair;
3350
3351       pair = (GKeyFileKeyValuePair *) tmp->data;
3352
3353       if (pair->key != NULL)
3354         {
3355           tmp = tmp->prev;
3356           break;
3357         }
3358
3359       if (tmp->next == NULL)
3360         break;
3361
3362       tmp = tmp->next;
3363     }
3364   
3365   while (tmp != NULL)
3366     {
3367       GKeyFileKeyValuePair *pair;
3368
3369       pair = (GKeyFileKeyValuePair *) tmp->data;
3370
3371       if (string == NULL)
3372         string = g_string_sized_new (512);
3373
3374       comment = g_key_file_parse_value_as_comment (key_file, pair->value);
3375       g_string_append (string, comment);
3376       g_free (comment);
3377
3378       tmp = tmp->prev;
3379     }
3380
3381   if (string != NULL)
3382     return g_string_free (string, FALSE);
3383
3384   return NULL;
3385 }
3386
3387 static gchar *
3388 g_key_file_get_group_comment (GKeyFile     *key_file,
3389                               const gchar  *group_name,
3390                               GError      **error)
3391 {
3392   GList *group_node;
3393   GKeyFileGroup *group;
3394   
3395   group = g_key_file_lookup_group (key_file, group_name);
3396   if (!group)
3397     {
3398       g_set_error (error, G_KEY_FILE_ERROR,
3399                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3400                    _("Key file does not have group '%s'"),
3401                    group_name ? group_name : "(null)");
3402
3403       return NULL;
3404     }
3405
3406   if (group->comment)
3407     return g_strdup (group->comment->value);
3408   
3409   group_node = g_key_file_lookup_group_node (key_file, group_name);
3410   group_node = group_node->next;
3411   group = (GKeyFileGroup *)group_node->data;  
3412   return get_group_comment (key_file, group, error);
3413 }
3414
3415 static gchar *
3416 g_key_file_get_top_comment (GKeyFile  *key_file,
3417                             GError   **error)
3418 {
3419   GList *group_node;
3420   GKeyFileGroup *group;
3421
3422   /* The last group in the list should be the top (comments only)
3423    * group in the file
3424    */
3425   g_warn_if_fail (key_file->groups != NULL);
3426   group_node = g_list_last (key_file->groups);
3427   group = (GKeyFileGroup *) group_node->data;
3428   g_warn_if_fail (group->name == NULL);
3429
3430   return get_group_comment (key_file, group, error);
3431 }
3432
3433 /**
3434  * g_key_file_get_comment:
3435  * @key_file: a #GKeyFile
3436  * @group_name: (allow-none): a group name, or %NULL
3437  * @key: a key
3438  * @error: return location for a #GError
3439  *
3440  * Retrieves a comment above @key from @group_name.
3441  * If @key is %NULL then @comment will be read from above 
3442  * @group_name. If both @key and @group_name are %NULL, then 
3443  * @comment will be read from above the first group in the file.
3444  *
3445  * Returns: a comment that should be freed with g_free()
3446  *
3447  * Since: 2.6
3448  **/
3449 gchar * 
3450 g_key_file_get_comment (GKeyFile     *key_file,
3451                         const gchar  *group_name,
3452                         const gchar  *key,
3453                         GError      **error)
3454 {
3455   g_return_val_if_fail (key_file != NULL, NULL);
3456
3457   if (group_name != NULL && key != NULL)
3458     return g_key_file_get_key_comment (key_file, group_name, key, error);
3459   else if (group_name != NULL)
3460     return g_key_file_get_group_comment (key_file, group_name, error);
3461   else
3462     return g_key_file_get_top_comment (key_file, error);
3463 }
3464
3465 /**
3466  * g_key_file_remove_comment:
3467  * @key_file: a #GKeyFile
3468  * @group_name: (allow-none): a group name, or %NULL
3469  * @key: (allow-none): a key
3470  * @error: return location for a #GError
3471  *
3472  * Removes a comment above @key from @group_name.
3473  * If @key is %NULL then @comment will be removed above @group_name. 
3474  * If both @key and @group_name are %NULL, then @comment will
3475  * be removed above the first group in the file.
3476  *
3477  * Returns: %TRUE if the comment was removed, %FALSE otherwise
3478  *
3479  * Since: 2.6
3480  **/
3481
3482 gboolean
3483 g_key_file_remove_comment (GKeyFile     *key_file,
3484                            const gchar  *group_name,
3485                            const gchar  *key,
3486                            GError      **error)
3487 {
3488   g_return_val_if_fail (key_file != NULL, FALSE);
3489
3490   if (group_name != NULL && key != NULL)
3491     return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
3492   else if (group_name != NULL)
3493     return g_key_file_set_group_comment (key_file, group_name, NULL, error);
3494   else
3495     return g_key_file_set_top_comment (key_file, NULL, error);
3496 }
3497
3498 /**
3499  * g_key_file_has_group:
3500  * @key_file: a #GKeyFile
3501  * @group_name: a group name
3502  *
3503  * Looks whether the key file has the group @group_name.
3504  *
3505  * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
3506  * otherwise.
3507  * Since: 2.6
3508  **/
3509 gboolean
3510 g_key_file_has_group (GKeyFile    *key_file,
3511                       const gchar *group_name)
3512 {
3513   g_return_val_if_fail (key_file != NULL, FALSE);
3514   g_return_val_if_fail (group_name != NULL, FALSE);
3515
3516   return g_key_file_lookup_group (key_file, group_name) != NULL;
3517 }
3518
3519 /* This code remains from a historical attempt to add a new public API
3520  * which respects the GError rules.
3521  */
3522 static gboolean
3523 g_key_file_has_key_full (GKeyFile     *key_file,
3524                          const gchar  *group_name,
3525                          const gchar  *key,
3526                          gboolean     *has_key,
3527                          GError      **error)
3528 {
3529   GKeyFileKeyValuePair *pair;
3530   GKeyFileGroup *group;
3531
3532   g_return_val_if_fail (key_file != NULL, FALSE);
3533   g_return_val_if_fail (group_name != NULL, FALSE);
3534   g_return_val_if_fail (key != NULL, FALSE);
3535
3536   group = g_key_file_lookup_group (key_file, group_name);
3537
3538   if (!group)
3539     {
3540       g_set_error (error, G_KEY_FILE_ERROR,
3541                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3542                    _("Key file does not have group '%s'"),
3543                    group_name ? group_name : "(null)");
3544
3545       return FALSE;
3546     }
3547
3548   pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3549
3550   if (has_key)
3551     *has_key = pair != NULL;
3552   return TRUE;
3553 }
3554
3555 /**
3556  * g_key_file_has_key: (skip)
3557  * @key_file: a #GKeyFile
3558  * @group_name: a group name
3559  * @key: a key name
3560  * @error: return location for a #GError
3561  *
3562  * Looks whether the key file has the key @key in the group
3563  * @group_name.
3564  *
3565  * Note that this function does not follow the rules for #GError strictly;
3566  * the return value both carries meaning and signals an error.  To use
3567  * this function, you must pass a #GError pointer in @error, and check
3568  * whether it is not %NULL to see if an error occurred.
3569  *
3570  * Language bindings should use g_key_file_get_value() to test whether
3571  * or not a key exists.
3572  *
3573  * Return value: %TRUE if @key is a part of @group_name, %FALSE otherwise
3574  *
3575  * Since: 2.6
3576  **/
3577 gboolean
3578 g_key_file_has_key (GKeyFile     *key_file,
3579                     const gchar  *group_name,
3580                     const gchar  *key,
3581                     GError      **error)
3582 {
3583   GError *temp_error = NULL;
3584   gboolean has_key;
3585
3586   if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
3587     {
3588       return has_key;
3589     }
3590   else
3591     {
3592       g_propagate_error (error, temp_error);
3593       return FALSE;
3594     }
3595 }
3596
3597 static void
3598 g_key_file_add_group (GKeyFile    *key_file,
3599                       const gchar *group_name)
3600 {
3601   GKeyFileGroup *group;
3602
3603   g_return_if_fail (key_file != NULL);
3604   g_return_if_fail (g_key_file_is_group_name (group_name));
3605
3606   group = g_key_file_lookup_group (key_file, group_name);
3607   if (group != NULL)
3608     {
3609       key_file->current_group = group;
3610       return;
3611     }
3612
3613   group = g_slice_new0 (GKeyFileGroup);
3614   group->name = g_strdup (group_name);
3615   group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3616   key_file->groups = g_list_prepend (key_file->groups, group);
3617   key_file->current_group = group;
3618
3619   if (key_file->start_group == NULL)
3620     key_file->start_group = group;
3621
3622   g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3623 }
3624
3625 static void
3626 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3627 {
3628   if (pair != NULL)
3629     {
3630       g_free (pair->key);
3631       g_free (pair->value);
3632       g_slice_free (GKeyFileKeyValuePair, pair);
3633     }
3634 }
3635
3636 /* Be careful not to call this function on a node with data in the
3637  * lookup map without removing it from the lookup map, first.
3638  *
3639  * Some current cases where this warning is not a concern are
3640  * when:
3641  *   - the node being removed is a comment node
3642  *   - the entire lookup map is getting destroyed soon after
3643  *     anyway.
3644  */ 
3645 static void
3646 g_key_file_remove_key_value_pair_node (GKeyFile      *key_file,
3647                                        GKeyFileGroup *group,
3648                                        GList         *pair_node)
3649 {
3650
3651   GKeyFileKeyValuePair *pair;
3652
3653   pair = (GKeyFileKeyValuePair *) pair_node->data;
3654
3655   group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3656
3657   g_warn_if_fail (pair->value != NULL);
3658
3659   g_key_file_key_value_pair_free (pair);
3660
3661   g_list_free_1 (pair_node);
3662 }
3663
3664 static void
3665 g_key_file_remove_group_node (GKeyFile *key_file,
3666                               GList    *group_node)
3667 {
3668   GKeyFileGroup *group;
3669   GList *tmp;
3670
3671   group = (GKeyFileGroup *) group_node->data;
3672
3673   if (group->name)
3674     g_hash_table_remove (key_file->group_hash, group->name);
3675
3676   /* If the current group gets deleted make the current group the last
3677    * added group.
3678    */
3679   if (key_file->current_group == group)
3680     {
3681       /* groups should always contain at least the top comment group,
3682        * unless g_key_file_clear has been called
3683        */
3684       if (key_file->groups)
3685         key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3686       else
3687         key_file->current_group = NULL;
3688     }
3689
3690   /* If the start group gets deleted make the start group the first
3691    * added group.
3692    */
3693   if (key_file->start_group == group)
3694     {
3695       tmp = g_list_last (key_file->groups);
3696       while (tmp != NULL)
3697         {
3698           if (tmp != group_node &&
3699               ((GKeyFileGroup *) tmp->data)->name != NULL)
3700             break;
3701
3702           tmp = tmp->prev;
3703         }
3704
3705       if (tmp)
3706         key_file->start_group = (GKeyFileGroup *) tmp->data;
3707       else
3708         key_file->start_group = NULL;
3709     }
3710
3711   key_file->groups = g_list_remove_link (key_file->groups, group_node);
3712
3713   tmp = group->key_value_pairs;
3714   while (tmp != NULL)
3715     {
3716       GList *pair_node;
3717
3718       pair_node = tmp;
3719       tmp = tmp->next;
3720       g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3721     }
3722
3723   g_warn_if_fail (group->key_value_pairs == NULL);
3724
3725   if (group->comment)
3726     {
3727       g_key_file_key_value_pair_free (group->comment);
3728       group->comment = NULL;
3729     }
3730
3731   if (group->lookup_map)
3732     {
3733       g_hash_table_destroy (group->lookup_map);
3734       group->lookup_map = NULL;
3735     }
3736
3737   g_free ((gchar *) group->name);
3738   g_slice_free (GKeyFileGroup, group);
3739   g_list_free_1 (group_node);
3740 }
3741
3742 /**
3743  * g_key_file_remove_group:
3744  * @key_file: a #GKeyFile
3745  * @group_name: a group name
3746  * @error: return location for a #GError or %NULL
3747  *
3748  * Removes the specified group, @group_name, 
3749  * from the key file. 
3750  *
3751  * Returns: %TRUE if the group was removed, %FALSE otherwise
3752  *
3753  * Since: 2.6
3754  **/
3755 gboolean
3756 g_key_file_remove_group (GKeyFile     *key_file,
3757                          const gchar  *group_name,
3758                          GError      **error)
3759 {
3760   GList *group_node;
3761
3762   g_return_val_if_fail (key_file != NULL, FALSE);
3763   g_return_val_if_fail (group_name != NULL, FALSE);
3764
3765   group_node = g_key_file_lookup_group_node (key_file, group_name);
3766
3767   if (!group_node)
3768     {
3769       g_set_error (error, G_KEY_FILE_ERROR,
3770                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3771                    _("Key file does not have group '%s'"),
3772                    group_name);
3773       return FALSE;
3774     }
3775
3776   g_key_file_remove_group_node (key_file, group_node);
3777
3778   return TRUE;  
3779 }
3780
3781 static void
3782 g_key_file_add_key_value_pair (GKeyFile             *key_file,
3783                                GKeyFileGroup        *group,
3784                                GKeyFileKeyValuePair *pair)
3785 {
3786   g_hash_table_replace (group->lookup_map, pair->key, pair);
3787   group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3788 }
3789
3790 static void
3791 g_key_file_add_key (GKeyFile      *key_file,
3792                     GKeyFileGroup *group,
3793                     const gchar   *key,
3794                     const gchar   *value)
3795 {
3796   GKeyFileKeyValuePair *pair;
3797
3798   pair = g_slice_new (GKeyFileKeyValuePair);
3799   pair->key = g_strdup (key);
3800   pair->value = g_strdup (value);
3801
3802   g_key_file_add_key_value_pair (key_file, group, pair);
3803 }
3804
3805 /**
3806  * g_key_file_remove_key:
3807  * @key_file: a #GKeyFile
3808  * @group_name: a group name
3809  * @key: a key name to remove
3810  * @error: return location for a #GError or %NULL
3811  *
3812  * Removes @key in @group_name from the key file. 
3813  *
3814  * Returns: %TRUE if the key was removed, %FALSE otherwise
3815  *
3816  * Since: 2.6
3817  **/
3818 gboolean
3819 g_key_file_remove_key (GKeyFile     *key_file,
3820                        const gchar  *group_name,
3821                        const gchar  *key,
3822                        GError      **error)
3823 {
3824   GKeyFileGroup *group;
3825   GKeyFileKeyValuePair *pair;
3826
3827   g_return_val_if_fail (key_file != NULL, FALSE);
3828   g_return_val_if_fail (group_name != NULL, FALSE);
3829   g_return_val_if_fail (key != NULL, FALSE);
3830
3831   pair = NULL;
3832
3833   group = g_key_file_lookup_group (key_file, group_name);
3834   if (!group)
3835     {
3836       g_set_error (error, G_KEY_FILE_ERROR,
3837                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3838                    _("Key file does not have group '%s'"),
3839                    group_name ? group_name : "(null)");
3840       return FALSE;
3841     }
3842
3843   pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3844
3845   if (!pair)
3846     {
3847       g_set_error (error, G_KEY_FILE_ERROR,
3848                    G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3849                    _("Key file does not have key '%s' in group '%s'"),
3850                    key, group->name);
3851       return FALSE;
3852     }
3853
3854   group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3855   g_hash_table_remove (group->lookup_map, pair->key);
3856   g_key_file_key_value_pair_free (pair);
3857
3858   return TRUE;
3859 }
3860
3861 static GList *
3862 g_key_file_lookup_group_node (GKeyFile    *key_file,
3863                               const gchar *group_name)
3864 {
3865   GKeyFileGroup *group;
3866   GList *tmp;
3867
3868   for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3869     {
3870       group = (GKeyFileGroup *) tmp->data;
3871
3872       if (group && group->name && strcmp (group->name, group_name) == 0)
3873         break;
3874     }
3875
3876   return tmp;
3877 }
3878
3879 static GKeyFileGroup *
3880 g_key_file_lookup_group (GKeyFile    *key_file,
3881                          const gchar *group_name)
3882 {
3883   return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3884 }
3885
3886 static GList *
3887 g_key_file_lookup_key_value_pair_node (GKeyFile       *key_file,
3888                                        GKeyFileGroup  *group,
3889                                        const gchar    *key)
3890 {
3891   GList *key_node;
3892
3893   for (key_node = group->key_value_pairs;
3894        key_node != NULL;
3895        key_node = key_node->next)
3896     {
3897       GKeyFileKeyValuePair *pair;
3898
3899       pair = (GKeyFileKeyValuePair *) key_node->data; 
3900
3901       if (pair->key && strcmp (pair->key, key) == 0)
3902         break;
3903     }
3904
3905   return key_node;
3906 }
3907
3908 static GKeyFileKeyValuePair *
3909 g_key_file_lookup_key_value_pair (GKeyFile      *key_file,
3910                                   GKeyFileGroup *group,
3911                                   const gchar   *key)
3912 {
3913   return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3914 }
3915
3916 /* Lines starting with # or consisting entirely of whitespace are merely
3917  * recorded, not parsed. This function assumes all leading whitespace
3918  * has been stripped.
3919  */
3920 static gboolean
3921 g_key_file_line_is_comment (const gchar *line)
3922 {
3923   return (*line == '#' || *line == '\0' || *line == '\n');
3924 }
3925
3926 static gboolean 
3927 g_key_file_is_group_name (const gchar *name)
3928 {
3929   gchar *p, *q;
3930
3931   if (name == NULL)
3932     return FALSE;
3933
3934   p = q = (gchar *) name;
3935   while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3936     q = g_utf8_find_next_char (q, NULL);
3937   
3938   if (*q != '\0' || q == p)
3939     return FALSE;
3940
3941   return TRUE;
3942 }
3943
3944 static gboolean
3945 g_key_file_is_key_name (const gchar *name)
3946 {
3947   gchar *p, *q;
3948
3949   if (name == NULL)
3950     return FALSE;
3951
3952   p = q = (gchar *) name;
3953   /* We accept a little more than the desktop entry spec says,
3954    * since gnome-vfs uses mime-types as keys in its cache.
3955    */
3956   while (*q && *q != '=' && *q != '[' && *q != ']')
3957     q = g_utf8_find_next_char (q, NULL);
3958   
3959   /* No empty keys, please */
3960   if (q == p)
3961     return FALSE;
3962
3963   /* We accept spaces in the middle of keys to not break
3964    * existing apps, but we don't tolerate initial or final
3965    * spaces, which would lead to silent corruption when
3966    * rereading the file.
3967    */
3968   if (*p == ' ' || q[-1] == ' ')
3969     return FALSE;
3970
3971   if (*q == '[')
3972     {
3973       q++;
3974       while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3975         q = g_utf8_find_next_char (q, NULL);
3976
3977       if (*q != ']')
3978         return FALSE;     
3979
3980       q++;
3981     }
3982
3983   if (*q != '\0')
3984     return FALSE;
3985
3986   return TRUE;
3987 }
3988
3989 /* A group in a key file is made up of a starting '[' followed by one
3990  * or more letters making up the group name followed by ']'.
3991  */
3992 static gboolean
3993 g_key_file_line_is_group (const gchar *line)
3994 {
3995   gchar *p;
3996
3997   p = (gchar *) line;
3998   if (*p != '[')
3999     return FALSE;
4000
4001   p++;
4002
4003   while (*p && *p != ']')
4004     p = g_utf8_find_next_char (p, NULL);
4005
4006   if (*p != ']')
4007     return FALSE;
4008  
4009   /* silently accept whitespace after the ] */
4010   p = g_utf8_find_next_char (p, NULL);
4011   while (*p == ' ' || *p == '\t')
4012     p = g_utf8_find_next_char (p, NULL);
4013      
4014   if (*p)
4015     return FALSE;
4016
4017   return TRUE;
4018 }
4019
4020 static gboolean
4021 g_key_file_line_is_key_value_pair (const gchar *line)
4022 {
4023   gchar *p;
4024
4025   p = (gchar *) g_utf8_strchr (line, -1, '=');
4026
4027   if (!p)
4028     return FALSE;
4029
4030   /* Key must be non-empty
4031    */
4032   if (*p == line[0])
4033     return FALSE;
4034
4035   return TRUE;
4036 }
4037
4038 static gchar *
4039 g_key_file_parse_value_as_string (GKeyFile     *key_file,
4040                                   const gchar  *value,
4041                                   GSList      **pieces,
4042                                   GError      **error)
4043 {
4044   gchar *string_value, *p, *q0, *q;
4045
4046   string_value = g_new (gchar, strlen (value) + 1);
4047
4048   p = (gchar *) value;
4049   q0 = q = string_value;
4050   while (*p)
4051     {
4052       if (*p == '\\')
4053         {
4054           p++;
4055
4056           switch (*p)
4057             {
4058             case 's':
4059               *q = ' ';
4060               break;
4061
4062             case 'n':
4063               *q = '\n';
4064               break;
4065
4066             case 't':
4067               *q = '\t';
4068               break;
4069
4070             case 'r':
4071               *q = '\r';
4072               break;
4073
4074             case '\\':
4075               *q = '\\';
4076               break;
4077
4078             case '\0':
4079               g_set_error_literal (error, G_KEY_FILE_ERROR,
4080                                    G_KEY_FILE_ERROR_INVALID_VALUE,
4081                                    _("Key file contains escape character "
4082                                      "at end of line"));
4083               break;
4084
4085             default:
4086               if (pieces && *p == key_file->list_separator)
4087                 *q = key_file->list_separator;
4088               else
4089                 {
4090                   *q++ = '\\';
4091                   *q = *p;
4092                   
4093                   if (*error == NULL)
4094                     {
4095                       gchar sequence[3];
4096                       
4097                       sequence[0] = '\\';
4098                       sequence[1] = *p;
4099                       sequence[2] = '\0';
4100                       
4101                       g_set_error (error, G_KEY_FILE_ERROR,
4102                                    G_KEY_FILE_ERROR_INVALID_VALUE,
4103                                    _("Key file contains invalid escape "
4104                                      "sequence '%s'"), sequence);
4105                     }
4106                 }
4107               break;
4108             }
4109         }
4110       else
4111         {
4112           *q = *p;
4113           if (pieces && (*p == key_file->list_separator))
4114             {
4115               *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4116               q0 = q + 1; 
4117             }
4118         }
4119
4120       if (*p == '\0')
4121         break;
4122
4123       q++;
4124       p++;
4125     }
4126
4127   *q = '\0';
4128   if (pieces)
4129   {
4130     if (q0 < q)
4131       *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
4132     *pieces = g_slist_reverse (*pieces);
4133   }
4134
4135   return string_value;
4136 }
4137
4138 static gchar *
4139 g_key_file_parse_string_as_value (GKeyFile    *key_file,
4140                                   const gchar *string,
4141                                   gboolean     escape_separator)
4142 {
4143   gchar *value, *p, *q;
4144   gsize length;
4145   gboolean parsing_leading_space;
4146
4147   length = strlen (string) + 1;
4148
4149   /* Worst case would be that every character needs to be escaped.
4150    * In other words every character turns to two characters
4151    */
4152   value = g_new (gchar, 2 * length);
4153
4154   p = (gchar *) string;
4155   q = value;
4156   parsing_leading_space = TRUE;
4157   while (p < (string + length - 1))
4158     {
4159       gchar escaped_character[3] = { '\\', 0, 0 };
4160
4161       switch (*p)
4162         {
4163         case ' ':
4164           if (parsing_leading_space)
4165             {
4166               escaped_character[1] = 's';
4167               strcpy (q, escaped_character);
4168               q += 2;
4169             }
4170           else
4171             {
4172               *q = *p;
4173               q++;
4174             }
4175           break;
4176         case '\t':
4177           if (parsing_leading_space)
4178             {
4179               escaped_character[1] = 't';
4180               strcpy (q, escaped_character);
4181               q += 2;
4182             }
4183           else
4184             {
4185               *q = *p;
4186               q++;
4187             }
4188           break;
4189         case '\n':
4190           escaped_character[1] = 'n';
4191           strcpy (q, escaped_character);
4192           q += 2;
4193           break;
4194         case '\r':
4195           escaped_character[1] = 'r';
4196           strcpy (q, escaped_character);
4197           q += 2;
4198           break;
4199         case '\\':
4200           escaped_character[1] = '\\';
4201           strcpy (q, escaped_character);
4202           q += 2;
4203           parsing_leading_space = FALSE;
4204           break;
4205         default:
4206           if (escape_separator && *p == key_file->list_separator)
4207             {
4208               escaped_character[1] = key_file->list_separator;
4209               strcpy (q, escaped_character);
4210               q += 2;
4211               parsing_leading_space = TRUE;
4212             }
4213           else 
4214             {
4215               *q = *p;
4216               q++;
4217               parsing_leading_space = FALSE;
4218             }
4219           break;
4220         }
4221       p++;
4222     }
4223   *q = '\0';
4224
4225   return value;
4226 }
4227
4228 static gint
4229 g_key_file_parse_value_as_integer (GKeyFile     *key_file,
4230                                    const gchar  *value,
4231                                    GError      **error)
4232 {
4233   gchar *eof_int;
4234   glong long_value;
4235   gint int_value;
4236
4237   errno = 0;
4238   long_value = strtol (value, &eof_int, 10);
4239
4240   if (*value == '\0' || (*eof_int != '\0' && !g_ascii_isspace(*eof_int)))
4241     {
4242       gchar *value_utf8 = _g_utf8_make_valid (value);
4243       g_set_error (error, G_KEY_FILE_ERROR,
4244                    G_KEY_FILE_ERROR_INVALID_VALUE,
4245                    _("Value '%s' cannot be interpreted "
4246                      "as a number."), value_utf8);
4247       g_free (value_utf8);
4248
4249       return 0;
4250     }
4251
4252   int_value = long_value;
4253   if (int_value != long_value || errno == ERANGE)
4254     {
4255       gchar *value_utf8 = _g_utf8_make_valid (value);
4256       g_set_error (error,
4257                    G_KEY_FILE_ERROR, 
4258                    G_KEY_FILE_ERROR_INVALID_VALUE,
4259                    _("Integer value '%s' out of range"), 
4260                    value_utf8);
4261       g_free (value_utf8);
4262
4263       return 0;
4264     }
4265   
4266   return int_value;
4267 }
4268
4269 static gchar *
4270 g_key_file_parse_integer_as_value (GKeyFile *key_file,
4271                                    gint      value)
4272
4273 {
4274   return g_strdup_printf ("%d", value);
4275 }
4276
4277 static gdouble
4278 g_key_file_parse_value_as_double  (GKeyFile     *key_file,
4279                                    const gchar  *value,
4280                                    GError      **error)
4281 {
4282   gchar *end_of_valid_d;
4283   gdouble double_value = 0;
4284
4285   double_value = g_ascii_strtod (value, &end_of_valid_d);
4286
4287   if (*end_of_valid_d != '\0' || end_of_valid_d == value)
4288     {
4289       gchar *value_utf8 = _g_utf8_make_valid (value);
4290       g_set_error (error, G_KEY_FILE_ERROR,
4291                    G_KEY_FILE_ERROR_INVALID_VALUE,
4292                    _("Value '%s' cannot be interpreted "
4293                      "as a float number."), 
4294                    value_utf8);
4295       g_free (value_utf8);
4296     }
4297
4298   return double_value;
4299 }
4300
4301 static gboolean
4302 g_key_file_parse_value_as_boolean (GKeyFile     *key_file,
4303                                    const gchar  *value,
4304                                    GError      **error)
4305 {
4306   gchar *value_utf8;
4307
4308   if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
4309     return TRUE;
4310   else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
4311     return FALSE;
4312
4313   value_utf8 = _g_utf8_make_valid (value);
4314   g_set_error (error, G_KEY_FILE_ERROR,
4315                G_KEY_FILE_ERROR_INVALID_VALUE,
4316                _("Value '%s' cannot be interpreted "
4317                  "as a boolean."), value_utf8);
4318   g_free (value_utf8);
4319
4320   return FALSE;
4321 }
4322
4323 static gchar *
4324 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
4325                                    gboolean  value)
4326 {
4327   if (value)
4328     return g_strdup ("true");
4329   else
4330     return g_strdup ("false");
4331 }
4332
4333 static gchar *
4334 g_key_file_parse_value_as_comment (GKeyFile    *key_file,
4335                                    const gchar *value)
4336 {
4337   GString *string;
4338   gchar **lines;
4339   gsize i;
4340
4341   string = g_string_sized_new (512);
4342
4343   lines = g_strsplit (value, "\n", 0);
4344
4345   for (i = 0; lines[i] != NULL; i++)
4346     {
4347         if (lines[i][0] != '#')
4348            g_string_append_printf (string, "%s\n", lines[i]);
4349         else 
4350            g_string_append_printf (string, "%s\n", lines[i] + 1);
4351     }
4352   g_strfreev (lines);
4353
4354   return g_string_free (string, FALSE);
4355 }
4356
4357 static gchar *
4358 g_key_file_parse_comment_as_value (GKeyFile      *key_file,
4359                                    const gchar   *comment)
4360 {
4361   GString *string;
4362   gchar **lines;
4363   gsize i;
4364
4365   string = g_string_sized_new (512);
4366
4367   lines = g_strsplit (comment, "\n", 0);
4368
4369   for (i = 0; lines[i] != NULL; i++)
4370     g_string_append_printf (string, "#%s%s", lines[i], 
4371                             lines[i + 1] == NULL? "" : "\n");
4372   g_strfreev (lines);
4373
4374   return g_string_free (string, FALSE);
4375 }
4376
4377 /**
4378  * g_key_file_save_to_file:
4379  * @key_file: a #GKeyFile
4380  * @filename: the name of the file to write to
4381  * @error: a pointer to a %NULL #GError, or %NULL
4382  *
4383  * Writes the contents of @key_file to @filename using
4384  * g_file_set_contents().
4385  *
4386  * This function can fail for any of the reasons that
4387  * g_file_set_contents() may fail.
4388  *
4389  * Returns: %TRUE if successful, else %FALSE with @error set
4390  *
4391  * Since: 2.40
4392  */
4393 gboolean
4394 g_key_file_save_to_file (GKeyFile     *key_file,
4395                          const gchar  *filename,
4396                          GError      **error)
4397 {
4398   gchar *contents;
4399   gboolean success;
4400   gsize length;
4401
4402   g_return_val_if_fail (key_file != NULL, FALSE);
4403   g_return_val_if_fail (filename != NULL, FALSE);
4404   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4405
4406   contents = g_key_file_to_data (key_file, &length, NULL);
4407   g_assert (contents != NULL);
4408
4409   success = g_file_set_contents (filename, contents, length, error);
4410   g_free (contents);
4411
4412   return success;
4413 }