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