Remove redundant code. (g_key_file_get_group_comment): Don't dereference
[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   gchar *data;
933   GList *group_node, *key_file_node;
934
935   g_return_val_if_fail (key_file != NULL, NULL);
936
937   data_string = g_string_sized_new (2 * key_file->approximate_size);
938   
939   for (group_node = g_list_last (key_file->groups);
940        group_node != NULL;
941        group_node = group_node->prev)
942     {
943       GKeyFileGroup *group;
944
945       group = (GKeyFileGroup *) group_node->data;
946
947       if (group->comment != NULL)
948         g_string_append_printf (data_string, "%s\n", group->comment->value);
949       if (group->name != NULL)
950         g_string_append_printf (data_string, "[%s]\n", group->name);
951
952       for (key_file_node = g_list_last (group->key_value_pairs);
953            key_file_node != NULL;
954            key_file_node = key_file_node->prev)
955         {
956           GKeyFileKeyValuePair *pair;
957
958           pair = (GKeyFileKeyValuePair *) key_file_node->data;
959
960           if (pair->key != NULL)
961             g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
962           else
963             g_string_append_printf (data_string, "%s\n", pair->value);
964         }
965     }
966
967   if (length)
968     *length = data_string->len;
969
970   data = data_string->str;
971
972   g_string_free (data_string, FALSE);
973
974   return data;
975 }
976
977 /**
978  * g_key_file_get_keys:
979  * @key_file: a #GKeyFile
980  * @group_name: a group name
981  * @length: return location for the number of keys returned, or %NULL
982  * @error: return location for a #GError, or %NULL
983  *
984  * Returns all keys for the group name @group_name.  The array of
985  * returned keys will be %NULL-terminated, so @length may
986  * optionally be %NULL. In the event that the @group_name cannot
987  * be found, %NULL is returned and @error is set to
988  * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
989  *
990  * Return value: a newly-allocated %NULL-terminated array of
991  * strings. Use g_strfreev() to free it.
992  *
993  * Since: 2.6
994  **/
995 gchar **
996 g_key_file_get_keys (GKeyFile     *key_file,
997                      const gchar  *group_name,
998                      gsize        *length,
999                      GError      **error)
1000 {
1001   GKeyFileGroup *group;
1002   GList *tmp;
1003   gchar **keys;
1004   gsize i, num_keys;
1005   
1006   g_return_val_if_fail (key_file != NULL, NULL);
1007   g_return_val_if_fail (group_name != NULL, NULL);
1008   
1009   group = g_key_file_lookup_group (key_file, group_name);
1010   
1011   if (!group)
1012     {
1013       g_set_error (error, G_KEY_FILE_ERROR,
1014                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1015                    _("Key file does not have group '%s'"),
1016                    group_name ? group_name : "(null)");
1017       return NULL;
1018     }
1019
1020   num_keys = 0;
1021   for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1022     {
1023       GKeyFileKeyValuePair *pair;
1024
1025       pair = (GKeyFileKeyValuePair *) tmp->data;
1026
1027       if (pair->key)
1028         num_keys++;
1029     }
1030   
1031   keys = g_new0 (gchar *, num_keys + 1);
1032
1033   i = num_keys - 1;
1034   for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1035     {
1036       GKeyFileKeyValuePair *pair;
1037
1038       pair = (GKeyFileKeyValuePair *) tmp->data;
1039
1040       if (pair->key)
1041         {
1042           keys[i] = g_strdup (pair->key);
1043           i--;
1044         }
1045     }
1046
1047   keys[num_keys] = NULL;
1048
1049   if (length)
1050     *length = num_keys;
1051
1052   return keys;
1053 }
1054
1055 /**
1056  * g_key_file_get_start_group:
1057  * @key_file: a #GKeyFile
1058  *
1059  * Returns the name of the start group of the file. 
1060  *
1061  * Return value: The start group of the key file.
1062  *
1063  * Since: 2.6
1064  **/
1065 gchar *
1066 g_key_file_get_start_group (GKeyFile *key_file)
1067 {
1068   g_return_val_if_fail (key_file != NULL, NULL);
1069
1070   if (key_file->start_group)
1071     return g_strdup (key_file->start_group->name);
1072
1073   return NULL;
1074 }
1075
1076 /**
1077  * g_key_file_get_groups:
1078  * @key_file: a #GKeyFile
1079  * @length: return location for the number of returned groups, or %NULL
1080  *
1081  * Returns all groups in the key file loaded with @key_file.  The
1082  * array of returned groups will be %NULL-terminated, so @length may
1083  * optionally be %NULL.
1084  *
1085  * Return value: a newly-allocated %NULL-terminated array of strings. 
1086  *   Use g_strfreev() to free it.
1087  * Since: 2.6
1088  **/
1089 gchar **
1090 g_key_file_get_groups (GKeyFile *key_file,
1091                        gsize    *length)
1092 {
1093   GList *group_node;
1094   gchar **groups;
1095   gsize i, num_groups;
1096
1097   g_return_val_if_fail (key_file != NULL, NULL);
1098
1099   num_groups = g_list_length (key_file->groups);
1100
1101   g_assert (num_groups > 0);
1102
1103   /* Only need num_groups instead of num_groups + 1
1104    * because the first group of the file (last in the
1105    * list) is always the comment group at the top,
1106    * which we skip
1107    */
1108   groups = g_new0 (gchar *, num_groups);
1109
1110   group_node = g_list_last (key_file->groups);
1111   
1112   g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1113
1114   i = 0;
1115   for (group_node = group_node->prev;
1116        group_node != NULL;
1117        group_node = group_node->prev)
1118     {
1119       GKeyFileGroup *group;
1120
1121       group = (GKeyFileGroup *) group_node->data;
1122
1123       g_assert (group->name != NULL);
1124
1125       groups[i++] = g_strdup (group->name);
1126     }
1127   groups[i] = NULL;
1128
1129   if (length)
1130     *length = i;
1131
1132   return groups;
1133 }
1134
1135 /**
1136  * g_key_file_get_value:
1137  * @key_file: a #GKeyFile
1138  * @group_name: a group name
1139  * @key: a key
1140  * @error: return location for a #GError, or %NULL
1141  *
1142  * Returns the value associated with @key under @group_name.  
1143  *
1144  * In the event the key cannot be found, %NULL is returned and 
1145  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND.  In the 
1146  * event that the @group_name cannot be found, %NULL is returned 
1147  * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1148  *
1149  * Return value: a newly allocated string or %NULL if the specified 
1150  *  key cannot be found.
1151  *
1152  * Since: 2.6
1153  **/
1154 gchar *
1155 g_key_file_get_value (GKeyFile     *key_file,
1156                       const gchar  *group_name,
1157                       const gchar  *key,
1158                       GError      **error)
1159 {
1160   GKeyFileGroup *group;
1161   GKeyFileKeyValuePair *pair;
1162   gchar *value = NULL;
1163
1164   g_return_val_if_fail (key_file != NULL, NULL);
1165   g_return_val_if_fail (group_name != NULL, NULL);
1166   g_return_val_if_fail (key != NULL, NULL);
1167   
1168   group = g_key_file_lookup_group (key_file, group_name);
1169
1170   if (!group)
1171     {
1172       g_set_error (error, G_KEY_FILE_ERROR,
1173                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1174                    _("Key file does not have group '%s'"),
1175                    group_name ? group_name : "(null)");
1176       return NULL;
1177     }
1178
1179   pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1180
1181   if (pair)
1182     value = g_strdup (pair->value);
1183   else
1184     g_set_error (error, G_KEY_FILE_ERROR,
1185                  G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1186                  _("Key file does not have key '%s'"), key);
1187
1188   return value;
1189 }
1190
1191 /**
1192  * g_key_file_set_value:
1193  * @key_file: a #GKeyFile
1194  * @group_name: a group name
1195  * @key: a key
1196  * @value: a string
1197  *
1198  * Associates a new value with @key under @group_name.  If @key
1199  * cannot be found then it is created. If @group_name cannot be
1200  * found then it is created.
1201  *
1202  * Since: 2.6
1203  **/
1204 void
1205 g_key_file_set_value (GKeyFile    *key_file,
1206                       const gchar *group_name,
1207                       const gchar *key,
1208                       const gchar *value)
1209 {
1210   GKeyFileGroup *group;
1211   GKeyFileKeyValuePair *pair;
1212
1213   g_return_if_fail (key_file != NULL);
1214   g_return_if_fail (group_name != NULL);
1215   g_return_if_fail (key != NULL);
1216   g_return_if_fail (value != NULL);
1217
1218   group = g_key_file_lookup_group (key_file, group_name);
1219
1220   if (!group)
1221     {
1222       g_key_file_add_group (key_file, group_name);
1223       group = (GKeyFileGroup *) key_file->groups->data;
1224
1225       g_key_file_add_key (key_file, group, key, value);
1226     }
1227   else
1228     {
1229       pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1230
1231       if (!pair)
1232         g_key_file_add_key (key_file, group, key, value);
1233       else
1234         {
1235           g_free (pair->value);
1236           pair->value = g_strdup (value);
1237         }
1238     }
1239 }
1240
1241 /**
1242  * g_key_file_get_string:
1243  * @key_file: a #GKeyFile
1244  * @group_name: a group name
1245  * @key: a key
1246  * @error: return location for a #GError, or %NULL
1247  *
1248  * Returns the value associated with @key under @group_name.  
1249  *
1250  * In the event the key cannot be found, %NULL is returned and 
1251  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND.  In the 
1252  * event that the @group_name cannot be found, %NULL is returned 
1253  * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1254  *
1255  * Return value: a newly allocated string or %NULL if the specified 
1256  *   key cannot be found.
1257  *
1258  * Since: 2.6
1259  **/
1260 gchar *
1261 g_key_file_get_string (GKeyFile     *key_file,
1262                        const gchar  *group_name,
1263                        const gchar  *key,
1264                        GError      **error)
1265 {
1266   gchar *value, *string_value;
1267   GError *key_file_error;
1268
1269   g_return_val_if_fail (key_file != NULL, NULL);
1270   g_return_val_if_fail (group_name != NULL, NULL);
1271   g_return_val_if_fail (key != NULL, NULL);
1272
1273   key_file_error = NULL;
1274
1275   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1276
1277   if (key_file_error)
1278     {
1279       g_propagate_error (error, key_file_error);
1280       return NULL;
1281     }
1282
1283   if (!g_utf8_validate (value, -1, NULL))
1284     {
1285       g_set_error (error, G_KEY_FILE_ERROR,
1286                    G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1287                    _("Key file contains key '%s' with value '%s' "
1288                      "which is not UTF-8"), key, value);
1289       g_free (value);
1290       return NULL;
1291     }
1292
1293   string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1294                                                    &key_file_error);
1295   g_free (value);
1296
1297   if (key_file_error)
1298     {
1299       if (g_error_matches (key_file_error,
1300                            G_KEY_FILE_ERROR,
1301                            G_KEY_FILE_ERROR_INVALID_VALUE))
1302         {
1303           g_set_error (error, G_KEY_FILE_ERROR,
1304                        G_KEY_FILE_ERROR_INVALID_VALUE,
1305                        _("Key file contains key '%s' "
1306                          "which has value that cannot be interpreted."),
1307                        key);
1308           g_error_free (key_file_error);
1309         }
1310       else
1311         g_propagate_error (error, key_file_error);
1312     }
1313
1314   return string_value;
1315 }
1316
1317 /**
1318  * g_key_file_set_string:
1319  * @key_file: a #GKeyFile
1320  * @group_name: a group name
1321  * @key: a key
1322  * @string: a string
1323  *
1324  * Associates a new string value with @key under @group_name.  If
1325  * @key cannot be found then it is created.  If @group_name
1326  * cannot be found then it is created.
1327  *
1328  * Since: 2.6
1329  **/
1330 void
1331 g_key_file_set_string (GKeyFile    *key_file,
1332                        const gchar *group_name,
1333                        const gchar *key,
1334                        const gchar *string)
1335 {
1336   gchar *value;
1337
1338   g_return_if_fail (key_file != NULL);
1339   g_return_if_fail (group_name != NULL);
1340   g_return_if_fail (key != NULL);
1341   g_return_if_fail (string != NULL);
1342
1343   value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1344   g_key_file_set_value (key_file, group_name, key, value);
1345   g_free (value);
1346 }
1347
1348 /**
1349  * g_key_file_get_string_list:
1350  * @key_file: a #GKeyFile
1351  * @group_name: a group name
1352  * @key: a key
1353  * @length: return location for the number of returned strings, or %NULL
1354  * @error: return location for a #GError, or %NULL
1355  *
1356  * Returns the values associated with @key under @group_name.
1357  *
1358  * In the event the key cannot be found, %NULL is returned and
1359  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND.  In the
1360  * event that the @group_name cannot be found, %NULL is returned
1361  * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1362  *
1363  * Return value: a %NULL-terminated string array or %NULL if the specified 
1364  *   key cannot be found. The array should be freed with g_strfreev().
1365  *
1366  * Since: 2.6
1367  **/
1368 gchar **
1369 g_key_file_get_string_list (GKeyFile     *key_file,
1370                             const gchar  *group_name,
1371                             const gchar  *key,
1372                             gsize        *length,
1373                             GError      **error)
1374 {
1375   GError *key_file_error = NULL;
1376   gchar *value, *string_value, **values;
1377   gint i, len;
1378   GSList *p, *pieces = NULL;
1379
1380   g_return_val_if_fail (key_file != NULL, NULL);
1381   g_return_val_if_fail (group_name != NULL, NULL);
1382   g_return_val_if_fail (key != NULL, NULL);
1383
1384   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1385
1386   if (key_file_error)
1387     {
1388       g_propagate_error (error, key_file_error);
1389       return NULL;
1390     }
1391
1392   if (!g_utf8_validate (value, -1, NULL))
1393     {
1394       g_set_error (error, G_KEY_FILE_ERROR,
1395                    G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1396                    _("Key file contains key '%s' with value '%s' "
1397                      "which is not UTF-8"), key, value);
1398       g_free (value);
1399       return NULL;
1400     }
1401
1402   string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1403   g_free (value);
1404   g_free (string_value);
1405
1406   if (key_file_error)
1407     {
1408       if (g_error_matches (key_file_error,
1409                            G_KEY_FILE_ERROR,
1410                            G_KEY_FILE_ERROR_INVALID_VALUE))
1411         {
1412           g_set_error (error, G_KEY_FILE_ERROR,
1413                        G_KEY_FILE_ERROR_INVALID_VALUE,
1414                        _("Key file contains key '%s' "
1415                          "which has value that cannot be interpreted."),
1416                        key);
1417           g_error_free (key_file_error);
1418         }
1419       else
1420         g_propagate_error (error, key_file_error);
1421     }
1422
1423   len = g_slist_length (pieces);
1424   values = g_new0 (gchar *, len + 1); 
1425   for (p = pieces, i = 0; p; p = p->next)
1426     values[i++] = p->data;
1427   values[len] = NULL;
1428
1429   g_slist_free (pieces);
1430
1431   if (length)
1432     *length = len;
1433
1434   return values;
1435 }
1436
1437 /**
1438  * g_key_file_set_string_list:
1439  * @key_file: a #GKeyFile
1440  * @group_name: a group name
1441  * @key: a key
1442  * @list: an array of locale string values
1443  * @length: number of locale string values in @list
1444  *
1445  * Associates a list of string values for @key under @group_name.
1446  * If @key cannot be found then it is created.  If @group_name 
1447  * cannot be found then it is created.
1448  *
1449  * Since: 2.6
1450  **/
1451 void
1452 g_key_file_set_string_list (GKeyFile            *key_file,
1453                             const gchar         *group_name,
1454                             const gchar         *key,
1455                             const gchar * const  list[],
1456                             gsize                length)
1457 {
1458   GString *value_list;
1459   gsize i;
1460
1461   g_return_if_fail (key_file != NULL);
1462   g_return_if_fail (group_name != NULL);
1463   g_return_if_fail (key != NULL);
1464   g_return_if_fail (list != NULL);
1465
1466   value_list = g_string_sized_new (length * 128);
1467   for (i = 0; list[i] != NULL && i < length; i++)
1468     {
1469       gchar *value;
1470
1471       value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1472       g_string_append (value_list, value);
1473       g_string_append_c (value_list, key_file->list_separator);
1474
1475       g_free (value);
1476     }
1477
1478   g_key_file_set_value (key_file, group_name, key, value_list->str);
1479   g_string_free (value_list, TRUE);
1480 }
1481
1482 /**
1483  * g_key_file_set_locale_string:
1484  * @key_file: a #GKeyFile
1485  * @group_name: a group name
1486  * @key: a key
1487  * @locale: a locale
1488  * @string: a string
1489  *
1490  * Associates a string value for @key and @locale under
1491  * @group_name.  If the translation for @key cannot be found 
1492  * then it is created.
1493  *
1494  * Since: 2.6
1495  **/
1496 void
1497 g_key_file_set_locale_string (GKeyFile     *key_file,
1498                               const gchar  *group_name,
1499                               const gchar  *key,
1500                               const gchar  *locale,
1501                               const gchar  *string)
1502 {
1503   gchar *full_key, *value;
1504
1505   g_return_if_fail (key_file != NULL);
1506   g_return_if_fail (group_name != NULL);
1507   g_return_if_fail (key != NULL);
1508   g_return_if_fail (locale != NULL);
1509   g_return_if_fail (string != NULL);
1510
1511   value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1512   full_key = g_strdup_printf ("%s[%s]", key, locale);
1513   g_key_file_set_value (key_file, group_name, full_key, value);
1514   g_free (full_key);
1515   g_free (value);
1516 }
1517
1518 extern GSList *_g_compute_locale_variants (const gchar *locale);
1519
1520 /**
1521  * g_key_file_get_locale_string:
1522  * @key_file: a #GKeyFile
1523  * @group_name: a group name
1524  * @key: a key
1525  * @locale: a locale or %NULL
1526  * @error: return location for a #GError, or %NULL
1527  *
1528  * Returns the value associated with @key under @group_name
1529  * translated in the given @locale if available.  If @locale is
1530  * %NULL then the current locale is assumed. 
1531  *
1532  * If @key cannot be found then %NULL is returned and @error is set to
1533  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1534  * with @key cannot be interpreted or no suitable translation can
1535  * be found then the untranslated value is returned.
1536  *
1537  * Return value: a newly allocated string or %NULL if the specified 
1538  *   key cannot be found.
1539  *
1540  * Since: 2.6
1541  **/
1542 gchar *
1543 g_key_file_get_locale_string (GKeyFile     *key_file,
1544                               const gchar  *group_name,
1545                               const gchar  *key,
1546                               const gchar  *locale,
1547                               GError      **error)
1548 {
1549   gchar *candidate_key, *translated_value;
1550   GError *key_file_error;
1551   gchar **languages;
1552   gboolean free_languages = FALSE;
1553   gint i;
1554
1555   g_return_val_if_fail (key_file != NULL, NULL);
1556   g_return_val_if_fail (group_name != NULL, NULL);
1557   g_return_val_if_fail (key != NULL, NULL);
1558
1559   candidate_key = NULL;
1560   translated_value = NULL;
1561   key_file_error = NULL;
1562
1563   if (locale)
1564     {
1565       GSList *l, *list;
1566
1567       list = _g_compute_locale_variants (locale);
1568
1569       languages = g_new0 (gchar *, g_slist_length (list) + 1);
1570       for (l = list, i = 0; l; l = l->next, i++)
1571         languages[i] = l->data;
1572       languages[i] = NULL;
1573
1574       g_slist_free (list);
1575       free_languages = TRUE;
1576     }
1577   else
1578     {
1579       languages = (gchar **) g_get_language_names ();
1580       free_languages = FALSE;
1581     }
1582   
1583   for (i = 0; languages[i]; i++)
1584     {
1585       candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1586       
1587       translated_value = g_key_file_get_string (key_file,
1588                                                 group_name,
1589                                                 candidate_key, NULL);
1590       g_free (candidate_key);
1591
1592       if (translated_value && g_utf8_validate (translated_value, -1, NULL))
1593         break;
1594
1595       g_free (translated_value);
1596       translated_value = NULL;
1597    }
1598
1599   /* Fallback to untranslated key
1600    */
1601   if (!translated_value)
1602     {
1603       translated_value = g_key_file_get_string (key_file, group_name, key,
1604                                                 &key_file_error);
1605       
1606       if (!translated_value)
1607         g_propagate_error (error, key_file_error);
1608     }
1609
1610   if (free_languages)
1611     g_strfreev (languages);
1612
1613   return translated_value;
1614 }
1615
1616 /** 
1617  * g_key_file_get_locale_string_list: 
1618  * @key_file: a #GKeyFile
1619  * @group_name: a group name
1620  * @key: a key
1621  * @locale: a locale
1622  * @length: return location for the number of returned strings or %NULL
1623  * @error: return location for a #GError or %NULL
1624  *
1625  * Returns the values associated with @key under @group_name
1626  * translated in the given @locale if available.  If @locale is
1627  * %NULL then the current locale is assumed.
1628
1629  * If @key cannot be found then %NULL is returned and @error is set to
1630  * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1631  * with @key cannot be interpreted or no suitable translations
1632  * can be found then the untranslated values are returned.
1633  * The returned array is %NULL-terminated, so @length may optionally be %NULL.
1634  *
1635  * Return value: a newly allocated %NULL-terminated string array
1636  *   or %NULL if the key isn't found. The string array should be freed
1637  *   with g_strfreev().
1638  *
1639  * Since: 2.6
1640  **/
1641 gchar **
1642 g_key_file_get_locale_string_list (GKeyFile     *key_file,
1643                                    const gchar  *group_name,
1644                                    const gchar  *key,
1645                                    const gchar  *locale,
1646                                    gsize        *length,
1647                                    GError      **error)
1648 {
1649   GError *key_file_error;
1650   gchar **values, *value;
1651
1652   g_return_val_if_fail (key_file != NULL, NULL);
1653   g_return_val_if_fail (group_name != NULL, NULL);
1654   g_return_val_if_fail (key != NULL, NULL);
1655
1656   key_file_error = NULL;
1657
1658   value = g_key_file_get_locale_string (key_file, group_name, 
1659                                         key, locale,
1660                                         &key_file_error);
1661   
1662   if (key_file_error)
1663     g_propagate_error (error, key_file_error);
1664   
1665   if (!value)
1666     return NULL;
1667
1668   if (value[strlen (value) - 1] == ';')
1669     value[strlen (value) - 1] = '\0';
1670
1671   values = g_strsplit (value, ";", 0);
1672
1673   g_free (value);
1674
1675   if (length)
1676     *length = g_strv_length (values);
1677
1678   return values;
1679 }
1680
1681 /**
1682  * g_key_file_set_locale_string_list:
1683  * @key_file: a #GKeyFile
1684  * @group_name: a group name
1685  * @key: a key
1686  * @locale: a locale
1687  * @list: a %NULL-terminated array of locale string values
1688  * @length: the length of @list
1689  *
1690  * Associates a list of string values for @key and @locale under
1691  * @group_name.  If the translation for @key cannot be found then
1692  * it is created. 
1693  *
1694  * Since: 2.6
1695  **/
1696 void
1697 g_key_file_set_locale_string_list (GKeyFile            *key_file,
1698                                    const gchar         *group_name,
1699                                    const gchar         *key,
1700                                    const gchar         *locale,
1701                                    const gchar * const  list[],
1702                                    gsize                length)
1703 {
1704   GString *value_list;
1705   gchar *full_key;
1706   gsize i;
1707
1708   g_return_if_fail (key_file != NULL);
1709   g_return_if_fail (group_name != NULL);
1710   g_return_if_fail (key != NULL);
1711   g_return_if_fail (locale != NULL);
1712   g_return_if_fail (length != 0);
1713
1714   value_list = g_string_sized_new (length * 128);
1715   for (i = 0; list[i] != NULL && i < length; i++)
1716     {
1717       gchar *value;
1718       
1719       value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1720       
1721       g_string_append (value_list, value);
1722       g_string_append_c (value_list, ';');
1723
1724       g_free (value);
1725     }
1726
1727   full_key = g_strdup_printf ("%s[%s]", key, locale);
1728   g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1729   g_free (full_key);
1730   g_string_free (value_list, TRUE);
1731 }
1732
1733 /**
1734  * g_key_file_get_boolean:
1735  * @key_file: a #GKeyFile
1736  * @group_name: a group name
1737  * @key: a key
1738  * @error: return location for a #GError
1739  *
1740  * Returns the value associated with @key under @group_name as a
1741  * boolean. 
1742  *
1743  * If @key cannot be found then the return value is undefined and
1744  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1745  * the value associated with @key cannot be interpreted as a boolean
1746  * then the return value is also undefined and @error is set to
1747  * #G_KEY_FILE_ERROR_INVALID_VALUE.
1748  *
1749  * Return value: the value associated with the key as a boolean
1750  * Since: 2.6
1751  **/
1752 gboolean
1753 g_key_file_get_boolean (GKeyFile     *key_file,
1754                         const gchar  *group_name,
1755                         const gchar  *key,
1756                         GError      **error)
1757 {
1758   GError *key_file_error = NULL;
1759   gchar *value;
1760   gboolean bool_value;
1761
1762   g_return_val_if_fail (key_file != NULL, FALSE);
1763   g_return_val_if_fail (group_name != NULL, FALSE);
1764   g_return_val_if_fail (key != NULL, FALSE);
1765
1766   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1767
1768   if (!value)
1769     {
1770       g_propagate_error (error, key_file_error);
1771       return FALSE;
1772     }
1773
1774   bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1775                                                   &key_file_error);
1776   g_free (value);
1777
1778   if (key_file_error)
1779     {
1780       if (g_error_matches (key_file_error,
1781                            G_KEY_FILE_ERROR,
1782                            G_KEY_FILE_ERROR_INVALID_VALUE))
1783         {
1784           g_set_error (error, G_KEY_FILE_ERROR,
1785                        G_KEY_FILE_ERROR_INVALID_VALUE,
1786                        _("Key file contains key '%s' "
1787                          "which has value that cannot be interpreted."),
1788                        key);
1789           g_error_free (key_file_error);
1790         }
1791       else
1792         g_propagate_error (error, key_file_error);
1793     }
1794
1795   return bool_value;
1796 }
1797
1798 /**
1799  * g_key_file_set_boolean:
1800  * @key_file: a #GKeyFile
1801  * @group_name: a group name
1802  * @key: a key
1803  * @value: %TRUE or %FALSE
1804  *
1805  * Associates a new boolean value with @key under @group_name.
1806  * If @key cannot be found then it is created. 
1807  *
1808  * Since: 2.6
1809  **/
1810 void
1811 g_key_file_set_boolean (GKeyFile    *key_file,
1812                         const gchar *group_name,
1813                         const gchar *key,
1814                         gboolean     value)
1815 {
1816   gchar *result;
1817
1818   g_return_if_fail (key_file != NULL);
1819   g_return_if_fail (group_name != NULL);
1820   g_return_if_fail (key != NULL);
1821
1822   result = g_key_file_parse_boolean_as_value (key_file, value);
1823   g_key_file_set_value (key_file, group_name, key, result);
1824   g_free (result);
1825 }
1826
1827 /**
1828  * g_key_file_get_boolean_list:
1829  * @key_file: a #GKeyFile
1830  * @group_name: a group name
1831  * @key: a key
1832  * @length: the number of booleans returned
1833  * @error: return location for a #GError
1834  *
1835  * Returns the values associated with @key under @group_name as
1836  * booleans. If @group_name is %NULL, the start_group is used.
1837  *
1838  * If @key cannot be found then the return value is undefined and
1839  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1840  * the values associated with @key cannot be interpreted as booleans
1841  * then the return value is also undefined and @error is set to
1842  * #G_KEY_FILE_ERROR_INVALID_VALUE.
1843  *
1844  * Return value: the values associated with the key as a boolean
1845  * 
1846  * Since: 2.6
1847  **/
1848 gboolean *
1849 g_key_file_get_boolean_list (GKeyFile     *key_file,
1850                              const gchar  *group_name,
1851                              const gchar  *key,
1852                              gsize        *length,
1853                              GError      **error)
1854 {
1855   GError *key_file_error;
1856   gchar **values;
1857   gboolean *bool_values;
1858   gsize i, num_bools;
1859
1860   g_return_val_if_fail (key_file != NULL, NULL);
1861   g_return_val_if_fail (group_name != NULL, NULL);
1862   g_return_val_if_fail (key != NULL, NULL);
1863
1864   key_file_error = NULL;
1865
1866   values = g_key_file_get_string_list (key_file, group_name, key,
1867                                        &num_bools, &key_file_error);
1868
1869   if (key_file_error)
1870     g_propagate_error (error, key_file_error);
1871
1872   if (!values)
1873     return NULL;
1874
1875   bool_values = g_new0 (gboolean, num_bools);
1876
1877   for (i = 0; i < num_bools; i++)
1878     {
1879       bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1880                                                           values[i],
1881                                                           &key_file_error);
1882
1883       if (key_file_error)
1884         {
1885           g_propagate_error (error, key_file_error);
1886           g_strfreev (values);
1887           g_free (bool_values);
1888
1889           return NULL;
1890         }
1891     }
1892   g_strfreev (values);
1893
1894   if (length)
1895     *length = num_bools;
1896
1897   return bool_values;
1898 }
1899
1900 /**
1901  * g_key_file_set_boolean_list:
1902  * @key_file: a #GKeyFile
1903  * @group_name: a group name
1904  * @key: a key
1905  * @list: an array of boolean values
1906  * @length: length of @list
1907  *
1908  * Associates a list of boolean values with @key under
1909  * @group_name.  If @key cannot be found then it is created.
1910  * If @group_name is %NULL, the start_group is used.
1911  *
1912  * Since: 2.6
1913  **/
1914 void
1915 g_key_file_set_boolean_list (GKeyFile    *key_file,
1916                              const gchar *group_name,
1917                              const gchar *key,
1918                              gboolean     list[],
1919                              gsize        length)
1920 {
1921   GString *value_list;
1922   gsize i;
1923
1924   g_return_if_fail (key_file != NULL);
1925   g_return_if_fail (group_name != NULL);
1926   g_return_if_fail (key != NULL);
1927   g_return_if_fail (list != NULL);
1928
1929   value_list = g_string_sized_new (length * 8);
1930   for (i = 0; i < length; i++)
1931     {
1932       gchar *value;
1933
1934       value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1935
1936       g_string_append (value_list, value);
1937       g_string_append_c (value_list, key_file->list_separator);
1938
1939       g_free (value);
1940     }
1941
1942   g_key_file_set_value (key_file, group_name, key, value_list->str);
1943   g_string_free (value_list, TRUE);
1944 }
1945
1946 /**
1947  * g_key_file_get_integer:
1948  * @key_file: a #GKeyFile
1949  * @group_name: a group name
1950  * @key: a key
1951  * @error: return location for a #GError
1952  *
1953  * Returns the value associated with @key under @group_name as an
1954  * integer. If @group_name is %NULL, the start_group is used.
1955  *
1956  * If @key cannot be found then the return value is undefined and
1957  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1958  * the value associated with @key cannot be interpreted as an integer
1959  * then the return value is also undefined and @error is set to
1960  * #G_KEY_FILE_ERROR_INVALID_VALUE.
1961  *
1962  * Return value: the value associated with the key as an integer.
1963  *
1964  * Since: 2.6
1965  **/
1966 gint
1967 g_key_file_get_integer (GKeyFile     *key_file,
1968                         const gchar  *group_name,
1969                         const gchar  *key,
1970                         GError      **error)
1971 {
1972   GError *key_file_error;
1973   gchar *value;
1974   gint int_value;
1975
1976   g_return_val_if_fail (key_file != NULL, -1);
1977   g_return_val_if_fail (group_name != NULL, -1);
1978   g_return_val_if_fail (key != NULL, -1);
1979
1980   key_file_error = NULL;
1981
1982   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1983
1984   if (key_file_error)
1985     {
1986       g_propagate_error (error, key_file_error);
1987       return 0;
1988     }
1989
1990   int_value = g_key_file_parse_value_as_integer (key_file, value,
1991                                                  &key_file_error);
1992   g_free (value);
1993
1994   if (key_file_error)
1995     {
1996       if (g_error_matches (key_file_error,
1997                            G_KEY_FILE_ERROR,
1998                            G_KEY_FILE_ERROR_INVALID_VALUE))
1999         {
2000           g_set_error (error, G_KEY_FILE_ERROR,
2001                        G_KEY_FILE_ERROR_INVALID_VALUE,
2002                        _("Key file contains key '%s' in group '%s' "
2003                          "which has value that cannot be interpreted."), key, 
2004                        group_name);
2005           g_error_free (key_file_error);
2006         }
2007       else
2008         g_propagate_error (error, key_file_error);
2009     }
2010
2011   return int_value;
2012 }
2013
2014 /**
2015  * g_key_file_set_integer:
2016  * @key_file: a #GKeyFile
2017  * @group_name: a group name
2018  * @key: a key
2019  * @value: an integer value
2020  *
2021  * Associates a new integer value with @key under @group_name.
2022  * If @key cannot be found then it is created.
2023  *
2024  * Since: 2.6
2025  **/
2026 void
2027 g_key_file_set_integer (GKeyFile    *key_file,
2028                         const gchar *group_name,
2029                         const gchar *key,
2030                         gint         value)
2031 {
2032   gchar *result;
2033
2034   g_return_if_fail (key_file != NULL);
2035   g_return_if_fail (group_name != NULL);
2036   g_return_if_fail (key != NULL);
2037
2038   result = g_key_file_parse_integer_as_value (key_file, value);
2039   g_key_file_set_value (key_file, group_name, key, result);
2040   g_free (result);
2041 }
2042
2043 /**
2044  * g_key_file_get_integer_list:
2045  * @key_file: a #GKeyFile
2046  * @group_name: a group name
2047  * @key: a key
2048  * @length: the number of integers returned
2049  * @error: return location for a #GError
2050  *
2051  * Returns the values associated with @key under @group_name as
2052  * integers. 
2053  *
2054  * If @key cannot be found then the return value is undefined and
2055  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2056  * the values associated with @key cannot be interpreted as integers
2057  * then the return value is also undefined and @error is set to
2058  * #G_KEY_FILE_ERROR_INVALID_VALUE.
2059  *
2060  * Return value: the values associated with the key as a integer
2061  *
2062  * Since: 2.6
2063  **/
2064 gint *
2065 g_key_file_get_integer_list (GKeyFile     *key_file,
2066                              const gchar  *group_name,
2067                              const gchar  *key,
2068                              gsize        *length,
2069                              GError      **error)
2070 {
2071   GError *key_file_error = NULL;
2072   gchar **values;
2073   gint *int_values;
2074   gsize i, num_ints;
2075
2076   g_return_val_if_fail (key_file != NULL, NULL);
2077   g_return_val_if_fail (group_name != NULL, NULL);
2078   g_return_val_if_fail (key != NULL, NULL);
2079
2080   values = g_key_file_get_string_list (key_file, group_name, key,
2081                                        &num_ints, &key_file_error);
2082
2083   if (key_file_error)
2084     g_propagate_error (error, key_file_error);
2085
2086   if (!values)
2087     return NULL;
2088
2089   int_values = g_new0 (gint, num_ints);
2090
2091   for (i = 0; i < num_ints; i++)
2092     {
2093       int_values[i] = g_key_file_parse_value_as_integer (key_file,
2094                                                          values[i],
2095                                                          &key_file_error);
2096
2097       if (key_file_error)
2098         {
2099           g_propagate_error (error, key_file_error);
2100           g_strfreev (values);
2101           g_free (int_values);
2102
2103           return NULL;
2104         }
2105     }
2106   g_strfreev (values);
2107
2108   if (length)
2109     *length = num_ints;
2110
2111   return int_values;
2112 }
2113
2114 /**
2115  * g_key_file_set_integer_list:
2116  * @key_file: a #GKeyFile
2117  * @group_name: a group name
2118  * @key: a key
2119  * @list: an array of integer values
2120  * @length: number of integer values in @list
2121  *
2122  * Associates a list of integer values with @key under
2123  * @group_name.  If @key cannot be found then it is created.
2124  *
2125  * Since: 2.6
2126  **/
2127 void
2128 g_key_file_set_integer_list (GKeyFile     *key_file,
2129                              const gchar  *group_name,
2130                              const gchar  *key,
2131                              gint          list[],
2132                              gsize         length)
2133 {
2134   GString *values;
2135   gsize i;
2136
2137   g_return_if_fail (key_file != NULL);
2138   g_return_if_fail (group_name != NULL);
2139   g_return_if_fail (key != NULL);
2140   g_return_if_fail (list != NULL);
2141
2142   values = g_string_sized_new (length * 16);
2143   for (i = 0; i < length; i++)
2144     {
2145       gchar *value;
2146
2147       value = g_key_file_parse_integer_as_value (key_file, list[i]);
2148
2149       g_string_append (values, value);
2150       g_string_append_c (values, ';');
2151
2152       g_free (value);
2153     }
2154
2155   g_key_file_set_value (key_file, group_name, key, values->str);
2156   g_string_free (values, TRUE);
2157 }
2158
2159 /**
2160  * g_key_file_get_double:
2161  * @key_file: a #GKeyFile
2162  * @group_name: a group name
2163  * @key: a key
2164  * @error: return location for a #GError
2165  *
2166  * Returns the value associated with @key under @group_name as an
2167  * integer. If @group_name is %NULL, the start_group is used.
2168  *
2169  * If @key cannot be found then the return value is undefined and
2170  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2171  * the value associated with @key cannot be interpreted as a double
2172  * then the return value is also undefined and @error is set to
2173  * #G_KEY_FILE_ERROR_INVALID_VALUE.
2174  *
2175  * Return value: the value associated with the key as a double.
2176  *
2177  * Since: 2.12
2178  **/
2179 gdouble
2180 g_key_file_get_double  (GKeyFile     *key_file,
2181                         const gchar  *group_name,
2182                         const gchar  *key,
2183                         GError      **error)
2184 {
2185   GError *key_file_error;
2186   gchar *value;
2187   gdouble double_value;
2188
2189   g_return_val_if_fail (key_file != NULL, -1);
2190   g_return_val_if_fail (group_name != NULL, -1);
2191   g_return_val_if_fail (key != NULL, -1);
2192
2193   key_file_error = NULL;
2194
2195   value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2196
2197   if (key_file_error)
2198     {
2199       g_propagate_error (error, key_file_error);
2200       return 0;
2201     }
2202
2203   double_value = g_key_file_parse_value_as_double (key_file, value,
2204                                                   &key_file_error);
2205   g_free (value);
2206
2207   if (key_file_error)
2208     {
2209       if (g_error_matches (key_file_error,
2210                            G_KEY_FILE_ERROR,
2211                            G_KEY_FILE_ERROR_INVALID_VALUE))
2212         {
2213           g_set_error (error, G_KEY_FILE_ERROR,
2214                        G_KEY_FILE_ERROR_INVALID_VALUE,
2215                        _("Key file contains key '%s' in group '%s' "
2216                          "which has value that cannot be interpreted."), key,
2217                        group_name);
2218           g_error_free (key_file_error);
2219         }
2220       else
2221         g_propagate_error (error, key_file_error);
2222     }
2223
2224   return double_value;
2225 }
2226
2227 /**
2228  * g_key_file_set_double:
2229  * @key_file: a #GKeyFile
2230  * @group_name: a group name
2231  * @key: a key
2232  * @value: an double value
2233  *
2234  * Associates a new double value with @key under @group_name.
2235  * If @key cannot be found then it is created. If @group_name
2236  * is %NULL, the start group is used.
2237  *
2238  * Since: 2.12
2239  **/
2240 void
2241 g_key_file_set_double  (GKeyFile    *key_file,
2242                         const gchar *group_name,
2243                         const gchar *key,
2244                         gdouble      value)
2245 {
2246   gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2247
2248   g_return_if_fail (key_file != NULL);
2249   g_return_if_fail (group_name != NULL);
2250   g_return_if_fail (key != NULL);
2251
2252   g_ascii_dtostr ( result, sizeof (result), value );
2253   g_key_file_set_value (key_file, group_name, key, result);
2254 }
2255
2256 /**
2257  * g_key_file_get_double_list:
2258  * @key_file: a #GKeyFile
2259  * @group_name: a group name
2260  * @key: a key
2261  * @length: the number of doubles returned
2262  * @error: return location for a #GError
2263  *
2264  * Returns the values associated with @key under @group_name as
2265  * doubles. If @group_name is %NULL, the start group is used.
2266  *
2267  * If @key cannot be found then the return value is undefined and
2268  * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2269  * the values associated with @key cannot be interpreted as doubles
2270  * then the return value is also undefined and @error is set to
2271  * #G_KEY_FILE_ERROR_INVALID_VALUE.
2272  *
2273  * Return value: the values associated with the key as a double
2274  *
2275  * Since: 2.12
2276  **/
2277 gdouble *
2278 g_key_file_get_double_list  (GKeyFile     *key_file,
2279                              const gchar  *group_name,
2280                              const gchar  *key,
2281                              gsize        *length,
2282                              GError      **error)
2283 {
2284   GError *key_file_error = NULL;
2285   gchar **values;
2286   gdouble *double_values;
2287   gsize i, num_doubles;
2288
2289   g_return_val_if_fail (key_file != NULL, NULL);
2290   g_return_val_if_fail (group_name != NULL, NULL);
2291   g_return_val_if_fail (key != NULL, NULL);
2292
2293   values = g_key_file_get_string_list (key_file, group_name, key,
2294                                        &num_doubles, &key_file_error);
2295
2296   if (key_file_error)
2297     g_propagate_error (error, key_file_error);
2298
2299   if (!values)
2300     return NULL;
2301
2302   double_values = g_new0 (gdouble, num_doubles);
2303
2304   for (i = 0; i < num_doubles; i++)
2305     {
2306       double_values[i] = g_key_file_parse_value_as_double (key_file,
2307                                                            values[i],
2308                                                            &key_file_error);
2309
2310       if (key_file_error)
2311         {
2312           g_propagate_error (error, key_file_error);
2313           g_strfreev (values);
2314           g_free (double_values);
2315
2316           return NULL;
2317         }
2318     }
2319   g_strfreev (values);
2320
2321   if (length)
2322     *length = num_doubles;
2323
2324   return double_values;
2325 }
2326
2327 /**
2328  * g_key_file_set_double_list:
2329  * @key_file: a #GKeyFile
2330  * @group_name: a group name
2331  * @key: a key
2332  * @list: an array of double values
2333  * @length: number of double values in @list
2334  *
2335  * Associates a list of double values with @key under
2336  * @group_name.  If @key cannot be found then it is created.
2337  * If @group_name is %NULL the start group is used.
2338  *
2339  * Since: 2.21
2340  **/
2341 void
2342 g_key_file_set_double_list (GKeyFile     *key_file,
2343                             const gchar  *group_name,
2344                             const gchar  *key,
2345                             gdouble       list[],
2346                             gsize         length)
2347 {
2348   GString *values;
2349   gsize i;
2350
2351   g_return_if_fail (key_file != NULL);
2352   g_return_if_fail (group_name != NULL);
2353   g_return_if_fail (key != NULL);
2354   g_return_if_fail (list != NULL);
2355
2356   values = g_string_sized_new (length * 16);
2357   for (i = 0; i < length; i++)
2358     {
2359       gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2360
2361       g_ascii_dtostr( result, sizeof (result), list[i] );
2362
2363       g_string_append (values, result);
2364       g_string_append_c (values, ';');
2365     }
2366
2367   g_key_file_set_value (key_file, group_name, key, values->str);
2368   g_string_free (values, TRUE);
2369 }
2370
2371 static void
2372 g_key_file_set_key_comment (GKeyFile             *key_file,
2373                             const gchar          *group_name,
2374                             const gchar          *key,
2375                             const gchar          *comment,
2376                             GError              **error)
2377 {
2378   GKeyFileGroup *group;
2379   GKeyFileKeyValuePair *pair;
2380   GList *key_node, *comment_node, *tmp;
2381   
2382   group = g_key_file_lookup_group (key_file, group_name);
2383   if (!group)
2384     {
2385       g_set_error (error, G_KEY_FILE_ERROR,
2386                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2387                    _("Key file does not have group '%s'"),
2388                    group_name ? group_name : "(null)");
2389
2390       return;
2391     }
2392
2393   /* First find the key the comments are supposed to be
2394    * associated with
2395    */
2396   key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2397
2398   if (key_node == NULL)
2399     {
2400       g_set_error (error, G_KEY_FILE_ERROR,
2401                    G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2402                    _("Key file does not have key '%s' in group '%s'"),
2403                    key, group->name);
2404       return;
2405     }
2406
2407   /* Then find all the comments already associated with the
2408    * key and free them
2409    */
2410   tmp = key_node->next;
2411   while (tmp != NULL)
2412     {
2413       GKeyFileKeyValuePair *pair;
2414
2415       pair = (GKeyFileKeyValuePair *) tmp->data;
2416
2417       if (pair->key != NULL)
2418         break;
2419
2420       comment_node = tmp;
2421       tmp = tmp->next;
2422       g_key_file_remove_key_value_pair_node (key_file, group,
2423                                              comment_node); 
2424     }
2425
2426   if (comment == NULL)
2427     return;
2428
2429   /* Now we can add our new comment
2430    */
2431   pair = g_new0 (GKeyFileKeyValuePair, 1);
2432   
2433   pair->key = NULL;
2434   pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2435   
2436   key_node = g_list_insert (key_node, pair, 1);
2437 }
2438
2439 static void
2440 g_key_file_set_group_comment (GKeyFile             *key_file,
2441                               const gchar          *group_name,
2442                               const gchar          *comment,
2443                               GError              **error)
2444 {
2445   GKeyFileGroup *group;
2446   
2447   group = g_key_file_lookup_group (key_file, group_name);
2448   if (!group)
2449     {
2450       g_set_error (error, G_KEY_FILE_ERROR,
2451                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2452                    _("Key file does not have group '%s'"),
2453                    group_name ? group_name : "(null)");
2454
2455       return;
2456     }
2457
2458   /* First remove any existing comment
2459    */
2460   if (group->comment)
2461     {
2462       g_key_file_key_value_pair_free (group->comment);
2463       group->comment = NULL;
2464     }
2465
2466   if (comment == NULL)
2467     return;
2468
2469   /* Now we can add our new comment
2470    */
2471   group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2472   
2473   group->comment->key = NULL;
2474   group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2475 }
2476
2477 static void
2478 g_key_file_set_top_comment (GKeyFile             *key_file,
2479                             const gchar          *comment,
2480                             GError              **error)
2481 {
2482   GList *group_node;
2483   GKeyFileGroup *group;
2484   GKeyFileKeyValuePair *pair;
2485
2486   /* The last group in the list should be the top (comments only)
2487    * group in the file
2488    */
2489   g_assert (key_file->groups != NULL);
2490   group_node = g_list_last (key_file->groups);
2491   group = (GKeyFileGroup *) group_node->data;
2492   g_assert (group->name == NULL);
2493
2494   /* Note all keys must be comments at the top of
2495    * the file, so we can just free it all.
2496    */
2497   if (group->key_value_pairs != NULL)
2498     {
2499       g_list_foreach (group->key_value_pairs, 
2500                       (GFunc) g_key_file_key_value_pair_free, 
2501                       NULL);
2502       g_list_free (group->key_value_pairs);
2503       group->key_value_pairs = NULL;
2504     }
2505
2506   if (comment == NULL)
2507      return;
2508
2509   pair = g_new0 (GKeyFileKeyValuePair, 1);
2510   
2511   pair->key = NULL;
2512   pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2513   
2514   group->key_value_pairs =
2515     g_list_prepend (group->key_value_pairs, pair);
2516 }
2517
2518 /**
2519  * g_key_file_set_comment:
2520  * @key_file: a #GKeyFile
2521  * @group_name: a group name, or %NULL
2522  * @key: a key
2523  * @comment: a comment
2524  * @error: return location for a #GError
2525  *
2526  * Places a comment above @key from @group_name.
2527  * @group_name. If @key is %NULL then @comment will
2528  * be written above @group_name.  If both @key
2529  * and @group_name are NULL, then @comment will
2530  * be written above the first group in the file.
2531  *
2532  * Since: 2.6
2533  **/
2534 void
2535 g_key_file_set_comment (GKeyFile             *key_file,
2536                         const gchar          *group_name,
2537                         const gchar          *key,
2538                         const gchar          *comment,
2539                         GError              **error)
2540 {
2541   g_return_if_fail (key_file != NULL);
2542
2543   if (group_name != NULL && key != NULL)
2544     g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2545   else if (group_name != NULL)
2546     g_key_file_set_group_comment (key_file, group_name, comment, error);
2547   else
2548     g_key_file_set_top_comment (key_file, comment, error);
2549
2550   if (comment != NULL)
2551     key_file->approximate_size += strlen (comment);
2552 }
2553
2554 static gchar *
2555 g_key_file_get_key_comment (GKeyFile             *key_file,
2556                             const gchar          *group_name,
2557                             const gchar          *key,
2558                             GError              **error)
2559 {
2560   GKeyFileGroup *group;
2561   GKeyFileKeyValuePair *pair;
2562   GList *key_node, *tmp;
2563   GString *string;
2564   gchar *comment;
2565
2566   group = g_key_file_lookup_group (key_file, group_name);
2567   if (!group)
2568     {
2569       g_set_error (error, G_KEY_FILE_ERROR,
2570                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2571                    _("Key file does not have group '%s'"),
2572                    group_name ? group_name : "(null)");
2573
2574       return NULL;
2575     }
2576
2577   /* First find the key the comments are supposed to be
2578    * associated with
2579    */
2580   key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2581
2582   if (key_node == NULL)
2583     {
2584       g_set_error (error, G_KEY_FILE_ERROR,
2585                    G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2586                    _("Key file does not have key '%s' in group '%s'"),
2587                    key, group->name);
2588       return NULL;
2589     }
2590
2591   string = NULL;
2592
2593   /* Then find all the comments already associated with the
2594    * key and concatentate them.
2595    */
2596   tmp = key_node->next;
2597   if (!key_node->next)
2598     return NULL;
2599
2600   pair = (GKeyFileKeyValuePair *) tmp->data;
2601   if (pair->key != NULL)
2602     return NULL;
2603
2604   while (tmp->next)
2605     {
2606       pair = (GKeyFileKeyValuePair *) tmp->next->data;
2607       
2608       if (pair->key != NULL)
2609         break;
2610
2611       tmp = tmp->next;
2612     }
2613
2614   while (tmp != key_node)
2615     {
2616       GKeyFileKeyValuePair *pair;
2617       
2618       pair = (GKeyFileKeyValuePair *) tmp->data;
2619       
2620       if (string == NULL)
2621         string = g_string_sized_new (512);
2622       
2623       comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2624       g_string_append (string, comment);
2625       g_free (comment);
2626       
2627       tmp = tmp->prev;
2628     }
2629
2630   if (string != NULL)
2631     {
2632       comment = string->str;
2633       g_string_free (string, FALSE);
2634     }
2635   else
2636     comment = NULL;
2637
2638   return comment;
2639 }
2640
2641 static gchar *
2642 get_group_comment (GKeyFile       *key_file,
2643                    GKeyFileGroup  *group,
2644                    GError        **error)
2645 {
2646   GString *string;
2647   GList *tmp;
2648   gchar *comment;
2649
2650   string = NULL;
2651
2652   tmp = group->key_value_pairs;
2653   while (tmp)
2654     {
2655       GKeyFileKeyValuePair *pair;
2656
2657       pair = (GKeyFileKeyValuePair *) tmp->data;
2658
2659       if (pair->key != NULL)
2660         {
2661           tmp = tmp->prev;
2662           break;
2663         }
2664
2665       if (tmp->next == NULL)
2666         break;
2667
2668       tmp = tmp->next;
2669     }
2670   
2671   while (tmp != NULL)
2672     {
2673       GKeyFileKeyValuePair *pair;
2674
2675       pair = (GKeyFileKeyValuePair *) tmp->data;
2676
2677       if (string == NULL)
2678         string = g_string_sized_new (512);
2679
2680       comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2681       g_string_append (string, comment);
2682       g_free (comment);
2683
2684       tmp = tmp->prev;
2685     }
2686
2687   if (string != NULL)
2688     return g_string_free (string, FALSE);
2689
2690   return NULL;
2691 }
2692
2693 static gchar *
2694 g_key_file_get_group_comment (GKeyFile             *key_file,
2695                               const gchar          *group_name,
2696                               GError              **error)
2697 {
2698   GList *group_node;
2699   GKeyFileGroup *group;
2700   
2701   group_node = g_key_file_lookup_group_node (key_file, group_name);
2702   if (!group_node)
2703     {
2704       g_set_error (error, G_KEY_FILE_ERROR,
2705                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2706                    _("Key file does not have group '%s'"),
2707                    group_name ? group_name : "(null)");
2708
2709       return NULL;
2710     }
2711
2712   group = (GKeyFileGroup *)group_node->data;
2713   if (group->comment)
2714     return g_strdup (group->comment->value);
2715   
2716   group_node = group_node->next;
2717   group = (GKeyFileGroup *)group_node->data;  
2718   return get_group_comment (key_file, group, error);
2719 }
2720
2721 static gchar *
2722 g_key_file_get_top_comment (GKeyFile             *key_file,
2723                             GError              **error)
2724 {
2725   GList *group_node;
2726   GKeyFileGroup *group;
2727
2728   /* The last group in the list should be the top (comments only)
2729    * group in the file
2730    */
2731   g_assert (key_file->groups != NULL);
2732   group_node = g_list_last (key_file->groups);
2733   group = (GKeyFileGroup *) group_node->data;
2734   g_assert (group->name == NULL);
2735
2736   return get_group_comment (key_file, group, error);
2737 }
2738
2739 /**
2740  * g_key_file_get_comment:
2741  * @key_file: a #GKeyFile
2742  * @group_name: a group name, or %NULL
2743  * @key: a key
2744  * @error: return location for a #GError
2745  *
2746  * Retrieves a comment above @key from @group_name.
2747  * @group_name. If @key is %NULL then @comment will
2748  * be read from above @group_name.  If both @key
2749  * and @group_name are NULL, then @comment will
2750  * be read from above the first group in the file.
2751  *
2752  * Returns: a comment that should be freed with g_free()
2753  *
2754  * Since: 2.6
2755  **/
2756 gchar * 
2757 g_key_file_get_comment (GKeyFile             *key_file,
2758                         const gchar          *group_name,
2759                         const gchar          *key,
2760                         GError              **error)
2761 {
2762   g_return_val_if_fail (key_file != NULL, NULL);
2763
2764   if (group_name != NULL && key != NULL)
2765     return g_key_file_get_key_comment (key_file, group_name, key, error);
2766   else if (group_name != NULL)
2767     return g_key_file_get_group_comment (key_file, group_name, error);
2768   else
2769     return g_key_file_get_top_comment (key_file, error);
2770 }
2771
2772 /**
2773  * g_key_file_remove_comment:
2774  * @key_file: a #GKeyFile
2775  * @group_name: a group name, or %NULL
2776  * @key: a key
2777  * @error: return location for a #GError
2778  *
2779  * Removes a comment above @key from @group_name.
2780  * @group_name. If @key is %NULL then @comment will
2781  * be written above @group_name.  If both @key
2782  * and @group_name are NULL, then @comment will
2783  * be written above the first group in the file.
2784  *
2785  * Since: 2.6
2786  **/
2787
2788 void
2789 g_key_file_remove_comment (GKeyFile             *key_file,
2790                            const gchar          *group_name,
2791                            const gchar          *key,
2792                            GError              **error)
2793 {
2794   g_return_if_fail (key_file != NULL);
2795
2796   if (group_name != NULL && key != NULL)
2797     g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2798   else if (group_name != NULL)
2799     g_key_file_set_group_comment (key_file, group_name, NULL, error);
2800   else
2801     g_key_file_set_top_comment (key_file, NULL, error);
2802 }
2803
2804 /**
2805  * g_key_file_has_group:
2806  * @key_file: a #GKeyFile
2807  * @group_name: a group name
2808  *
2809  * Looks whether the key file has the group @group_name.
2810  *
2811  * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2812  * otherwise.
2813  * Since: 2.6
2814  **/
2815 gboolean
2816 g_key_file_has_group (GKeyFile    *key_file,
2817                       const gchar *group_name)
2818 {
2819   g_return_val_if_fail (key_file != NULL, FALSE);
2820   g_return_val_if_fail (group_name != NULL, FALSE);
2821
2822   return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2823 }
2824
2825 /**
2826  * g_key_file_has_key:
2827  * @key_file: a #GKeyFile
2828  * @group_name: a group name
2829  * @key: a key name
2830  * @error: return location for a #GError
2831  *
2832  * Looks whether the key file has the key @key in the group
2833  * @group_name. 
2834  *
2835  * Return value: %TRUE if @key is a part of @group_name, %FALSE
2836  * otherwise.
2837  *
2838  * Since: 2.6
2839  **/
2840 gboolean
2841 g_key_file_has_key (GKeyFile     *key_file,
2842                     const gchar  *group_name,
2843                     const gchar  *key,
2844                     GError      **error)
2845 {
2846   GKeyFileKeyValuePair *pair;
2847   GKeyFileGroup *group;
2848
2849   g_return_val_if_fail (key_file != NULL, FALSE);
2850   g_return_val_if_fail (group_name != NULL, FALSE);
2851   g_return_val_if_fail (key != NULL, FALSE);
2852
2853   group = g_key_file_lookup_group (key_file, group_name);
2854
2855   if (!group)
2856     {
2857       g_set_error (error, G_KEY_FILE_ERROR,
2858                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2859                    _("Key file does not have group '%s'"),
2860                    group_name ? group_name : "(null)");
2861
2862       return FALSE;
2863     }
2864
2865   pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2866
2867   return pair != NULL;
2868 }
2869
2870 static void
2871 g_key_file_add_group (GKeyFile    *key_file,
2872                       const gchar *group_name)
2873 {
2874   GKeyFileGroup *group;
2875
2876   g_return_if_fail (key_file != NULL);
2877   g_return_if_fail (group_name != NULL);
2878
2879   if (g_key_file_lookup_group_node (key_file, group_name) != NULL)
2880     return;
2881
2882   group = g_new0 (GKeyFileGroup, 1);
2883   group->name = g_strdup (group_name);
2884   group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2885   key_file->groups = g_list_prepend (key_file->groups, group);
2886   key_file->approximate_size += strlen (group_name) + 3;
2887   key_file->current_group = group;
2888
2889   if (key_file->start_group == NULL)
2890     key_file->start_group = group;
2891 }
2892
2893 static void
2894 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2895 {
2896   if (pair != NULL)
2897     {
2898       g_free (pair->key);
2899       g_free (pair->value);
2900       g_free (pair);
2901     }
2902 }
2903
2904 /* Be careful not to call this function on a node with data in the
2905  * lookup map without removing it from the lookup map, first.
2906  *
2907  * Some current cases where this warning is not a concern are
2908  * when:
2909  *   - the node being removed is a comment node
2910  *   - the entire lookup map is getting destroyed soon after
2911  *     anyway.
2912  */ 
2913 static void
2914 g_key_file_remove_key_value_pair_node (GKeyFile      *key_file,
2915                                        GKeyFileGroup *group,
2916                                        GList         *pair_node)
2917 {
2918
2919   GKeyFileKeyValuePair *pair;
2920
2921   pair = (GKeyFileKeyValuePair *) pair_node->data;
2922
2923   group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2924
2925   if (pair->key != NULL)
2926     key_file->approximate_size -= strlen (pair->key) + 1;
2927
2928   g_assert (pair->value != NULL);
2929   key_file->approximate_size -= strlen (pair->value);
2930
2931   g_key_file_key_value_pair_free (pair);
2932
2933   g_list_free_1 (pair_node);
2934 }
2935
2936 static void
2937 g_key_file_remove_group_node (GKeyFile *key_file,
2938                               GList    *group_node)
2939 {
2940   GKeyFileGroup *group;
2941   GList *tmp;
2942
2943   group = (GKeyFileGroup *) group_node->data;
2944
2945   /* If the current group gets deleted make the current group the last
2946    * added group.
2947    */
2948   if (key_file->current_group == group)
2949     {
2950       /* groups should always contain at least the top comment group,
2951        * unless g_key_file_clear has been called
2952        */
2953       if (key_file->groups)
2954         key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2955       else
2956         key_file->current_group = NULL;
2957     }
2958
2959   /* If the start group gets deleted make the start group the first
2960    * added group.
2961    */
2962   if (key_file->start_group == group)
2963     {
2964       tmp = g_list_last (key_file->groups);
2965       while (tmp != NULL)
2966         {
2967           if (tmp != group_node &&
2968               ((GKeyFileGroup *) tmp->data)->name != NULL)
2969             break;
2970
2971           tmp = tmp->prev;
2972         }
2973
2974       if (tmp)
2975         key_file->start_group = (GKeyFileGroup *) tmp->data;
2976       else
2977         key_file->start_group = NULL;
2978     }
2979
2980   key_file->groups = g_list_remove_link (key_file->groups, group_node);
2981
2982   if (group->name != NULL)
2983     key_file->approximate_size -= strlen (group->name) + 3;
2984
2985   tmp = group->key_value_pairs;
2986   while (tmp != NULL)
2987     {
2988       GList *pair_node;
2989
2990       pair_node = tmp;
2991       tmp = tmp->next;
2992       g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
2993     }
2994
2995   g_assert (group->key_value_pairs == NULL);
2996
2997   if (group->lookup_map)
2998     {
2999       g_hash_table_destroy (group->lookup_map);
3000       group->lookup_map = NULL;
3001     }
3002
3003   g_free ((gchar *) group->name);
3004   g_free (group);
3005   g_list_free_1 (group_node);
3006 }
3007
3008 /**
3009  * g_key_file_remove_group:
3010  * @key_file: a #GKeyFile
3011  * @group_name: a group name
3012  * @error: return location for a #GError or %NULL
3013  *
3014  * Removes the specified group, @group_name, 
3015  * from the key file. 
3016  *
3017  * Since: 2.6
3018  **/
3019 void
3020 g_key_file_remove_group (GKeyFile     *key_file,
3021                          const gchar  *group_name,
3022                          GError      **error)
3023 {
3024   GList *group_node;
3025
3026   g_return_if_fail (key_file != NULL);
3027   g_return_if_fail (group_name != NULL);
3028
3029   group_node = g_key_file_lookup_group_node (key_file, group_name);
3030
3031   if (!group_node)
3032     {
3033       g_set_error (error, G_KEY_FILE_ERROR,
3034                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3035                    _("Key file does not have group '%s'"),
3036                    group_name);
3037       return;
3038     }
3039
3040     g_key_file_remove_group_node (key_file, group_node);
3041 }
3042
3043 static void
3044 g_key_file_add_key (GKeyFile      *key_file,
3045                     GKeyFileGroup *group,
3046                     const gchar   *key,
3047                     const gchar   *value)
3048 {
3049   GKeyFileKeyValuePair *pair;
3050
3051   pair = g_new0 (GKeyFileKeyValuePair, 1);
3052
3053   pair->key = g_strdup (key);
3054   pair->value = g_strdup (value);
3055
3056   g_hash_table_replace (group->lookup_map, pair->key, pair);
3057   group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3058   key_file->approximate_size += strlen (key) + strlen (value) + 2;
3059 }
3060
3061 /**
3062  * g_key_file_remove_key:
3063  * @key_file: a #GKeyFile
3064  * @group_name: a group name
3065  * @key: a key name to remove
3066  * @error: return location for a #GError or %NULL
3067  *
3068  * Removes @key in @group_name from the key file. 
3069  *
3070  * Since: 2.6
3071  **/
3072 void
3073 g_key_file_remove_key (GKeyFile     *key_file,
3074                        const gchar  *group_name,
3075                        const gchar  *key,
3076                        GError      **error)
3077 {
3078   GKeyFileGroup *group;
3079   GKeyFileKeyValuePair *pair;
3080
3081   g_return_if_fail (key_file != NULL);
3082   g_return_if_fail (group_name != NULL);
3083   g_return_if_fail (key != NULL);
3084
3085   pair = NULL;
3086
3087   group = g_key_file_lookup_group (key_file, group_name);
3088   if (!group)
3089     {
3090       g_set_error (error, G_KEY_FILE_ERROR,
3091                    G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3092                    _("Key file does not have group '%s'"),
3093                    group_name ? group_name : "(null)");
3094       return;
3095     }
3096
3097   pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3098
3099   if (!pair)
3100     {
3101       g_set_error (error, G_KEY_FILE_ERROR,
3102                    G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3103                    _("Key file does not have key '%s' in group '%s'"),
3104                    key, group->name);
3105       return;
3106     }
3107
3108   key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3109
3110   group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3111   g_hash_table_remove (group->lookup_map, pair->key);  
3112   g_key_file_key_value_pair_free (pair);
3113 }
3114
3115 static GList *
3116 g_key_file_lookup_group_node (GKeyFile    *key_file,
3117                               const gchar *group_name)
3118 {
3119   GKeyFileGroup *group;
3120   GList *tmp;
3121
3122   for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3123     {
3124       group = (GKeyFileGroup *) tmp->data;
3125
3126       if (group && group->name && strcmp (group->name, group_name) == 0)
3127         break;
3128     }
3129
3130   return tmp;
3131 }
3132
3133 static GKeyFileGroup *
3134 g_key_file_lookup_group (GKeyFile    *key_file,
3135                          const gchar *group_name)
3136 {
3137   GList *group_node;
3138
3139   group_node = g_key_file_lookup_group_node (key_file, group_name);
3140
3141   if (group_node != NULL)
3142     return (GKeyFileGroup *) group_node->data; 
3143
3144   return NULL;
3145 }
3146
3147 static GList *
3148 g_key_file_lookup_key_value_pair_node (GKeyFile       *key_file,
3149                                        GKeyFileGroup  *group,
3150                                        const gchar    *key)
3151 {
3152   GList *key_node;
3153
3154   for (key_node = group->key_value_pairs;
3155        key_node != NULL;
3156        key_node = key_node->next)
3157     {
3158       GKeyFileKeyValuePair *pair;
3159
3160       pair = (GKeyFileKeyValuePair *) key_node->data; 
3161
3162       if (pair->key && strcmp (pair->key, key) == 0)
3163         break;
3164     }
3165
3166   return key_node;
3167 }
3168
3169 static GKeyFileKeyValuePair *
3170 g_key_file_lookup_key_value_pair (GKeyFile      *key_file,
3171                                   GKeyFileGroup *group,
3172                                   const gchar   *key)
3173 {
3174   return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3175 }
3176
3177 /* Lines starting with # or consisting entirely of whitespace are merely
3178  * recorded, not parsed. This function assumes all leading whitespace
3179  * has been stripped.
3180  */
3181 static gboolean
3182 g_key_file_line_is_comment (const gchar *line)
3183 {
3184   return (*line == '#' || *line == '\0' || *line == '\n');
3185 }
3186
3187 /* A group in a key file is made up of a starting '[' followed by one
3188  * or more letters making up the group name followed by ']'.
3189  */
3190 static gboolean
3191 g_key_file_line_is_group (const gchar *line)
3192 {
3193   gchar *p;
3194
3195   p = (gchar *) line;
3196   if (*p != '[')
3197     return FALSE;
3198
3199   p = g_utf8_next_char (p);
3200
3201   /* Group name must be non-empty
3202    */
3203   if (!*p || *p == ']')
3204     return FALSE;
3205
3206   while (*p && *p != ']')
3207     p = g_utf8_next_char (p);
3208
3209   if (!*p)
3210     return FALSE;
3211
3212   return TRUE;
3213 }
3214
3215 static gboolean
3216 g_key_file_line_is_key_value_pair (const gchar *line)
3217 {
3218   gchar *p;
3219
3220   p = (gchar *) g_utf8_strchr (line, -1, '=');
3221
3222   if (!p)
3223     return FALSE;
3224
3225   /* Key must be non-empty
3226    */
3227   if (*p == line[0])
3228     return FALSE;
3229
3230   return TRUE;
3231 }
3232
3233 static gchar *
3234 g_key_file_parse_value_as_string (GKeyFile     *key_file,
3235                                   const gchar  *value,
3236                                   GSList      **pieces,
3237                                   GError      **error)
3238 {
3239   gchar *string_value, *p, *q0, *q;
3240
3241   string_value = g_new0 (gchar, strlen (value) + 1);
3242
3243   p = (gchar *) value;
3244   q0 = q = string_value;
3245   while (*p)
3246     {
3247       if (*p == '\\')
3248         {
3249           p++;
3250
3251           switch (*p)
3252             {
3253             case 's':
3254               *q = ' ';
3255               break;
3256
3257             case 'n':
3258               *q = '\n';
3259               break;
3260
3261             case 't':
3262               *q = '\t';
3263               break;
3264
3265             case 'r':
3266               *q = '\r';
3267               break;
3268
3269             case '\\':
3270               *q = '\\';
3271               break;
3272
3273             case '\0':
3274               g_set_error (error, G_KEY_FILE_ERROR,
3275                            G_KEY_FILE_ERROR_INVALID_VALUE,
3276                            _("Key file contains escape character "
3277                              "at end of line"));
3278               break;
3279
3280             default:
3281               if (pieces && *p == key_file->list_separator)
3282                 *q = key_file->list_separator;
3283               else
3284                 {
3285                   *q++ = '\\';
3286                   *q = *p;
3287                   
3288                   if (*error == NULL)
3289                     {
3290                       gchar sequence[3];
3291                       
3292                       sequence[0] = '\\';
3293                       sequence[1] = *p;
3294                       sequence[2] = '\0';
3295                       
3296                       g_set_error (error, G_KEY_FILE_ERROR,
3297                                    G_KEY_FILE_ERROR_INVALID_VALUE,
3298                                    _("Key file contains invalid escape "
3299                                      "sequence '%s'"), sequence);
3300                     }
3301                 }
3302               break;
3303             }
3304         }
3305       else
3306         {
3307           *q = *p;
3308           if (pieces && (*p == key_file->list_separator))
3309             {
3310               *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3311               q0 = q + 1; 
3312             }
3313         }
3314
3315       if (*p == '\0')
3316         break;
3317
3318       q++;
3319       p++;
3320     }
3321
3322   *q = '\0';
3323   if (pieces)
3324   {
3325     if (q0 < q)
3326       *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3327     *pieces = g_slist_reverse (*pieces);
3328   }
3329
3330   return string_value;
3331 }
3332
3333 static gchar *
3334 g_key_file_parse_string_as_value (GKeyFile    *key_file,
3335                                   const gchar *string,
3336                                   gboolean     escape_separator)
3337 {
3338   gchar *value, *p, *q;
3339   gsize length;
3340   gboolean parsing_leading_space;
3341
3342   length = strlen (string) + 1;
3343
3344   /* Worst case would be that every character needs to be escaped.
3345    * In other words every character turns to two characters
3346    */
3347   value = g_new0 (gchar, 2 * length);
3348
3349   p = (gchar *) string;
3350   q = value;
3351   parsing_leading_space = TRUE;
3352   while (p < (string + length - 1))
3353     {
3354       gchar escaped_character[3] = { '\\', 0, 0 };
3355
3356       switch (*p)
3357         {
3358         case ' ':
3359           if (parsing_leading_space)
3360             {
3361               escaped_character[1] = 's';
3362               strcpy (q, escaped_character);
3363               q += 2;
3364             }
3365           else
3366             {
3367               *q = *p;
3368               q++;
3369             }
3370           break;
3371         case '\t':
3372           if (parsing_leading_space)
3373             {
3374               escaped_character[1] = 't';
3375               strcpy (q, escaped_character);
3376               q += 2;
3377             }
3378           else
3379             {
3380               *q = *p;
3381               q++;
3382             }
3383           break;
3384         case '\n':
3385           escaped_character[1] = 'n';
3386           strcpy (q, escaped_character);
3387           q += 2;
3388           break;
3389         case '\r':
3390           escaped_character[1] = 'r';
3391           strcpy (q, escaped_character);
3392           q += 2;
3393           break;
3394         case '\\':
3395           escaped_character[1] = '\\';
3396           strcpy (q, escaped_character);
3397           q += 2;
3398           parsing_leading_space = FALSE;
3399           break;
3400         default:
3401           if (escape_separator && *p == key_file->list_separator)
3402             {
3403               escaped_character[1] = key_file->list_separator;
3404               strcpy (q, escaped_character);
3405               q += 2;
3406               parsing_leading_space = TRUE;
3407             }
3408           else 
3409             {
3410               *q = *p;
3411               q++;
3412               parsing_leading_space = FALSE;
3413             }
3414           break;
3415         }
3416       p++;
3417     }
3418   *q = '\0';
3419
3420   return value;
3421 }
3422
3423 static gint
3424 g_key_file_parse_value_as_integer (GKeyFile     *key_file,
3425                                    const gchar  *value,
3426                                    GError      **error)
3427 {
3428   gchar *end_of_valid_int;
3429   glong long_value;
3430   gint int_value;
3431
3432   errno = 0;
3433   long_value = strtol (value, &end_of_valid_int, 10);
3434
3435   if (*value == '\0' || *end_of_valid_int != '\0')
3436     {
3437       g_set_error (error, G_KEY_FILE_ERROR,
3438                    G_KEY_FILE_ERROR_INVALID_VALUE,
3439                    _("Value '%s' cannot be interpreted as a number."), value);
3440       return 0;
3441     }
3442
3443   int_value = long_value;
3444   if (int_value != long_value || errno == ERANGE)
3445     {
3446       g_set_error (error,
3447                    G_KEY_FILE_ERROR, 
3448                    G_KEY_FILE_ERROR_INVALID_VALUE,
3449                    _("Integer value '%s' out of range"), value);
3450       return 0;
3451     }
3452   
3453   return int_value;
3454 }
3455
3456 static gchar *
3457 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3458                                    gint      value)
3459
3460 {
3461   return g_strdup_printf ("%d", value);
3462 }
3463
3464 static gdouble
3465 g_key_file_parse_value_as_double  (GKeyFile     *key_file,
3466                                    const gchar  *value,
3467                                    GError      **error)
3468 {
3469   gchar *end_of_valid_d;
3470   gdouble double_value = 0;
3471
3472   double_value = g_ascii_strtod (value, &end_of_valid_d);
3473
3474   if (*end_of_valid_d != '\0')
3475     g_set_error (error, G_KEY_FILE_ERROR,
3476                  G_KEY_FILE_ERROR_INVALID_VALUE,
3477                  _("Value '%s' cannot be interpreted as a float number."), value);
3478
3479   return double_value;
3480 }
3481
3482 static gboolean
3483 g_key_file_parse_value_as_boolean (GKeyFile     *key_file,
3484                                    const gchar  *value,
3485                                    GError      **error)
3486 {
3487   if (value)
3488     {
3489       if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3490         return TRUE;
3491       else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3492         return FALSE;
3493     }
3494
3495   g_set_error (error, G_KEY_FILE_ERROR,
3496                G_KEY_FILE_ERROR_INVALID_VALUE,
3497                _("Value '%s' cannot be interpreted as a boolean."), value);
3498
3499   return FALSE;
3500 }
3501
3502 static gchar *
3503 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3504                                    gboolean  value)
3505 {
3506   if (value)
3507     return g_strdup ("true");
3508   else
3509     return g_strdup ("false");
3510 }
3511
3512 static gchar *
3513 g_key_file_parse_value_as_comment (GKeyFile    *key_file,
3514                                    const gchar *value)
3515 {
3516   GString *string;
3517   gchar **lines, *comment;
3518   gsize i;
3519
3520   string = g_string_sized_new (512);
3521
3522   lines = g_strsplit (value, "\n", 0);
3523
3524   for (i = 0; lines[i] != NULL; i++)
3525     {
3526         if (lines[i][0] != '#')
3527            g_string_append_printf (string, "%s\n", lines[i]);
3528         else 
3529            g_string_append_printf (string, "%s\n", lines[i] + 1);
3530     }
3531   g_strfreev (lines);
3532
3533   comment = string->str;
3534
3535   g_string_free (string, FALSE);
3536
3537   return comment;
3538 }
3539
3540 static gchar *
3541 g_key_file_parse_comment_as_value (GKeyFile      *key_file,
3542                                    const gchar   *comment)
3543 {
3544   GString *string;
3545   gchar **lines, *value;
3546   gsize i;
3547
3548   string = g_string_sized_new (512);
3549
3550   lines = g_strsplit (comment, "\n", 0);
3551
3552   for (i = 0; lines[i] != NULL; i++)
3553     g_string_append_printf (string, "#%s%s", lines[i], 
3554                             lines[i + 1] == NULL? "" : "\n");
3555   g_strfreev (lines);
3556
3557   value = string->str;
3558
3559   g_string_free (string, FALSE);
3560
3561   return value;
3562 }
3563
3564 #define __G_KEY_FILE_C__
3565 #include "galiasdef.c"