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