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