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