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