Add a missing "Since: 2.6" comment.
[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  * Since: 2.6
1827  **/
1828 gboolean *
1829 g_key_file_get_boolean_list (GKeyFile     *key_file,
1830                              const gchar  *group_name,
1831                              const gchar  *key,
1832                              gsize        *length,
1833                              GError      **error)
1834 {
1835   GError *key_file_error;
1836   gchar **values;
1837   gboolean *bool_values;
1838   gsize i, num_bools;
1839
1840   g_return_val_if_fail (key_file != NULL, NULL);
1841   g_return_val_if_fail (group_name != NULL, NULL);
1842   g_return_val_if_fail (key != NULL, NULL);
1843
1844   key_file_error = NULL;
1845
1846   values = g_key_file_get_string_list (key_file, group_name, key,
1847                                        &num_bools, &key_file_error);
1848
1849   if (key_file_error)
1850     g_propagate_error (error, key_file_error);
1851
1852   if (!values)
1853     return NULL;
1854
1855   bool_values = g_new0 (gboolean, num_bools);
1856
1857   for (i = 0; i < num_bools; i++)
1858     {
1859       bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1860                                                           values[i],
1861                                                           &key_file_error);
1862
1863       if (key_file_error)
1864         {
1865           g_propagate_error (error, key_file_error);
1866           g_strfreev (values);
1867           g_free (bool_values);
1868
1869           return NULL;
1870         }
1871     }
1872   g_strfreev (values);
1873
1874   if (length)
1875     *length = num_bools;
1876
1877   return bool_values;
1878 }
1879
1880 /**
1881  * g_key_file_set_boolean_list:
1882  * @key_file: a #GKeyFile
1883  * @group_name: a group name
1884  * @key: a key
1885  * @list: an array of boolean values
1886  * @length: length of @list
1887  *
1888  * Associates a list of boolean values with @key under
1889  * @group_name.  If @key cannot be found then it is created.
1890  *
1891  * Since: 2.6
1892  **/
1893 void
1894 g_key_file_set_boolean_list (GKeyFile    *key_file,
1895                              const gchar *group_name,
1896                              const gchar *key,
1897                              gboolean     list[],
1898                              gsize        length)
1899 {
1900   GString *value_list;
1901   gsize i;
1902
1903   g_return_if_fail (key_file != NULL);
1904   g_return_if_fail (group_name != NULL);
1905   g_return_if_fail (key != NULL);
1906   g_return_if_fail (list != NULL);
1907
1908   value_list = g_string_sized_new (length * 8);
1909   for (i = 0; i < length; i++)
1910     {
1911       gchar *value;
1912
1913       value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1914
1915       g_string_append (value_list, value);
1916       g_string_append_c (value_list, key_file->list_separator);
1917
1918       g_free (value);
1919     }
1920
1921   g_key_file_set_value (key_file, group_name, key, value_list->str);
1922   g_string_free (value_list, TRUE);
1923 }
1924
1925 /**
1926  * g_key_file_get_integer:
1927  * @key_file: a #GKeyFile
1928  * @group_name: a group name
1929  * @key: a key
1930  * @error: return location for a #GError
1931  *
1932  * Returns the value associated with @key under @group_name as an
1933  * integer. If @key cannot be found then the return value is
1934  * undefined and @error is set to
1935  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1936  * associated with @key cannot be interpreted as an integer then
1937  * the return value is also undefined and @error is set to
1938  * #G_KEY_FILE_ERROR_INVALID_VALUE.
1939  *
1940  * Return value: the value associated with the key as an integer.
1941  *
1942  * Since: 2.6
1943  **/
1944 gint
1945 g_key_file_get_integer (GKeyFile     *key_file,
1946                         const gchar  *group_name,
1947                         const gchar  *key,
1948                         GError      **error)
1949 {
1950   GError *key_file_error;
1951   gchar *value;
1952   gint int_value;
1953
1954   g_return_val_if_fail (key_file != NULL, -1);
1955   g_return_val_if_fail (group_name != NULL, -1);
1956   g_return_val_if_fail (key != NULL, -1);
1957
1958   key_file_error = NULL;
1959
1960   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1961
1962   if (key_file_error)
1963     {
1964       g_propagate_error (error, key_file_error);
1965       return 0;
1966     }
1967
1968   int_value = g_key_file_parse_value_as_integer (key_file, value,
1969                                                  &key_file_error);
1970   g_free (value);
1971
1972   if (key_file_error)
1973     {
1974       if (g_error_matches (key_file_error,
1975                            G_KEY_FILE_ERROR,
1976                            G_KEY_FILE_ERROR_INVALID_VALUE))
1977         {
1978           g_set_error (error, G_KEY_FILE_ERROR,
1979                        G_KEY_FILE_ERROR_INVALID_VALUE,
1980                        _("Key file contains key '%s' in group '%s' "
1981                          "which has value that cannot be interpreted."), key, 
1982                        group_name);
1983           g_error_free (key_file_error);
1984         }
1985       else
1986         g_propagate_error (error, key_file_error);
1987     }
1988
1989   return int_value;
1990 }
1991
1992 /**
1993  * g_key_file_set_integer:
1994  * @key_file: a #GKeyFile
1995  * @group_name: a group name
1996  * @key: a key
1997  * @value: an integer value
1998  *
1999  * Associates a new integer value with @key under @group_name.
2000  * If @key cannot be found then it is created.
2001  *
2002  * Since: 2.6
2003  **/
2004 void
2005 g_key_file_set_integer (GKeyFile    *key_file,
2006                         const gchar *group_name,
2007                         const gchar *key,
2008                         gint         value)
2009 {
2010   gchar *result;
2011
2012   g_return_if_fail (key_file != NULL);
2013   g_return_if_fail (group_name != NULL);
2014   g_return_if_fail (key != NULL);
2015
2016   result = g_key_file_parse_integer_as_value (key_file, value);
2017   g_key_file_set_value (key_file, group_name, key, result);
2018   g_free (result);
2019 }
2020
2021 /**
2022  * g_key_file_get_integer_list:
2023  * @key_file: a #GKeyFile
2024  * @group_name: a group name
2025  * @key: a key
2026  * @length: the number of integers returned
2027  * @error: return location for a #GError
2028  *
2029  * Returns the values associated with @key under @group_name as
2030  * integers.  If @key cannot be found then the return value is
2031  * undefined and @error is set to
2032  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values
2033  * associated with @key cannot be interpreted as integers then
2034  * the return value is also undefined and @error is set to
2035  * #G_KEY_FILE_ERROR_INVALID_VALUE.
2036  *
2037  * Return value: the values associated with the key as a integer
2038  *
2039  * Since: 2.6
2040  **/
2041 gint *
2042 g_key_file_get_integer_list (GKeyFile     *key_file,
2043                              const gchar  *group_name,
2044                              const gchar  *key,
2045                              gsize        *length,
2046                              GError      **error)
2047 {
2048   GError *key_file_error = NULL;
2049   gchar **values;
2050   gint *int_values;
2051   gsize i, num_ints;
2052
2053   g_return_val_if_fail (key_file != NULL, NULL);
2054   g_return_val_if_fail (group_name != NULL, NULL);
2055   g_return_val_if_fail (key != NULL, NULL);
2056
2057   values = g_key_file_get_string_list (key_file, group_name, key,
2058                                        &num_ints, &key_file_error);
2059
2060   if (key_file_error)
2061     g_propagate_error (error, key_file_error);
2062
2063   if (!values)
2064     return NULL;
2065
2066   int_values = g_new0 (gint, num_ints);
2067
2068   for (i = 0; i < num_ints; i++)
2069     {
2070       int_values[i] = g_key_file_parse_value_as_integer (key_file,
2071                                                          values[i],
2072                                                          &key_file_error);
2073
2074       if (key_file_error)
2075         {
2076           g_propagate_error (error, key_file_error);
2077           g_strfreev (values);
2078           g_free (int_values);
2079
2080           return NULL;
2081         }
2082     }
2083   g_strfreev (values);
2084
2085   if (length)
2086     *length = num_ints;
2087
2088   return int_values;
2089 }
2090
2091 /**
2092  * g_key_file_set_integer_list:
2093  * @key_file: a #GKeyFile
2094  * @group_name: a group name
2095  * @key: a key
2096  * @list: an array of integer values
2097  * @length: number of integer values in @list
2098  *
2099  * Associates a list of integer values with @key under
2100  * @group_name.  If @key cannot be found then it is created.
2101  *
2102  * Since: 2.6
2103  **/
2104 void
2105 g_key_file_set_integer_list (GKeyFile     *key_file,
2106                              const gchar  *group_name,
2107                              const gchar  *key,
2108                              gint          list[],
2109                              gsize         length)
2110 {
2111   GString *values;
2112   gsize i;
2113
2114   g_return_if_fail (key_file != NULL);
2115   g_return_if_fail (group_name != NULL);
2116   g_return_if_fail (key != NULL);
2117   g_return_if_fail (list != NULL);
2118
2119   values = g_string_sized_new (length * 16);
2120   for (i = 0; i < length; i++)
2121     {
2122       gchar *value;
2123
2124       value = g_key_file_parse_integer_as_value (key_file, list[i]);
2125
2126       g_string_append (values, value);
2127       g_string_append_c (values, ';');
2128
2129       g_free (value);
2130     }
2131
2132   g_key_file_set_value (key_file, group_name, key, values->str);
2133   g_string_free (values, TRUE);
2134 }
2135
2136 static void
2137 g_key_file_set_key_comment (GKeyFile             *key_file,
2138                             const gchar          *group_name,
2139                             const gchar          *key,
2140                             const gchar          *comment,
2141                             GError              **error)
2142 {
2143   GKeyFileGroup *group;
2144   GKeyFileKeyValuePair *pair;
2145   GList *key_node, *comment_node, *tmp;
2146   
2147   group = g_key_file_lookup_group (key_file, group_name);
2148   if (!group)
2149     {
2150       g_set_error (error, G_KEY_FILE_ERROR,
2151                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2152                    _("Key file does not have group '%s'"),
2153                    group_name);
2154
2155       return;
2156     }
2157
2158   /* First find the key the comments are supposed to be
2159    * associated with
2160    */
2161   key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2162
2163   if (key_node == NULL)
2164     {
2165       g_set_error (error, G_KEY_FILE_ERROR,
2166                    G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2167                    _("Key file does not have key '%s' in group '%s'"),
2168                    key, group_name);
2169       return;
2170     }
2171
2172   /* Then find all the comments already associated with the
2173    * key and free them
2174    */
2175   tmp = key_node->next;
2176   while (tmp != NULL)
2177     {
2178       GKeyFileKeyValuePair *pair;
2179
2180       pair = (GKeyFileKeyValuePair *) tmp->data;
2181
2182       if (pair->key != NULL)
2183         break;
2184
2185       comment_node = tmp;
2186       tmp = tmp->next;
2187       g_key_file_remove_key_value_pair_node (key_file, group,
2188                                              comment_node); 
2189     }
2190
2191   if (comment == NULL)
2192     return;
2193
2194   /* Now we can add our new comment
2195    */
2196   pair = g_new0 (GKeyFileKeyValuePair, 1);
2197   
2198   pair->key = NULL;
2199   pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2200   
2201   key_node = g_list_insert (key_node, pair, 1);
2202 }
2203
2204 static void
2205 g_key_file_set_group_comment (GKeyFile             *key_file,
2206                               const gchar          *group_name,
2207                               const gchar          *comment,
2208                               GError              **error)
2209 {
2210   GKeyFileGroup *group;
2211   
2212   group = g_key_file_lookup_group (key_file, group_name);
2213   if (!group)
2214     {
2215       g_set_error (error, G_KEY_FILE_ERROR,
2216                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2217                    _("Key file does not have group '%s'"),
2218                    group_name);
2219
2220       return;
2221     }
2222
2223   /* First remove any existing comment
2224    */
2225   if (group->comment)
2226     {
2227       g_key_file_key_value_pair_free (group->comment);
2228       group->comment = NULL;
2229     }
2230
2231   if (comment == NULL)
2232     return;
2233
2234   /* Now we can add our new comment
2235    */
2236   group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2237   
2238   group->comment->key = NULL;
2239   group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2240 }
2241
2242 static void
2243 g_key_file_set_top_comment (GKeyFile             *key_file,
2244                             const gchar          *comment,
2245                             GError              **error)
2246 {
2247   GList *group_node;
2248   GKeyFileGroup *group;
2249   GKeyFileKeyValuePair *pair;
2250
2251   /* The last group in the list should be the top (comments only)
2252    * group in the file
2253    */
2254   g_assert (key_file->groups != NULL);
2255   group_node = g_list_last (key_file->groups);
2256   group = (GKeyFileGroup *) group_node->data;
2257   g_assert (group->name == NULL);
2258
2259   /* Note all keys must be comments at the top of
2260    * the file, so we can just free it all.
2261    */
2262   if (group->key_value_pairs != NULL)
2263     {
2264       g_list_foreach (group->key_value_pairs, 
2265                       (GFunc) g_key_file_key_value_pair_free, 
2266                       NULL);
2267       g_list_free (group->key_value_pairs);
2268       group->key_value_pairs = NULL;
2269     }
2270
2271   if (comment == NULL)
2272      return;
2273
2274   pair = g_new0 (GKeyFileKeyValuePair, 1);
2275   
2276   pair->key = NULL;
2277   pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2278   
2279   group->key_value_pairs =
2280     g_list_prepend (group->key_value_pairs, pair);
2281 }
2282
2283 /**
2284  * g_key_file_set_comment:
2285  * @key_file: a #GKeyFile
2286  * @group_name: a group name
2287  * @key: a key
2288  * @comment: a comment
2289  * @error: return location for a #GError
2290  *
2291  * Places a comment above @key from @group_name.
2292  * @group_name. If @key is %NULL then @comment will
2293  * be written above @group_name.  If both @key
2294  * and @group_name are NULL, then @comment will
2295  * be written above the first group in the file.
2296  *
2297  * Since: 2.6
2298  **/
2299 void
2300 g_key_file_set_comment (GKeyFile             *key_file,
2301                         const gchar          *group_name,
2302                         const gchar          *key,
2303                         const gchar          *comment,
2304                         GError              **error)
2305 {
2306   g_return_if_fail (key_file != NULL);
2307
2308   if (group_name != NULL && key != NULL)
2309     g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2310   else if (group_name != NULL)
2311     g_key_file_set_group_comment (key_file, group_name, comment, error);
2312   else
2313     g_key_file_set_top_comment (key_file, comment, error);
2314
2315   if (comment != NULL)
2316     key_file->approximate_size += strlen (comment);
2317 }
2318
2319 static gchar *
2320 g_key_file_get_key_comment (GKeyFile             *key_file,
2321                             const gchar          *group_name,
2322                             const gchar          *key,
2323                             GError              **error)
2324 {
2325   GKeyFileGroup *group;
2326   GList *key_node, *tmp;
2327   GString *string;
2328   gchar *comment;
2329
2330   group = g_key_file_lookup_group (key_file, group_name);
2331   if (!group)
2332     {
2333       g_set_error (error, G_KEY_FILE_ERROR,
2334                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2335                    _("Key file does not have group '%s'"),
2336                    group_name);
2337
2338       return NULL;
2339     }
2340
2341   /* First find the key the comments are supposed to be
2342    * associated with
2343    */
2344   key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2345
2346   if (key_node == NULL)
2347     {
2348       g_set_error (error, G_KEY_FILE_ERROR,
2349                    G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2350                    _("Key file does not have key '%s' in group '%s'"),
2351                    key, group_name);
2352       return NULL;
2353     }
2354
2355   string = NULL;
2356
2357   /* Then find all the comments already associated with the
2358    * key and concatentate them.
2359    */
2360   tmp = key_node->next;
2361   while (tmp != NULL)
2362     {
2363       GKeyFileKeyValuePair *pair;
2364
2365       pair = (GKeyFileKeyValuePair *) tmp->data;
2366
2367       if (pair->key != NULL)
2368         break;
2369       
2370       if (string == NULL)
2371         string = g_string_sized_new (512);
2372
2373       comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2374       g_string_append (string, comment);
2375       g_free (comment);
2376
2377       tmp = tmp->next;
2378     }
2379
2380   if (string != NULL)
2381     {
2382       comment = string->str;
2383       g_string_free (string, FALSE);
2384     }
2385   else
2386     comment = NULL;
2387
2388   return comment;
2389 }
2390
2391 static gchar *
2392 g_key_file_get_group_comment (GKeyFile             *key_file,
2393                               const gchar          *group_name,
2394                               GError              **error)
2395 {
2396   GKeyFileGroup *group;
2397   
2398   group = g_key_file_lookup_group (key_file, group_name);
2399   if (!group)
2400     {
2401       g_set_error (error, G_KEY_FILE_ERROR,
2402                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2403                    _("Key file does not have group '%s'"),
2404                    group_name);
2405
2406       return NULL;
2407     }
2408
2409   if (group->comment)
2410     return g_strdup (group->comment->value);
2411
2412   return NULL;
2413 }
2414
2415 static gchar *
2416 g_key_file_get_top_comment (GKeyFile             *key_file,
2417                             GError              **error)
2418 {
2419   GList *group_node, *tmp;
2420   GKeyFileGroup *group;
2421   GString *string;
2422   gchar *comment;
2423
2424   /* The last group in the list should be the top (comments only)
2425    * group in the file
2426    */
2427   g_assert (key_file->groups != NULL);
2428   group_node = g_list_last (key_file->groups);
2429   group = (GKeyFileGroup *) group_node->data;
2430   g_assert (group->name == NULL);
2431
2432   string = NULL;
2433
2434   /* Then find all the comments already associated with the
2435    * key and concatentate them.
2436    */
2437   tmp = group->key_value_pairs;
2438   while (tmp != NULL)
2439     {
2440       GKeyFileKeyValuePair *pair;
2441
2442       pair = (GKeyFileKeyValuePair *) tmp->data;
2443
2444       if (pair->key != NULL)
2445         break;
2446       
2447       if (string == NULL)
2448         string = g_string_sized_new (512);
2449
2450       comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2451       g_string_append (string, comment);
2452       g_free (comment);
2453
2454       tmp = tmp->next;
2455     }
2456
2457   if (string != NULL)
2458     {
2459       comment = string->str;
2460       g_string_free (string, FALSE);
2461     }
2462   else
2463     comment = NULL;
2464
2465   return comment;
2466 }
2467
2468 /**
2469  * g_key_file_get_comment:
2470  * @key_file: a #GKeyFile
2471  * @group_name: a group name
2472  * @key: a key
2473  * @error: return location for a #GError
2474  *
2475  * Retreives a comment above @key from @group_name.
2476  * @group_name. If @key is %NULL then @comment will
2477  * be read from above @group_name.  If both @key
2478  * and @group_name are NULL, then @comment will
2479  * be read from above the first group in the file.
2480  *
2481  * Since: 2.6
2482  * Returns: a comment that should be freed with g_free()
2483  **/
2484 gchar * 
2485 g_key_file_get_comment (GKeyFile             *key_file,
2486                         const gchar          *group_name,
2487                         const gchar          *key,
2488                         GError              **error)
2489 {
2490   g_return_val_if_fail (key_file != NULL, NULL);
2491
2492   if (group_name != NULL && key != NULL)
2493     return g_key_file_get_key_comment (key_file, group_name, key, error);
2494   else if (group_name != NULL)
2495     return g_key_file_get_group_comment (key_file, group_name, error);
2496   else
2497     return g_key_file_get_top_comment (key_file, error);
2498 }
2499
2500 /**
2501  * g_key_file_remove_comment:
2502  * @key_file: a #GKeyFile
2503  * @group_name: a group name
2504  * @key: a key
2505  * @error: return location for a #GError
2506  *
2507  * Removes a comment above @key from @group_name.
2508  * @group_name. If @key is %NULL then @comment will
2509  * be written above @group_name.  If both @key
2510  * and @group_name are NULL, then @comment will
2511  * be written above the first group in the file.
2512  *
2513  * Since: 2.6
2514  **/
2515
2516 void
2517 g_key_file_remove_comment (GKeyFile             *key_file,
2518                            const gchar          *group_name,
2519                            const gchar          *key,
2520                            GError              **error)
2521 {
2522   g_return_if_fail (key_file != NULL);
2523
2524   if (group_name != NULL && key != NULL)
2525     g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2526   else if (group_name != NULL)
2527     g_key_file_set_group_comment (key_file, group_name, NULL, error);
2528   else
2529     g_key_file_set_top_comment (key_file, NULL, error);
2530 }
2531
2532 /**
2533  * g_key_file_has_group:
2534  * @key_file: a #GKeyFile
2535  * @group_name: a group name
2536  *
2537  * Looks whether the key file has the group @group_name.
2538  *
2539  * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2540  * otherwise.
2541  * Since: 2.6
2542  **/
2543 gboolean
2544 g_key_file_has_group (GKeyFile    *key_file,
2545                       const gchar *group_name)
2546 {
2547   GList *tmp;
2548   GKeyFileGroup *group;
2549
2550   g_return_val_if_fail (key_file != NULL, FALSE);
2551   g_return_val_if_fail (group_name != NULL, FALSE);
2552
2553   for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2554     {
2555       group = (GKeyFileGroup *) tmp->data;
2556       if (group && group->name && (strcmp (group->name, group_name) == 0))
2557         return TRUE;
2558     }
2559
2560   return FALSE;
2561 }
2562
2563 /**
2564  * g_key_file_has_key:
2565  * @key_file: a #GKeyFile
2566  * @group_name: a group name
2567  * @key: a key name
2568  * @error: return location for a #GError
2569  *
2570  * Looks whether the key file has the key @key in the group
2571  * @group_name.
2572  *
2573  * Return value: %TRUE if @key is a part of @group_name, %FALSE
2574  * otherwise.
2575  *
2576  * Since: 2.6
2577  **/
2578 gboolean
2579 g_key_file_has_key (GKeyFile     *key_file,
2580                     const gchar  *group_name,
2581                     const gchar  *key,
2582                     GError      **error)
2583 {
2584   GKeyFileKeyValuePair *pair;
2585   GKeyFileGroup *group;
2586
2587   g_return_val_if_fail (key_file != NULL, FALSE);
2588   g_return_val_if_fail (key != NULL, FALSE);
2589   g_return_val_if_fail (group_name != NULL, FALSE);
2590
2591   group = g_key_file_lookup_group (key_file, group_name);
2592
2593   if (!group)
2594     {
2595       g_set_error (error, G_KEY_FILE_ERROR,
2596                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2597                    _("Key file does not have group '%s'"),
2598                    group_name);
2599
2600       return FALSE;
2601     }
2602
2603   pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2604
2605   return pair != NULL;
2606 }
2607
2608 static void
2609 g_key_file_add_group (GKeyFile    *key_file,
2610                       const gchar *group_name)
2611 {
2612   GKeyFileGroup *group;
2613
2614   g_return_if_fail (key_file != NULL);
2615   g_return_if_fail (group_name != NULL);
2616   g_return_if_fail (g_key_file_lookup_group (key_file, group_name) == NULL);
2617
2618   group = g_new0 (GKeyFileGroup, 1);
2619   group->name = g_strdup (group_name);
2620   group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2621   key_file->groups = g_list_prepend (key_file->groups, group);
2622   key_file->approximate_size += strlen (group_name) + 3;
2623   key_file->current_group = group;
2624
2625   if (key_file->start_group_name == NULL)
2626     key_file->start_group_name = g_strdup (group_name);
2627 }
2628
2629 static void
2630 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2631 {
2632   if (pair != NULL)
2633     {
2634       g_free (pair->key);
2635       g_free (pair->value);
2636       g_free (pair);
2637     }
2638 }
2639
2640 /* Be careful not to call this function on a node with data in the
2641  * lookup map without removing it from the lookup map, first.
2642  *
2643  * Some current cases where this warning is not a concern are
2644  * when:
2645  *   - the node being removed is a comment node
2646  *   - the entire lookup map is getting destroyed soon after
2647  *     anyway.
2648  */ 
2649 static void
2650 g_key_file_remove_key_value_pair_node (GKeyFile      *key_file,
2651                                        GKeyFileGroup *group,
2652                                        GList         *pair_node)
2653 {
2654
2655   GKeyFileKeyValuePair *pair;
2656
2657   pair = (GKeyFileKeyValuePair *) pair_node->data;
2658
2659   group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2660
2661   if (pair->key != NULL)
2662     key_file->approximate_size -= strlen (pair->key) + 1;
2663
2664   g_assert (pair->value != NULL);
2665   key_file->approximate_size -= strlen (pair->value);
2666
2667   g_key_file_key_value_pair_free (pair);
2668
2669   g_list_free_1 (pair_node);
2670 }
2671
2672 static void
2673 g_key_file_remove_group_node (GKeyFile *key_file,
2674                               GList    *group_node)
2675 {
2676   GKeyFileGroup *group;
2677   GList *tmp;
2678
2679   group = (GKeyFileGroup *) group_node->data;
2680
2681   /* If the current group gets deleted make the current group the first
2682    * group.
2683    */
2684   if (key_file->current_group == group)
2685     {
2686       /* groups should always contain at least the top comment group,
2687        * unless g_key_file_clear has been called
2688        */
2689       if (key_file->groups)
2690         key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2691       else
2692         key_file->current_group = NULL;
2693     }
2694
2695   key_file->groups = g_list_remove_link (key_file->groups, group_node);
2696
2697   if (group->name != NULL)
2698     key_file->approximate_size -= strlen (group->name) + 3;
2699
2700   tmp = group->key_value_pairs;
2701   while (tmp != NULL)
2702     {
2703       GList *pair_node;
2704
2705       pair_node = tmp;
2706       tmp = tmp->next;
2707       g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2708     }
2709
2710   g_assert (group->key_value_pairs == NULL);
2711
2712   if (group->lookup_map)
2713     {
2714       g_hash_table_destroy (group->lookup_map);
2715       group->lookup_map = NULL;
2716     }
2717
2718   g_free ((gchar *) group->name);
2719   g_free (group);
2720   g_list_free_1 (group_node);
2721 }
2722
2723 /**
2724  * g_key_file_remove_group:
2725  * @key_file: a #GKeyFile
2726  * @group_name: a group name
2727  * @error: return location for a #GError or %NULL
2728  *
2729  * Removes the specified group, @group_name, 
2730  * from the key file. 
2731  *
2732  * Since: 2.6
2733  **/
2734 void
2735 g_key_file_remove_group (GKeyFile     *key_file,
2736                          const gchar  *group_name,
2737                          GError      **error)
2738 {
2739   GList *group_node;
2740
2741   g_return_if_fail (key_file != NULL);
2742   g_return_if_fail (group_name != NULL);
2743
2744   group_node = g_key_file_lookup_group_node (key_file, group_name);
2745
2746   if (!group_node)
2747     g_set_error (error, G_KEY_FILE_ERROR,
2748                  G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2749                  _("Key file does not have group '%s'"),
2750                  group_name);
2751
2752   g_key_file_remove_group_node (key_file, group_node);
2753 }
2754
2755 static void
2756 g_key_file_add_key (GKeyFile    *key_file,
2757                     const gchar *group_name,
2758                     const gchar *key,
2759                     const gchar *value)
2760 {
2761   GKeyFileGroup *group;
2762   GKeyFileKeyValuePair *pair;
2763
2764   group = g_key_file_lookup_group (key_file, group_name);
2765
2766   if (!group)
2767     {
2768       g_key_file_add_group (key_file, group_name);
2769       group = (GKeyFileGroup *) key_file->groups->data;
2770     }
2771
2772   pair = g_new0 (GKeyFileKeyValuePair, 1);
2773
2774   pair->key = g_strdup (key);
2775   pair->value = g_strdup (value);
2776
2777   g_hash_table_replace (group->lookup_map, pair->key, pair);
2778   group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
2779   key_file->approximate_size += strlen (key) + strlen (value) + 2;
2780 }
2781
2782 /**
2783  * g_key_file_remove_key:
2784  * @key_file: a #GKeyFile
2785  * @group_name: a group name
2786  * @key: a key name to remove
2787  * @error: return location for a #GError or %NULL
2788  *
2789  * Removes @key in @group_name from the key file. 
2790  *
2791  * Since: 2.6
2792  **/
2793 void
2794 g_key_file_remove_key (GKeyFile     *key_file,
2795                        const gchar  *group_name,
2796                        const gchar  *key,
2797                        GError      **error)
2798 {
2799   GKeyFileGroup *group;
2800   GKeyFileKeyValuePair *pair;
2801
2802   g_return_if_fail (key_file != NULL);
2803   g_return_if_fail (group_name != NULL);
2804   g_return_if_fail (key != NULL);
2805
2806   pair = NULL;
2807
2808   if (group_name == NULL)
2809     group = key_file->current_group;
2810   else
2811     group = g_key_file_lookup_group (key_file, group_name);
2812
2813   if (!group)
2814     {
2815       g_set_error (error, G_KEY_FILE_ERROR,
2816                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2817                    _("Key file does not have group '%s'"),
2818                    group_name);
2819       return;
2820     }
2821
2822   group->key_value_pairs = g_list_remove (group->key_value_pairs, key_file);
2823   pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2824
2825   if (!pair)
2826     {
2827       g_set_error (error, G_KEY_FILE_ERROR,
2828                    G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2829                    _("Key file does not have key '%s' in group '%s'"), key, group_name);
2830       return;
2831     }
2832
2833   g_hash_table_remove (group->lookup_map, pair->key);
2834
2835   key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
2836   g_key_file_key_value_pair_free (pair);
2837 }
2838
2839 static GList *
2840 g_key_file_lookup_group_node (GKeyFile    *key_file,
2841                               const gchar *group_name)
2842 {
2843   GKeyFileGroup *group;
2844   GList *tmp;
2845
2846   group = NULL;
2847   for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2848     {
2849       group = (GKeyFileGroup *) tmp->data;
2850
2851       if (group && group->name && strcmp (group->name, group_name) == 0)
2852         break;
2853
2854       group = NULL;
2855     }
2856
2857   return tmp;
2858 }
2859
2860 static GKeyFileGroup *
2861 g_key_file_lookup_group (GKeyFile    *key_file,
2862                          const gchar *group_name)
2863 {
2864   GList *group_node;
2865
2866   group_node = g_key_file_lookup_group_node (key_file, group_name);
2867
2868   if (group_node != NULL)
2869     return (GKeyFileGroup *) group_node->data; 
2870
2871   return NULL;
2872 }
2873
2874 static GList *
2875 g_key_file_lookup_key_value_pair_node (GKeyFile       *key_file,
2876                                        GKeyFileGroup  *group,
2877                                        const gchar    *key)
2878 {
2879   GList *key_node;
2880
2881   for (key_node = group->key_value_pairs;
2882        key_node != NULL;
2883        key_node = key_node->next)
2884     {
2885       GKeyFileKeyValuePair *pair;
2886
2887       pair = (GKeyFileKeyValuePair *) key_node->data; 
2888
2889       if (pair->key && strcmp (pair->key, key) == 0)
2890         break;
2891     }
2892
2893   return key_node;
2894 }
2895
2896 static GKeyFileKeyValuePair *
2897 g_key_file_lookup_key_value_pair (GKeyFile      *key_file,
2898                                   GKeyFileGroup *group,
2899                                   const gchar   *key)
2900 {
2901   return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
2902 }
2903
2904 /* Lines starting with # or consisting entirely of whitespace are merely
2905  * recorded, not parsed. This function assumes all leading whitespace
2906  * has been stripped.
2907  */
2908 static gboolean
2909 g_key_file_line_is_comment (const gchar *line)
2910 {
2911   return (*line == '#' || *line == '\0' || *line == '\n');
2912 }
2913
2914 /* A group in a key file is made up of a starting '[' followed by one
2915  * or more letters making up the group name followed by ']'.
2916  */
2917 static gboolean
2918 g_key_file_line_is_group (const gchar *line)
2919 {
2920   gchar *p;
2921
2922   p = (gchar *) line;
2923   if (*p != '[')
2924     return FALSE;
2925
2926   p = g_utf8_next_char (p);
2927
2928   if (!*p)
2929     return FALSE;
2930
2931   p = g_utf8_next_char (p);
2932
2933   /* Group name must be non-empty
2934    */
2935   if (*p == ']')
2936     return FALSE;
2937
2938   while (*p && *p != ']')
2939     p = g_utf8_next_char (p);
2940
2941   if (!*p)
2942     return FALSE;
2943
2944   return TRUE;
2945 }
2946
2947 static gboolean
2948 g_key_file_line_is_key_value_pair (const gchar *line)
2949 {
2950   gchar *p;
2951
2952   p = (gchar *) g_utf8_strchr (line, -1, '=');
2953
2954   if (!p)
2955     return FALSE;
2956
2957   /* Key must be non-empty
2958    */
2959   if (*p == line[0])
2960     return FALSE;
2961
2962   return TRUE;
2963 }
2964
2965 static gchar *
2966 g_key_file_parse_value_as_string (GKeyFile     *key_file,
2967                                   const gchar  *value,
2968                                   GSList      **pieces,
2969                                   GError      **error)
2970 {
2971   GError *parse_error = NULL;
2972   gchar *string_value, *p, *q0, *q;
2973
2974   string_value = g_new0 (gchar, strlen (value) + 1);
2975
2976   p = (gchar *) value;
2977   q0 = q = string_value;
2978   while (*p)
2979     {
2980       if (*p == '\\')
2981         {
2982           p++;
2983
2984           switch (*p)
2985             {
2986             case 's':
2987               *q = ' ';
2988               break;
2989
2990             case 'n':
2991               *q = '\n';
2992               break;
2993
2994             case 't':
2995               *q = '\t';
2996               break;
2997
2998             case 'r':
2999               *q = '\r';
3000               break;
3001
3002             case '\\':
3003               *q = '\\';
3004               break;
3005
3006             default:
3007               if (pieces && *p == key_file->list_separator)
3008                 *q = key_file->list_separator;
3009               else
3010                 {
3011                   *q++ = '\\';
3012                   *q = *p;
3013                   
3014                   if (parse_error == NULL)
3015                     {
3016                       gchar sequence[3];
3017                       
3018                       sequence[0] = '\\';
3019                       sequence[1] = *p;
3020                       sequence[2] = '\0';
3021                       
3022                       g_set_error (error, G_KEY_FILE_ERROR,
3023                                    G_KEY_FILE_ERROR_INVALID_VALUE,
3024                                    _("Key file contains invalid escape "
3025                                      "sequence '%s'"), sequence);
3026                     }
3027                 }
3028               break;
3029             }
3030         }
3031       else
3032         {
3033           *q = *p;
3034           if (pieces && (*p == key_file->list_separator))
3035             {
3036               *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3037               q0 = q + 1; 
3038             }
3039         }
3040
3041       q++;
3042       p++;
3043     }
3044
3045   if (p[-1] == '\\' && error == NULL)
3046     g_set_error (error, G_KEY_FILE_ERROR,
3047                  G_KEY_FILE_ERROR_INVALID_VALUE,
3048                  _("Key file contains escape character at end of line"));
3049
3050   *q = '\0';
3051   if (pieces)
3052   {
3053     if (q0 < q)
3054       *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3055     *pieces = g_slist_reverse (*pieces);
3056   }
3057
3058   return string_value;
3059 }
3060
3061 static gchar *
3062 g_key_file_parse_string_as_value (GKeyFile    *key_file,
3063                                   const gchar *string,
3064                                   gboolean     escape_separator)
3065 {
3066   gchar *value, *p, *q;
3067   gsize length;
3068
3069   length = strlen (string) + 1;
3070
3071   /* Worst case would be that every character needs to be escaped.
3072    * In other words every character turns to two characters
3073    */
3074   value = g_new0 (gchar, 2 * length);
3075
3076   p = (gchar *) string;
3077   q = value;
3078   while (p < (string + length - 1))
3079     {
3080       gchar escaped_character[3] = { '\\', 0, 0 };
3081
3082       switch (*p)
3083         {
3084         case ' ':
3085           escaped_character[1] = 's';
3086           strcpy (q, escaped_character);
3087           q += 2;
3088           break;
3089         case '\n':
3090           escaped_character[1] = 'n';
3091           strcpy (q, escaped_character);
3092           q += 2;
3093           break;
3094         case '\t':
3095           escaped_character[1] = 't';
3096           strcpy (q, escaped_character);
3097           q += 2;
3098           break;
3099         case '\r':
3100           escaped_character[1] = 'r';
3101           strcpy (q, escaped_character);
3102           q += 2;
3103           break;
3104         case '\\':
3105           escaped_character[1] = '\\';
3106           strcpy (q, escaped_character);
3107           q += 2;
3108           break;
3109         default:
3110           if (escape_separator && *p == key_file->list_separator)
3111             {
3112               escaped_character[1] = key_file->list_separator;
3113               strcpy (q, escaped_character);
3114               q += 2;
3115             }
3116           else 
3117             {
3118               *q = *p;
3119               q++;
3120             }
3121           break;
3122         }
3123       p++;
3124     }
3125   *q = '\0';
3126
3127   return value;
3128 }
3129
3130 static gint
3131 g_key_file_parse_value_as_integer (GKeyFile     *key_file,
3132                                    const gchar  *value,
3133                                    GError      **error)
3134 {
3135   gchar *end_of_valid_int;
3136   gint int_value = 0;
3137
3138   int_value = strtol (value, &end_of_valid_int, 0);
3139
3140   if (*end_of_valid_int != '\0')
3141     g_set_error (error, G_KEY_FILE_ERROR,
3142                  G_KEY_FILE_ERROR_INVALID_VALUE,
3143                  _("Value '%s' cannot be interpreted as a number."), value);
3144
3145   return int_value;
3146 }
3147
3148 static gchar *
3149 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3150                                    gint      value)
3151
3152 {
3153   return g_strdup_printf ("%d", value);
3154 }
3155
3156 static gboolean
3157 g_key_file_parse_value_as_boolean (GKeyFile     *key_file,
3158                                    const gchar  *value,
3159                                    GError      **error)
3160 {
3161   if (value)
3162     {
3163       if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3164         return TRUE;
3165       else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3166         return FALSE;
3167     }
3168
3169   g_set_error (error, G_KEY_FILE_ERROR,
3170                G_KEY_FILE_ERROR_INVALID_VALUE,
3171                _("Value '%s' cannot be interpreted as a boolean."), value);
3172
3173   return FALSE;
3174 }
3175
3176 static gchar *
3177 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3178                                    gboolean  value)
3179 {
3180   if (value)
3181     return g_strdup ("true");
3182   else
3183     return g_strdup ("false");
3184 }
3185
3186 static gchar *
3187 g_key_file_parse_value_as_comment (GKeyFile    *key_file,
3188                                    const gchar *value)
3189 {
3190   GString *string;
3191   gchar **lines, *comment;
3192   gsize i;
3193
3194   string = g_string_sized_new (512);
3195
3196   lines = g_strsplit (value, "\n", 0);
3197
3198   for (i = 0; lines[i] != NULL; i++)
3199     {
3200         if (lines[i][0] != '#')
3201            g_string_append_printf (string, "%s\n", lines[i]);
3202         else 
3203            g_string_append_printf (string, "%s\n", lines[i] + 1);
3204     }
3205   g_strfreev (lines);
3206
3207   comment = string->str;
3208
3209   g_string_free (string, FALSE);
3210
3211   return comment;
3212 }
3213
3214 static gchar *
3215 g_key_file_parse_comment_as_value (GKeyFile      *key_file,
3216                                    const gchar   *comment)
3217 {
3218   GString *string;
3219   gchar **lines, *value;
3220   gsize i;
3221
3222   string = g_string_sized_new (512);
3223
3224   lines = g_strsplit (comment, "\n", 0);
3225
3226   for (i = 0; lines[i] != NULL; i++)
3227     g_string_append_printf (string, "#%s%s", lines[i], 
3228                             lines[i + 1] == NULL? "" : "\n");
3229   g_strfreev (lines);
3230
3231   value = string->str;
3232
3233   g_string_free (string, FALSE);
3234
3235   return value;
3236 }
3237
3238