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