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