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