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