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