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