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