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