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