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