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