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