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