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