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