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