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