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