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