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