Include gstdio.h and use g_open().
[platform/upstream/glib.git] / glib / gkeyfile.c
1 /* gkeyfile.c - key file parser
2  *
3  *  Copyright 2004  Red Hat, Inc.  
4  *
5  * Written by Ray Strode <rstrode@redhat.com>
6  *            Matthias Clasen <mclasen@redhat.com>
7  *
8  * GLib is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * GLib is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with GLib; see the file COPYING.LIB.  If not,
20  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  *   Boston, MA 02111-1307, USA.
22  */
23
24 #include "config.h"
25 #include "galias.h"
26
27 #include "gkeyfile.h"
28
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <locale.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40 #ifdef G_OS_WIN32
41 #include <io.h>
42 #endif
43
44 #include "gconvert.h"
45 #include "gdataset.h"
46 #include "gerror.h"
47 #include "gfileutils.h"
48 #include "ghash.h"
49 #include "glibintl.h"
50 #include "glist.h"
51 #include "gslist.h"
52 #include "gmem.h"
53 #include "gmessages.h"
54 #include "gstdio.h"
55 #include "gstring.h"
56 #include "gstrfuncs.h"
57 #include "gutils.h"
58
59 typedef struct _GKeyFileGroup GKeyFileGroup;
60
61 struct _GKeyFile
62 {
63   GList *groups;
64
65   gchar  *start_group_name;
66
67   GKeyFileGroup *current_group;
68
69   GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
70
71   /* Used for sizing the output buffer during serialization
72    */
73   gsize approximate_size;
74
75   gchar list_separator;
76
77   GKeyFileFlags flags;
78 };
79
80 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
81
82 struct _GKeyFileGroup
83 {
84   const gchar *name;  /* NULL for above first group (which will be comments) */
85
86   GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
87
88   GList *key_value_pairs; 
89
90   /* Used in parallel with key_value_pairs for
91    * increased lookup performance
92    */
93   GHashTable *lookup_map;
94 };
95
96 struct _GKeyFileKeyValuePair
97 {
98   gchar *key;  /* NULL for comments */
99   gchar *value;
100 };
101
102 static gint                  find_file_in_data_dirs            (const gchar            *file,
103                                                                 gchar                 **output_file,
104                                                                 gchar                ***data_dirs,
105                                                                 GError                **error);
106 static gboolean              g_key_file_load_from_fd           (GKeyFile               *key_file,
107                                                                 gint                    fd,
108                                                                 GKeyFileFlags           flags,
109                                                                 GError                **error);
110 static GList                *g_key_file_lookup_group_node      (GKeyFile    *key_file,
111                                                                 const gchar *group_name);
112 static GKeyFileGroup        *g_key_file_lookup_group           (GKeyFile               *key_file,
113                                                                 const gchar            *group_name);
114
115 static GList                *g_key_file_lookup_key_value_pair_node  (GKeyFile       *key_file,
116                                                                      GKeyFileGroup  *group,
117                                                                      const gchar    *key);
118 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair       (GKeyFile       *key_file,
119                                                                      GKeyFileGroup  *group,
120                                                                      const gchar    *key);
121
122 static void                  g_key_file_remove_group_node          (GKeyFile      *entry,
123                                                                     GList         *group_node);
124 static void                  g_key_file_remove_key_value_pair_node (GKeyFile      *key_file,
125                                                                     GKeyFileGroup *group,
126                                                                     GList         *pair_node);
127
128 static void                  g_key_file_add_key                (GKeyFile               *entry,
129                                                                 const gchar            *group_name,
130                                                                 const gchar            *key,
131                                                                 const gchar            *value);
132 static void                  g_key_file_add_group              (GKeyFile               *entry,
133                                                                 const gchar            *group_name);
134 static void                  g_key_file_key_value_pair_free    (GKeyFileKeyValuePair   *pair);
135 static gboolean              g_key_file_line_is_comment        (const gchar            *line);
136 static gboolean              g_key_file_line_is_group          (const gchar            *line);
137 static gboolean              g_key_file_line_is_key_value_pair (const gchar            *line);
138 static gchar                *g_key_file_parse_value_as_string  (GKeyFile               *entry,
139                                                                 const gchar            *value,
140                                                                 GSList                **separators,
141                                                                 GError                **error);
142 static gchar                *g_key_file_parse_string_as_value  (GKeyFile               *entry,
143                                                                 const gchar            *string,
144                                                                 gboolean                escape_separator);
145 static gint                  g_key_file_parse_value_as_integer (GKeyFile               *entry,
146                                                                 const gchar            *value,
147                                                                 GError                **error);
148 static gchar                *g_key_file_parse_integer_as_value (GKeyFile               *entry,
149                                                                 gint                    value);
150 static gboolean              g_key_file_parse_value_as_boolean (GKeyFile               *entry,
151                                                                 const gchar            *value,
152                                                                 GError                **error);
153 static gchar                *g_key_file_parse_boolean_as_value (GKeyFile               *entry,
154                                                                 gboolean                value);
155 static gchar                *g_key_file_parse_value_as_comment (GKeyFile *key_file,
156                                                                 const gchar *value);
157 static gchar                *g_key_file_parse_comment_as_value (GKeyFile *key_file,
158                                                                 const gchar *comment);
159 static void                  g_key_file_parse_key_value_pair   (GKeyFile               *entry,
160                                                                 const gchar            *line,
161                                                                 gsize                   length,
162                                                                 GError                **error);
163 static void                  g_key_file_parse_comment          (GKeyFile               *entry,
164                                                                 const gchar            *line,
165                                                                 gsize                   length,
166                                                                 GError                **error);
167 static void                  g_key_file_parse_group            (GKeyFile               *entry,
168                                                                 const gchar            *line,
169                                                                 gsize                   length,
170                                                                 GError                **error);
171 static gchar                *key_get_locale                    (const gchar            *key);
172 static void                  g_key_file_parse_data             (GKeyFile               *entry,
173                                                                 const gchar            *data,
174                                                                 gsize                   length,
175                                                                 GError                **error);
176 static void                  g_key_file_flush_parse_buffer     (GKeyFile               *entry,
177                                                                 GError                **error);
178
179
180 GQuark
181 g_key_file_error_quark (void)
182 {
183   static GQuark error_quark = 0;
184
185   if (error_quark == 0)
186     error_quark = g_quark_from_static_string ("g-key-file-error-quark");
187
188   return error_quark;
189 }
190
191 static void
192 g_key_file_init (GKeyFile *key_file)
193 {  
194   key_file->current_group = g_new0 (GKeyFileGroup, 1);
195   key_file->groups = g_list_prepend (NULL, key_file->current_group);
196   key_file->start_group_name = NULL;
197   key_file->parse_buffer = g_string_sized_new (128);
198   key_file->approximate_size = 0;
199   key_file->list_separator = ';';
200   key_file->flags = 0;
201 }
202
203 static void
204 g_key_file_clear (GKeyFile *key_file)
205 {
206   GList *tmp, *group_node;
207
208   if (key_file->parse_buffer)
209     g_string_free (key_file->parse_buffer, TRUE);
210
211   g_free (key_file->start_group_name);
212
213   tmp = key_file->groups;
214   while (tmp != NULL)
215     {
216       group_node = tmp;
217       tmp = tmp->next;
218       g_key_file_remove_group_node (key_file, group_node);
219     }
220
221   g_assert (key_file->groups == NULL);
222 }
223
224
225 /**
226  * g_key_file_new:
227  *
228  * Creates a new empty #GKeyFile object. Use g_key_file_load_from_file(),
229  * g_key_file_load_from_data() or g_key_file_load_from_data_dirs() to
230  * read an existing key file.
231  *
232  * Return value: an empty #GKeyFile.
233  *
234  * Since: 2.6
235  **/
236 GKeyFile *
237 g_key_file_new (void)
238 {
239   GKeyFile *key_file;
240
241   key_file = g_new0 (GKeyFile, 1);
242   g_key_file_init (key_file);
243
244   return key_file;
245 }
246
247 /**
248  * g_key_file_set_list_separator:
249  * @key_file: a #GKeyFile 
250  * @separator: the separator
251  *
252  * Sets the character which is used to separate
253  * values in lists. Typically ';' or ',' are used
254  * as separators. The default list separator is ';'.
255  *
256  * Since: 2.6
257  */
258 void
259 g_key_file_set_list_separator (GKeyFile *key_file,
260                                gchar     separator)
261 {
262   key_file->list_separator = separator;
263 }
264
265
266 /* Iterates through all the directories in *dirs trying to
267  * open file.  When it successfully locates and opens a file it
268  * returns the file descriptor to the open file.  It also
269  * outputs the absolute path of the file in output_file and
270  * leaves the unchecked directories in *dirs.
271  */
272 static gint
273 find_file_in_data_dirs (const gchar   *file,
274                         gchar        **output_file,
275                         gchar       ***dirs,
276                         GError       **error)
277 {
278   gchar **data_dirs, *data_dir, *path;
279   gint fd;
280   GError *file_error;
281
282   file_error = NULL;
283   path = NULL;
284   fd = -1;
285
286   if (dirs == NULL)
287     return fd;
288
289   data_dirs = *dirs;
290
291   while (data_dirs && (data_dir = *data_dirs) && fd < 0)
292     {
293       gchar *candidate_file, *sub_dir;
294
295       candidate_file = (gchar *) file;
296       sub_dir = g_strdup ("");
297       while (candidate_file != NULL && fd < 0)
298         {
299           gchar *p;
300
301           path = g_build_filename (data_dir, sub_dir,
302                                    candidate_file, NULL);
303
304           fd = g_open (path, O_RDONLY, 0);
305
306           if (output_file != NULL)
307             *output_file = g_strdup (path);
308
309           g_free (path);
310
311           if (fd < 0 && file_error == NULL)
312             file_error = g_error_new (G_KEY_FILE_ERROR,
313                                       G_KEY_FILE_ERROR_NOT_FOUND,
314                                       _("Valid key file could not be "
315                                         "found in data dirs")); 
316
317           candidate_file = strchr (candidate_file, '-');
318
319           if (candidate_file == NULL)
320             break;
321
322           candidate_file++;
323
324           g_free (sub_dir);
325           sub_dir = g_strndup (file, candidate_file - file - 1);
326
327           for (p = sub_dir; *p != '\0'; p++)
328             {
329               if (*p == '-')
330                 *p = G_DIR_SEPARATOR;
331             }
332         }
333       g_free (sub_dir);
334       data_dirs++;
335     }
336
337   *dirs = data_dirs;
338
339   if (file_error)
340     {
341       if (fd >= 0)
342         g_error_free (file_error);
343       else
344         g_propagate_error (error, file_error);
345     }
346
347   if (output_file && fd < 0)
348     {
349       g_free (*output_file);
350       *output_file = NULL;
351     }
352
353   return fd;
354 }
355
356 static gboolean
357 g_key_file_load_from_fd (GKeyFile       *key_file,
358                          gint            fd,
359                          GKeyFileFlags   flags,
360                          GError        **error)
361 {
362   GError *key_file_error = NULL;
363   gsize bytes_read;
364   struct stat stat_buf;
365   gchar read_buf[4096];
366
367   fstat (fd, &stat_buf);
368   if (!S_ISREG (stat_buf.st_mode))
369     {
370       g_set_error (error, G_KEY_FILE_ERROR,
371                    G_KEY_FILE_ERROR_PARSE,
372                    _("Not a regular file"));
373       return FALSE;
374     }
375
376   if (stat_buf.st_size == 0)
377     {
378       g_set_error (error, G_KEY_FILE_ERROR,
379                    G_KEY_FILE_ERROR_PARSE,
380                    _("File is empty"));
381       return FALSE;
382     }
383
384   if (key_file->approximate_size > 0)
385     {
386       g_key_file_clear (key_file);
387       g_key_file_init (key_file);
388     }
389   key_file->flags = flags;
390
391   bytes_read = 0;
392   do
393     {
394       bytes_read = read (fd, read_buf, 4096);
395
396       if (bytes_read == 0)  /* End of File */
397         break;
398
399       if (bytes_read < 0)
400         {
401           if (errno == EINTR)
402             continue;
403
404           g_set_error (error, G_FILE_ERROR,
405                        g_file_error_from_errno (errno),
406                        "%s", g_strerror (errno));
407           return FALSE;
408         }
409
410       g_key_file_parse_data (key_file, 
411                              read_buf, bytes_read,
412                              &key_file_error);
413     }
414   while (!key_file_error);
415
416   if (key_file_error)
417     {
418       g_propagate_error (error, key_file_error);
419       return FALSE;
420     }
421
422   g_key_file_flush_parse_buffer (key_file, &key_file_error);
423
424   if (key_file_error)
425     {
426       g_propagate_error (error, key_file_error);
427       return FALSE;
428     }
429
430   return TRUE;
431 }
432
433 /**
434  * g_key_file_load_from_file:
435  * @key_file: an empty #GKeyFile struct
436  * @file: the path of a filename to load, in the GLib file name encoding
437  * @flags: flags from #GKeyFileFlags
438  * @error: return location for a #GError, or %NULL
439  *
440  * Loads a key file into an empty #GKeyFile structure.
441  * If the file could not be loaded then %error is set to 
442  * either a #GFileError or #GKeyFileError.
443  *
444  * Return value: %TRUE if a key file could be loaded, %FALSE othewise
445  * Since: 2.6
446  **/
447 gboolean
448 g_key_file_load_from_file (GKeyFile       *key_file,
449                            const gchar    *file,
450                            GKeyFileFlags   flags,
451                            GError        **error)
452 {
453   GError *key_file_error = NULL;
454   gint fd;
455
456   g_return_val_if_fail (key_file != NULL, FALSE);
457   g_return_val_if_fail (file != NULL, FALSE);
458
459   fd = g_open (file, O_RDONLY, 0);
460
461   if (fd < 0)
462     {
463       g_set_error (error, G_FILE_ERROR,
464                    g_file_error_from_errno (errno),
465                    "%s", g_strerror (errno));
466       return FALSE;
467     }
468
469   g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
470   close (fd);
471
472   if (key_file_error)
473     {
474       g_propagate_error (error, key_file_error);
475       return FALSE;
476     }
477
478   return TRUE;
479 }
480
481 /**
482  * g_key_file_load_from_data:
483  * @key_file: an empty #GKeyFile struct
484  * @data: key file loaded in memory.
485  * @length: the length of @data in bytes
486  * @flags: flags from #GKeyFileFlags
487  * @error: return location for a #GError, or %NULL
488  *
489  * Loads a key file from memory into an empty #GKeyFile structure.  
490  * If the object cannot be created then %error is set to a #GKeyFileError.
491  *
492  * Return value: %TRUE if a key file could be loaded, %FALSE othewise
493  * Since: 2.6
494  **/
495 gboolean
496 g_key_file_load_from_data (GKeyFile       *key_file,
497                            const gchar    *data,
498                            gsize           length,
499                            GKeyFileFlags   flags,
500                            GError        **error)
501 {
502   GError *key_file_error = NULL;
503
504   g_return_val_if_fail (key_file != NULL, FALSE);
505   g_return_val_if_fail (data != NULL, FALSE);
506   g_return_val_if_fail (length != 0, FALSE);
507
508   if (key_file->approximate_size > 0)
509     {
510       g_key_file_clear (key_file);
511       g_key_file_init (key_file);
512     }
513   key_file->flags = flags;
514
515   g_key_file_parse_data (key_file, data, length, &key_file_error);
516   
517   if (key_file_error)
518     {
519       g_propagate_error (error, key_file_error);
520       return FALSE;
521     }
522
523   g_key_file_flush_parse_buffer (key_file, &key_file_error);
524   
525   if (key_file_error)
526     {
527       g_propagate_error (error, key_file_error);
528       return FALSE;
529     }
530
531   return TRUE;
532 }
533
534 /**
535  * g_key_file_load_from_data_dirs:
536  * @key_file: an empty #GKeyFile struct
537  * @file: a relative path to a filename to open and parse
538  * @full_path: return location for a string containing the full path
539  *   of the file, or %NULL
540  * @flags: flags from #GKeyFileFlags 
541  * @error: return location for a #GError, or %NULL
542  *
543  * This function looks for a key file named @file in the paths 
544  * returned from g_get_user_data_dir() and g_get_system_data_dirs(), 
545  * loads the file into @key_file and returns the file's full path in 
546  * @full_path.  If the file could not be loaded then an %error is
547  * set to either a #GFileError or #GKeyFileError.
548  *
549  * Return value: %TRUE if a key file could be loaded, %FALSE othewise
550  * Since: 2.6
551  **/
552 gboolean
553 g_key_file_load_from_data_dirs (GKeyFile       *key_file,
554                                 const gchar    *file,
555                                 gchar         **full_path,
556                                 GKeyFileFlags   flags,
557                                 GError        **error)
558 {
559   GError *key_file_error = NULL;
560   gchar **all_data_dirs, **data_dirs;
561   const gchar * user_data_dir;
562   const gchar * const * system_data_dirs;
563   gsize i, j;
564   gchar *output_path;
565   gint fd;
566   gboolean found_file;
567   
568   g_return_val_if_fail (key_file != NULL, FALSE);
569   g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
570
571   user_data_dir = g_get_user_data_dir ();
572   system_data_dirs = g_get_system_data_dirs ();
573   all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 1);
574
575   i = 0;
576   all_data_dirs[i++] = g_strdup (user_data_dir);
577
578   j = 0;
579   while (system_data_dirs[j] != NULL)
580     all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
581
582   found_file = FALSE;
583   data_dirs = all_data_dirs;
584   while (*data_dirs != NULL && !found_file)
585     {
586       fd = find_file_in_data_dirs (file, &output_path, &data_dirs, 
587                                    &key_file_error);
588       
589       if (fd < 0)
590         {
591           if (key_file_error)
592             g_propagate_error (error, key_file_error);
593           break;
594         }
595
596       found_file = g_key_file_load_from_fd (key_file, fd, flags,
597                                             &key_file_error);
598       close (fd);
599       
600       if (key_file_error)
601         {
602           g_propagate_error (error, key_file_error);
603           g_free (output_path);
604           break;
605         }
606       
607       if (full_path)
608         *full_path = output_path;
609     }
610
611   g_strfreev (all_data_dirs);
612   return found_file;
613 }
614
615 /**
616  * g_key_file_free:
617  * @key_file: a #GKeyFile
618  *
619  * Frees a #GKeyFile.
620  *
621  * Since: 2.6
622  **/
623 void
624 g_key_file_free (GKeyFile *key_file)
625 {
626   g_return_if_fail (key_file != NULL);
627   
628   g_key_file_clear (key_file);
629   g_free (key_file);
630 }
631
632 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
633  * true for locales that match those in g_get_language_names().
634  */
635 static gboolean
636 g_key_file_locale_is_interesting (GKeyFile    *key_file,
637                                   const gchar *locale)
638 {
639   const gchar * const * current_locales;
640   gsize i;
641
642   if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
643     return TRUE;
644
645   current_locales = g_get_language_names ();
646
647   for (i = 0; current_locales[i] != NULL; i++)
648     {
649       if (g_ascii_strcasecmp (current_locales[i], locale) == 0)
650         return TRUE;
651     }
652
653   return FALSE;
654 }
655
656 static void
657 g_key_file_parse_line (GKeyFile     *key_file,
658                        const gchar  *line,
659                        gsize         length,
660                        GError      **error)
661 {
662   GError *parse_error = NULL;
663   gchar *line_start;
664
665   g_return_if_fail (key_file != NULL);
666   g_return_if_fail (line != NULL);
667
668   line_start = (gchar *) line;
669   while (g_ascii_isspace (*line_start))
670     line_start++;
671
672   if (g_key_file_line_is_comment (line_start))
673     g_key_file_parse_comment (key_file, line, length, &parse_error);
674   else if (g_key_file_line_is_group (line_start))
675     g_key_file_parse_group (key_file, line_start,
676                             length - (line_start - line),
677                             &parse_error);
678   else if (g_key_file_line_is_key_value_pair (line_start))
679     g_key_file_parse_key_value_pair (key_file, line_start,
680                                      length - (line_start - line),
681                                      &parse_error);
682   else
683     {
684       g_set_error (error, G_KEY_FILE_ERROR,
685                    G_KEY_FILE_ERROR_PARSE,
686                    _("Key file contains line '%s' which is not "
687                      "a key-value pair, group, or comment"), line);
688       return;
689     }
690
691   if (parse_error)
692     g_propagate_error (error, parse_error);
693 }
694
695 static void
696 g_key_file_parse_comment (GKeyFile     *key_file,
697                           const gchar  *line,
698                           gsize         length,
699                           GError      **error)
700 {
701   GKeyFileKeyValuePair *pair;
702   
703   if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
704     return;
705   
706   g_assert (key_file->current_group != NULL);
707
708   pair = g_new0 (GKeyFileKeyValuePair, 1);
709   
710   pair->key = NULL;
711   pair->value = g_strndup (line, length);
712   
713   key_file->current_group->key_value_pairs =
714     g_list_prepend (key_file->current_group->key_value_pairs, pair);
715 }
716
717 static void
718 g_key_file_parse_group (GKeyFile     *key_file,
719                         const gchar  *line,
720                         gsize         length,
721                         GError      **error)
722 {
723   gchar *group_name;
724   const gchar *group_name_start, *group_name_end;
725   
726   /* advance past opening '['
727    */
728   group_name_start = line + 1;
729   group_name_end = line + length - 1;
730   
731   while (*group_name_end != ']')
732     group_name_end--;
733
734   group_name = g_strndup (group_name_start, 
735                           group_name_end - group_name_start);
736   
737   if (key_file->start_group_name == NULL)
738     key_file->start_group_name = g_strdup (group_name);
739   
740   g_key_file_add_group (key_file, group_name);
741   g_free (group_name);
742 }
743
744 static void
745 g_key_file_parse_key_value_pair (GKeyFile     *key_file,
746                                  const gchar  *line,
747                                  gsize         length,
748                                  GError      **error)
749 {
750   gchar *key, *value, *key_end, *value_start, *locale;
751   gsize key_len, value_len;
752
753   if (key_file->current_group == NULL || key_file->current_group->name == NULL)
754     {
755       g_set_error (error, G_KEY_FILE_ERROR,
756                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
757                    _("Key file does not start with a group"));
758       return;
759     }
760
761   key_end = value_start = strchr (line, '=');
762
763   g_assert (key_end != NULL);
764
765   key_end--;
766   value_start++;
767
768   /* Pull the key name from the line (chomping trailing whitespace)
769    */
770   while (g_ascii_isspace (*key_end))
771     key_end--;
772
773   key_len = key_end - line + 2;
774
775   g_assert (key_len <= length);
776
777   key = g_strndup (line, key_len - 1);
778
779   /* Pull the value from the line (chugging leading whitespace)
780    */
781   while (g_ascii_isspace (*value_start))
782     value_start++;
783
784   value_len = line + length - value_start + 1;
785
786   value = g_strndup (value_start, value_len);
787
788   g_assert (key_file->start_group_name != NULL);
789
790   if (key_file->current_group
791       && key_file->current_group->name
792       && strcmp (key_file->start_group_name,
793                  key_file->current_group->name) == 0
794       && strcmp (key, "Encoding") == 0)
795     {
796       if (g_ascii_strcasecmp (value, "UTF-8") != 0)
797         {
798           g_set_error (error, G_KEY_FILE_ERROR,
799                        G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
800                        _("Key file contains unsupported encoding '%s'"), value);
801
802           g_free (key);
803           g_free (value);
804           return;
805         }
806     }
807
808   /* Is this key a translation? If so, is it one that we care about?
809    */
810   locale = key_get_locale (key);
811
812   if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
813     g_key_file_add_key (key_file, key_file->current_group->name, key, value);
814
815   g_free (locale);
816   g_free (key);
817   g_free (value);
818 }
819
820 static gchar *
821 key_get_locale (const gchar *key)
822 {
823   gchar *locale;
824
825   locale = g_strrstr (key, "[");
826
827   if (locale && strlen (locale) <= 2)
828     locale = NULL;
829
830   if (locale)
831     locale = g_strndup (locale + 1, strlen (locale) - 2);
832
833   return locale;
834 }
835
836 static void
837 g_key_file_parse_data (GKeyFile     *key_file,
838                        const gchar  *data,
839                        gsize         length,
840                        GError      **error)
841 {
842   GError *parse_error;
843   gsize i;
844
845   g_return_if_fail (key_file != NULL);
846   g_return_if_fail (data != NULL);
847
848   parse_error = NULL;
849
850   for (i = 0; i < length; i++)
851     {
852       if (data[i] == '\n')
853         {
854           /* When a newline is encountered flush the parse buffer so that the
855            * line can be parsed.  Note that completely blank lines won't show
856            * up in the parse buffer, so they get parsed directly.
857            */
858           if (key_file->parse_buffer->len > 0)
859             g_key_file_flush_parse_buffer (key_file, &parse_error);
860           else
861             g_key_file_parse_comment (key_file, "", 1, &parse_error);
862
863           if (parse_error)
864             {
865               g_propagate_error (error, parse_error);
866               return;
867             }
868         }
869       else
870         g_string_append_c (key_file->parse_buffer, data[i]);
871     }
872
873   key_file->approximate_size += length;
874 }
875
876 static void
877 g_key_file_flush_parse_buffer (GKeyFile  *key_file,
878                                GError   **error)
879 {
880   GError *file_error = NULL;
881
882   g_return_if_fail (key_file != NULL);
883
884   file_error = NULL;
885
886   if (key_file->parse_buffer->len > 0)
887     {
888       g_key_file_parse_line (key_file, key_file->parse_buffer->str,
889                              key_file->parse_buffer->len,
890                              &file_error);
891       g_string_erase (key_file->parse_buffer, 0, -1);
892
893       if (file_error)
894         {
895           g_propagate_error (error, file_error);
896           return;
897         }
898     }
899 }
900
901 /**
902  * g_key_file_to_data:
903  * @key_file: a #GKeyFile
904  * @length: return location for the length of the 
905  *   returned string, or %NULL
906  * @error: return location for a #GError, or %NULL
907  *
908  * This function outputs @key_file as a string.  
909  *
910  * Return value: a newly allocated string holding
911  *   the contents of the #GKeyFile 
912  *
913  * Since: 2.6
914  **/
915 gchar *
916 g_key_file_to_data (GKeyFile  *key_file,
917                     gsize     *length,
918                     GError   **error)
919 {
920   GString *data_string;
921   gchar *data;
922   GList *group_node, *key_file_node;
923
924   g_return_val_if_fail (key_file != NULL, NULL);
925
926   data_string = g_string_sized_new (2 * key_file->approximate_size);
927   
928   for (group_node = g_list_last (key_file->groups);
929        group_node != NULL;
930        group_node = group_node->prev)
931     {
932       GKeyFileGroup *group;
933
934       group = (GKeyFileGroup *) group_node->data;
935
936       if (group->comment != NULL)
937         g_string_append_printf (data_string, "%s\n", group->comment->value);
938       if (group->name != NULL)
939         g_string_append_printf (data_string, "[%s]\n", group->name);
940
941       for (key_file_node = g_list_last (group->key_value_pairs);
942            key_file_node != NULL;
943            key_file_node = key_file_node->prev)
944         {
945           GKeyFileKeyValuePair *pair;
946
947           pair = (GKeyFileKeyValuePair *) key_file_node->data;
948
949           if (pair->key != NULL)
950             g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
951           else
952             g_string_append_printf (data_string, "%s\n", pair->value);
953         }
954     }
955
956   if (length)
957     *length = data_string->len;
958
959   data = data_string->str;
960
961   g_string_free (data_string, FALSE);
962
963   return data;
964 }
965
966 /**
967  * g_key_file_get_keys:
968  * @key_file: a #GKeyFile
969  * @group_name: a group name, or %NULL
970  * @length: return location for the number of keys returned, or %NULL
971  * @error: return location for a #GError, or %NULL
972  *
973  * Returns all keys for the group name @group_name. If @group_name is
974  * %NULL, the start group is used. The array of returned keys will be 
975  * %NULL-terminated, so @length may optionally be %NULL.
976  *
977  * Return value: a newly-allocated %NULL-terminated array of
978  * strings. Use g_strfreev() to free it.
979  *
980  * Since: 2.6
981  **/
982 gchar **
983 g_key_file_get_keys (GKeyFile     *key_file,
984                      const gchar  *group_name,
985                      gsize        *length,
986                      GError      **error)
987 {
988   GKeyFileGroup *group;
989   GList *tmp;
990   gchar **keys;
991   gsize i, num_keys;
992   
993   g_return_val_if_fail (key_file != NULL, NULL);
994   g_return_val_if_fail (group_name != NULL, NULL);
995   
996   group = g_key_file_lookup_group (key_file, group_name);
997   
998   if (!group)
999     {
1000       g_set_error (error, G_KEY_FILE_ERROR,
1001                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1002                    _("Key file does not have group '%s'"),
1003                    group_name);
1004       return NULL;
1005     }
1006
1007   num_keys = g_list_length (group->key_value_pairs);
1008   
1009   keys = (gchar **) g_new0 (gchar **, num_keys + 1);
1010
1011   tmp = group->key_value_pairs;
1012   for (i = 0; i < num_keys; i++)
1013     {
1014       GKeyFileKeyValuePair *pair;
1015
1016       pair = (GKeyFileKeyValuePair *) tmp->data;
1017       keys[i] = g_strdup (pair->key);
1018
1019       tmp = tmp->next;
1020     }
1021   keys[i] = NULL;
1022
1023   if (length)
1024     *length = num_keys;
1025
1026   return keys;
1027 }
1028
1029 /**
1030  * g_key_file_get_start_group:
1031  * @key_file: a #GKeyFile
1032  *
1033  * Returns the name of the start group of the file. 
1034  *
1035  * Return value: The start group of the key file.
1036  *
1037  * Since: 2.6
1038  **/
1039 gchar *
1040 g_key_file_get_start_group (GKeyFile *key_file)
1041 {
1042   g_return_val_if_fail (key_file != NULL, NULL);
1043
1044   return g_strdup (key_file->start_group_name);
1045 }
1046
1047 /**
1048  * g_key_file_get_groups:
1049  * @key_file: a #GKeyFile
1050  * @length: return location for the number of returned groups, or %NULL
1051  *
1052  * Returns all groups in the key file loaded with @key_file.  The
1053  * array of returned groups will be %NULL-terminated, so @length may
1054  * optionally be %NULL.
1055  *
1056  * Return value: a newly-allocated %NULL-terminated array of strings. 
1057  *   Use g_strfreev() to free it.
1058  * Since: 2.6
1059  **/
1060 gchar **
1061 g_key_file_get_groups (GKeyFile *key_file,
1062                        gsize    *length)
1063 {
1064   GList *group_node;
1065   gchar **groups;
1066   gsize i, num_groups;
1067
1068   g_return_val_if_fail (key_file != NULL, NULL);
1069
1070   num_groups = g_list_length (key_file->groups);
1071
1072   g_assert (num_groups > 0);
1073
1074   /* Only need num_groups instead of num_groups + 1
1075    * because the first group of the file (last in the
1076    * list) is always the comment group at the top,
1077    * which we skip
1078    */
1079   groups = (gchar **) g_new0 (gchar **, num_groups);
1080
1081   group_node = g_list_last (key_file->groups);
1082   
1083   g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1084
1085   i = 0;
1086   for (group_node = group_node->prev;
1087        group_node != NULL;
1088        group_node = group_node->prev)
1089     {
1090       GKeyFileGroup *group;
1091
1092       group = (GKeyFileGroup *) group_node->data;
1093
1094       g_assert (group->name != NULL);
1095
1096       groups[i++] = g_strdup (group->name);
1097     }
1098   groups[i] = NULL;
1099
1100   if (length)
1101     *length = i;
1102
1103   return groups;
1104 }
1105
1106 /**
1107  * g_key_file_get_value:
1108  * @key_file: a #GKeyFile
1109  * @group_name: a group name
1110  * @key: a key
1111  * @error: return location for a #GError, or #NULL
1112  *
1113  * Returns the value associated with @key under @group_name.  
1114  * In the event the key cannot be found, %NULL is returned and 
1115  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND.  In the 
1116  * event that the @group_name cannot be found, %NULL is returned 
1117  * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1118  *
1119  * Return value: a string or %NULL if the specified key cannot be
1120  * found.
1121  *
1122  * Since: 2.6
1123  **/
1124 gchar *
1125 g_key_file_get_value (GKeyFile     *key_file,
1126                       const gchar  *group_name,
1127                       const gchar  *key,
1128                       GError      **error)
1129 {
1130   GKeyFileGroup *group;
1131   GKeyFileKeyValuePair *pair;
1132   gchar *value = NULL;
1133
1134   g_return_val_if_fail (key_file != NULL, NULL);
1135   g_return_val_if_fail (group_name != NULL, NULL);
1136   g_return_val_if_fail (key != NULL, NULL);
1137   
1138   group = g_key_file_lookup_group (key_file, group_name);
1139
1140   if (!group)
1141     {
1142       g_set_error (error, G_KEY_FILE_ERROR,
1143                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1144                    _("Key file does not have group '%s'"),
1145                    group_name);
1146       return NULL;
1147     }
1148
1149   pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1150
1151   if (pair)
1152     value = g_strdup (pair->value);
1153   else
1154     g_set_error (error, G_KEY_FILE_ERROR,
1155                  G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1156                  _("Key file does not have key '%s'"), key);
1157
1158   return value;
1159 }
1160
1161 /**
1162  * g_key_file_set_value:
1163  * @key_file: a #GKeyFile
1164  * @group_name: a group name
1165  * @key: a key
1166  * @value: a string
1167  *
1168  * Associates a new value with @key under @group_name.
1169  * If @key cannot be found then it is created. If @group_name
1170  * cannot be found then it is created as well.
1171  *
1172  * Since: 2.6
1173  **/
1174 void
1175 g_key_file_set_value (GKeyFile    *key_file,
1176                       const gchar *group_name,
1177                       const gchar *key,
1178                       const gchar *value)
1179 {
1180   GKeyFileGroup *group;
1181   GKeyFileKeyValuePair *pair;
1182
1183   g_return_if_fail (key_file != NULL);
1184   g_return_if_fail (key != NULL);
1185   g_return_if_fail (value != NULL);
1186
1187   if (!g_key_file_has_key (key_file, group_name, key, NULL))
1188     g_key_file_add_key (key_file, group_name, key, value);
1189   else
1190     {
1191       group = g_key_file_lookup_group (key_file, group_name);
1192       pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1193       g_free (pair->value);
1194       pair->value = g_strdup (value);
1195     }
1196 }
1197
1198 /**
1199  * g_key_file_get_string:
1200  * @key_file: a #GKeyFile
1201  * @group_name: a group name
1202  * @key: a key
1203  * @error: return location for a #GError, or #NULL
1204  *
1205  * Returns the value associated with @key under @group_name.  
1206  * In the event the key cannot be found, %NULL is returned and 
1207  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND.  In the 
1208  * event that the @group_name cannot be found, %NULL is returned 
1209  * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1210  *
1211  * Return value: a string or %NULL if the specified key cannot be
1212  * found.
1213  *
1214  * Since: 2.6
1215  **/
1216 gchar *
1217 g_key_file_get_string (GKeyFile     *key_file,
1218                        const gchar  *group_name,
1219                        const gchar  *key,
1220                        GError      **error)
1221 {
1222   gchar *value, *string_value;
1223   GError *key_file_error;
1224
1225   g_return_val_if_fail (key_file != NULL, NULL);
1226   g_return_val_if_fail (group_name != NULL, NULL);
1227   g_return_val_if_fail (key != NULL, NULL);
1228
1229   key_file_error = NULL;
1230
1231   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1232
1233   if (key_file_error)
1234     {
1235       g_propagate_error (error, key_file_error);
1236       return NULL;
1237     }
1238
1239   if (!g_utf8_validate (value, -1, NULL))
1240     {
1241       g_set_error (error, G_KEY_FILE_ERROR,
1242                    G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1243                    _("Key file contains key '%s' with value '%s' "
1244                      "which is not UTF-8"), key, value);
1245       g_free (value);
1246       return NULL;
1247     }
1248
1249   string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1250                                                    &key_file_error);
1251   g_free (value);
1252
1253   if (key_file_error)
1254     {
1255       if (g_error_matches (key_file_error,
1256                            G_KEY_FILE_ERROR,
1257                            G_KEY_FILE_ERROR_INVALID_VALUE))
1258         {
1259           g_set_error (error, G_KEY_FILE_ERROR,
1260                        G_KEY_FILE_ERROR_INVALID_VALUE,
1261                        _("Key file contains key '%s' "
1262                          "which has value that cannot be interpreted."),
1263                        key);
1264           g_error_free (key_file_error);
1265         }
1266       else
1267         g_propagate_error (error, key_file_error);
1268     }
1269
1270   return string_value;
1271 }
1272
1273 /**
1274  * g_key_file_set_string:
1275  * @key_file: a #GKeyFile
1276  * @group_name: a group name
1277  * @key: a key
1278  * @string: a string
1279  *
1280  * Associates a new string value with @key under @group_name.
1281  * If @key cannot be found then it is created. If @group_name
1282  * cannot be found then it is created as well.
1283  *
1284  * Since: 2.6
1285  **/
1286 void
1287 g_key_file_set_string (GKeyFile    *key_file,
1288                        const gchar *group_name,
1289                        const gchar *key,
1290                        const gchar *string)
1291 {
1292   gchar *value;
1293
1294   g_return_if_fail (key_file != NULL);
1295   g_return_if_fail (group_name != NULL);
1296   g_return_if_fail (key != NULL);
1297   g_return_if_fail (string != NULL);
1298
1299   value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1300   g_key_file_set_value (key_file, group_name, key, value);
1301   g_free (value);
1302 }
1303
1304 /**
1305  * g_key_file_get_string_list:
1306  * @key_file: a #GKeyFile
1307  * @group_name: a group name
1308  * @key: a key
1309  * @length: return location for the number of returned strings, or %NULL
1310  * @error: return location for a #GError, or %NULL
1311  *
1312  * Returns the values associated with @key under @group_name.
1313  * In the event the key cannot be found, %NULL is returned and
1314  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND.  In the
1315  * event that the @group_name cannot be found, %NULL is returned
1316  * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1317  *
1318  * Return value: a %NULL-terminated string array or %NULL if the specified 
1319  *   key cannot be found. The array should be freed with g_strfreev().
1320  *
1321  * Since: 2.6
1322  **/
1323 gchar **
1324 g_key_file_get_string_list (GKeyFile     *key_file,
1325                             const gchar  *group_name,
1326                             const gchar  *key,
1327                             gsize        *length,
1328                             GError      **error)
1329 {
1330   GError *key_file_error = NULL;
1331   gchar *value, *string_value, **values;
1332   gint i, len;
1333   GSList *p, *pieces = NULL;
1334
1335   g_return_val_if_fail (key_file != NULL, NULL);
1336   g_return_val_if_fail (group_name != NULL, NULL);
1337   g_return_val_if_fail (key != NULL, NULL);
1338
1339   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1340
1341   if (key_file_error)
1342     {
1343       g_propagate_error (error, key_file_error);
1344       return NULL;
1345     }
1346
1347   if (!g_utf8_validate (value, -1, NULL))
1348     {
1349       g_set_error (error, G_KEY_FILE_ERROR,
1350                    G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1351                    _("Key file contains key '%s' with value '%s' "
1352                      "which is not UTF-8"), key, value);
1353       g_free (value);
1354       return NULL;
1355     }
1356
1357   string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1358   g_free (value);
1359   g_free (string_value);
1360
1361   if (key_file_error)
1362     {
1363       if (g_error_matches (key_file_error,
1364                            G_KEY_FILE_ERROR,
1365                            G_KEY_FILE_ERROR_INVALID_VALUE))
1366         {
1367           g_set_error (error, G_KEY_FILE_ERROR,
1368                        G_KEY_FILE_ERROR_INVALID_VALUE,
1369                        _("Key file contains key '%s' "
1370                          "which has value that cannot be interpreted."),
1371                        key);
1372           g_error_free (key_file_error);
1373         }
1374       else
1375         g_propagate_error (error, key_file_error);
1376     }
1377
1378   len = g_slist_length (pieces);
1379   values = g_new0 (gchar *, len + 1); 
1380   for (p = pieces, i = 0; p; p = p->next)
1381     values[i++] = p->data;
1382   values[len] = NULL;
1383
1384   g_slist_free (pieces);
1385
1386   if (length)
1387     *length = len;
1388
1389   return values;
1390 }
1391
1392 /**
1393  * g_key_file_set_string_list:
1394  * @key_file: a #GKeyFile
1395  * @group_name: a group name
1396  * @key: a key
1397  * @list: an array of locale string values
1398  * @length: number of locale string values in @list
1399  *
1400  * Associates a list of string values for @key under @group_name.
1401  * If the @key cannot be found then it is created.
1402  *
1403  * Since: 2.6
1404  **/
1405 void
1406 g_key_file_set_string_list (GKeyFile            *key_file,
1407                             const gchar         *group_name,
1408                             const gchar         *key,
1409                             const gchar * const  list[],
1410                             gsize                length)
1411 {
1412   GString *value_list;
1413   gsize i;
1414
1415   g_return_if_fail (key_file != NULL);
1416   g_return_if_fail (group_name != NULL);
1417   g_return_if_fail (key != NULL);
1418   g_return_if_fail (list != NULL);
1419
1420   value_list = g_string_sized_new (length * 128);
1421   for (i = 0; list[i] != NULL && i < length; i++)
1422     {
1423       gchar *value;
1424
1425       value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1426       g_string_append (value_list, value);
1427       g_string_append_c (value_list, key_file->list_separator);
1428
1429       g_free (value);
1430     }
1431
1432   g_key_file_set_value (key_file, group_name, key, value_list->str);
1433   g_string_free (value_list, TRUE);
1434 }
1435
1436 /**
1437  * g_key_file_set_locale_string:
1438  * @key_file: a #GKeyFile
1439  * @group_name: a group name
1440  * @key: a key
1441  * @locale: a locale
1442  * @string: a string
1443  *
1444  * Associates a string value for @key and @locale under
1445  * @group_name.  If the translation for @key cannot be found 
1446  * then it is created.
1447  *
1448  * Since: 2.6
1449  **/
1450 void
1451 g_key_file_set_locale_string (GKeyFile     *key_file,
1452                               const gchar  *group_name,
1453                               const gchar  *key,
1454                               const gchar  *locale,
1455                               const gchar  *string)
1456 {
1457   gchar *full_key, *value;
1458
1459   g_return_if_fail (key_file != NULL);
1460   g_return_if_fail (group_name != NULL);
1461   g_return_if_fail (key != NULL);
1462   g_return_if_fail (locale != NULL);
1463   g_return_if_fail (string != NULL);
1464
1465   value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1466   full_key = g_strdup_printf ("%s[%s]", key, locale);
1467   g_key_file_set_value (key_file, group_name, full_key, value);
1468   g_free (full_key);
1469   g_free (value);
1470 }
1471
1472 extern GSList *_g_compute_locale_variants (const gchar *locale);
1473
1474 /**
1475  * g_key_file_get_locale_string:
1476  * @key_file: a #GKeyFile
1477  * @group_name: a group name
1478  * @key: a key
1479  * @locale: a locale or %NULL
1480  * @error: return location for a #GError, or %NULL
1481  *
1482  * Returns the value associated with @key under @group_name
1483  * translated in the given @locale if available.  If @locale is
1484  * %NULL then the current locale is assumed. If @key cannot be
1485  * found then %NULL is returned and @error is set to
1486  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1487  * with @key cannot be interpreted or no suitable translation can
1488  * be found then the untranslated value is returned and @error is
1489  * set to #G_KEY_FILE_ERROR_INVALID_VALUE and
1490  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND, respectively. In the
1491  * event that the @group_name cannot be found, %NULL is returned
1492  * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1493  *
1494  * Return value: a string or %NULL if the specified key cannot be
1495  *               found.
1496  * Since: 2.6
1497  **/
1498 gchar *
1499 g_key_file_get_locale_string (GKeyFile     *key_file,
1500                               const gchar  *group_name,
1501                               const gchar  *key,
1502                               const gchar  *locale,
1503                               GError      **error)
1504 {
1505   gchar *candidate_key, *translated_value;
1506   GError *key_file_error;
1507   gchar **languages;
1508   gboolean free_languages = FALSE;
1509   gint i;
1510
1511   g_return_val_if_fail (key_file != NULL, NULL);
1512   g_return_val_if_fail (group_name != NULL, NULL);
1513   g_return_val_if_fail (key != NULL, NULL);
1514
1515   candidate_key = NULL;
1516   translated_value = NULL;
1517   key_file_error = NULL;
1518
1519   if (!g_key_file_has_group (key_file, group_name))
1520     {
1521       g_set_error (error, G_KEY_FILE_ERROR,
1522                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1523                    _("Key file does not have group '%s'"),
1524                    group_name);
1525       return NULL;
1526     }
1527
1528   if (locale)
1529     {
1530       GSList *l, *list;
1531
1532       list = _g_compute_locale_variants (locale);
1533
1534       languages = g_new0 (gchar *, g_slist_length (list) + 1);
1535       for (l = list, i = 0; l; l = l->next, i++)
1536         languages[i] = l->data;
1537       languages[i] = NULL;
1538
1539       g_slist_free (list);
1540       free_languages = TRUE;
1541     }
1542   else
1543     {
1544       languages = (gchar **) g_get_language_names ();
1545       free_languages = FALSE;
1546     }
1547   
1548   for (i = 0; languages[i]; i++)
1549     {
1550       candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1551       
1552       translated_value = g_key_file_get_string (key_file,
1553                                                 group_name,
1554                                                 candidate_key, NULL);
1555       g_free (candidate_key);
1556
1557       if (translated_value)
1558         break;
1559    }
1560
1561   if (translated_value && !g_utf8_validate (translated_value, -1, NULL))
1562     {
1563       g_set_error (error, G_KEY_FILE_ERROR,
1564                    G_KEY_FILE_ERROR_INVALID_VALUE,
1565                    _("Key file contains key '%s' "
1566                      "which has value that cannot be interpreted."),
1567                    candidate_key);
1568       g_free (translated_value);
1569       translated_value = NULL;
1570   }
1571
1572   /* Fallback to untranslated key
1573    */
1574   if (!translated_value)
1575     {
1576       translated_value = g_key_file_get_string (key_file, group_name, key,
1577                                                 &key_file_error);
1578       
1579       if (!translated_value)
1580         g_propagate_error (error, key_file_error);
1581       else
1582         g_set_error (error, G_KEY_FILE_ERROR,
1583                      G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1584                      _("Key file contains no translated value "
1585                        "for key '%s' with locale '%s'."),
1586                      key, locale);
1587     }
1588
1589   if (free_languages)
1590     g_strfreev (languages);
1591
1592   return translated_value;
1593 }
1594
1595 /** 
1596  * g_key_file_get_locale_string_list: 
1597  * @key_file: a #GKeyFile
1598  * @group_name: a group name
1599  * @key: a key
1600  * @locale: a locale
1601  * @length: return location for the number of returned strings or %NULL
1602  * @error: return location for a #GError or %NULL
1603  *
1604  * Returns the values associated with @key under @group_name
1605  * translated in the given @locale if available.  If @locale is
1606  * %NULL then the current locale is assumed. If @key cannot be
1607  * found then %NULL is returned and @error is set to
1608  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1609  * with @key cannot be interpreted or no suitable translations
1610  * can be found then the untranslated values are returned and
1611  * @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE and
1612  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND, respectively. In the
1613  * event that the @group_name cannot be found, %NULL is returned
1614  * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1615  * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1616  *
1617  * Return value: a newly allocated %NULL-terminated string array
1618  *   or %NULL if the key isn't found. The string array should be freed
1619  *   with g_strfreev().
1620  *
1621  * Since: 2.6
1622  **/
1623 gchar **
1624 g_key_file_get_locale_string_list (GKeyFile     *key_file,
1625                                    const gchar  *group_name,
1626                                    const gchar  *key,
1627                                    const gchar  *locale,
1628                                    gsize        *length,
1629                                    GError      **error)
1630 {
1631   GError *key_file_error;
1632   gchar **values, *value;
1633
1634   g_return_val_if_fail (key_file != NULL, NULL);
1635   g_return_val_if_fail (group_name != NULL, NULL);
1636   g_return_val_if_fail (key != NULL, NULL);
1637
1638   key_file_error = NULL;
1639
1640   value = g_key_file_get_locale_string (key_file, group_name, 
1641                                         key, locale,
1642                                         &key_file_error);
1643   
1644   if (key_file_error)
1645     g_propagate_error (error, key_file_error);
1646   
1647   if (!value)
1648     return NULL;
1649
1650   if (value[strlen (value) - 1] == ';')
1651     value[strlen (value) - 1] = '\0';
1652
1653   values = g_strsplit (value, ";", 0);
1654
1655   g_free (value);
1656
1657   if (length)
1658     *length = g_strv_length (values);
1659
1660   return values;
1661 }
1662
1663 /**
1664  * g_key_file_set_locale_string_list:
1665  * @key_file: a #GKeyFile
1666  * @group_name: a group name
1667  * @key: a key
1668  * @locale: a locale
1669  * @list: a %NULL-terminated array of locale string values
1670  * @length: the length of @list
1671  *
1672  * Associates a list of string values for @key and @locale under
1673  * @group_name.  If the translation for @key cannot be found then
1674  * it is created.
1675  *
1676  * Since: 2.6
1677  **/
1678 void
1679 g_key_file_set_locale_string_list (GKeyFile            *key_file,
1680                                    const gchar         *group_name,
1681                                    const gchar         *key,
1682                                    const gchar         *locale,
1683                                    const gchar * const  list[],
1684                                    gsize                length)
1685 {
1686   GString *value_list;
1687   gchar *full_key;
1688   gsize i;
1689
1690   g_return_if_fail (key_file != NULL);
1691   g_return_if_fail (group_name != NULL);
1692   g_return_if_fail (key != NULL);
1693   g_return_if_fail (locale != NULL);
1694   g_return_if_fail (length != 0);
1695
1696   value_list = g_string_sized_new (length * 128);
1697   for (i = 0; list[i] != NULL && i < length; i++)
1698     {
1699       gchar *value;
1700       
1701       value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1702       
1703       g_string_append (value_list, value);
1704       g_string_append_c (value_list, ';');
1705
1706       g_free (value);
1707     }
1708
1709   full_key = g_strdup_printf ("%s[%s]", key, locale);
1710   g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1711   g_free (full_key);
1712   g_string_free (value_list, TRUE);
1713 }
1714
1715 /**
1716  * g_key_file_get_boolean:
1717  * @key_file: a #GKeyFile
1718  * @group_name: a group name
1719  * @key: a key
1720  * @error: return location for a #GError
1721  *
1722  * Returns the value associated with @key under @group_name as a
1723  * boolean.  If @key cannot be found then the return value is
1724  * undefined and @error is set to
1725  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1726  * associated with @key cannot be interpreted as a boolean then
1727  * the return value is also undefined and @error is set to
1728  * #G_KEY_FILE_ERROR_INVALID_VALUE.
1729  *
1730  * Return value: the value associated with the key as a boolean
1731  * Since: 2.6
1732  **/
1733 gboolean
1734 g_key_file_get_boolean (GKeyFile     *key_file,
1735                         const gchar  *group_name,
1736                         const gchar  *key,
1737                         GError      **error)
1738 {
1739   GError *key_file_error = NULL;
1740   gchar *value;
1741   gboolean bool_value;
1742
1743   g_return_val_if_fail (key_file != NULL, FALSE);
1744   g_return_val_if_fail (group_name != NULL, FALSE);
1745   g_return_val_if_fail (key != NULL, FALSE);
1746
1747   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1748
1749   if (!value)
1750     {
1751       g_propagate_error (error, key_file_error);
1752       return FALSE;
1753     }
1754
1755   bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1756                                                   &key_file_error);
1757   g_free (value);
1758
1759   if (key_file_error)
1760     {
1761       if (g_error_matches (key_file_error,
1762                            G_KEY_FILE_ERROR,
1763                            G_KEY_FILE_ERROR_INVALID_VALUE))
1764         {
1765           g_set_error (error, G_KEY_FILE_ERROR,
1766                        G_KEY_FILE_ERROR_INVALID_VALUE,
1767                        _("Key file contains key '%s' "
1768                          "which has value that cannot be interpreted."),
1769                        key);
1770           g_error_free (key_file_error);
1771         }
1772       else
1773         g_propagate_error (error, key_file_error);
1774     }
1775
1776   return bool_value;
1777 }
1778
1779 /**
1780  * g_key_file_set_boolean:
1781  * @key_file: a #GKeyFile
1782  * @group_name: a group name
1783  * @key: a key
1784  * @value: %TRUE or %FALSE
1785  *
1786  * Associates a new boolean value with @key under @group_name.
1787  * If @key cannot be found then it is created.
1788  *
1789  * Since: 2.6
1790  **/
1791 void
1792 g_key_file_set_boolean (GKeyFile    *key_file,
1793                         const gchar *group_name,
1794                         const gchar *key,
1795                         gboolean     value)
1796 {
1797   gchar *result;
1798
1799   g_return_if_fail (key_file != NULL);
1800   g_return_if_fail (group_name != NULL);
1801   g_return_if_fail (key != NULL);
1802
1803   result = g_key_file_parse_boolean_as_value (key_file, value);
1804   g_key_file_set_value (key_file, group_name, key, result);
1805   g_free (result);
1806 }
1807
1808 /**
1809  * g_key_file_get_boolean_list:
1810  * @key_file: a #GKeyFile
1811  * @group_name: a group name
1812  * @key: a key
1813  * @length: the number of booleans returned
1814  * @error: return location for a #GError
1815  *
1816  * Returns the values associated with @key under @group_name as
1817  * booleans.  If @key cannot be found then the return value is
1818  * undefined and @error is set to
1819  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values
1820  * associated with @key cannot be interpreted as booleans then
1821  * the return value is also undefined and @error is set to
1822  * #G_KEY_FILE_ERROR_INVALID_VALUE.
1823  *
1824  * Return value: the values associated with the key as a boolean
1825  **/
1826 gboolean *
1827 g_key_file_get_boolean_list (GKeyFile     *key_file,
1828                              const gchar  *group_name,
1829                              const gchar  *key,
1830                              gsize        *length,
1831                              GError      **error)
1832 {
1833   GError *key_file_error;
1834   gchar **values;
1835   gboolean *bool_values;
1836   gsize i, num_bools;
1837
1838   g_return_val_if_fail (key_file != NULL, NULL);
1839   g_return_val_if_fail (group_name != NULL, NULL);
1840   g_return_val_if_fail (key != NULL, NULL);
1841
1842   key_file_error = NULL;
1843
1844   values = g_key_file_get_string_list (key_file, group_name, key,
1845                                        &num_bools, &key_file_error);
1846
1847   if (key_file_error)
1848     g_propagate_error (error, key_file_error);
1849
1850   if (!values)
1851     return NULL;
1852
1853   bool_values = g_new0 (gboolean, num_bools);
1854
1855   for (i = 0; i < num_bools; i++)
1856     {
1857       bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1858                                                           values[i],
1859                                                           &key_file_error);
1860
1861       if (key_file_error)
1862         {
1863           g_propagate_error (error, key_file_error);
1864           g_strfreev (values);
1865           g_free (bool_values);
1866
1867           return NULL;
1868         }
1869     }
1870   g_strfreev (values);
1871
1872   if (length)
1873     *length = num_bools;
1874
1875   return bool_values;
1876 }
1877
1878 /**
1879  * g_key_file_set_boolean_list:
1880  * @key_file: a #GKeyFile
1881  * @group_name: a group name
1882  * @key: a key
1883  * @list: an array of boolean values
1884  * @length: length of @list
1885  *
1886  * Associates a list of boolean values with @key under
1887  * @group_name.  If @key cannot be found then it is created.
1888  *
1889  * Since: 2.6
1890  **/
1891 void
1892 g_key_file_set_boolean_list (GKeyFile    *key_file,
1893                              const gchar *group_name,
1894                              const gchar *key,
1895                              gboolean     list[],
1896                              gsize        length)
1897 {
1898   GString *value_list;
1899   gsize i;
1900
1901   g_return_if_fail (key_file != NULL);
1902   g_return_if_fail (group_name != NULL);
1903   g_return_if_fail (key != NULL);
1904   g_return_if_fail (list != NULL);
1905
1906   value_list = g_string_sized_new (length * 8);
1907   for (i = 0; i < length; i++)
1908     {
1909       gchar *value;
1910
1911       value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1912
1913       g_string_append (value_list, value);
1914       g_string_append_c (value_list, key_file->list_separator);
1915
1916       g_free (value);
1917     }
1918
1919   g_key_file_set_value (key_file, group_name, key, value_list->str);
1920   g_string_free (value_list, TRUE);
1921 }
1922
1923 /**
1924  * g_key_file_get_integer:
1925  * @key_file: a #GKeyFile
1926  * @group_name: a group name
1927  * @key: a key
1928  * @error: return location for a #GError
1929  *
1930  * Returns the value associated with @key under @group_name as an
1931  * integer. If @key cannot be found then the return value is
1932  * undefined and @error is set to
1933  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1934  * associated with @key cannot be interpreted as an integer then
1935  * the return value is also undefined and @error is set to
1936  * #G_KEY_FILE_ERROR_INVALID_VALUE.
1937  *
1938  * Return value: the value associated with the key as an integer.
1939  * Since: 2.6
1940  **/
1941 gint
1942 g_key_file_get_integer (GKeyFile     *key_file,
1943                         const gchar  *group_name,
1944                         const gchar  *key,
1945                         GError      **error)
1946 {
1947   GError *key_file_error;
1948   gchar *value;
1949   gint int_value;
1950
1951   g_return_val_if_fail (key_file != NULL, -1);
1952   g_return_val_if_fail (group_name != NULL, -1);
1953   g_return_val_if_fail (key != NULL, -1);
1954
1955   key_file_error = NULL;
1956
1957   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1958
1959   if (key_file_error)
1960     {
1961       g_propagate_error (error, key_file_error);
1962       return 0;
1963     }
1964
1965   int_value = g_key_file_parse_value_as_integer (key_file, value,
1966                                                  &key_file_error);
1967   g_free (value);
1968
1969   if (key_file_error)
1970     {
1971       if (g_error_matches (key_file_error,
1972                            G_KEY_FILE_ERROR,
1973                            G_KEY_FILE_ERROR_INVALID_VALUE))
1974         {
1975           g_set_error (error, G_KEY_FILE_ERROR,
1976                        G_KEY_FILE_ERROR_INVALID_VALUE,
1977                        _("Key file contains key '%s' in group '%s' "
1978                          "which has value that cannot be interpreted."), key, 
1979                        group_name);
1980           g_error_free (key_file_error);
1981         }
1982       else
1983         g_propagate_error (error, key_file_error);
1984     }
1985
1986   return int_value;
1987 }
1988
1989 /**
1990  * g_key_file_set_integer:
1991  * @key_file: a #GKeyFile
1992  * @group_name: a group name
1993  * @key: a key
1994  * @value: an integer value
1995  *
1996  * Associates a new integer value with @key under @group_name.
1997  * If @key cannot be found then it is created.
1998  *
1999  * Since: 2.6
2000  **/
2001 void
2002 g_key_file_set_integer (GKeyFile    *key_file,
2003                         const gchar *group_name,
2004                         const gchar *key,
2005                         gint         value)
2006 {
2007   gchar *result;
2008
2009   g_return_if_fail (key_file != NULL);
2010   g_return_if_fail (group_name != NULL);
2011   g_return_if_fail (key != NULL);
2012
2013   result = g_key_file_parse_integer_as_value (key_file, value);
2014   g_key_file_set_value (key_file, group_name, key, result);
2015   g_free (result);
2016 }
2017
2018 /**
2019  * g_key_file_get_integer_list:
2020  * @key_file: a #GKeyFile
2021  * @group_name: a group name
2022  * @key: a key
2023  * @length: the number of integers returned
2024  * @error: return location for a #GError
2025  *
2026  * Returns the values associated with @key under @group_name as
2027  * integers.  If @key cannot be found then the return value is
2028  * undefined and @error is set to
2029  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values
2030  * associated with @key cannot be interpreted as integers then
2031  * the return value is also undefined and @error is set to
2032  * #G_KEY_FILE_ERROR_INVALID_VALUE.
2033  *
2034  * Return value: the values associated with the key as a integer
2035  * Since: 2.6
2036  **/
2037 gint *
2038 g_key_file_get_integer_list (GKeyFile     *key_file,
2039                              const gchar  *group_name,
2040                              const gchar  *key,
2041                              gsize        *length,
2042                              GError      **error)
2043 {
2044   GError *key_file_error = NULL;
2045   gchar **values;
2046   gint *int_values;
2047   gsize i, num_ints;
2048
2049   g_return_val_if_fail (key_file != NULL, NULL);
2050   g_return_val_if_fail (group_name != NULL, NULL);
2051   g_return_val_if_fail (key != NULL, NULL);
2052
2053   values = g_key_file_get_string_list (key_file, group_name, key,
2054                                        &num_ints, &key_file_error);
2055
2056   if (key_file_error)
2057     g_propagate_error (error, key_file_error);
2058
2059   if (!values)
2060     return NULL;
2061
2062   int_values = g_new0 (gint, num_ints);
2063
2064   for (i = 0; i < num_ints; i++)
2065     {
2066       int_values[i] = g_key_file_parse_value_as_integer (key_file,
2067                                                          values[i],
2068                                                          &key_file_error);
2069
2070       if (key_file_error)
2071         {
2072           g_propagate_error (error, key_file_error);
2073           g_strfreev (values);
2074           g_free (int_values);
2075
2076           return NULL;
2077         }
2078     }
2079   g_strfreev (values);
2080
2081   if (length)
2082     *length = num_ints;
2083
2084   return int_values;
2085 }
2086
2087 /**
2088  * g_key_file_set_integer_list:
2089  * @key_file: a #GKeyFile
2090  * @group_name: a group name
2091  * @key: a key
2092  * @list: an array of integer values
2093  * @length: number of integer values in @list
2094  *
2095  * Associates a list of integer values with @key under
2096  * @group_name.  If @key cannot be found then it is created.
2097  *
2098  * Since: 2.6
2099  **/
2100 void
2101 g_key_file_set_integer_list (GKeyFile     *key_file,
2102                              const gchar  *group_name,
2103                              const gchar  *key,
2104                              gint          list[],
2105                              gsize         length)
2106 {
2107   GString *values;
2108   gsize i;
2109
2110   g_return_if_fail (key_file != NULL);
2111   g_return_if_fail (group_name != NULL);
2112   g_return_if_fail (key != NULL);
2113   g_return_if_fail (list != NULL);
2114
2115   values = g_string_sized_new (length * 16);
2116   for (i = 0; i < length; i++)
2117     {
2118       gchar *value;
2119
2120       value = g_key_file_parse_integer_as_value (key_file, list[i]);
2121
2122       g_string_append (values, value);
2123       g_string_append_c (values, ';');
2124
2125       g_free (value);
2126     }
2127
2128   g_key_file_set_value (key_file, group_name, key, values->str);
2129   g_string_free (values, TRUE);
2130 }
2131
2132 static void
2133 g_key_file_set_key_comment (GKeyFile             *key_file,
2134                             const gchar          *group_name,
2135                             const gchar          *key,
2136                             const gchar          *comment,
2137                             GError              **error)
2138 {
2139   GKeyFileGroup *group;
2140   GKeyFileKeyValuePair *pair;
2141   GList *key_node, *comment_node, *tmp;
2142   
2143   group = g_key_file_lookup_group (key_file, group_name);
2144   if (!group)
2145     {
2146       g_set_error (error, G_KEY_FILE_ERROR,
2147                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2148                    _("Key file does not have group '%s'"),
2149                    group_name);
2150
2151       return;
2152     }
2153
2154   /* First find the key the comments are supposed to be
2155    * associated with
2156    */
2157   key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2158
2159   if (key_node == NULL)
2160     {
2161       g_set_error (error, G_KEY_FILE_ERROR,
2162                    G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2163                    _("Key file does not have key '%s' in group '%s'"),
2164                    key, group_name);
2165       return;
2166     }
2167
2168   /* Then find all the comments already associated with the
2169    * key and free them
2170    */
2171   tmp = key_node->next;
2172   while (tmp != NULL)
2173     {
2174       GKeyFileKeyValuePair *pair;
2175
2176       pair = (GKeyFileKeyValuePair *) tmp->data;
2177
2178       if (pair->key != NULL)
2179         break;
2180
2181       comment_node = tmp;
2182       tmp = tmp->next;
2183       g_key_file_remove_key_value_pair_node (key_file, group,
2184                                              comment_node); 
2185     }
2186
2187   if (comment == NULL)
2188     return;
2189
2190   /* Now we can add our new comment
2191    */
2192   pair = g_new0 (GKeyFileKeyValuePair, 1);
2193   
2194   pair->key = NULL;
2195   pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2196   
2197   key_node = g_list_insert (key_node, pair, 1);
2198 }
2199
2200 static void
2201 g_key_file_set_group_comment (GKeyFile             *key_file,
2202                               const gchar          *group_name,
2203                               const gchar          *comment,
2204                               GError              **error)
2205 {
2206   GKeyFileGroup *group;
2207   
2208   group = g_key_file_lookup_group (key_file, group_name);
2209   if (!group)
2210     {
2211       g_set_error (error, G_KEY_FILE_ERROR,
2212                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2213                    _("Key file does not have group '%s'"),
2214                    group_name);
2215
2216       return;
2217     }
2218
2219   /* First remove any existing comment
2220    */
2221   if (group->comment)
2222     {
2223       g_key_file_key_value_pair_free (group->comment);
2224       group->comment = NULL;
2225     }
2226
2227   if (comment == NULL)
2228     return;
2229
2230   /* Now we can add our new comment
2231    */
2232   group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2233   
2234   group->comment->key = NULL;
2235   group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2236 }
2237
2238 static void
2239 g_key_file_set_top_comment (GKeyFile             *key_file,
2240                             const gchar          *comment,
2241                             GError              **error)
2242 {
2243   GList *group_node;
2244   GKeyFileGroup *group;
2245   GKeyFileKeyValuePair *pair;
2246
2247   /* The last group in the list should be the top (comments only)
2248    * group in the file
2249    */
2250   g_assert (key_file->groups != NULL);
2251   group_node = g_list_last (key_file->groups);
2252   group = (GKeyFileGroup *) group_node->data;
2253   g_assert (group->name == NULL);
2254
2255   /* Note all keys must be comments at the top of
2256    * the file, so we can just free it all.
2257    */
2258   if (group->key_value_pairs != NULL)
2259     {
2260       g_list_foreach (group->key_value_pairs, 
2261                       (GFunc) g_key_file_key_value_pair_free, 
2262                       NULL);
2263       g_list_free (group->key_value_pairs);
2264       group->key_value_pairs = NULL;
2265     }
2266
2267   if (comment == NULL)
2268      return;
2269
2270   pair = g_new0 (GKeyFileKeyValuePair, 1);
2271   
2272   pair->key = NULL;
2273   pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2274   
2275   group->key_value_pairs =
2276     g_list_prepend (group->key_value_pairs, pair);
2277 }
2278
2279 /**
2280  * g_key_file_set_comment:
2281  * @key_file: a #GKeyFile
2282  * @group_name: a group name
2283  * @key: a key
2284  * @comment: a comment
2285  * @error: return location for a #GError
2286  *
2287  * Places a comment above @key from @group_name.
2288  * @group_name. If @key is %NULL then @comment will
2289  * be written above @group_name.  If both @key
2290  * and @group_name are NULL, then @comment will
2291  * be written above the first group in the file.
2292  *
2293  * Since: 2.6
2294  **/
2295 void
2296 g_key_file_set_comment (GKeyFile             *key_file,
2297                         const gchar          *group_name,
2298                         const gchar          *key,
2299                         const gchar          *comment,
2300                         GError              **error)
2301 {
2302   g_return_if_fail (key_file != NULL);
2303
2304   if (group_name != NULL && key != NULL)
2305     g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2306   else if (group_name != NULL)
2307     g_key_file_set_group_comment (key_file, group_name, comment, error);
2308   else
2309     g_key_file_set_top_comment (key_file, comment, error);
2310
2311   if (comment != NULL)
2312     key_file->approximate_size += strlen (comment);
2313 }
2314
2315 static gchar *
2316 g_key_file_get_key_comment (GKeyFile             *key_file,
2317                             const gchar          *group_name,
2318                             const gchar          *key,
2319                             GError              **error)
2320 {
2321   GKeyFileGroup *group;
2322   GList *key_node, *tmp;
2323   GString *string;
2324   gchar *comment;
2325
2326   group = g_key_file_lookup_group (key_file, group_name);
2327   if (!group)
2328     {
2329       g_set_error (error, G_KEY_FILE_ERROR,
2330                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2331                    _("Key file does not have group '%s'"),
2332                    group_name);
2333
2334       return NULL;
2335     }
2336
2337   /* First find the key the comments are supposed to be
2338    * associated with
2339    */
2340   key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2341
2342   if (key_node == NULL)
2343     {
2344       g_set_error (error, G_KEY_FILE_ERROR,
2345                    G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2346                    _("Key file does not have key '%s' in group '%s'"),
2347                    key, group_name);
2348       return NULL;
2349     }
2350
2351   string = NULL;
2352
2353   /* Then find all the comments already associated with the
2354    * key and concatentate them.
2355    */
2356   tmp = key_node->next;
2357   while (tmp != NULL)
2358     {
2359       GKeyFileKeyValuePair *pair;
2360
2361       pair = (GKeyFileKeyValuePair *) tmp->data;
2362
2363       if (pair->key != NULL)
2364         break;
2365       
2366       if (string == NULL)
2367         string = g_string_sized_new (512);
2368
2369       comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2370       g_string_append (string, comment);
2371       g_free (comment);
2372
2373       tmp = tmp->next;
2374     }
2375
2376   if (string != NULL)
2377     {
2378       comment = string->str;
2379       g_string_free (string, FALSE);
2380     }
2381   else
2382     comment = NULL;
2383
2384   return comment;
2385 }
2386
2387 static gchar *
2388 g_key_file_get_group_comment (GKeyFile             *key_file,
2389                               const gchar          *group_name,
2390                               GError              **error)
2391 {
2392   GKeyFileGroup *group;
2393   
2394   group = g_key_file_lookup_group (key_file, group_name);
2395   if (!group)
2396     {
2397       g_set_error (error, G_KEY_FILE_ERROR,
2398                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2399                    _("Key file does not have group '%s'"),
2400                    group_name);
2401
2402       return NULL;
2403     }
2404
2405   if (group->comment)
2406     return g_strdup (group->comment->value);
2407
2408   return NULL;
2409 }
2410
2411 static gchar *
2412 g_key_file_get_top_comment (GKeyFile             *key_file,
2413                             GError              **error)
2414 {
2415   GList *group_node, *tmp;
2416   GKeyFileGroup *group;
2417   GString *string;
2418   gchar *comment;
2419
2420   /* The last group in the list should be the top (comments only)
2421    * group in the file
2422    */
2423   g_assert (key_file->groups != NULL);
2424   group_node = g_list_last (key_file->groups);
2425   group = (GKeyFileGroup *) group_node->data;
2426   g_assert (group->name == NULL);
2427
2428   string = NULL;
2429
2430   /* Then find all the comments already associated with the
2431    * key and concatentate them.
2432    */
2433   tmp = group->key_value_pairs;
2434   while (tmp != NULL)
2435     {
2436       GKeyFileKeyValuePair *pair;
2437
2438       pair = (GKeyFileKeyValuePair *) tmp->data;
2439
2440       if (pair->key != NULL)
2441         break;
2442       
2443       if (string == NULL)
2444         string = g_string_sized_new (512);
2445
2446       comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2447       g_string_append (string, comment);
2448       g_free (comment);
2449
2450       tmp = tmp->next;
2451     }
2452
2453   if (string != NULL)
2454     {
2455       comment = string->str;
2456       g_string_free (string, FALSE);
2457     }
2458   else
2459     comment = NULL;
2460
2461   return comment;
2462 }
2463
2464 /**
2465  * g_key_file_get_comment:
2466  * @key_file: a #GKeyFile
2467  * @group_name: a group name
2468  * @key: a key
2469  * @error: return location for a #GError
2470  *
2471  * Retreives a comment above @key from @group_name.
2472  * @group_name. If @key is %NULL then @comment will
2473  * be read from above @group_name.  If both @key
2474  * and @group_name are NULL, then @comment will
2475  * be read from above the first group in the file.
2476  *
2477  * Since: 2.6
2478  * Returns: a comment that should be freed with g_free()
2479  **/
2480 gchar * 
2481 g_key_file_get_comment (GKeyFile             *key_file,
2482                         const gchar          *group_name,
2483                         const gchar          *key,
2484                         GError              **error)
2485 {
2486   g_return_val_if_fail (key_file != NULL, NULL);
2487
2488   if (group_name != NULL && key != NULL)
2489     return g_key_file_get_key_comment (key_file, group_name, key, error);
2490   else if (group_name != NULL)
2491     return g_key_file_get_group_comment (key_file, group_name, error);
2492   else
2493     return g_key_file_get_top_comment (key_file, error);
2494 }
2495
2496 /**
2497  * g_key_file_remove_comment:
2498  * @key_file: a #GKeyFile
2499  * @group_name: a group name
2500  * @key: a key
2501  * @error: return location for a #GError
2502  *
2503  * Removes a comment above @key from @group_name.
2504  * @group_name. If @key is %NULL then @comment will
2505  * be written above @group_name.  If both @key
2506  * and @group_name are NULL, then @comment will
2507  * be written above the first group in the file.
2508  *
2509  * Since: 2.6
2510  **/
2511
2512 void
2513 g_key_file_remove_comment (GKeyFile             *key_file,
2514                            const gchar          *group_name,
2515                            const gchar          *key,
2516                            GError              **error)
2517 {
2518   g_return_if_fail (key_file != NULL);
2519
2520   if (group_name != NULL && key != NULL)
2521     g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2522   else if (group_name != NULL)
2523     g_key_file_set_group_comment (key_file, group_name, NULL, error);
2524   else
2525     g_key_file_set_top_comment (key_file, NULL, error);
2526 }
2527
2528 /**
2529  * g_key_file_has_group:
2530  * @key_file: a #GKeyFile
2531  * @group_name: a group name
2532  *
2533  * Looks whether the key file has the group @group_name.
2534  *
2535  * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2536  * otherwise.
2537  * Since: 2.6
2538  **/
2539 gboolean
2540 g_key_file_has_group (GKeyFile    *key_file,
2541                       const gchar *group_name)
2542 {
2543   GList *tmp;
2544   GKeyFileGroup *group;
2545
2546   g_return_val_if_fail (key_file != NULL, FALSE);
2547   g_return_val_if_fail (group_name != NULL, FALSE);
2548
2549   for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2550     {
2551       group = (GKeyFileGroup *) tmp->data;
2552       if (group && group->name && (strcmp (group->name, group_name) == 0))
2553         return TRUE;
2554     }
2555
2556   return FALSE;
2557 }
2558
2559 /**
2560  * g_key_file_has_key:
2561  * @key_file: a #GKeyFile
2562  * @group_name: a group name
2563  * @key: a key name
2564  * @error: return location for a #GError
2565  *
2566  * Looks whether the key file has the key @key in the group
2567  * @group_name.
2568  *
2569  * Return value: %TRUE if @key is a part of @group_name, %FALSE
2570  * otherwise.
2571  * Since: 2.6
2572  **/
2573 gboolean
2574 g_key_file_has_key (GKeyFile     *key_file,
2575                     const gchar  *group_name,
2576                     const gchar  *key,
2577                     GError      **error)
2578 {
2579   GKeyFileKeyValuePair *pair;
2580   GKeyFileGroup *group;
2581
2582   g_return_val_if_fail (key_file != NULL, FALSE);
2583   g_return_val_if_fail (key != NULL, FALSE);
2584   g_return_val_if_fail (group_name != NULL, FALSE);
2585
2586   group = g_key_file_lookup_group (key_file, group_name);
2587
2588   if (!group)
2589     {
2590       g_set_error (error, G_KEY_FILE_ERROR,
2591                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2592                    _("Key file does not have group '%s'"),
2593                    group_name);
2594
2595       return FALSE;
2596     }
2597
2598   pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2599
2600   return pair != NULL;
2601 }
2602
2603 static void
2604 g_key_file_add_group (GKeyFile    *key_file,
2605                       const gchar *group_name)
2606 {
2607   GKeyFileGroup *group;
2608
2609   g_return_if_fail (key_file != NULL);
2610   g_return_if_fail (group_name != NULL);
2611   g_return_if_fail (g_key_file_lookup_group (key_file, group_name) == NULL);
2612
2613   group = g_new0 (GKeyFileGroup, 1);
2614   group->name = g_strdup (group_name);
2615   group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2616   key_file->groups = g_list_prepend (key_file->groups, group);
2617   key_file->approximate_size += strlen (group_name) + 3;
2618   key_file->current_group = group;
2619
2620   if (key_file->start_group_name == NULL)
2621     key_file->start_group_name = g_strdup (group_name);
2622 }
2623
2624 static void
2625 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2626 {
2627   if (pair != NULL)
2628     {
2629       g_free (pair->key);
2630       g_free (pair->value);
2631       g_free (pair);
2632     }
2633 }
2634
2635 /* Be careful not to call this function on a node with data in the
2636  * lookup map without removing it from the lookup map, first.
2637  *
2638  * Some current cases where this warning is not a concern are
2639  * when:
2640  *   - the node being removed is a comment node
2641  *   - the entire lookup map is getting destroyed soon after
2642  *     anyway.
2643  */ 
2644 static void
2645 g_key_file_remove_key_value_pair_node (GKeyFile      *key_file,
2646                                        GKeyFileGroup *group,
2647                                        GList         *pair_node)
2648 {
2649
2650   GKeyFileKeyValuePair *pair;
2651
2652   pair = (GKeyFileKeyValuePair *) pair_node->data;
2653
2654   group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2655
2656   if (pair->key != NULL)
2657     key_file->approximate_size -= strlen (pair->key) + 1;
2658
2659   g_assert (pair->value != NULL);
2660   key_file->approximate_size -= strlen (pair->value);
2661
2662   g_key_file_key_value_pair_free (pair);
2663
2664   g_list_free_1 (pair_node);
2665 }
2666
2667 static void
2668 g_key_file_remove_group_node (GKeyFile *key_file,
2669                               GList    *group_node)
2670 {
2671   GKeyFileGroup *group;
2672   GList *tmp;
2673
2674   group = (GKeyFileGroup *) group_node->data;
2675
2676   /* If the current group gets deleted make the current group the first
2677    * group.
2678    */
2679   if (key_file->current_group == group)
2680     {
2681       /* groups should always contain at least the top comment group,
2682        * unless g_key_file_clear has been called
2683        */
2684       if (key_file->groups)
2685         key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2686       else
2687         key_file->current_group = NULL;
2688     }
2689
2690   key_file->groups = g_list_remove_link (key_file->groups, group_node);
2691
2692   if (group->name != NULL)
2693     key_file->approximate_size -= strlen (group->name) + 3;
2694
2695   tmp = group->key_value_pairs;
2696   while (tmp != NULL)
2697     {
2698       GList *pair_node;
2699
2700       pair_node = tmp;
2701       tmp = tmp->next;
2702       g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2703     }
2704
2705   g_assert (group->key_value_pairs == NULL);
2706
2707   if (group->lookup_map)
2708     {
2709       g_hash_table_destroy (group->lookup_map);
2710       group->lookup_map = NULL;
2711     }
2712
2713   g_free ((gchar *) group->name);
2714   g_free (group);
2715   g_list_free_1 (group_node);
2716 }
2717
2718 /**
2719  * g_key_file_remove_group:
2720  * @key_file: a #GKeyFile
2721  * @group_name: a group name
2722  * @error: return location for a #GError or %NULL
2723  *
2724  * Removes the specified group, @group_name, 
2725  * from the key file. 
2726  *
2727  * Since: 2.6
2728  **/
2729 void
2730 g_key_file_remove_group (GKeyFile     *key_file,
2731                          const gchar  *group_name,
2732                          GError      **error)
2733 {
2734   GList *group_node;
2735
2736   g_return_if_fail (key_file != NULL);
2737   g_return_if_fail (group_name != NULL);
2738
2739   group_node = g_key_file_lookup_group_node (key_file, group_name);
2740
2741   if (!group_node)
2742     g_set_error (error, G_KEY_FILE_ERROR,
2743                  G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2744                  _("Key file does not have group '%s'"),
2745                  group_name);
2746
2747   g_key_file_remove_group_node (key_file, group_node);
2748 }
2749
2750 static void
2751 g_key_file_add_key (GKeyFile    *key_file,
2752                     const gchar *group_name,
2753                     const gchar *key,
2754                     const gchar *value)
2755 {
2756   GKeyFileGroup *group;
2757   GKeyFileKeyValuePair *pair;
2758
2759   group = g_key_file_lookup_group (key_file, group_name);
2760
2761   if (!group)
2762     {
2763       g_key_file_add_group (key_file, group_name);
2764       group = (GKeyFileGroup *) key_file->groups->data;
2765     }
2766
2767   pair = g_new0 (GKeyFileKeyValuePair, 1);
2768
2769   pair->key = g_strdup (key);
2770   pair->value = g_strdup (value);
2771
2772   g_hash_table_replace (group->lookup_map, pair->key, pair);
2773   group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
2774   key_file->approximate_size += strlen (key) + strlen (value) + 2;
2775 }
2776
2777 /**
2778  * g_key_file_remove_key:
2779  * @key_file: a #GKeyFile
2780  * @group_name: a group name
2781  * @key: a key name to remove
2782  * @error: return location for a #GError or %NULL
2783  *
2784  * Removes @key in @group_name from the key file. 
2785  *
2786  * Since: 2.6
2787  **/
2788 void
2789 g_key_file_remove_key (GKeyFile     *key_file,
2790                        const gchar  *group_name,
2791                        const gchar  *key,
2792                        GError      **error)
2793 {
2794   GKeyFileGroup *group;
2795   GKeyFileKeyValuePair *pair;
2796
2797   g_return_if_fail (key_file != NULL);
2798   g_return_if_fail (group_name != NULL);
2799   g_return_if_fail (key != NULL);
2800
2801   pair = NULL;
2802
2803   if (group_name == NULL)
2804     group = key_file->current_group;
2805   else
2806     group = g_key_file_lookup_group (key_file, group_name);
2807
2808   if (!group)
2809     {
2810       g_set_error (error, G_KEY_FILE_ERROR,
2811                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2812                    _("Key file does not have group '%s'"),
2813                    group_name);
2814       return;
2815     }
2816
2817   group->key_value_pairs = g_list_remove (group->key_value_pairs, key_file);
2818   pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2819
2820   if (!pair)
2821     {
2822       g_set_error (error, G_KEY_FILE_ERROR,
2823                    G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2824                    _("Key file does not have key '%s' in group '%s'"), key, group_name);
2825       return;
2826     }
2827
2828   g_hash_table_remove (group->lookup_map, pair->key);
2829
2830   key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
2831   g_key_file_key_value_pair_free (pair);
2832 }
2833
2834 static GList *
2835 g_key_file_lookup_group_node (GKeyFile    *key_file,
2836                               const gchar *group_name)
2837 {
2838   GKeyFileGroup *group;
2839   GList *tmp;
2840
2841   group = NULL;
2842   for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2843     {
2844       group = (GKeyFileGroup *) tmp->data;
2845
2846       if (group && group->name && strcmp (group->name, group_name) == 0)
2847         break;
2848
2849       group = NULL;
2850     }
2851
2852   return tmp;
2853 }
2854
2855 static GKeyFileGroup *
2856 g_key_file_lookup_group (GKeyFile    *key_file,
2857                          const gchar *group_name)
2858 {
2859   GList *group_node;
2860
2861   group_node = g_key_file_lookup_group_node (key_file, group_name);
2862
2863   if (group_node != NULL)
2864     return (GKeyFileGroup *) group_node->data; 
2865
2866   return NULL;
2867 }
2868
2869 static GList *
2870 g_key_file_lookup_key_value_pair_node (GKeyFile       *key_file,
2871                                        GKeyFileGroup  *group,
2872                                        const gchar    *key)
2873 {
2874   GList *key_node;
2875
2876   for (key_node = group->key_value_pairs;
2877        key_node != NULL;
2878        key_node = key_node->next)
2879     {
2880       GKeyFileKeyValuePair *pair;
2881
2882       pair = (GKeyFileKeyValuePair *) key_node->data; 
2883
2884       if (pair->key && strcmp (pair->key, key) == 0)
2885         break;
2886     }
2887
2888   return key_node;
2889 }
2890
2891 static GKeyFileKeyValuePair *
2892 g_key_file_lookup_key_value_pair (GKeyFile      *key_file,
2893                                   GKeyFileGroup *group,
2894                                   const gchar   *key)
2895 {
2896   return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
2897 }
2898
2899 /* Lines starting with # or consisting entirely of whitespace are merely
2900  * recorded, not parsed. This function assumes all leading whitespace
2901  * has been stripped.
2902  */
2903 static gboolean
2904 g_key_file_line_is_comment (const gchar *line)
2905 {
2906   return (*line == '#' || *line == '\0' || *line == '\n');
2907 }
2908
2909 /* A group in a key file is made up of a starting '[' followed by one
2910  * or more letters making up the group name followed by ']'.
2911  */
2912 static gboolean
2913 g_key_file_line_is_group (const gchar *line)
2914 {
2915   gchar *p;
2916
2917   p = (gchar *) line;
2918   if (*p != '[')
2919     return FALSE;
2920
2921   p = g_utf8_next_char (p);
2922
2923   if (!*p)
2924     return FALSE;
2925
2926   p = g_utf8_next_char (p);
2927
2928   /* Group name must be non-empty
2929    */
2930   if (*p == ']')
2931     return FALSE;
2932
2933   while (*p && *p != ']')
2934     p = g_utf8_next_char (p);
2935
2936   if (!*p)
2937     return FALSE;
2938
2939   return TRUE;
2940 }
2941
2942 static gboolean
2943 g_key_file_line_is_key_value_pair (const gchar *line)
2944 {
2945   gchar *p;
2946
2947   p = (gchar *) g_utf8_strchr (line, -1, '=');
2948
2949   if (!p)
2950     return FALSE;
2951
2952   /* Key must be non-empty
2953    */
2954   if (*p == line[0])
2955     return FALSE;
2956
2957   return TRUE;
2958 }
2959
2960 static gchar *
2961 g_key_file_parse_value_as_string (GKeyFile     *key_file,
2962                                   const gchar  *value,
2963                                   GSList      **pieces,
2964                                   GError      **error)
2965 {
2966   GError *parse_error = NULL;
2967   gchar *string_value, *p, *q0, *q;
2968
2969   string_value = g_new0 (gchar, strlen (value) + 1);
2970
2971   p = (gchar *) value;
2972   q0 = q = string_value;
2973   while (*p)
2974     {
2975       if (*p == '\\')
2976         {
2977           p++;
2978
2979           switch (*p)
2980             {
2981             case 's':
2982               *q = ' ';
2983               break;
2984
2985             case 'n':
2986               *q = '\n';
2987               break;
2988
2989             case 't':
2990               *q = '\t';
2991               break;
2992
2993             case 'r':
2994               *q = '\r';
2995               break;
2996
2997             case '\\':
2998               *q = '\\';
2999               break;
3000
3001             default:
3002               if (pieces && *p == key_file->list_separator)
3003                 *q = key_file->list_separator;
3004               else
3005                 {
3006                   *q++ = '\\';
3007                   *q = *p;
3008                   
3009                   if (parse_error == NULL)
3010                     {
3011                       gchar sequence[3];
3012                       
3013                       sequence[0] = '\\';
3014                       sequence[1] = *p;
3015                       sequence[2] = '\0';
3016                       
3017                       g_set_error (error, G_KEY_FILE_ERROR,
3018                                    G_KEY_FILE_ERROR_INVALID_VALUE,
3019                                    _("Key file contains invalid escape "
3020                                      "sequence '%s'"), sequence);
3021                     }
3022                 }
3023               break;
3024             }
3025         }
3026       else
3027         {
3028           *q = *p;
3029           if (pieces && (*p == key_file->list_separator))
3030             {
3031               *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3032               q0 = q + 1; 
3033             }
3034         }
3035
3036       q++;
3037       p++;
3038     }
3039
3040   if (p[-1] == '\\' && error == NULL)
3041     g_set_error (error, G_KEY_FILE_ERROR,
3042                  G_KEY_FILE_ERROR_INVALID_VALUE,
3043                  _("Key file contains escape character at end of line"));
3044
3045   *q = '\0';
3046   if (pieces)
3047   {
3048     if (q0 < q)
3049       *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3050     *pieces = g_slist_reverse (*pieces);
3051   }
3052
3053   return string_value;
3054 }
3055
3056 static gchar *
3057 g_key_file_parse_string_as_value (GKeyFile    *key_file,
3058                                   const gchar *string,
3059                                   gboolean     escape_separator)
3060 {
3061   gchar *value, *p, *q;
3062   gsize length;
3063
3064   length = strlen (string) + 1;
3065
3066   /* Worst case would be that every character needs to be escaped.
3067    * In other words every character turns to two characters
3068    */
3069   value = g_new0 (gchar, 2 * length);
3070
3071   p = (gchar *) string;
3072   q = value;
3073   while (p < (string + length - 1))
3074     {
3075       gchar escaped_character[3] = { '\\', 0, 0 };
3076
3077       switch (*p)
3078         {
3079         case ' ':
3080           escaped_character[1] = 's';
3081           strcpy (q, escaped_character);
3082           q += 2;
3083           break;
3084         case '\n':
3085           escaped_character[1] = 'n';
3086           strcpy (q, escaped_character);
3087           q += 2;
3088           break;
3089         case '\t':
3090           escaped_character[1] = 't';
3091           strcpy (q, escaped_character);
3092           q += 2;
3093           break;
3094         case '\r':
3095           escaped_character[1] = 'r';
3096           strcpy (q, escaped_character);
3097           q += 2;
3098           break;
3099         case '\\':
3100           escaped_character[1] = '\\';
3101           strcpy (q, escaped_character);
3102           q += 2;
3103           break;
3104         default:
3105           if (escape_separator && *p == key_file->list_separator)
3106             {
3107               escaped_character[1] = key_file->list_separator;
3108               strcpy (q, escaped_character);
3109               q += 2;
3110             }
3111           else 
3112             {
3113               *q = *p;
3114               q++;
3115             }
3116           break;
3117         }
3118       p++;
3119     }
3120   *q = '\0';
3121
3122   return value;
3123 }
3124
3125 static gint
3126 g_key_file_parse_value_as_integer (GKeyFile     *key_file,
3127                                    const gchar  *value,
3128                                    GError      **error)
3129 {
3130   gchar *end_of_valid_int;
3131   gint int_value = 0;
3132
3133   int_value = strtol (value, &end_of_valid_int, 0);
3134
3135   if (*end_of_valid_int != '\0')
3136     g_set_error (error, G_KEY_FILE_ERROR,
3137                  G_KEY_FILE_ERROR_INVALID_VALUE,
3138                  _("Value '%s' cannot be interpreted as a number."), value);
3139
3140   return int_value;
3141 }
3142
3143 static gchar *
3144 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3145                                    gint      value)
3146
3147 {
3148   return g_strdup_printf ("%d", value);
3149 }
3150
3151 static gboolean
3152 g_key_file_parse_value_as_boolean (GKeyFile     *key_file,
3153                                    const gchar  *value,
3154                                    GError      **error)
3155 {
3156   if (value)
3157     {
3158       if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3159         return TRUE;
3160       else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3161         return FALSE;
3162     }
3163
3164   g_set_error (error, G_KEY_FILE_ERROR,
3165                G_KEY_FILE_ERROR_INVALID_VALUE,
3166                _("Value '%s' cannot be interpreted as a boolean."), value);
3167
3168   return FALSE;
3169 }
3170
3171 static gchar *
3172 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3173                                    gboolean  value)
3174 {
3175   if (value)
3176     return g_strdup ("true");
3177   else
3178     return g_strdup ("false");
3179 }
3180
3181 static gchar *
3182 g_key_file_parse_value_as_comment (GKeyFile    *key_file,
3183                                    const gchar *value)
3184 {
3185   GString *string;
3186   gchar **lines, *comment;
3187   gsize i;
3188
3189   string = g_string_sized_new (512);
3190
3191   lines = g_strsplit (value, "\n", 0);
3192
3193   for (i = 0; lines[i] != NULL; i++)
3194     {
3195         if (lines[i][0] != '#')
3196            g_string_append_printf (string, "%s\n", lines[i]);
3197         else 
3198            g_string_append_printf (string, "%s\n", lines[i] + 1);
3199     }
3200   g_strfreev (lines);
3201
3202   comment = string->str;
3203
3204   g_string_free (string, FALSE);
3205
3206   return comment;
3207 }
3208
3209 static gchar *
3210 g_key_file_parse_comment_as_value (GKeyFile      *key_file,
3211                                    const gchar   *comment)
3212 {
3213   GString *string;
3214   gchar **lines, *value;
3215   gsize i;
3216
3217   string = g_string_sized_new (512);
3218
3219   lines = g_strsplit (comment, "\n", 0);
3220
3221   for (i = 0; lines[i] != NULL; i++)
3222     g_string_append_printf (string, "#%s%s", lines[i], 
3223                             lines[i + 1] == NULL? "" : "\n");
3224   g_strfreev (lines);
3225
3226   value = string->str;
3227
3228   g_string_free (string, FALSE);
3229
3230   return value;
3231 }
3232
3233