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