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