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