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