Make PLT-reduction work with gcc4, and don't include everything in
[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. If @group_name is
1612  * %NULL, then the start group is used.
1613
1614  * If @key cannot be found then %NULL is returned and @error is set to
1615  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1616  * with @key cannot be interpreted or no suitable translations
1617  * can be found then the untranslated values are returned.
1618  * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1619  *
1620  * Return value: a newly allocated %NULL-terminated string array
1621  *   or %NULL if the key isn't found. The string array should be freed
1622  *   with g_strfreev().
1623  *
1624  * Since: 2.6
1625  **/
1626 gchar **
1627 g_key_file_get_locale_string_list (GKeyFile     *key_file,
1628                                    const gchar  *group_name,
1629                                    const gchar  *key,
1630                                    const gchar  *locale,
1631                                    gsize        *length,
1632                                    GError      **error)
1633 {
1634   GError *key_file_error;
1635   gchar **values, *value;
1636
1637   g_return_val_if_fail (key_file != NULL, NULL);
1638   g_return_val_if_fail (group_name != NULL, NULL);
1639   g_return_val_if_fail (key != NULL, NULL);
1640
1641   key_file_error = NULL;
1642
1643   value = g_key_file_get_locale_string (key_file, group_name, 
1644                                         key, locale,
1645                                         &key_file_error);
1646   
1647   if (key_file_error)
1648     g_propagate_error (error, key_file_error);
1649   
1650   if (!value)
1651     return NULL;
1652
1653   if (value[strlen (value) - 1] == ';')
1654     value[strlen (value) - 1] = '\0';
1655
1656   values = g_strsplit (value, ";", 0);
1657
1658   g_free (value);
1659
1660   if (length)
1661     *length = g_strv_length (values);
1662
1663   return values;
1664 }
1665
1666 /**
1667  * g_key_file_set_locale_string_list:
1668  * @key_file: a #GKeyFile
1669  * @group_name: a group name
1670  * @key: a key
1671  * @locale: a locale
1672  * @list: a %NULL-terminated array of locale string values
1673  * @length: the length of @list
1674  *
1675  * Associates a list of string values for @key and @locale under
1676  * @group_name.  If the translation for @key cannot be found then
1677  * it is created. If @group_name is %NULL, the start group is
1678  * used.
1679  *
1680  * Since: 2.6
1681  **/
1682 void
1683 g_key_file_set_locale_string_list (GKeyFile            *key_file,
1684                                    const gchar         *group_name,
1685                                    const gchar         *key,
1686                                    const gchar         *locale,
1687                                    const gchar * const  list[],
1688                                    gsize                length)
1689 {
1690   GString *value_list;
1691   gchar *full_key;
1692   gsize i;
1693
1694   g_return_if_fail (key_file != NULL);
1695   g_return_if_fail (group_name != NULL);
1696   g_return_if_fail (key != NULL);
1697   g_return_if_fail (locale != NULL);
1698   g_return_if_fail (length != 0);
1699
1700   value_list = g_string_sized_new (length * 128);
1701   for (i = 0; list[i] != NULL && i < length; i++)
1702     {
1703       gchar *value;
1704       
1705       value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1706       
1707       g_string_append (value_list, value);
1708       g_string_append_c (value_list, ';');
1709
1710       g_free (value);
1711     }
1712
1713   full_key = g_strdup_printf ("%s[%s]", key, locale);
1714   g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1715   g_free (full_key);
1716   g_string_free (value_list, TRUE);
1717 }
1718
1719 /**
1720  * g_key_file_get_boolean:
1721  * @key_file: a #GKeyFile
1722  * @group_name: a group name
1723  * @key: a key
1724  * @error: return location for a #GError
1725  *
1726  * Returns the value associated with @key under @group_name as a
1727  * boolean. If @group_name is %NULL, the start group is used.
1728  *
1729  * If @key cannot be found then the return value is undefined and
1730  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1731  * the value associated with @key cannot be interpreted as a boolean
1732  * then the return value is also undefined and @error is set to
1733  * #G_KEY_FILE_ERROR_INVALID_VALUE.
1734  *
1735  * Return value: the value associated with the key as a boolean
1736  * Since: 2.6
1737  **/
1738 gboolean
1739 g_key_file_get_boolean (GKeyFile     *key_file,
1740                         const gchar  *group_name,
1741                         const gchar  *key,
1742                         GError      **error)
1743 {
1744   GError *key_file_error = NULL;
1745   gchar *value;
1746   gboolean bool_value;
1747
1748   g_return_val_if_fail (key_file != NULL, FALSE);
1749   g_return_val_if_fail (group_name != NULL, FALSE);
1750   g_return_val_if_fail (key != NULL, FALSE);
1751
1752   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1753
1754   if (!value)
1755     {
1756       g_propagate_error (error, key_file_error);
1757       return FALSE;
1758     }
1759
1760   bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1761                                                   &key_file_error);
1762   g_free (value);
1763
1764   if (key_file_error)
1765     {
1766       if (g_error_matches (key_file_error,
1767                            G_KEY_FILE_ERROR,
1768                            G_KEY_FILE_ERROR_INVALID_VALUE))
1769         {
1770           g_set_error (error, G_KEY_FILE_ERROR,
1771                        G_KEY_FILE_ERROR_INVALID_VALUE,
1772                        _("Key file contains key '%s' "
1773                          "which has value that cannot be interpreted."),
1774                        key);
1775           g_error_free (key_file_error);
1776         }
1777       else
1778         g_propagate_error (error, key_file_error);
1779     }
1780
1781   return bool_value;
1782 }
1783
1784 /**
1785  * g_key_file_set_boolean:
1786  * @key_file: a #GKeyFile
1787  * @group_name: a group name
1788  * @key: a key
1789  * @value: %TRUE or %FALSE
1790  *
1791  * Associates a new boolean value with @key under @group_name.
1792  * If @key cannot be found then it is created. If @group_name
1793  * is %NULL, the start group is used.
1794  *
1795  * Since: 2.6
1796  **/
1797 void
1798 g_key_file_set_boolean (GKeyFile    *key_file,
1799                         const gchar *group_name,
1800                         const gchar *key,
1801                         gboolean     value)
1802 {
1803   gchar *result;
1804
1805   g_return_if_fail (key_file != NULL);
1806   g_return_if_fail (group_name != NULL);
1807   g_return_if_fail (key != NULL);
1808
1809   result = g_key_file_parse_boolean_as_value (key_file, value);
1810   g_key_file_set_value (key_file, group_name, key, result);
1811   g_free (result);
1812 }
1813
1814 /**
1815  * g_key_file_get_boolean_list:
1816  * @key_file: a #GKeyFile
1817  * @group_name: a group name
1818  * @key: a key
1819  * @length: the number of booleans returned
1820  * @error: return location for a #GError
1821  *
1822  * Returns the values associated with @key under @group_name as
1823  * booleans. If @group_name is %NULL, the start_group is used.
1824  *
1825  * If @key cannot be found then the return value is undefined and
1826  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1827  * the values associated with @key cannot be interpreted as booleans
1828  * then the return value is also undefined and @error is set to
1829  * #G_KEY_FILE_ERROR_INVALID_VALUE.
1830  *
1831  * Return value: the values associated with the key as a boolean
1832  * 
1833  * Since: 2.6
1834  **/
1835 gboolean *
1836 g_key_file_get_boolean_list (GKeyFile     *key_file,
1837                              const gchar  *group_name,
1838                              const gchar  *key,
1839                              gsize        *length,
1840                              GError      **error)
1841 {
1842   GError *key_file_error;
1843   gchar **values;
1844   gboolean *bool_values;
1845   gsize i, num_bools;
1846
1847   g_return_val_if_fail (key_file != NULL, NULL);
1848   g_return_val_if_fail (group_name != NULL, NULL);
1849   g_return_val_if_fail (key != NULL, NULL);
1850
1851   key_file_error = NULL;
1852
1853   values = g_key_file_get_string_list (key_file, group_name, key,
1854                                        &num_bools, &key_file_error);
1855
1856   if (key_file_error)
1857     g_propagate_error (error, key_file_error);
1858
1859   if (!values)
1860     return NULL;
1861
1862   bool_values = g_new0 (gboolean, num_bools);
1863
1864   for (i = 0; i < num_bools; i++)
1865     {
1866       bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1867                                                           values[i],
1868                                                           &key_file_error);
1869
1870       if (key_file_error)
1871         {
1872           g_propagate_error (error, key_file_error);
1873           g_strfreev (values);
1874           g_free (bool_values);
1875
1876           return NULL;
1877         }
1878     }
1879   g_strfreev (values);
1880
1881   if (length)
1882     *length = num_bools;
1883
1884   return bool_values;
1885 }
1886
1887 /**
1888  * g_key_file_set_boolean_list:
1889  * @key_file: a #GKeyFile
1890  * @group_name: a group name
1891  * @key: a key
1892  * @list: an array of boolean values
1893  * @length: length of @list
1894  *
1895  * Associates a list of boolean values with @key under
1896  * @group_name.  If @key cannot be found then it is created.
1897  * If @group_name is %NULL, the start_group is used.
1898  *
1899  * Since: 2.6
1900  **/
1901 void
1902 g_key_file_set_boolean_list (GKeyFile    *key_file,
1903                              const gchar *group_name,
1904                              const gchar *key,
1905                              gboolean     list[],
1906                              gsize        length)
1907 {
1908   GString *value_list;
1909   gsize i;
1910
1911   g_return_if_fail (key_file != NULL);
1912   g_return_if_fail (group_name != NULL);
1913   g_return_if_fail (key != NULL);
1914   g_return_if_fail (list != NULL);
1915
1916   value_list = g_string_sized_new (length * 8);
1917   for (i = 0; i < length; i++)
1918     {
1919       gchar *value;
1920
1921       value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1922
1923       g_string_append (value_list, value);
1924       g_string_append_c (value_list, key_file->list_separator);
1925
1926       g_free (value);
1927     }
1928
1929   g_key_file_set_value (key_file, group_name, key, value_list->str);
1930   g_string_free (value_list, TRUE);
1931 }
1932
1933 /**
1934  * g_key_file_get_integer:
1935  * @key_file: a #GKeyFile
1936  * @group_name: a group name
1937  * @key: a key
1938  * @error: return location for a #GError
1939  *
1940  * Returns the value associated with @key under @group_name as an
1941  * integer. If @group_name is %NULL, the start_group is used.
1942  *
1943  * If @key cannot be found then the return value is undefined and
1944  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1945  * the value associated with @key cannot be interpreted as an integer
1946  * then the return value is also undefined and @error is set to
1947  * #G_KEY_FILE_ERROR_INVALID_VALUE.
1948  *
1949  * Return value: the value associated with the key as an integer.
1950  *
1951  * Since: 2.6
1952  **/
1953 gint
1954 g_key_file_get_integer (GKeyFile     *key_file,
1955                         const gchar  *group_name,
1956                         const gchar  *key,
1957                         GError      **error)
1958 {
1959   GError *key_file_error;
1960   gchar *value;
1961   gint int_value;
1962
1963   g_return_val_if_fail (key_file != NULL, -1);
1964   g_return_val_if_fail (group_name != NULL, -1);
1965   g_return_val_if_fail (key != NULL, -1);
1966
1967   key_file_error = NULL;
1968
1969   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1970
1971   if (key_file_error)
1972     {
1973       g_propagate_error (error, key_file_error);
1974       return 0;
1975     }
1976
1977   int_value = g_key_file_parse_value_as_integer (key_file, value,
1978                                                  &key_file_error);
1979   g_free (value);
1980
1981   if (key_file_error)
1982     {
1983       if (g_error_matches (key_file_error,
1984                            G_KEY_FILE_ERROR,
1985                            G_KEY_FILE_ERROR_INVALID_VALUE))
1986         {
1987           g_set_error (error, G_KEY_FILE_ERROR,
1988                        G_KEY_FILE_ERROR_INVALID_VALUE,
1989                        _("Key file contains key '%s' in group '%s' "
1990                          "which has value that cannot be interpreted."), key, 
1991                        group_name);
1992           g_error_free (key_file_error);
1993         }
1994       else
1995         g_propagate_error (error, key_file_error);
1996     }
1997
1998   return int_value;
1999 }
2000
2001 /**
2002  * g_key_file_set_integer:
2003  * @key_file: a #GKeyFile
2004  * @group_name: a group name
2005  * @key: a key
2006  * @value: an integer value
2007  *
2008  * Associates a new integer value with @key under @group_name.
2009  * If @key cannot be found then it is created. If @group_name
2010  * is %NULL, the start group is used.
2011  *
2012  * Since: 2.6
2013  **/
2014 void
2015 g_key_file_set_integer (GKeyFile    *key_file,
2016                         const gchar *group_name,
2017                         const gchar *key,
2018                         gint         value)
2019 {
2020   gchar *result;
2021
2022   g_return_if_fail (key_file != NULL);
2023   g_return_if_fail (group_name != NULL);
2024   g_return_if_fail (key != NULL);
2025
2026   result = g_key_file_parse_integer_as_value (key_file, value);
2027   g_key_file_set_value (key_file, group_name, key, result);
2028   g_free (result);
2029 }
2030
2031 /**
2032  * g_key_file_get_integer_list:
2033  * @key_file: a #GKeyFile
2034  * @group_name: a group name
2035  * @key: a key
2036  * @length: the number of integers returned
2037  * @error: return location for a #GError
2038  *
2039  * Returns the values associated with @key under @group_name as
2040  * integers. If @group_name is %NULL, the start group is used.
2041  *
2042  * If @key cannot be found then the return value is undefined and
2043  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2044  * the values associated with @key cannot be interpreted as integers
2045  * then the return value is also undefined and @error is set to
2046  * #G_KEY_FILE_ERROR_INVALID_VALUE.
2047  *
2048  * Return value: the values associated with the key as a integer
2049  *
2050  * Since: 2.6
2051  **/
2052 gint *
2053 g_key_file_get_integer_list (GKeyFile     *key_file,
2054                              const gchar  *group_name,
2055                              const gchar  *key,
2056                              gsize        *length,
2057                              GError      **error)
2058 {
2059   GError *key_file_error = NULL;
2060   gchar **values;
2061   gint *int_values;
2062   gsize i, num_ints;
2063
2064   g_return_val_if_fail (key_file != NULL, NULL);
2065   g_return_val_if_fail (group_name != NULL, NULL);
2066   g_return_val_if_fail (key != NULL, NULL);
2067
2068   values = g_key_file_get_string_list (key_file, group_name, key,
2069                                        &num_ints, &key_file_error);
2070
2071   if (key_file_error)
2072     g_propagate_error (error, key_file_error);
2073
2074   if (!values)
2075     return NULL;
2076
2077   int_values = g_new0 (gint, num_ints);
2078
2079   for (i = 0; i < num_ints; i++)
2080     {
2081       int_values[i] = g_key_file_parse_value_as_integer (key_file,
2082                                                          values[i],
2083                                                          &key_file_error);
2084
2085       if (key_file_error)
2086         {
2087           g_propagate_error (error, key_file_error);
2088           g_strfreev (values);
2089           g_free (int_values);
2090
2091           return NULL;
2092         }
2093     }
2094   g_strfreev (values);
2095
2096   if (length)
2097     *length = num_ints;
2098
2099   return int_values;
2100 }
2101
2102 /**
2103  * g_key_file_set_integer_list:
2104  * @key_file: a #GKeyFile
2105  * @group_name: a group name
2106  * @key: a key
2107  * @list: an array of integer values
2108  * @length: number of integer values in @list
2109  *
2110  * Associates a list of integer values with @key under
2111  * @group_name.  If @key cannot be found then it is created.
2112  * If @group_name is %NULL the start group is used.
2113  *
2114  * Since: 2.6
2115  **/
2116 void
2117 g_key_file_set_integer_list (GKeyFile     *key_file,
2118                              const gchar  *group_name,
2119                              const gchar  *key,
2120                              gint          list[],
2121                              gsize         length)
2122 {
2123   GString *values;
2124   gsize i;
2125
2126   g_return_if_fail (key_file != NULL);
2127   g_return_if_fail (group_name != NULL);
2128   g_return_if_fail (key != NULL);
2129   g_return_if_fail (list != NULL);
2130
2131   values = g_string_sized_new (length * 16);
2132   for (i = 0; i < length; i++)
2133     {
2134       gchar *value;
2135
2136       value = g_key_file_parse_integer_as_value (key_file, list[i]);
2137
2138       g_string_append (values, value);
2139       g_string_append_c (values, ';');
2140
2141       g_free (value);
2142     }
2143
2144   g_key_file_set_value (key_file, group_name, key, values->str);
2145   g_string_free (values, TRUE);
2146 }
2147
2148 static void
2149 g_key_file_set_key_comment (GKeyFile             *key_file,
2150                             const gchar          *group_name,
2151                             const gchar          *key,
2152                             const gchar          *comment,
2153                             GError              **error)
2154 {
2155   GKeyFileGroup *group;
2156   GKeyFileKeyValuePair *pair;
2157   GList *key_node, *comment_node, *tmp;
2158   
2159   group = g_key_file_lookup_group (key_file, group_name);
2160   if (!group)
2161     {
2162       g_set_error (error, G_KEY_FILE_ERROR,
2163                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2164                    _("Key file does not have group '%s'"),
2165                    group_name ? group_name : "(null)");
2166
2167       return;
2168     }
2169
2170   /* First find the key the comments are supposed to be
2171    * associated with
2172    */
2173   key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2174
2175   if (key_node == NULL)
2176     {
2177       g_set_error (error, G_KEY_FILE_ERROR,
2178                    G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2179                    _("Key file does not have key '%s' in group '%s'"),
2180                    key, group->name);
2181       return;
2182     }
2183
2184   /* Then find all the comments already associated with the
2185    * key and free them
2186    */
2187   tmp = key_node->next;
2188   while (tmp != NULL)
2189     {
2190       GKeyFileKeyValuePair *pair;
2191
2192       pair = (GKeyFileKeyValuePair *) tmp->data;
2193
2194       if (pair->key != NULL)
2195         break;
2196
2197       comment_node = tmp;
2198       tmp = tmp->next;
2199       g_key_file_remove_key_value_pair_node (key_file, group,
2200                                              comment_node); 
2201     }
2202
2203   if (comment == NULL)
2204     return;
2205
2206   /* Now we can add our new comment
2207    */
2208   pair = g_new0 (GKeyFileKeyValuePair, 1);
2209   
2210   pair->key = NULL;
2211   pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2212   
2213   key_node = g_list_insert (key_node, pair, 1);
2214 }
2215
2216 static void
2217 g_key_file_set_group_comment (GKeyFile             *key_file,
2218                               const gchar          *group_name,
2219                               const gchar          *comment,
2220                               GError              **error)
2221 {
2222   GKeyFileGroup *group;
2223   
2224   group = g_key_file_lookup_group (key_file, group_name);
2225   if (!group)
2226     {
2227       g_set_error (error, G_KEY_FILE_ERROR,
2228                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2229                    _("Key file does not have group '%s'"),
2230                    group_name ? group_name : "(null)");
2231
2232       return;
2233     }
2234
2235   /* First remove any existing comment
2236    */
2237   if (group->comment)
2238     {
2239       g_key_file_key_value_pair_free (group->comment);
2240       group->comment = NULL;
2241     }
2242
2243   if (comment == NULL)
2244     return;
2245
2246   /* Now we can add our new comment
2247    */
2248   group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2249   
2250   group->comment->key = NULL;
2251   group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2252 }
2253
2254 static void
2255 g_key_file_set_top_comment (GKeyFile             *key_file,
2256                             const gchar          *comment,
2257                             GError              **error)
2258 {
2259   GList *group_node;
2260   GKeyFileGroup *group;
2261   GKeyFileKeyValuePair *pair;
2262
2263   /* The last group in the list should be the top (comments only)
2264    * group in the file
2265    */
2266   g_assert (key_file->groups != NULL);
2267   group_node = g_list_last (key_file->groups);
2268   group = (GKeyFileGroup *) group_node->data;
2269   g_assert (group->name == NULL);
2270
2271   /* Note all keys must be comments at the top of
2272    * the file, so we can just free it all.
2273    */
2274   if (group->key_value_pairs != NULL)
2275     {
2276       g_list_foreach (group->key_value_pairs, 
2277                       (GFunc) g_key_file_key_value_pair_free, 
2278                       NULL);
2279       g_list_free (group->key_value_pairs);
2280       group->key_value_pairs = NULL;
2281     }
2282
2283   if (comment == NULL)
2284      return;
2285
2286   pair = g_new0 (GKeyFileKeyValuePair, 1);
2287   
2288   pair->key = NULL;
2289   pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2290   
2291   group->key_value_pairs =
2292     g_list_prepend (group->key_value_pairs, pair);
2293 }
2294
2295 /**
2296  * g_key_file_set_comment:
2297  * @key_file: a #GKeyFile
2298  * @group_name: a group name, or %NULL
2299  * @key: a key
2300  * @comment: a comment
2301  * @error: return location for a #GError
2302  *
2303  * Places a comment above @key from @group_name.
2304  * @group_name. If @key is %NULL then @comment will
2305  * be written above @group_name.  If both @key
2306  * and @group_name are NULL, then @comment will
2307  * be written above the first group in the file.
2308  *
2309  * Since: 2.6
2310  **/
2311 void
2312 g_key_file_set_comment (GKeyFile             *key_file,
2313                         const gchar          *group_name,
2314                         const gchar          *key,
2315                         const gchar          *comment,
2316                         GError              **error)
2317 {
2318   g_return_if_fail (key_file != NULL);
2319
2320   if (group_name != NULL && key != NULL)
2321     g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2322   else if (group_name != NULL)
2323     g_key_file_set_group_comment (key_file, group_name, comment, error);
2324   else
2325     g_key_file_set_top_comment (key_file, comment, error);
2326
2327   if (comment != NULL)
2328     key_file->approximate_size += strlen (comment);
2329 }
2330
2331 static gchar *
2332 g_key_file_get_key_comment (GKeyFile             *key_file,
2333                             const gchar          *group_name,
2334                             const gchar          *key,
2335                             GError              **error)
2336 {
2337   GKeyFileGroup *group;
2338   GList *key_node, *tmp;
2339   GString *string;
2340   gchar *comment;
2341
2342   group = g_key_file_lookup_group (key_file, group_name);
2343   if (!group)
2344     {
2345       g_set_error (error, G_KEY_FILE_ERROR,
2346                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2347                    _("Key file does not have group '%s'"),
2348                    group_name ? group_name : "(null)");
2349
2350       return NULL;
2351     }
2352
2353   /* First find the key the comments are supposed to be
2354    * associated with
2355    */
2356   key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2357
2358   if (key_node == NULL)
2359     {
2360       g_set_error (error, G_KEY_FILE_ERROR,
2361                    G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2362                    _("Key file does not have key '%s' in group '%s'"),
2363                    key, group->name);
2364       return NULL;
2365     }
2366
2367   string = NULL;
2368
2369   /* Then find all the comments already associated with the
2370    * key and concatentate them.
2371    */
2372   tmp = key_node->next;
2373   while (tmp != NULL)
2374     {
2375       GKeyFileKeyValuePair *pair;
2376
2377       pair = (GKeyFileKeyValuePair *) tmp->data;
2378
2379       if (pair->key != NULL)
2380         break;
2381       
2382       if (string == NULL)
2383         string = g_string_sized_new (512);
2384
2385       comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2386       g_string_append (string, comment);
2387       g_free (comment);
2388
2389       tmp = tmp->next;
2390     }
2391
2392   if (string != NULL)
2393     {
2394       comment = string->str;
2395       g_string_free (string, FALSE);
2396     }
2397   else
2398     comment = NULL;
2399
2400   return comment;
2401 }
2402
2403 static gchar *
2404 g_key_file_get_group_comment (GKeyFile             *key_file,
2405                               const gchar          *group_name,
2406                               GError              **error)
2407 {
2408   GKeyFileGroup *group;
2409   
2410   group = g_key_file_lookup_group (key_file, group_name);
2411   if (!group)
2412     {
2413       g_set_error (error, G_KEY_FILE_ERROR,
2414                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2415                    _("Key file does not have group '%s'"),
2416                    group_name ? group_name : "(null)");
2417
2418       return NULL;
2419     }
2420
2421   if (group->comment)
2422     return g_strdup (group->comment->value);
2423
2424   return NULL;
2425 }
2426
2427 static gchar *
2428 g_key_file_get_top_comment (GKeyFile             *key_file,
2429                             GError              **error)
2430 {
2431   GList *group_node, *tmp;
2432   GKeyFileGroup *group;
2433   GString *string;
2434   gchar *comment;
2435
2436   /* The last group in the list should be the top (comments only)
2437    * group in the file
2438    */
2439   g_assert (key_file->groups != NULL);
2440   group_node = g_list_last (key_file->groups);
2441   group = (GKeyFileGroup *) group_node->data;
2442   g_assert (group->name == NULL);
2443
2444   string = NULL;
2445
2446   /* Then find all the comments already associated with the
2447    * key and concatentate them.
2448    */
2449   tmp = group->key_value_pairs;
2450   while (tmp != NULL)
2451     {
2452       GKeyFileKeyValuePair *pair;
2453
2454       pair = (GKeyFileKeyValuePair *) tmp->data;
2455
2456       if (pair->key != NULL)
2457         break;
2458       
2459       if (string == NULL)
2460         string = g_string_sized_new (512);
2461
2462       comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2463       g_string_append (string, comment);
2464       g_free (comment);
2465
2466       tmp = tmp->next;
2467     }
2468
2469   if (string != NULL)
2470     {
2471       comment = string->str;
2472       g_string_free (string, FALSE);
2473     }
2474   else
2475     comment = NULL;
2476
2477   return comment;
2478 }
2479
2480 /**
2481  * g_key_file_get_comment:
2482  * @key_file: a #GKeyFile
2483  * @group_name: a group name, or %NULL
2484  * @key: a key
2485  * @error: return location for a #GError
2486  *
2487  * Retreives a comment above @key from @group_name.
2488  * @group_name. If @key is %NULL then @comment will
2489  * be read from above @group_name.  If both @key
2490  * and @group_name are NULL, then @comment will
2491  * be read from above the first group in the file.
2492  *
2493  * Since: 2.6
2494  * Returns: a comment that should be freed with g_free()
2495  **/
2496 gchar * 
2497 g_key_file_get_comment (GKeyFile             *key_file,
2498                         const gchar          *group_name,
2499                         const gchar          *key,
2500                         GError              **error)
2501 {
2502   g_return_val_if_fail (key_file != NULL, NULL);
2503
2504   if (group_name != NULL && key != NULL)
2505     return g_key_file_get_key_comment (key_file, group_name, key, error);
2506   else if (group_name != NULL)
2507     return g_key_file_get_group_comment (key_file, group_name, error);
2508   else
2509     return g_key_file_get_top_comment (key_file, error);
2510 }
2511
2512 /**
2513  * g_key_file_remove_comment:
2514  * @key_file: a #GKeyFile
2515  * @group_name: a group name, or %NULL
2516  * @key: a key
2517  * @error: return location for a #GError
2518  *
2519  * Removes a comment above @key from @group_name.
2520  * @group_name. If @key is %NULL then @comment will
2521  * be written above @group_name.  If both @key
2522  * and @group_name are NULL, then @comment will
2523  * be written above the first group in the file.
2524  *
2525  * Since: 2.6
2526  **/
2527
2528 void
2529 g_key_file_remove_comment (GKeyFile             *key_file,
2530                            const gchar          *group_name,
2531                            const gchar          *key,
2532                            GError              **error)
2533 {
2534   g_return_if_fail (key_file != NULL);
2535
2536   if (group_name != NULL && key != NULL)
2537     g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2538   else if (group_name != NULL)
2539     g_key_file_set_group_comment (key_file, group_name, NULL, error);
2540   else
2541     g_key_file_set_top_comment (key_file, NULL, error);
2542 }
2543
2544 /**
2545  * g_key_file_has_group:
2546  * @key_file: a #GKeyFile
2547  * @group_name: a group name
2548  *
2549  * Looks whether the key file has the group @group_name.
2550  *
2551  * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2552  * otherwise.
2553  * Since: 2.6
2554  **/
2555 gboolean
2556 g_key_file_has_group (GKeyFile    *key_file,
2557                       const gchar *group_name)
2558 {
2559   g_return_val_if_fail (key_file != NULL, FALSE);
2560   g_return_val_if_fail (group_name != NULL, FALSE);
2561
2562   return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2563 }
2564
2565 /**
2566  * g_key_file_has_key:
2567  * @key_file: a #GKeyFile
2568  * @group_name: a group name
2569  * @key: a key name
2570  * @error: return location for a #GError
2571  *
2572  * Looks whether the key file has the key @key in the group
2573  * @group_name. If @group_name is %NULL, the start group is
2574  * used.
2575  *
2576  * Return value: %TRUE if @key is a part of @group_name, %FALSE
2577  * otherwise.
2578  *
2579  * Since: 2.6
2580  **/
2581 gboolean
2582 g_key_file_has_key (GKeyFile     *key_file,
2583                     const gchar  *group_name,
2584                     const gchar  *key,
2585                     GError      **error)
2586 {
2587   GKeyFileKeyValuePair *pair;
2588   GKeyFileGroup *group;
2589
2590   g_return_val_if_fail (key_file != NULL, FALSE);
2591   g_return_val_if_fail (group_name != NULL, FALSE);
2592   g_return_val_if_fail (key != NULL, FALSE);
2593
2594   group = g_key_file_lookup_group (key_file, group_name);
2595
2596   if (!group)
2597     {
2598       g_set_error (error, G_KEY_FILE_ERROR,
2599                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2600                    _("Key file does not have group '%s'"),
2601                    group_name ? group_name : "(null)");
2602
2603       return FALSE;
2604     }
2605
2606   pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2607
2608   return pair != NULL;
2609 }
2610
2611 static void
2612 g_key_file_add_group (GKeyFile    *key_file,
2613                       const gchar *group_name)
2614 {
2615   GKeyFileGroup *group;
2616
2617   g_return_if_fail (key_file != NULL);
2618   g_return_if_fail (group_name != NULL);
2619   g_return_if_fail (g_key_file_lookup_group_node (key_file, group_name) == NULL);
2620
2621   group = g_new0 (GKeyFileGroup, 1);
2622   group->name = g_strdup (group_name);
2623   group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2624   key_file->groups = g_list_prepend (key_file->groups, group);
2625   key_file->approximate_size += strlen (group_name) + 3;
2626   key_file->current_group = group;
2627
2628   if (key_file->start_group == NULL)
2629     key_file->start_group = group;
2630 }
2631
2632 static void
2633 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2634 {
2635   if (pair != NULL)
2636     {
2637       g_free (pair->key);
2638       g_free (pair->value);
2639       g_free (pair);
2640     }
2641 }
2642
2643 /* Be careful not to call this function on a node with data in the
2644  * lookup map without removing it from the lookup map, first.
2645  *
2646  * Some current cases where this warning is not a concern are
2647  * when:
2648  *   - the node being removed is a comment node
2649  *   - the entire lookup map is getting destroyed soon after
2650  *     anyway.
2651  */ 
2652 static void
2653 g_key_file_remove_key_value_pair_node (GKeyFile      *key_file,
2654                                        GKeyFileGroup *group,
2655                                        GList         *pair_node)
2656 {
2657
2658   GKeyFileKeyValuePair *pair;
2659
2660   pair = (GKeyFileKeyValuePair *) pair_node->data;
2661
2662   group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2663
2664   if (pair->key != NULL)
2665     key_file->approximate_size -= strlen (pair->key) + 1;
2666
2667   g_assert (pair->value != NULL);
2668   key_file->approximate_size -= strlen (pair->value);
2669
2670   g_key_file_key_value_pair_free (pair);
2671
2672   g_list_free_1 (pair_node);
2673 }
2674
2675 static void
2676 g_key_file_remove_group_node (GKeyFile *key_file,
2677                               GList    *group_node)
2678 {
2679   GKeyFileGroup *group;
2680   GList *tmp;
2681
2682   group = (GKeyFileGroup *) group_node->data;
2683
2684   /* If the current group gets deleted make the current group the last
2685    * added group.
2686    */
2687   if (key_file->current_group == group)
2688     {
2689       /* groups should always contain at least the top comment group,
2690        * unless g_key_file_clear has been called
2691        */
2692       if (key_file->groups)
2693         key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2694       else
2695         key_file->current_group = NULL;
2696     }
2697
2698   /* If the start group gets deleted make the start group the first
2699    * added group.
2700    */
2701   if (key_file->start_group == group)
2702     {
2703       tmp = g_list_last (key_file->groups);
2704       while (tmp != NULL)
2705         {
2706           if (tmp != group_node &&
2707               ((GKeyFileGroup *) tmp->data)->name != NULL)
2708             break;
2709
2710           tmp = tmp->prev;
2711         }
2712
2713       if (tmp)
2714         key_file->start_group = (GKeyFileGroup *) tmp->data;
2715       else
2716         key_file->start_group = NULL;
2717     }
2718
2719   key_file->groups = g_list_remove_link (key_file->groups, group_node);
2720
2721   if (group->name != NULL)
2722     key_file->approximate_size -= strlen (group->name) + 3;
2723
2724   tmp = group->key_value_pairs;
2725   while (tmp != NULL)
2726     {
2727       GList *pair_node;
2728
2729       pair_node = tmp;
2730       tmp = tmp->next;
2731       g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2732     }
2733
2734   g_assert (group->key_value_pairs == NULL);
2735
2736   if (group->lookup_map)
2737     {
2738       g_hash_table_destroy (group->lookup_map);
2739       group->lookup_map = NULL;
2740     }
2741
2742   g_free ((gchar *) group->name);
2743   g_free (group);
2744   g_list_free_1 (group_node);
2745 }
2746
2747 /**
2748  * g_key_file_remove_group:
2749  * @key_file: a #GKeyFile
2750  * @group_name: a group name
2751  * @error: return location for a #GError or %NULL
2752  *
2753  * Removes the specified group, @group_name, 
2754  * from the key file. 
2755  *
2756  * Since: 2.6
2757  **/
2758 void
2759 g_key_file_remove_group (GKeyFile     *key_file,
2760                          const gchar  *group_name,
2761                          GError      **error)
2762 {
2763   GList *group_node;
2764
2765   g_return_if_fail (key_file != NULL);
2766   g_return_if_fail (group_name != NULL);
2767
2768   group_node = g_key_file_lookup_group_node (key_file, group_name);
2769
2770   if (!group_node)
2771     {
2772       g_set_error (error, G_KEY_FILE_ERROR,
2773                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2774                    _("Key file does not have group '%s'"),
2775                    group_name);
2776       return;
2777     }
2778
2779     g_key_file_remove_group_node (key_file, group_node);
2780 }
2781
2782 static void
2783 g_key_file_add_key (GKeyFile      *key_file,
2784                     GKeyFileGroup *group,
2785                     const gchar   *key,
2786                     const gchar   *value)
2787 {
2788   GKeyFileKeyValuePair *pair;
2789
2790   pair = g_new0 (GKeyFileKeyValuePair, 1);
2791
2792   pair->key = g_strdup (key);
2793   pair->value = g_strdup (value);
2794
2795   g_hash_table_replace (group->lookup_map, pair->key, pair);
2796   group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
2797   key_file->approximate_size += strlen (key) + strlen (value) + 2;
2798 }
2799
2800 /**
2801  * g_key_file_remove_key:
2802  * @key_file: a #GKeyFile
2803  * @group_name: a group name
2804  * @key: a key name to remove
2805  * @error: return location for a #GError or %NULL
2806  *
2807  * Removes @key in @group_name from the key file. 
2808  *
2809  * Since: 2.6
2810  **/
2811 void
2812 g_key_file_remove_key (GKeyFile     *key_file,
2813                        const gchar  *group_name,
2814                        const gchar  *key,
2815                        GError      **error)
2816 {
2817   GKeyFileGroup *group;
2818   GKeyFileKeyValuePair *pair;
2819
2820   g_return_if_fail (key_file != NULL);
2821   g_return_if_fail (group_name != NULL);
2822   g_return_if_fail (key != NULL);
2823
2824   pair = NULL;
2825
2826   group = g_key_file_lookup_group (key_file, group_name);
2827   if (!group)
2828     {
2829       g_set_error (error, G_KEY_FILE_ERROR,
2830                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2831                    _("Key file does not have group '%s'"),
2832                    group_name ? group_name : "(null)");
2833       return;
2834     }
2835
2836   pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2837
2838   if (!pair)
2839     {
2840       g_set_error (error, G_KEY_FILE_ERROR,
2841                    G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2842                    _("Key file does not have key '%s' in group '%s'"),
2843                    key, group->name);
2844       return;
2845     }
2846
2847   key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
2848
2849   group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
2850   g_hash_table_remove (group->lookup_map, pair->key);  
2851   g_key_file_key_value_pair_free (pair);
2852 }
2853
2854 static GList *
2855 g_key_file_lookup_group_node (GKeyFile    *key_file,
2856                               const gchar *group_name)
2857 {
2858   GKeyFileGroup *group;
2859   GList *tmp;
2860
2861   group = NULL;
2862   for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
2863     {
2864       group = (GKeyFileGroup *) tmp->data;
2865
2866       if (group && group->name && strcmp (group->name, group_name) == 0)
2867         break;
2868
2869       group = NULL;
2870     }
2871
2872   return tmp;
2873 }
2874
2875 static GKeyFileGroup *
2876 g_key_file_lookup_group (GKeyFile    *key_file,
2877                          const gchar *group_name)
2878 {
2879   GList *group_node;
2880
2881   group_node = g_key_file_lookup_group_node (key_file, group_name);
2882
2883   if (group_node != NULL)
2884     return (GKeyFileGroup *) group_node->data; 
2885
2886   return NULL;
2887 }
2888
2889 static GList *
2890 g_key_file_lookup_key_value_pair_node (GKeyFile       *key_file,
2891                                        GKeyFileGroup  *group,
2892                                        const gchar    *key)
2893 {
2894   GList *key_node;
2895
2896   for (key_node = group->key_value_pairs;
2897        key_node != NULL;
2898        key_node = key_node->next)
2899     {
2900       GKeyFileKeyValuePair *pair;
2901
2902       pair = (GKeyFileKeyValuePair *) key_node->data; 
2903
2904       if (pair->key && strcmp (pair->key, key) == 0)
2905         break;
2906     }
2907
2908   return key_node;
2909 }
2910
2911 static GKeyFileKeyValuePair *
2912 g_key_file_lookup_key_value_pair (GKeyFile      *key_file,
2913                                   GKeyFileGroup *group,
2914                                   const gchar   *key)
2915 {
2916   return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
2917 }
2918
2919 /* Lines starting with # or consisting entirely of whitespace are merely
2920  * recorded, not parsed. This function assumes all leading whitespace
2921  * has been stripped.
2922  */
2923 static gboolean
2924 g_key_file_line_is_comment (const gchar *line)
2925 {
2926   return (*line == '#' || *line == '\0' || *line == '\n');
2927 }
2928
2929 /* A group in a key file is made up of a starting '[' followed by one
2930  * or more letters making up the group name followed by ']'.
2931  */
2932 static gboolean
2933 g_key_file_line_is_group (const gchar *line)
2934 {
2935   gchar *p;
2936
2937   p = (gchar *) line;
2938   if (*p != '[')
2939     return FALSE;
2940
2941   p = g_utf8_next_char (p);
2942
2943   if (!*p)
2944     return FALSE;
2945
2946   p = g_utf8_next_char (p);
2947
2948   /* Group name must be non-empty
2949    */
2950   if (*p == ']')
2951     return FALSE;
2952
2953   while (*p && *p != ']')
2954     p = g_utf8_next_char (p);
2955
2956   if (!*p)
2957     return FALSE;
2958
2959   return TRUE;
2960 }
2961
2962 static gboolean
2963 g_key_file_line_is_key_value_pair (const gchar *line)
2964 {
2965   gchar *p;
2966
2967   p = (gchar *) g_utf8_strchr (line, -1, '=');
2968
2969   if (!p)
2970     return FALSE;
2971
2972   /* Key must be non-empty
2973    */
2974   if (*p == line[0])
2975     return FALSE;
2976
2977   return TRUE;
2978 }
2979
2980 static gchar *
2981 g_key_file_parse_value_as_string (GKeyFile     *key_file,
2982                                   const gchar  *value,
2983                                   GSList      **pieces,
2984                                   GError      **error)
2985 {
2986   gchar *string_value, *p, *q0, *q;
2987
2988   string_value = g_new0 (gchar, strlen (value) + 1);
2989
2990   p = (gchar *) value;
2991   q0 = q = string_value;
2992   while (*p)
2993     {
2994       if (*p == '\\')
2995         {
2996           p++;
2997
2998           switch (*p)
2999             {
3000             case 's':
3001               *q = ' ';
3002               break;
3003
3004             case 'n':
3005               *q = '\n';
3006               break;
3007
3008             case 't':
3009               *q = '\t';
3010               break;
3011
3012             case 'r':
3013               *q = '\r';
3014               break;
3015
3016             case '\\':
3017               *q = '\\';
3018               break;
3019
3020             case '\0':
3021               g_set_error (error, G_KEY_FILE_ERROR,
3022                            G_KEY_FILE_ERROR_INVALID_VALUE,
3023                            _("Key file contains escape character "
3024                              "at end of line"));
3025               break;
3026
3027             default:
3028               if (pieces && *p == key_file->list_separator)
3029                 *q = key_file->list_separator;
3030               else
3031                 {
3032                   *q++ = '\\';
3033                   *q = *p;
3034                   
3035                   if (*error == NULL)
3036                     {
3037                       gchar sequence[3];
3038                       
3039                       sequence[0] = '\\';
3040                       sequence[1] = *p;
3041                       sequence[2] = '\0';
3042                       
3043                       g_set_error (error, G_KEY_FILE_ERROR,
3044                                    G_KEY_FILE_ERROR_INVALID_VALUE,
3045                                    _("Key file contains invalid escape "
3046                                      "sequence '%s'"), sequence);
3047                     }
3048                 }
3049               break;
3050             }
3051         }
3052       else
3053         {
3054           *q = *p;
3055           if (pieces && (*p == key_file->list_separator))
3056             {
3057               *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3058               q0 = q + 1; 
3059             }
3060         }
3061
3062       if (*p == '\0')
3063         break;
3064
3065       q++;
3066       p++;
3067     }
3068
3069   *q = '\0';
3070   if (pieces)
3071   {
3072     if (q0 < q)
3073       *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3074     *pieces = g_slist_reverse (*pieces);
3075   }
3076
3077   return string_value;
3078 }
3079
3080 static gchar *
3081 g_key_file_parse_string_as_value (GKeyFile    *key_file,
3082                                   const gchar *string,
3083                                   gboolean     escape_separator)
3084 {
3085   gchar *value, *p, *q;
3086   gsize length;
3087   gboolean parsing_leading_space;
3088
3089   length = strlen (string) + 1;
3090
3091   /* Worst case would be that every character needs to be escaped.
3092    * In other words every character turns to two characters
3093    */
3094   value = g_new0 (gchar, 2 * length);
3095
3096   p = (gchar *) string;
3097   q = value;
3098   parsing_leading_space = TRUE;
3099   while (p < (string + length - 1))
3100     {
3101       gchar escaped_character[3] = { '\\', 0, 0 };
3102
3103       switch (*p)
3104         {
3105         case ' ':
3106           if (parsing_leading_space)
3107             {
3108               escaped_character[1] = 's';
3109               strcpy (q, escaped_character);
3110               q += 2;
3111             }
3112           else
3113             {
3114               *q = *p;
3115               q++;
3116             }
3117           break;
3118         case '\t':
3119           if (parsing_leading_space)
3120             {
3121               escaped_character[1] = 't';
3122               strcpy (q, escaped_character);
3123               q += 2;
3124             }
3125           else
3126             {
3127               *q = *p;
3128               q++;
3129             }
3130           break;
3131         case '\n':
3132           escaped_character[1] = 'n';
3133           strcpy (q, escaped_character);
3134           q += 2;
3135           break;
3136         case '\r':
3137           escaped_character[1] = 'r';
3138           strcpy (q, escaped_character);
3139           q += 2;
3140           break;
3141         case '\\':
3142           escaped_character[1] = '\\';
3143           strcpy (q, escaped_character);
3144           q += 2;
3145           parsing_leading_space = FALSE;
3146           break;
3147         default:
3148           if (escape_separator && *p == key_file->list_separator)
3149             {
3150               escaped_character[1] = key_file->list_separator;
3151               strcpy (q, escaped_character);
3152               q += 2;
3153               parsing_leading_space = TRUE;
3154             }
3155           else 
3156             {
3157               *q = *p;
3158               q++;
3159               parsing_leading_space = FALSE;
3160             }
3161           break;
3162         }
3163       p++;
3164     }
3165   *q = '\0';
3166
3167   return value;
3168 }
3169
3170 static gint
3171 g_key_file_parse_value_as_integer (GKeyFile     *key_file,
3172                                    const gchar  *value,
3173                                    GError      **error)
3174 {
3175   gchar *end_of_valid_int;
3176   gint int_value = 0;
3177
3178   int_value = strtol (value, &end_of_valid_int, 10);
3179
3180   if (*end_of_valid_int != '\0')
3181     g_set_error (error, G_KEY_FILE_ERROR,
3182                  G_KEY_FILE_ERROR_INVALID_VALUE,
3183                  _("Value '%s' cannot be interpreted as a number."), value);
3184
3185   return int_value;
3186 }
3187
3188 static gchar *
3189 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3190                                    gint      value)
3191
3192 {
3193   return g_strdup_printf ("%d", value);
3194 }
3195
3196 static gboolean
3197 g_key_file_parse_value_as_boolean (GKeyFile     *key_file,
3198                                    const gchar  *value,
3199                                    GError      **error)
3200 {
3201   if (value)
3202     {
3203       if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3204         return TRUE;
3205       else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3206         return FALSE;
3207     }
3208
3209   g_set_error (error, G_KEY_FILE_ERROR,
3210                G_KEY_FILE_ERROR_INVALID_VALUE,
3211                _("Value '%s' cannot be interpreted as a boolean."), value);
3212
3213   return FALSE;
3214 }
3215
3216 static gchar *
3217 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3218                                    gboolean  value)
3219 {
3220   if (value)
3221     return g_strdup ("true");
3222   else
3223     return g_strdup ("false");
3224 }
3225
3226 static gchar *
3227 g_key_file_parse_value_as_comment (GKeyFile    *key_file,
3228                                    const gchar *value)
3229 {
3230   GString *string;
3231   gchar **lines, *comment;
3232   gsize i;
3233
3234   string = g_string_sized_new (512);
3235
3236   lines = g_strsplit (value, "\n", 0);
3237
3238   for (i = 0; lines[i] != NULL; i++)
3239     {
3240         if (lines[i][0] != '#')
3241            g_string_append_printf (string, "%s\n", lines[i]);
3242         else 
3243            g_string_append_printf (string, "%s\n", lines[i] + 1);
3244     }
3245   g_strfreev (lines);
3246
3247   comment = string->str;
3248
3249   g_string_free (string, FALSE);
3250
3251   return comment;
3252 }
3253
3254 static gchar *
3255 g_key_file_parse_comment_as_value (GKeyFile      *key_file,
3256                                    const gchar   *comment)
3257 {
3258   GString *string;
3259   gchar **lines, *value;
3260   gsize i;
3261
3262   string = g_string_sized_new (512);
3263
3264   lines = g_strsplit (comment, "\n", 0);
3265
3266   for (i = 0; lines[i] != NULL; i++)
3267     g_string_append_printf (string, "#%s%s", lines[i], 
3268                             lines[i + 1] == NULL? "" : "\n");
3269   g_strfreev (lines);
3270
3271   value = string->str;
3272
3273   g_string_free (string, FALSE);
3274
3275   return value;
3276 }
3277
3278 #define __G_KEY_FILE_C__
3279 #include "galiasdef.c"