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