Call setlocale initially
[glib.git] / glib / gkeyfile.c
blob2226e7031043b571616905af3250444ed309ee60
1 /* gkeyfile.c - key file parser
3 * Copyright 2004 Red Hat, Inc.
5 * Written by Ray Strode <rstrode@redhat.com>
6 * Matthias Clasen <mclasen@redhat.com>
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.
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.
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.
24 #include "config.h"
26 #include "gkeyfile.h"
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>
42 #define fstat(a,b) _fstati64(a,b)
43 #define stat _stati64
45 #ifndef S_ISREG
46 #define S_ISREG(mode) ((mode)&_S_IFREG)
47 #endif
49 #endif /* G_OS_WIN23 */
51 #include "gconvert.h"
52 #include "gdataset.h"
53 #include "gerror.h"
54 #include "gfileutils.h"
55 #include "ghash.h"
56 #include "glibintl.h"
57 #include "glist.h"
58 #include "gslist.h"
59 #include "gmem.h"
60 #include "gmessages.h"
61 #include "gstdio.h"
62 #include "gstring.h"
63 #include "gstrfuncs.h"
64 #include "gutils.h"
66 #include "galias.h"
68 typedef struct _GKeyFileGroup GKeyFileGroup;
70 struct _GKeyFile
72 GList *groups;
73 GHashTable *group_hash;
75 GKeyFileGroup *start_group;
76 GKeyFileGroup *current_group;
78 GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
80 /* Used for sizing the output buffer during serialization
82 gsize approximate_size;
84 gchar list_separator;
86 GKeyFileFlags flags;
88 gchar **locales;
91 typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
93 struct _GKeyFileGroup
95 const gchar *name; /* NULL for above first group (which will be comments) */
97 GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
98 gboolean has_trailing_blank_line;
100 GList *key_value_pairs;
102 /* Used in parallel with key_value_pairs for
103 * increased lookup performance
105 GHashTable *lookup_map;
108 struct _GKeyFileKeyValuePair
110 gchar *key; /* NULL for comments */
111 gchar *value;
114 static gint find_file_in_data_dirs (const gchar *file,
115 const gchar **data_dirs,
116 gchar **output_file,
117 GError **error);
118 static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
119 gint fd,
120 GKeyFileFlags flags,
121 GError **error);
122 static GList *g_key_file_lookup_group_node (GKeyFile *key_file,
123 const gchar *group_name);
124 static GKeyFileGroup *g_key_file_lookup_group (GKeyFile *key_file,
125 const gchar *group_name);
127 static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
128 GKeyFileGroup *group,
129 const gchar *key);
130 static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
131 GKeyFileGroup *group,
132 const gchar *key);
134 static void g_key_file_remove_group_node (GKeyFile *key_file,
135 GList *group_node);
136 static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
137 GKeyFileGroup *group,
138 GList *pair_node);
140 static void g_key_file_add_key (GKeyFile *key_file,
141 GKeyFileGroup *group,
142 const gchar *key,
143 const gchar *value);
144 static void g_key_file_add_group (GKeyFile *key_file,
145 const gchar *group_name);
146 static gboolean g_key_file_is_group_name (const gchar *name);
147 static gboolean g_key_file_is_key_name (const gchar *name);
148 static void g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair);
149 static gboolean g_key_file_line_is_comment (const gchar *line);
150 static gboolean g_key_file_line_is_group (const gchar *line);
151 static gboolean g_key_file_line_is_key_value_pair (const gchar *line);
152 static gchar *g_key_file_parse_value_as_string (GKeyFile *key_file,
153 const gchar *value,
154 GSList **separators,
155 GError **error);
156 static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
157 const gchar *string,
158 gboolean escape_separator);
159 static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
160 const gchar *value,
161 GError **error);
162 static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
163 gint value);
164 static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
165 const gchar *value,
166 GError **error);
167 static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
168 const gchar *value,
169 GError **error);
170 static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
171 gboolean value);
172 static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
173 const gchar *value);
174 static gchar *g_key_file_parse_comment_as_value (GKeyFile *key_file,
175 const gchar *comment);
176 static void g_key_file_parse_key_value_pair (GKeyFile *key_file,
177 const gchar *line,
178 gsize length,
179 GError **error);
180 static void g_key_file_parse_comment (GKeyFile *key_file,
181 const gchar *line,
182 gsize length,
183 GError **error);
184 static void g_key_file_parse_group (GKeyFile *key_file,
185 const gchar *line,
186 gsize length,
187 GError **error);
188 static gchar *key_get_locale (const gchar *key);
189 static void g_key_file_parse_data (GKeyFile *key_file,
190 const gchar *data,
191 gsize length,
192 GError **error);
193 static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
194 GError **error);
197 GQuark
198 g_key_file_error_quark (void)
200 return g_quark_from_static_string ("g-key-file-error-quark");
203 static void
204 g_key_file_init (GKeyFile *key_file)
206 key_file->current_group = g_slice_new0 (GKeyFileGroup);
207 key_file->groups = g_list_prepend (NULL, key_file->current_group);
208 key_file->group_hash = g_hash_table_new (g_str_hash, g_str_equal);
209 key_file->start_group = NULL;
210 key_file->parse_buffer = g_string_sized_new (128);
211 key_file->approximate_size = 0;
212 key_file->list_separator = ';';
213 key_file->flags = 0;
214 key_file->locales = g_strdupv ((gchar **)g_get_language_names ());
217 static void
218 g_key_file_clear (GKeyFile *key_file)
220 GList *tmp, *group_node;
222 if (key_file->locales)
224 g_strfreev (key_file->locales);
225 key_file->locales = NULL;
228 if (key_file->parse_buffer)
230 g_string_free (key_file->parse_buffer, TRUE);
231 key_file->parse_buffer = NULL;
234 tmp = key_file->groups;
235 while (tmp != NULL)
237 group_node = tmp;
238 tmp = tmp->next;
239 g_key_file_remove_group_node (key_file, group_node);
242 g_hash_table_destroy (key_file->group_hash);
243 key_file->group_hash = NULL;
245 g_warn_if_fail (key_file->groups == NULL);
250 * g_key_file_new:
252 * Creates a new empty #GKeyFile object. Use
253 * g_key_file_load_from_file(), g_key_file_load_from_data(),
254 * g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to
255 * read an existing key file.
257 * Return value: an empty #GKeyFile.
259 * Since: 2.6
261 GKeyFile *
262 g_key_file_new (void)
264 GKeyFile *key_file;
266 key_file = g_slice_new0 (GKeyFile);
267 g_key_file_init (key_file);
269 return key_file;
273 * g_key_file_set_list_separator:
274 * @key_file: a #GKeyFile
275 * @separator: the separator
277 * Sets the character which is used to separate
278 * values in lists. Typically ';' or ',' are used
279 * as separators. The default list separator is ';'.
281 * Since: 2.6
283 void
284 g_key_file_set_list_separator (GKeyFile *key_file,
285 gchar separator)
287 g_return_if_fail (key_file != NULL);
289 key_file->list_separator = separator;
293 /* Iterates through all the directories in *dirs trying to
294 * open file. When it successfully locates and opens a file it
295 * returns the file descriptor to the open file. It also
296 * outputs the absolute path of the file in output_file.
298 static gint
299 find_file_in_data_dirs (const gchar *file,
300 const gchar **dirs,
301 gchar **output_file,
302 GError **error)
304 const gchar **data_dirs, *data_dir;
305 gchar *path;
306 gint fd;
308 path = NULL;
309 fd = -1;
311 if (dirs == NULL)
312 return fd;
314 data_dirs = dirs;
316 while (data_dirs && (data_dir = *data_dirs) && fd < 0)
318 gchar *candidate_file, *sub_dir;
320 candidate_file = (gchar *) file;
321 sub_dir = g_strdup ("");
322 while (candidate_file != NULL && fd < 0)
324 gchar *p;
326 path = g_build_filename (data_dir, sub_dir,
327 candidate_file, NULL);
329 fd = g_open (path, O_RDONLY, 0);
331 if (fd < 0)
333 g_free (path);
334 path = NULL;
337 candidate_file = strchr (candidate_file, '-');
339 if (candidate_file == NULL)
340 break;
342 candidate_file++;
344 g_free (sub_dir);
345 sub_dir = g_strndup (file, candidate_file - file - 1);
347 for (p = sub_dir; *p != '\0'; p++)
349 if (*p == '-')
350 *p = G_DIR_SEPARATOR;
353 g_free (sub_dir);
354 data_dirs++;
357 if (fd < 0)
359 g_set_error_literal (error, G_KEY_FILE_ERROR,
360 G_KEY_FILE_ERROR_NOT_FOUND,
361 _("Valid key file could not be "
362 "found in search dirs"));
365 if (output_file != NULL && fd > 0)
366 *output_file = g_strdup (path);
368 g_free (path);
370 return fd;
373 static gboolean
374 g_key_file_load_from_fd (GKeyFile *key_file,
375 gint fd,
376 GKeyFileFlags flags,
377 GError **error)
379 GError *key_file_error = NULL;
380 gssize bytes_read;
381 struct stat stat_buf;
382 gchar read_buf[4096];
384 if (fstat (fd, &stat_buf) < 0)
386 g_set_error_literal (error, G_FILE_ERROR,
387 g_file_error_from_errno (errno),
388 g_strerror (errno));
389 return FALSE;
392 if (!S_ISREG (stat_buf.st_mode))
394 g_set_error_literal (error, G_KEY_FILE_ERROR,
395 G_KEY_FILE_ERROR_PARSE,
396 _("Not a regular file"));
397 return FALSE;
400 if (stat_buf.st_size == 0)
402 g_set_error_literal (error, G_KEY_FILE_ERROR,
403 G_KEY_FILE_ERROR_PARSE,
404 _("File is empty"));
405 return FALSE;
408 if (key_file->approximate_size > 0)
410 g_key_file_clear (key_file);
411 g_key_file_init (key_file);
413 key_file->flags = flags;
417 bytes_read = read (fd, read_buf, 4096);
419 if (bytes_read == 0) /* End of File */
420 break;
422 if (bytes_read < 0)
424 if (errno == EINTR || errno == EAGAIN)
425 continue;
427 g_set_error_literal (error, G_FILE_ERROR,
428 g_file_error_from_errno (errno),
429 g_strerror (errno));
430 return FALSE;
433 g_key_file_parse_data (key_file,
434 read_buf, bytes_read,
435 &key_file_error);
437 while (!key_file_error);
439 if (key_file_error)
441 g_propagate_error (error, key_file_error);
442 return FALSE;
445 g_key_file_flush_parse_buffer (key_file, &key_file_error);
447 if (key_file_error)
449 g_propagate_error (error, key_file_error);
450 return FALSE;
453 return TRUE;
457 * g_key_file_load_from_file:
458 * @key_file: an empty #GKeyFile struct
459 * @file: the path of a filename to load, in the GLib filename encoding
460 * @flags: flags from #GKeyFileFlags
461 * @error: return location for a #GError, or %NULL
463 * Loads a key file into an empty #GKeyFile structure.
464 * If the file could not be loaded then %error is set to
465 * either a #GFileError or #GKeyFileError.
467 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
469 * Since: 2.6
471 gboolean
472 g_key_file_load_from_file (GKeyFile *key_file,
473 const gchar *file,
474 GKeyFileFlags flags,
475 GError **error)
477 GError *key_file_error = NULL;
478 gint fd;
480 g_return_val_if_fail (key_file != NULL, FALSE);
481 g_return_val_if_fail (file != NULL, FALSE);
483 fd = g_open (file, O_RDONLY, 0);
485 if (fd < 0)
487 g_set_error_literal (error, G_FILE_ERROR,
488 g_file_error_from_errno (errno),
489 g_strerror (errno));
490 return FALSE;
493 g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
494 close (fd);
496 if (key_file_error)
498 g_propagate_error (error, key_file_error);
499 return FALSE;
502 return TRUE;
506 * g_key_file_load_from_data:
507 * @key_file: an empty #GKeyFile struct
508 * @data: key file loaded in memory
509 * @length: the length of @data in bytes
510 * @flags: flags from #GKeyFileFlags
511 * @error: return location for a #GError, or %NULL
513 * Loads a key file from memory into an empty #GKeyFile structure.
514 * If the object cannot be created then %error is set to a #GKeyFileError.
516 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
518 * Since: 2.6
520 gboolean
521 g_key_file_load_from_data (GKeyFile *key_file,
522 const gchar *data,
523 gsize length,
524 GKeyFileFlags flags,
525 GError **error)
527 GError *key_file_error = NULL;
529 g_return_val_if_fail (key_file != NULL, FALSE);
530 g_return_val_if_fail (data != NULL, FALSE);
531 g_return_val_if_fail (length != 0, FALSE);
533 if (length == (gsize)-1)
534 length = strlen (data);
536 if (key_file->approximate_size > 0)
538 g_key_file_clear (key_file);
539 g_key_file_init (key_file);
541 key_file->flags = flags;
543 g_key_file_parse_data (key_file, data, length, &key_file_error);
545 if (key_file_error)
547 g_propagate_error (error, key_file_error);
548 return FALSE;
551 g_key_file_flush_parse_buffer (key_file, &key_file_error);
553 if (key_file_error)
555 g_propagate_error (error, key_file_error);
556 return FALSE;
559 return TRUE;
563 * g_key_file_load_from_dirs:
564 * @key_file: an empty #GKeyFile struct
565 * @file: a relative path to a filename to open and parse
566 * @search_dirs: %NULL-terminated array of directories to search
567 * @full_path: return location for a string containing the full path
568 * of the file, or %NULL
569 * @flags: flags from #GKeyFileFlags
570 * @error: return location for a #GError, or %NULL
572 * This function looks for a key file named @file in the paths
573 * specified in @search_dirs, loads the file into @key_file and
574 * returns the file's full path in @full_path. If the file could not
575 * be loaded then an %error is set to either a #GFileError or
576 * #GKeyFileError.
578 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
580 * Since: 2.14
582 gboolean
583 g_key_file_load_from_dirs (GKeyFile *key_file,
584 const gchar *file,
585 const gchar **search_dirs,
586 gchar **full_path,
587 GKeyFileFlags flags,
588 GError **error)
590 GError *key_file_error = NULL;
591 const gchar **data_dirs;
592 gchar *output_path;
593 gint fd;
594 gboolean found_file;
596 g_return_val_if_fail (key_file != NULL, FALSE);
597 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
598 g_return_val_if_fail (search_dirs != NULL, FALSE);
600 found_file = FALSE;
601 data_dirs = search_dirs;
602 output_path = NULL;
603 while (*data_dirs != NULL && !found_file)
605 g_free (output_path);
607 fd = find_file_in_data_dirs (file, data_dirs, &output_path,
608 &key_file_error);
610 if (fd < 0)
612 if (key_file_error)
613 g_propagate_error (error, key_file_error);
614 break;
617 found_file = g_key_file_load_from_fd (key_file, fd, flags,
618 &key_file_error);
619 close (fd);
621 if (key_file_error)
623 g_propagate_error (error, key_file_error);
624 break;
628 if (found_file && full_path)
629 *full_path = output_path;
630 else
631 g_free (output_path);
633 return found_file;
637 * g_key_file_load_from_data_dirs:
638 * @key_file: an empty #GKeyFile struct
639 * @file: a relative path to a filename to open and parse
640 * @full_path: return location for a string containing the full path
641 * of the file, or %NULL
642 * @flags: flags from #GKeyFileFlags
643 * @error: return location for a #GError, or %NULL
645 * This function looks for a key file named @file in the paths
646 * returned from g_get_user_data_dir() and g_get_system_data_dirs(),
647 * loads the file into @key_file and returns the file's full path in
648 * @full_path. If the file could not be loaded then an %error is
649 * set to either a #GFileError or #GKeyFileError.
651 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
652 * Since: 2.6
654 gboolean
655 g_key_file_load_from_data_dirs (GKeyFile *key_file,
656 const gchar *file,
657 gchar **full_path,
658 GKeyFileFlags flags,
659 GError **error)
661 gchar **all_data_dirs;
662 const gchar * user_data_dir;
663 const gchar * const * system_data_dirs;
664 gsize i, j;
665 gboolean found_file;
667 g_return_val_if_fail (key_file != NULL, FALSE);
668 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
670 user_data_dir = g_get_user_data_dir ();
671 system_data_dirs = g_get_system_data_dirs ();
672 all_data_dirs = g_new (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
674 i = 0;
675 all_data_dirs[i++] = g_strdup (user_data_dir);
677 j = 0;
678 while (system_data_dirs[j] != NULL)
679 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
680 all_data_dirs[i] = NULL;
682 found_file = g_key_file_load_from_dirs (key_file,
683 file,
684 (const gchar **)all_data_dirs,
685 full_path,
686 flags,
687 error);
689 g_strfreev (all_data_dirs);
691 return found_file;
695 * g_key_file_free:
696 * @key_file: a #GKeyFile
698 * Frees a #GKeyFile.
700 * Since: 2.6
702 void
703 g_key_file_free (GKeyFile *key_file)
705 g_return_if_fail (key_file != NULL);
707 g_key_file_clear (key_file);
708 g_slice_free (GKeyFile, key_file);
711 /* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
712 * true for locales that match those in g_get_language_names().
714 static gboolean
715 g_key_file_locale_is_interesting (GKeyFile *key_file,
716 const gchar *locale)
718 gsize i;
720 if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
721 return TRUE;
723 for (i = 0; key_file->locales[i] != NULL; i++)
725 if (g_ascii_strcasecmp (key_file->locales[i], locale) == 0)
726 return TRUE;
729 return FALSE;
732 static void
733 g_key_file_parse_line (GKeyFile *key_file,
734 const gchar *line,
735 gsize length,
736 GError **error)
738 GError *parse_error = NULL;
739 gchar *line_start;
741 g_return_if_fail (key_file != NULL);
742 g_return_if_fail (line != NULL);
744 line_start = (gchar *) line;
745 while (g_ascii_isspace (*line_start))
746 line_start++;
748 if (g_key_file_line_is_comment (line_start))
749 g_key_file_parse_comment (key_file, line, length, &parse_error);
750 else if (g_key_file_line_is_group (line_start))
751 g_key_file_parse_group (key_file, line_start,
752 length - (line_start - line),
753 &parse_error);
754 else if (g_key_file_line_is_key_value_pair (line_start))
755 g_key_file_parse_key_value_pair (key_file, line_start,
756 length - (line_start - line),
757 &parse_error);
758 else
760 gchar *line_utf8 = _g_utf8_make_valid (line);
761 g_set_error (error, G_KEY_FILE_ERROR,
762 G_KEY_FILE_ERROR_PARSE,
763 _("Key file contains line '%s' which is not "
764 "a key-value pair, group, or comment"),
765 line_utf8);
766 g_free (line_utf8);
768 return;
771 if (parse_error)
772 g_propagate_error (error, parse_error);
775 static void
776 g_key_file_parse_comment (GKeyFile *key_file,
777 const gchar *line,
778 gsize length,
779 GError **error)
781 GKeyFileKeyValuePair *pair;
783 if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
784 return;
786 g_warn_if_fail (key_file->current_group != NULL);
788 pair = g_slice_new (GKeyFileKeyValuePair);
789 pair->key = NULL;
790 pair->value = g_strndup (line, length);
792 key_file->current_group->key_value_pairs =
793 g_list_prepend (key_file->current_group->key_value_pairs, pair);
795 if (length == 0 || line[0] != '#')
796 key_file->current_group->has_trailing_blank_line = TRUE;
799 static void
800 g_key_file_parse_group (GKeyFile *key_file,
801 const gchar *line,
802 gsize length,
803 GError **error)
805 gchar *group_name;
806 const gchar *group_name_start, *group_name_end;
808 /* advance past opening '['
810 group_name_start = line + 1;
811 group_name_end = line + length - 1;
813 while (*group_name_end != ']')
814 group_name_end--;
816 group_name = g_strndup (group_name_start,
817 group_name_end - group_name_start);
819 if (!g_key_file_is_group_name (group_name))
821 g_set_error (error, G_KEY_FILE_ERROR,
822 G_KEY_FILE_ERROR_PARSE,
823 _("Invalid group name: %s"), group_name);
824 g_free (group_name);
825 return;
828 g_key_file_add_group (key_file, group_name);
829 g_free (group_name);
832 static void
833 g_key_file_parse_key_value_pair (GKeyFile *key_file,
834 const gchar *line,
835 gsize length,
836 GError **error)
838 gchar *key, *value, *key_end, *value_start, *locale;
839 gsize key_len, value_len;
841 if (key_file->current_group == NULL || key_file->current_group->name == NULL)
843 g_set_error_literal (error, G_KEY_FILE_ERROR,
844 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
845 _("Key file does not start with a group"));
846 return;
849 key_end = value_start = strchr (line, '=');
851 g_warn_if_fail (key_end != NULL);
853 key_end--;
854 value_start++;
856 /* Pull the key name from the line (chomping trailing whitespace)
858 while (g_ascii_isspace (*key_end))
859 key_end--;
861 key_len = key_end - line + 2;
863 g_warn_if_fail (key_len <= length);
865 key = g_strndup (line, key_len - 1);
867 if (!g_key_file_is_key_name (key))
869 g_set_error (error, G_KEY_FILE_ERROR,
870 G_KEY_FILE_ERROR_PARSE,
871 _("Invalid key name: %s"), key);
872 g_free (key);
873 return;
876 /* Pull the value from the line (chugging leading whitespace)
878 while (g_ascii_isspace (*value_start))
879 value_start++;
881 value_len = line + length - value_start + 1;
883 value = g_strndup (value_start, value_len);
885 g_warn_if_fail (key_file->start_group != NULL);
887 if (key_file->current_group
888 && key_file->current_group->name
889 && strcmp (key_file->start_group->name,
890 key_file->current_group->name) == 0
891 && strcmp (key, "Encoding") == 0)
893 if (g_ascii_strcasecmp (value, "UTF-8") != 0)
895 gchar *value_utf8 = _g_utf8_make_valid (value);
896 g_set_error (error, G_KEY_FILE_ERROR,
897 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
898 _("Key file contains unsupported "
899 "encoding '%s'"), value_utf8);
900 g_free (value_utf8);
902 g_free (key);
903 g_free (value);
904 return;
908 /* Is this key a translation? If so, is it one that we care about?
910 locale = key_get_locale (key);
912 if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
913 g_key_file_add_key (key_file, key_file->current_group, key, value);
915 g_free (locale);
916 g_free (key);
917 g_free (value);
920 static gchar *
921 key_get_locale (const gchar *key)
923 gchar *locale;
925 locale = g_strrstr (key, "[");
927 if (locale && strlen (locale) <= 2)
928 locale = NULL;
930 if (locale)
931 locale = g_strndup (locale + 1, strlen (locale) - 2);
933 return locale;
936 static void
937 g_key_file_parse_data (GKeyFile *key_file,
938 const gchar *data,
939 gsize length,
940 GError **error)
942 GError *parse_error;
943 gsize i;
945 g_return_if_fail (key_file != NULL);
946 g_return_if_fail (data != NULL);
948 parse_error = NULL;
950 for (i = 0; i < length; i++)
952 if (data[i] == '\n')
954 if (i > 0 && data[i - 1] == '\r')
955 g_string_erase (key_file->parse_buffer,
956 key_file->parse_buffer->len - 1,
959 /* When a newline is encountered flush the parse buffer so that the
960 * line can be parsed. Note that completely blank lines won't show
961 * up in the parse buffer, so they get parsed directly.
963 if (key_file->parse_buffer->len > 0)
964 g_key_file_flush_parse_buffer (key_file, &parse_error);
965 else
966 g_key_file_parse_comment (key_file, "", 1, &parse_error);
968 if (parse_error)
970 g_propagate_error (error, parse_error);
971 return;
974 else
975 g_string_append_c (key_file->parse_buffer, data[i]);
978 key_file->approximate_size += length;
981 static void
982 g_key_file_flush_parse_buffer (GKeyFile *key_file,
983 GError **error)
985 GError *file_error = NULL;
987 g_return_if_fail (key_file != NULL);
989 file_error = NULL;
991 if (key_file->parse_buffer->len > 0)
993 g_key_file_parse_line (key_file, key_file->parse_buffer->str,
994 key_file->parse_buffer->len,
995 &file_error);
996 g_string_erase (key_file->parse_buffer, 0, -1);
998 if (file_error)
1000 g_propagate_error (error, file_error);
1001 return;
1007 * g_key_file_to_data:
1008 * @key_file: a #GKeyFile
1009 * @length: return location for the length of the
1010 * returned string, or %NULL
1011 * @error: return location for a #GError, or %NULL
1013 * This function outputs @key_file as a string.
1015 * Note that this function never reports an error,
1016 * so it is safe to pass %NULL as @error.
1018 * Return value: a newly allocated string holding
1019 * the contents of the #GKeyFile
1021 * Since: 2.6
1023 gchar *
1024 g_key_file_to_data (GKeyFile *key_file,
1025 gsize *length,
1026 GError **error)
1028 GString *data_string;
1029 GList *group_node, *key_file_node;
1030 gboolean has_blank_line = TRUE;
1032 g_return_val_if_fail (key_file != NULL, NULL);
1034 data_string = g_string_sized_new (2 * key_file->approximate_size);
1036 for (group_node = g_list_last (key_file->groups);
1037 group_node != NULL;
1038 group_node = group_node->prev)
1040 GKeyFileGroup *group;
1042 group = (GKeyFileGroup *) group_node->data;
1044 /* separate groups by at least an empty line */
1045 if (!has_blank_line)
1046 g_string_append_c (data_string, '\n');
1047 has_blank_line = group->has_trailing_blank_line;
1049 if (group->comment != NULL)
1050 g_string_append_printf (data_string, "%s\n", group->comment->value);
1052 if (group->name != NULL)
1053 g_string_append_printf (data_string, "[%s]\n", group->name);
1055 for (key_file_node = g_list_last (group->key_value_pairs);
1056 key_file_node != NULL;
1057 key_file_node = key_file_node->prev)
1059 GKeyFileKeyValuePair *pair;
1061 pair = (GKeyFileKeyValuePair *) key_file_node->data;
1063 if (pair->key != NULL)
1064 g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1065 else
1066 g_string_append_printf (data_string, "%s\n", pair->value);
1070 if (length)
1071 *length = data_string->len;
1073 return g_string_free (data_string, FALSE);
1077 * g_key_file_get_keys:
1078 * @key_file: a #GKeyFile
1079 * @group_name: a group name
1080 * @length: return location for the number of keys returned, or %NULL
1081 * @error: return location for a #GError, or %NULL
1083 * Returns all keys for the group name @group_name. The array of
1084 * returned keys will be %NULL-terminated, so @length may
1085 * optionally be %NULL. In the event that the @group_name cannot
1086 * be found, %NULL is returned and @error is set to
1087 * #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1089 * Return value: a newly-allocated %NULL-terminated array of strings.
1090 * Use g_strfreev() to free it.
1092 * Since: 2.6
1094 gchar **
1095 g_key_file_get_keys (GKeyFile *key_file,
1096 const gchar *group_name,
1097 gsize *length,
1098 GError **error)
1100 GKeyFileGroup *group;
1101 GList *tmp;
1102 gchar **keys;
1103 gsize i, num_keys;
1105 g_return_val_if_fail (key_file != NULL, NULL);
1106 g_return_val_if_fail (group_name != NULL, NULL);
1108 group = g_key_file_lookup_group (key_file, group_name);
1110 if (!group)
1112 g_set_error (error, G_KEY_FILE_ERROR,
1113 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1114 _("Key file does not have group '%s'"),
1115 group_name ? group_name : "(null)");
1116 return NULL;
1119 num_keys = 0;
1120 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1122 GKeyFileKeyValuePair *pair;
1124 pair = (GKeyFileKeyValuePair *) tmp->data;
1126 if (pair->key)
1127 num_keys++;
1130 keys = g_new (gchar *, num_keys + 1);
1132 i = num_keys - 1;
1133 for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1135 GKeyFileKeyValuePair *pair;
1137 pair = (GKeyFileKeyValuePair *) tmp->data;
1139 if (pair->key)
1141 keys[i] = g_strdup (pair->key);
1142 i--;
1146 keys[num_keys] = NULL;
1148 if (length)
1149 *length = num_keys;
1151 return keys;
1155 * g_key_file_get_start_group:
1156 * @key_file: a #GKeyFile
1158 * Returns the name of the start group of the file.
1160 * Return value: The start group of the key file.
1162 * Since: 2.6
1164 gchar *
1165 g_key_file_get_start_group (GKeyFile *key_file)
1167 g_return_val_if_fail (key_file != NULL, NULL);
1169 if (key_file->start_group)
1170 return g_strdup (key_file->start_group->name);
1172 return NULL;
1176 * g_key_file_get_groups:
1177 * @key_file: a #GKeyFile
1178 * @length: return location for the number of returned groups, or %NULL
1180 * Returns all groups in the key file loaded with @key_file.
1181 * The array of returned groups will be %NULL-terminated, so
1182 * @length may optionally be %NULL.
1184 * Return value: a newly-allocated %NULL-terminated array of strings.
1185 * Use g_strfreev() to free it.
1186 * Since: 2.6
1188 gchar **
1189 g_key_file_get_groups (GKeyFile *key_file,
1190 gsize *length)
1192 GList *group_node;
1193 gchar **groups;
1194 gsize i, num_groups;
1196 g_return_val_if_fail (key_file != NULL, NULL);
1198 num_groups = g_list_length (key_file->groups);
1200 g_return_val_if_fail (num_groups > 0, NULL);
1202 group_node = g_list_last (key_file->groups);
1204 g_return_val_if_fail (((GKeyFileGroup *) group_node->data)->name == NULL, NULL);
1206 /* Only need num_groups instead of num_groups + 1
1207 * because the first group of the file (last in the
1208 * list) is always the comment group at the top,
1209 * which we skip
1211 groups = g_new (gchar *, num_groups);
1214 i = 0;
1215 for (group_node = group_node->prev;
1216 group_node != NULL;
1217 group_node = group_node->prev)
1219 GKeyFileGroup *group;
1221 group = (GKeyFileGroup *) group_node->data;
1223 g_warn_if_fail (group->name != NULL);
1225 groups[i++] = g_strdup (group->name);
1227 groups[i] = NULL;
1229 if (length)
1230 *length = i;
1232 return groups;
1236 * g_key_file_get_value:
1237 * @key_file: a #GKeyFile
1238 * @group_name: a group name
1239 * @key: a key
1240 * @error: return location for a #GError, or %NULL
1242 * Returns the raw value associated with @key under @group_name.
1243 * Use g_key_file_get_string() to retrieve an unescaped UTF-8 string.
1245 * In the event the key cannot be found, %NULL is returned and
1246 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1247 * event that the @group_name cannot be found, %NULL is returned
1248 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1251 * Return value: a newly allocated string or %NULL if the specified
1252 * key cannot be found.
1254 * Since: 2.6
1256 gchar *
1257 g_key_file_get_value (GKeyFile *key_file,
1258 const gchar *group_name,
1259 const gchar *key,
1260 GError **error)
1262 GKeyFileGroup *group;
1263 GKeyFileKeyValuePair *pair;
1264 gchar *value = NULL;
1266 g_return_val_if_fail (key_file != NULL, NULL);
1267 g_return_val_if_fail (group_name != NULL, NULL);
1268 g_return_val_if_fail (key != NULL, NULL);
1270 group = g_key_file_lookup_group (key_file, group_name);
1272 if (!group)
1274 g_set_error (error, G_KEY_FILE_ERROR,
1275 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1276 _("Key file does not have group '%s'"),
1277 group_name ? group_name : "(null)");
1278 return NULL;
1281 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1283 if (pair)
1284 value = g_strdup (pair->value);
1285 else
1286 g_set_error (error, G_KEY_FILE_ERROR,
1287 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1288 _("Key file does not have key '%s'"), key);
1290 return value;
1294 * g_key_file_set_value:
1295 * @key_file: a #GKeyFile
1296 * @group_name: a group name
1297 * @key: a key
1298 * @value: a string
1300 * Associates a new value with @key under @group_name.
1302 * If @key cannot be found then it is created. If @group_name cannot
1303 * be found then it is created. To set an UTF-8 string which may contain
1304 * characters that need escaping (such as newlines or spaces), use
1305 * g_key_file_set_string().
1307 * Since: 2.6
1309 void
1310 g_key_file_set_value (GKeyFile *key_file,
1311 const gchar *group_name,
1312 const gchar *key,
1313 const gchar *value)
1315 GKeyFileGroup *group;
1316 GKeyFileKeyValuePair *pair;
1318 g_return_if_fail (key_file != NULL);
1319 g_return_if_fail (g_key_file_is_group_name (group_name));
1320 g_return_if_fail (g_key_file_is_key_name (key));
1321 g_return_if_fail (value != NULL);
1323 group = g_key_file_lookup_group (key_file, group_name);
1325 if (!group)
1327 g_key_file_add_group (key_file, group_name);
1328 group = (GKeyFileGroup *) key_file->groups->data;
1330 g_key_file_add_key (key_file, group, key, value);
1332 else
1334 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1336 if (!pair)
1337 g_key_file_add_key (key_file, group, key, value);
1338 else
1340 g_free (pair->value);
1341 pair->value = g_strdup (value);
1347 * g_key_file_get_string:
1348 * @key_file: a #GKeyFile
1349 * @group_name: a group name
1350 * @key: a key
1351 * @error: return location for a #GError, or %NULL
1353 * Returns the string value associated with @key under @group_name.
1354 * Unlike g_key_file_get_value(), this function handles escape sequences
1355 * like \s.
1357 * In the event the key cannot be found, %NULL is returned and
1358 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1359 * event that the @group_name cannot be found, %NULL is returned
1360 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1362 * Return value: a newly allocated string or %NULL if the specified
1363 * key cannot be found.
1365 * Since: 2.6
1367 gchar *
1368 g_key_file_get_string (GKeyFile *key_file,
1369 const gchar *group_name,
1370 const gchar *key,
1371 GError **error)
1373 gchar *value, *string_value;
1374 GError *key_file_error;
1376 g_return_val_if_fail (key_file != NULL, NULL);
1377 g_return_val_if_fail (group_name != NULL, NULL);
1378 g_return_val_if_fail (key != NULL, NULL);
1380 key_file_error = NULL;
1382 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1384 if (key_file_error)
1386 g_propagate_error (error, key_file_error);
1387 return NULL;
1390 if (!g_utf8_validate (value, -1, NULL))
1392 gchar *value_utf8 = _g_utf8_make_valid (value);
1393 g_set_error (error, G_KEY_FILE_ERROR,
1394 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1395 _("Key file contains key '%s' with value '%s' "
1396 "which is not UTF-8"), key, value_utf8);
1397 g_free (value_utf8);
1398 g_free (value);
1400 return NULL;
1403 string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1404 &key_file_error);
1405 g_free (value);
1407 if (key_file_error)
1409 if (g_error_matches (key_file_error,
1410 G_KEY_FILE_ERROR,
1411 G_KEY_FILE_ERROR_INVALID_VALUE))
1413 g_set_error (error, G_KEY_FILE_ERROR,
1414 G_KEY_FILE_ERROR_INVALID_VALUE,
1415 _("Key file contains key '%s' "
1416 "which has value that cannot be interpreted."),
1417 key);
1418 g_error_free (key_file_error);
1420 else
1421 g_propagate_error (error, key_file_error);
1424 return string_value;
1428 * g_key_file_set_string:
1429 * @key_file: a #GKeyFile
1430 * @group_name: a group name
1431 * @key: a key
1432 * @string: a string
1434 * Associates a new string value with @key under @group_name.
1435 * If @key cannot be found then it is created.
1436 * If @group_name cannot be found then it is created.
1437 * Unlike g_key_file_set_value(), this function handles characters
1438 * that need escaping, such as newlines.
1440 * Since: 2.6
1442 void
1443 g_key_file_set_string (GKeyFile *key_file,
1444 const gchar *group_name,
1445 const gchar *key,
1446 const gchar *string)
1448 gchar *value;
1450 g_return_if_fail (key_file != NULL);
1451 g_return_if_fail (string != NULL);
1453 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1454 g_key_file_set_value (key_file, group_name, key, value);
1455 g_free (value);
1459 * g_key_file_get_string_list:
1460 * @key_file: a #GKeyFile
1461 * @group_name: a group name
1462 * @key: a key
1463 * @length: return location for the number of returned strings, or %NULL
1464 * @error: return location for a #GError, or %NULL
1466 * Returns the values associated with @key under @group_name.
1468 * In the event the key cannot be found, %NULL is returned and
1469 * @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1470 * event that the @group_name cannot be found, %NULL is returned
1471 * and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1473 * Return value: a %NULL-terminated string array or %NULL if the specified
1474 * key cannot be found. The array should be freed with g_strfreev().
1476 * Since: 2.6
1478 gchar **
1479 g_key_file_get_string_list (GKeyFile *key_file,
1480 const gchar *group_name,
1481 const gchar *key,
1482 gsize *length,
1483 GError **error)
1485 GError *key_file_error = NULL;
1486 gchar *value, *string_value, **values;
1487 gint i, len;
1488 GSList *p, *pieces = NULL;
1490 g_return_val_if_fail (key_file != NULL, NULL);
1491 g_return_val_if_fail (group_name != NULL, NULL);
1492 g_return_val_if_fail (key != NULL, NULL);
1494 if (length)
1495 *length = 0;
1497 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1499 if (key_file_error)
1501 g_propagate_error (error, key_file_error);
1502 return NULL;
1505 if (!g_utf8_validate (value, -1, NULL))
1507 gchar *value_utf8 = _g_utf8_make_valid (value);
1508 g_set_error (error, G_KEY_FILE_ERROR,
1509 G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1510 _("Key file contains key '%s' with value '%s' "
1511 "which is not UTF-8"), key, value_utf8);
1512 g_free (value_utf8);
1513 g_free (value);
1515 return NULL;
1518 string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1519 g_free (value);
1520 g_free (string_value);
1522 if (key_file_error)
1524 if (g_error_matches (key_file_error,
1525 G_KEY_FILE_ERROR,
1526 G_KEY_FILE_ERROR_INVALID_VALUE))
1528 g_set_error (error, G_KEY_FILE_ERROR,
1529 G_KEY_FILE_ERROR_INVALID_VALUE,
1530 _("Key file contains key '%s' "
1531 "which has a value that cannot be interpreted."),
1532 key);
1533 g_error_free (key_file_error);
1535 else
1536 g_propagate_error (error, key_file_error);
1538 return NULL;
1541 len = g_slist_length (pieces);
1542 values = g_new (gchar *, len + 1);
1543 for (p = pieces, i = 0; p; p = p->next)
1544 values[i++] = p->data;
1545 values[len] = NULL;
1547 g_slist_free (pieces);
1549 if (length)
1550 *length = len;
1552 return values;
1556 * g_key_file_set_string_list:
1557 * @key_file: a #GKeyFile
1558 * @group_name: a group name
1559 * @key: a key
1560 * @list: an array of string values
1561 * @length: number of string values in @list
1563 * Associates a list of string values for @key under @group_name.
1564 * If @key cannot be found then it is created.
1565 * If @group_name cannot be found then it is created.
1567 * Since: 2.6
1569 void
1570 g_key_file_set_string_list (GKeyFile *key_file,
1571 const gchar *group_name,
1572 const gchar *key,
1573 const gchar * const list[],
1574 gsize length)
1576 GString *value_list;
1577 gsize i;
1579 g_return_if_fail (key_file != NULL);
1580 g_return_if_fail (list != NULL || length == 0);
1582 value_list = g_string_sized_new (length * 128);
1583 for (i = 0; i < length && list[i] != NULL; i++)
1585 gchar *value;
1587 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1588 g_string_append (value_list, value);
1589 g_string_append_c (value_list, key_file->list_separator);
1591 g_free (value);
1594 g_key_file_set_value (key_file, group_name, key, value_list->str);
1595 g_string_free (value_list, TRUE);
1599 * g_key_file_set_locale_string:
1600 * @key_file: a #GKeyFile
1601 * @group_name: a group name
1602 * @key: a key
1603 * @locale: a locale identifier
1604 * @string: a string
1606 * Associates a string value for @key and @locale under @group_name.
1607 * If the translation for @key cannot be found then it is created.
1609 * Since: 2.6
1611 void
1612 g_key_file_set_locale_string (GKeyFile *key_file,
1613 const gchar *group_name,
1614 const gchar *key,
1615 const gchar *locale,
1616 const gchar *string)
1618 gchar *full_key, *value;
1620 g_return_if_fail (key_file != NULL);
1621 g_return_if_fail (key != NULL);
1622 g_return_if_fail (locale != NULL);
1623 g_return_if_fail (string != NULL);
1625 value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1626 full_key = g_strdup_printf ("%s[%s]", key, locale);
1627 g_key_file_set_value (key_file, group_name, full_key, value);
1628 g_free (full_key);
1629 g_free (value);
1632 extern GSList *_g_compute_locale_variants (const gchar *locale);
1635 * g_key_file_get_locale_string:
1636 * @key_file: a #GKeyFile
1637 * @group_name: a group name
1638 * @key: a key
1639 * @locale: a locale identifier or %NULL
1640 * @error: return location for a #GError, or %NULL
1642 * Returns the value associated with @key under @group_name
1643 * translated in the given @locale if available. If @locale is
1644 * %NULL then the current locale is assumed.
1646 * If @key cannot be found then %NULL is returned and @error is set
1647 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1648 * with @key cannot be interpreted or no suitable translation can
1649 * be found then the untranslated value is returned.
1651 * Return value: a newly allocated string or %NULL if the specified
1652 * key cannot be found.
1654 * Since: 2.6
1656 gchar *
1657 g_key_file_get_locale_string (GKeyFile *key_file,
1658 const gchar *group_name,
1659 const gchar *key,
1660 const gchar *locale,
1661 GError **error)
1663 gchar *candidate_key, *translated_value;
1664 GError *key_file_error;
1665 gchar **languages;
1666 gboolean free_languages = FALSE;
1667 gint i;
1669 g_return_val_if_fail (key_file != NULL, NULL);
1670 g_return_val_if_fail (group_name != NULL, NULL);
1671 g_return_val_if_fail (key != NULL, NULL);
1673 candidate_key = NULL;
1674 translated_value = NULL;
1675 key_file_error = NULL;
1677 if (locale)
1679 GSList *l, *list;
1681 list = _g_compute_locale_variants (locale);
1683 languages = g_new (gchar *, g_slist_length (list) + 1);
1684 for (l = list, i = 0; l; l = l->next, i++)
1685 languages[i] = l->data;
1686 languages[i] = NULL;
1688 g_slist_free (list);
1689 free_languages = TRUE;
1691 else
1693 languages = (gchar **) g_get_language_names ();
1694 free_languages = FALSE;
1697 for (i = 0; languages[i]; i++)
1699 candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1701 translated_value = g_key_file_get_string (key_file,
1702 group_name,
1703 candidate_key, NULL);
1704 g_free (candidate_key);
1706 if (translated_value)
1707 break;
1709 g_free (translated_value);
1710 translated_value = NULL;
1713 /* Fallback to untranslated key
1715 if (!translated_value)
1717 translated_value = g_key_file_get_string (key_file, group_name, key,
1718 &key_file_error);
1720 if (!translated_value)
1721 g_propagate_error (error, key_file_error);
1724 if (free_languages)
1725 g_strfreev (languages);
1727 return translated_value;
1730 /**
1731 * g_key_file_get_locale_string_list:
1732 * @key_file: a #GKeyFile
1733 * @group_name: a group name
1734 * @key: a key
1735 * @locale: a locale identifier or %NULL
1736 * @length: return location for the number of returned strings or %NULL
1737 * @error: return location for a #GError or %NULL
1739 * Returns the values associated with @key under @group_name
1740 * translated in the given @locale if available. If @locale is
1741 * %NULL then the current locale is assumed.
1743 * If @key cannot be found then %NULL is returned and @error is set
1744 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1745 * with @key cannot be interpreted or no suitable translations
1746 * can be found then the untranslated values are returned. The
1747 * returned array is %NULL-terminated, so @length may optionally
1748 * be %NULL.
1750 * Return value: a newly allocated %NULL-terminated string array
1751 * or %NULL if the key isn't found. The string array should be freed
1752 * with g_strfreev().
1754 * Since: 2.6
1756 gchar **
1757 g_key_file_get_locale_string_list (GKeyFile *key_file,
1758 const gchar *group_name,
1759 const gchar *key,
1760 const gchar *locale,
1761 gsize *length,
1762 GError **error)
1764 GError *key_file_error;
1765 gchar **values, *value;
1766 char list_separator[2];
1767 gsize len;
1769 g_return_val_if_fail (key_file != NULL, NULL);
1770 g_return_val_if_fail (group_name != NULL, NULL);
1771 g_return_val_if_fail (key != NULL, NULL);
1773 key_file_error = NULL;
1775 value = g_key_file_get_locale_string (key_file, group_name,
1776 key, locale,
1777 &key_file_error);
1779 if (key_file_error)
1780 g_propagate_error (error, key_file_error);
1782 if (!value)
1784 if (length)
1785 *length = 0;
1786 return NULL;
1789 len = strlen (value);
1790 if (value[len - 1] == key_file->list_separator)
1791 value[len - 1] = '\0';
1793 list_separator[0] = key_file->list_separator;
1794 list_separator[1] = '\0';
1795 values = g_strsplit (value, list_separator, 0);
1797 g_free (value);
1799 if (length)
1800 *length = g_strv_length (values);
1802 return values;
1806 * g_key_file_set_locale_string_list:
1807 * @key_file: a #GKeyFile
1808 * @group_name: a group name
1809 * @key: a key
1810 * @locale: a locale identifier
1811 * @list: a %NULL-terminated array of locale string values
1812 * @length: the length of @list
1814 * Associates a list of string values for @key and @locale under
1815 * @group_name. If the translation for @key cannot be found then
1816 * it is created.
1818 * Since: 2.6
1820 void
1821 g_key_file_set_locale_string_list (GKeyFile *key_file,
1822 const gchar *group_name,
1823 const gchar *key,
1824 const gchar *locale,
1825 const gchar * const list[],
1826 gsize length)
1828 GString *value_list;
1829 gchar *full_key;
1830 gsize i;
1832 g_return_if_fail (key_file != NULL);
1833 g_return_if_fail (key != NULL);
1834 g_return_if_fail (locale != NULL);
1835 g_return_if_fail (length != 0);
1837 value_list = g_string_sized_new (length * 128);
1838 for (i = 0; i < length && list[i] != NULL; i++)
1840 gchar *value;
1842 value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1843 g_string_append (value_list, value);
1844 g_string_append_c (value_list, key_file->list_separator);
1846 g_free (value);
1849 full_key = g_strdup_printf ("%s[%s]", key, locale);
1850 g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1851 g_free (full_key);
1852 g_string_free (value_list, TRUE);
1856 * g_key_file_get_boolean:
1857 * @key_file: a #GKeyFile
1858 * @group_name: a group name
1859 * @key: a key
1860 * @error: return location for a #GError
1862 * Returns the value associated with @key under @group_name as a
1863 * boolean.
1865 * If @key cannot be found then %FALSE is returned and @error is set
1866 * to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value
1867 * associated with @key cannot be interpreted as a boolean then %FALSE
1868 * is returned and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1870 * Return value: the value associated with the key as a boolean,
1871 * or %FALSE if the key was not found or could not be parsed.
1873 * Since: 2.6
1875 gboolean
1876 g_key_file_get_boolean (GKeyFile *key_file,
1877 const gchar *group_name,
1878 const gchar *key,
1879 GError **error)
1881 GError *key_file_error = NULL;
1882 gchar *value;
1883 gboolean bool_value;
1885 g_return_val_if_fail (key_file != NULL, FALSE);
1886 g_return_val_if_fail (group_name != NULL, FALSE);
1887 g_return_val_if_fail (key != NULL, FALSE);
1889 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1891 if (!value)
1893 g_propagate_error (error, key_file_error);
1894 return FALSE;
1897 bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1898 &key_file_error);
1899 g_free (value);
1901 if (key_file_error)
1903 if (g_error_matches (key_file_error,
1904 G_KEY_FILE_ERROR,
1905 G_KEY_FILE_ERROR_INVALID_VALUE))
1907 g_set_error (error, G_KEY_FILE_ERROR,
1908 G_KEY_FILE_ERROR_INVALID_VALUE,
1909 _("Key file contains key '%s' "
1910 "which has value that cannot be interpreted."),
1911 key);
1912 g_error_free (key_file_error);
1914 else
1915 g_propagate_error (error, key_file_error);
1918 return bool_value;
1922 * g_key_file_set_boolean:
1923 * @key_file: a #GKeyFile
1924 * @group_name: a group name
1925 * @key: a key
1926 * @value: %TRUE or %FALSE
1928 * Associates a new boolean value with @key under @group_name.
1929 * If @key cannot be found then it is created.
1931 * Since: 2.6
1933 void
1934 g_key_file_set_boolean (GKeyFile *key_file,
1935 const gchar *group_name,
1936 const gchar *key,
1937 gboolean value)
1939 gchar *result;
1941 g_return_if_fail (key_file != NULL);
1943 result = g_key_file_parse_boolean_as_value (key_file, value);
1944 g_key_file_set_value (key_file, group_name, key, result);
1945 g_free (result);
1949 * g_key_file_get_boolean_list:
1950 * @key_file: a #GKeyFile
1951 * @group_name: a group name
1952 * @key: a key
1953 * @length: the number of booleans returned
1954 * @error: return location for a #GError
1956 * Returns the values associated with @key under @group_name as
1957 * booleans.
1959 * If @key cannot be found then %NULL is returned and @error is set to
1960 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
1961 * with @key cannot be interpreted as booleans then %NULL is returned
1962 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
1964 * Return value: the values associated with the key as a list of
1965 * booleans, or %NULL if the key was not found or could not be parsed.
1967 * Since: 2.6
1969 gboolean *
1970 g_key_file_get_boolean_list (GKeyFile *key_file,
1971 const gchar *group_name,
1972 const gchar *key,
1973 gsize *length,
1974 GError **error)
1976 GError *key_file_error;
1977 gchar **values;
1978 gboolean *bool_values;
1979 gsize i, num_bools;
1981 g_return_val_if_fail (key_file != NULL, NULL);
1982 g_return_val_if_fail (group_name != NULL, NULL);
1983 g_return_val_if_fail (key != NULL, NULL);
1985 if (length)
1986 *length = 0;
1988 key_file_error = NULL;
1990 values = g_key_file_get_string_list (key_file, group_name, key,
1991 &num_bools, &key_file_error);
1993 if (key_file_error)
1994 g_propagate_error (error, key_file_error);
1996 if (!values)
1997 return NULL;
1999 bool_values = g_new (gboolean, num_bools);
2001 for (i = 0; i < num_bools; i++)
2003 bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
2004 values[i],
2005 &key_file_error);
2007 if (key_file_error)
2009 g_propagate_error (error, key_file_error);
2010 g_strfreev (values);
2011 g_free (bool_values);
2013 return NULL;
2016 g_strfreev (values);
2018 if (length)
2019 *length = num_bools;
2021 return bool_values;
2025 * g_key_file_set_boolean_list:
2026 * @key_file: a #GKeyFile
2027 * @group_name: a group name
2028 * @key: a key
2029 * @list: an array of boolean values
2030 * @length: length of @list
2032 * Associates a list of boolean values with @key under @group_name.
2033 * If @key cannot be found then it is created.
2034 * If @group_name is %NULL, the start_group is used.
2036 * Since: 2.6
2038 void
2039 g_key_file_set_boolean_list (GKeyFile *key_file,
2040 const gchar *group_name,
2041 const gchar *key,
2042 gboolean list[],
2043 gsize length)
2045 GString *value_list;
2046 gsize i;
2048 g_return_if_fail (key_file != NULL);
2049 g_return_if_fail (list != NULL);
2051 value_list = g_string_sized_new (length * 8);
2052 for (i = 0; i < length; i++)
2054 gchar *value;
2056 value = g_key_file_parse_boolean_as_value (key_file, list[i]);
2058 g_string_append (value_list, value);
2059 g_string_append_c (value_list, key_file->list_separator);
2061 g_free (value);
2064 g_key_file_set_value (key_file, group_name, key, value_list->str);
2065 g_string_free (value_list, TRUE);
2069 * g_key_file_get_integer:
2070 * @key_file: a #GKeyFile
2071 * @group_name: a group name
2072 * @key: a key
2073 * @error: return location for a #GError
2075 * Returns the value associated with @key under @group_name as an
2076 * integer.
2078 * If @key cannot be found then 0 is returned and @error is set to
2079 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2080 * with @key cannot be interpreted as an integer then 0 is returned
2081 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2083 * Return value: the value associated with the key as an integer, or
2084 * 0 if the key was not found or could not be parsed.
2086 * Since: 2.6
2088 gint
2089 g_key_file_get_integer (GKeyFile *key_file,
2090 const gchar *group_name,
2091 const gchar *key,
2092 GError **error)
2094 GError *key_file_error;
2095 gchar *value;
2096 gint int_value;
2098 g_return_val_if_fail (key_file != NULL, -1);
2099 g_return_val_if_fail (group_name != NULL, -1);
2100 g_return_val_if_fail (key != NULL, -1);
2102 key_file_error = NULL;
2104 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2106 if (key_file_error)
2108 g_propagate_error (error, key_file_error);
2109 return 0;
2112 int_value = g_key_file_parse_value_as_integer (key_file, value,
2113 &key_file_error);
2114 g_free (value);
2116 if (key_file_error)
2118 if (g_error_matches (key_file_error,
2119 G_KEY_FILE_ERROR,
2120 G_KEY_FILE_ERROR_INVALID_VALUE))
2122 g_set_error (error, G_KEY_FILE_ERROR,
2123 G_KEY_FILE_ERROR_INVALID_VALUE,
2124 _("Key file contains key '%s' in group '%s' "
2125 "which has value that cannot be interpreted."), key,
2126 group_name);
2127 g_error_free (key_file_error);
2129 else
2130 g_propagate_error (error, key_file_error);
2133 return int_value;
2137 * g_key_file_set_integer:
2138 * @key_file: a #GKeyFile
2139 * @group_name: a group name
2140 * @key: a key
2141 * @value: an integer value
2143 * Associates a new integer value with @key under @group_name.
2144 * If @key cannot be found then it is created.
2146 * Since: 2.6
2148 void
2149 g_key_file_set_integer (GKeyFile *key_file,
2150 const gchar *group_name,
2151 const gchar *key,
2152 gint value)
2154 gchar *result;
2156 g_return_if_fail (key_file != NULL);
2158 result = g_key_file_parse_integer_as_value (key_file, value);
2159 g_key_file_set_value (key_file, group_name, key, result);
2160 g_free (result);
2164 * g_key_file_get_integer_list:
2165 * @key_file: a #GKeyFile
2166 * @group_name: a group name
2167 * @key: a key
2168 * @length: the number of integers returned
2169 * @error: return location for a #GError
2171 * Returns the values associated with @key under @group_name as
2172 * integers.
2174 * If @key cannot be found then %NULL is returned and @error is set to
2175 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2176 * with @key cannot be interpreted as integers then %NULL is returned
2177 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2179 * Return value: the values associated with the key as a list of
2180 * integers, or %NULL if the key was not found or could not be parsed.
2182 * Since: 2.6
2184 gint *
2185 g_key_file_get_integer_list (GKeyFile *key_file,
2186 const gchar *group_name,
2187 const gchar *key,
2188 gsize *length,
2189 GError **error)
2191 GError *key_file_error = NULL;
2192 gchar **values;
2193 gint *int_values;
2194 gsize i, num_ints;
2196 g_return_val_if_fail (key_file != NULL, NULL);
2197 g_return_val_if_fail (group_name != NULL, NULL);
2198 g_return_val_if_fail (key != NULL, NULL);
2200 if (length)
2201 *length = 0;
2203 values = g_key_file_get_string_list (key_file, group_name, key,
2204 &num_ints, &key_file_error);
2206 if (key_file_error)
2207 g_propagate_error (error, key_file_error);
2209 if (!values)
2210 return NULL;
2212 int_values = g_new (gint, num_ints);
2214 for (i = 0; i < num_ints; i++)
2216 int_values[i] = g_key_file_parse_value_as_integer (key_file,
2217 values[i],
2218 &key_file_error);
2220 if (key_file_error)
2222 g_propagate_error (error, key_file_error);
2223 g_strfreev (values);
2224 g_free (int_values);
2226 return NULL;
2229 g_strfreev (values);
2231 if (length)
2232 *length = num_ints;
2234 return int_values;
2238 * g_key_file_set_integer_list:
2239 * @key_file: a #GKeyFile
2240 * @group_name: a group name
2241 * @key: a key
2242 * @list: an array of integer values
2243 * @length: number of integer values in @list
2245 * Associates a list of integer values with @key under @group_name.
2246 * If @key cannot be found then it is created.
2248 * Since: 2.6
2250 void
2251 g_key_file_set_integer_list (GKeyFile *key_file,
2252 const gchar *group_name,
2253 const gchar *key,
2254 gint list[],
2255 gsize length)
2257 GString *values;
2258 gsize i;
2260 g_return_if_fail (key_file != NULL);
2261 g_return_if_fail (list != NULL);
2263 values = g_string_sized_new (length * 16);
2264 for (i = 0; i < length; i++)
2266 gchar *value;
2268 value = g_key_file_parse_integer_as_value (key_file, list[i]);
2270 g_string_append (values, value);
2271 g_string_append_c (values, key_file->list_separator);
2273 g_free (value);
2276 g_key_file_set_value (key_file, group_name, key, values->str);
2277 g_string_free (values, TRUE);
2281 * g_key_file_get_double:
2282 * @key_file: a #GKeyFile
2283 * @group_name: a group name
2284 * @key: a key
2285 * @error: return location for a #GError
2287 * Returns the value associated with @key under @group_name as a
2288 * double. If @group_name is %NULL, the start_group is used.
2290 * If @key cannot be found then 0.0 is returned and @error is set to
2291 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated
2292 * with @key cannot be interpreted as a double then 0.0 is returned
2293 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2295 * Return value: the value associated with the key as a double, or
2296 * 0.0 if the key was not found or could not be parsed.
2298 * Since: 2.12
2300 gdouble
2301 g_key_file_get_double (GKeyFile *key_file,
2302 const gchar *group_name,
2303 const gchar *key,
2304 GError **error)
2306 GError *key_file_error;
2307 gchar *value;
2308 gdouble double_value;
2310 g_return_val_if_fail (key_file != NULL, -1);
2311 g_return_val_if_fail (group_name != NULL, -1);
2312 g_return_val_if_fail (key != NULL, -1);
2314 key_file_error = NULL;
2316 value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2318 if (key_file_error)
2320 g_propagate_error (error, key_file_error);
2321 return 0;
2324 double_value = g_key_file_parse_value_as_double (key_file, value,
2325 &key_file_error);
2326 g_free (value);
2328 if (key_file_error)
2330 if (g_error_matches (key_file_error,
2331 G_KEY_FILE_ERROR,
2332 G_KEY_FILE_ERROR_INVALID_VALUE))
2334 g_set_error (error, G_KEY_FILE_ERROR,
2335 G_KEY_FILE_ERROR_INVALID_VALUE,
2336 _("Key file contains key '%s' in group '%s' "
2337 "which has value that cannot be interpreted."), key,
2338 group_name);
2339 g_error_free (key_file_error);
2341 else
2342 g_propagate_error (error, key_file_error);
2345 return double_value;
2349 * g_key_file_set_double:
2350 * @key_file: a #GKeyFile
2351 * @group_name: a group name
2352 * @key: a key
2353 * @value: an double value
2355 * Associates a new double value with @key under @group_name.
2356 * If @key cannot be found then it is created.
2358 * Since: 2.12
2360 void
2361 g_key_file_set_double (GKeyFile *key_file,
2362 const gchar *group_name,
2363 const gchar *key,
2364 gdouble value)
2366 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2368 g_return_if_fail (key_file != NULL);
2370 g_ascii_dtostr (result, sizeof (result), value);
2371 g_key_file_set_value (key_file, group_name, key, result);
2375 * g_key_file_get_double_list:
2376 * @key_file: a #GKeyFile
2377 * @group_name: a group name
2378 * @key: a key
2379 * @length: the number of doubles returned
2380 * @error: return location for a #GError
2382 * Returns the values associated with @key under @group_name as
2383 * doubles.
2385 * If @key cannot be found then %NULL is returned and @error is set to
2386 * #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values associated
2387 * with @key cannot be interpreted as doubles then %NULL is returned
2388 * and @error is set to #G_KEY_FILE_ERROR_INVALID_VALUE.
2390 * Return value: the values associated with the key as a list of
2391 * doubles, or %NULL if the key was not found or could not be parsed.
2393 * Since: 2.12
2395 gdouble *
2396 g_key_file_get_double_list (GKeyFile *key_file,
2397 const gchar *group_name,
2398 const gchar *key,
2399 gsize *length,
2400 GError **error)
2402 GError *key_file_error = NULL;
2403 gchar **values;
2404 gdouble *double_values;
2405 gsize i, num_doubles;
2407 g_return_val_if_fail (key_file != NULL, NULL);
2408 g_return_val_if_fail (group_name != NULL, NULL);
2409 g_return_val_if_fail (key != NULL, NULL);
2411 if (length)
2412 *length = 0;
2414 values = g_key_file_get_string_list (key_file, group_name, key,
2415 &num_doubles, &key_file_error);
2417 if (key_file_error)
2418 g_propagate_error (error, key_file_error);
2420 if (!values)
2421 return NULL;
2423 double_values = g_new (gdouble, num_doubles);
2425 for (i = 0; i < num_doubles; i++)
2427 double_values[i] = g_key_file_parse_value_as_double (key_file,
2428 values[i],
2429 &key_file_error);
2431 if (key_file_error)
2433 g_propagate_error (error, key_file_error);
2434 g_strfreev (values);
2435 g_free (double_values);
2437 return NULL;
2440 g_strfreev (values);
2442 if (length)
2443 *length = num_doubles;
2445 return double_values;
2449 * g_key_file_set_double_list:
2450 * @key_file: a #GKeyFile
2451 * @group_name: a group name
2452 * @key: a key
2453 * @list: an array of double values
2454 * @length: number of double values in @list
2456 * Associates a list of double values with @key under
2457 * @group_name. If @key cannot be found then it is created.
2459 * Since: 2.12
2461 void
2462 g_key_file_set_double_list (GKeyFile *key_file,
2463 const gchar *group_name,
2464 const gchar *key,
2465 gdouble list[],
2466 gsize length)
2468 GString *values;
2469 gsize i;
2471 g_return_if_fail (key_file != NULL);
2472 g_return_if_fail (list != NULL);
2474 values = g_string_sized_new (length * 16);
2475 for (i = 0; i < length; i++)
2477 gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2479 g_ascii_dtostr( result, sizeof (result), list[i] );
2481 g_string_append (values, result);
2482 g_string_append_c (values, key_file->list_separator);
2485 g_key_file_set_value (key_file, group_name, key, values->str);
2486 g_string_free (values, TRUE);
2489 static gboolean
2490 g_key_file_set_key_comment (GKeyFile *key_file,
2491 const gchar *group_name,
2492 const gchar *key,
2493 const gchar *comment,
2494 GError **error)
2496 GKeyFileGroup *group;
2497 GKeyFileKeyValuePair *pair;
2498 GList *key_node, *comment_node, *tmp;
2500 group = g_key_file_lookup_group (key_file, group_name);
2501 if (!group)
2503 g_set_error (error, G_KEY_FILE_ERROR,
2504 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2505 _("Key file does not have group '%s'"),
2506 group_name ? group_name : "(null)");
2508 return FALSE;
2511 /* First find the key the comments are supposed to be
2512 * associated with
2514 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2516 if (key_node == NULL)
2518 g_set_error (error, G_KEY_FILE_ERROR,
2519 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2520 _("Key file does not have key '%s' in group '%s'"),
2521 key, group->name);
2522 return FALSE;
2525 /* Then find all the comments already associated with the
2526 * key and free them
2528 tmp = key_node->next;
2529 while (tmp != NULL)
2531 pair = (GKeyFileKeyValuePair *) tmp->data;
2533 if (pair->key != NULL)
2534 break;
2536 comment_node = tmp;
2537 tmp = tmp->next;
2538 g_key_file_remove_key_value_pair_node (key_file, group,
2539 comment_node);
2542 if (comment == NULL)
2543 return TRUE;
2545 /* Now we can add our new comment
2547 pair = g_slice_new (GKeyFileKeyValuePair);
2548 pair->key = NULL;
2549 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2551 key_node = g_list_insert (key_node, pair, 1);
2553 return TRUE;
2556 static gboolean
2557 g_key_file_set_group_comment (GKeyFile *key_file,
2558 const gchar *group_name,
2559 const gchar *comment,
2560 GError **error)
2562 GKeyFileGroup *group;
2564 g_return_val_if_fail (g_key_file_is_group_name (group_name), FALSE);
2566 group = g_key_file_lookup_group (key_file, group_name);
2567 if (!group)
2569 g_set_error (error, G_KEY_FILE_ERROR,
2570 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2571 _("Key file does not have group '%s'"),
2572 group_name ? group_name : "(null)");
2574 return FALSE;
2577 /* First remove any existing comment
2579 if (group->comment)
2581 g_key_file_key_value_pair_free (group->comment);
2582 group->comment = NULL;
2585 if (comment == NULL)
2586 return TRUE;
2588 /* Now we can add our new comment
2590 group->comment = g_slice_new (GKeyFileKeyValuePair);
2591 group->comment->key = NULL;
2592 group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2594 return TRUE;
2597 static gboolean
2598 g_key_file_set_top_comment (GKeyFile *key_file,
2599 const gchar *comment,
2600 GError **error)
2602 GList *group_node;
2603 GKeyFileGroup *group;
2604 GKeyFileKeyValuePair *pair;
2606 /* The last group in the list should be the top (comments only)
2607 * group in the file
2609 g_warn_if_fail (key_file->groups != NULL);
2610 group_node = g_list_last (key_file->groups);
2611 group = (GKeyFileGroup *) group_node->data;
2612 g_warn_if_fail (group->name == NULL);
2614 /* Note all keys must be comments at the top of
2615 * the file, so we can just free it all.
2617 if (group->key_value_pairs != NULL)
2619 g_list_foreach (group->key_value_pairs,
2620 (GFunc) g_key_file_key_value_pair_free,
2621 NULL);
2622 g_list_free (group->key_value_pairs);
2623 group->key_value_pairs = NULL;
2626 if (comment == NULL)
2627 return TRUE;
2629 pair = g_slice_new (GKeyFileKeyValuePair);
2630 pair->key = NULL;
2631 pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2633 group->key_value_pairs =
2634 g_list_prepend (group->key_value_pairs, pair);
2636 return TRUE;
2640 * g_key_file_set_comment:
2641 * @key_file: a #GKeyFile
2642 * @group_name: a group name, or %NULL
2643 * @key: a key
2644 * @comment: a comment
2645 * @error: return location for a #GError
2647 * Places a comment above @key from @group_name.
2648 * If @key is %NULL then @comment will be written above @group_name.
2649 * If both @key and @group_name are %NULL, then @comment will be
2650 * written above the first group in the file.
2652 * Returns: %TRUE if the comment was written, %FALSE otherwise
2654 * Since: 2.6
2656 gboolean
2657 g_key_file_set_comment (GKeyFile *key_file,
2658 const gchar *group_name,
2659 const gchar *key,
2660 const gchar *comment,
2661 GError **error)
2663 g_return_val_if_fail (key_file != NULL, FALSE);
2665 if (group_name != NULL && key != NULL)
2667 if (!g_key_file_set_key_comment (key_file, group_name, key, comment, error))
2668 return FALSE;
2670 else if (group_name != NULL)
2672 if (!g_key_file_set_group_comment (key_file, group_name, comment, error))
2673 return FALSE;
2675 else
2677 if (!g_key_file_set_top_comment (key_file, comment, error))
2678 return FALSE;
2681 if (comment != NULL)
2682 key_file->approximate_size += strlen (comment);
2684 return TRUE;
2687 static gchar *
2688 g_key_file_get_key_comment (GKeyFile *key_file,
2689 const gchar *group_name,
2690 const gchar *key,
2691 GError **error)
2693 GKeyFileGroup *group;
2694 GKeyFileKeyValuePair *pair;
2695 GList *key_node, *tmp;
2696 GString *string;
2697 gchar *comment;
2699 g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2701 group = g_key_file_lookup_group (key_file, group_name);
2702 if (!group)
2704 g_set_error (error, G_KEY_FILE_ERROR,
2705 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2706 _("Key file does not have group '%s'"),
2707 group_name ? group_name : "(null)");
2709 return NULL;
2712 /* First find the key the comments are supposed to be
2713 * associated with
2715 key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2717 if (key_node == NULL)
2719 g_set_error (error, G_KEY_FILE_ERROR,
2720 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2721 _("Key file does not have key '%s' in group '%s'"),
2722 key, group->name);
2723 return NULL;
2726 string = NULL;
2728 /* Then find all the comments already associated with the
2729 * key and concatentate them.
2731 tmp = key_node->next;
2732 if (!key_node->next)
2733 return NULL;
2735 pair = (GKeyFileKeyValuePair *) tmp->data;
2736 if (pair->key != NULL)
2737 return NULL;
2739 while (tmp->next)
2741 pair = (GKeyFileKeyValuePair *) tmp->next->data;
2743 if (pair->key != NULL)
2744 break;
2746 tmp = tmp->next;
2749 while (tmp != key_node)
2751 pair = (GKeyFileKeyValuePair *) tmp->data;
2753 if (string == NULL)
2754 string = g_string_sized_new (512);
2756 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2757 g_string_append (string, comment);
2758 g_free (comment);
2760 tmp = tmp->prev;
2763 if (string != NULL)
2765 comment = string->str;
2766 g_string_free (string, FALSE);
2768 else
2769 comment = NULL;
2771 return comment;
2774 static gchar *
2775 get_group_comment (GKeyFile *key_file,
2776 GKeyFileGroup *group,
2777 GError **error)
2779 GString *string;
2780 GList *tmp;
2781 gchar *comment;
2783 string = NULL;
2785 tmp = group->key_value_pairs;
2786 while (tmp)
2788 GKeyFileKeyValuePair *pair;
2790 pair = (GKeyFileKeyValuePair *) tmp->data;
2792 if (pair->key != NULL)
2794 tmp = tmp->prev;
2795 break;
2798 if (tmp->next == NULL)
2799 break;
2801 tmp = tmp->next;
2804 while (tmp != NULL)
2806 GKeyFileKeyValuePair *pair;
2808 pair = (GKeyFileKeyValuePair *) tmp->data;
2810 if (string == NULL)
2811 string = g_string_sized_new (512);
2813 comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2814 g_string_append (string, comment);
2815 g_free (comment);
2817 tmp = tmp->prev;
2820 if (string != NULL)
2821 return g_string_free (string, FALSE);
2823 return NULL;
2826 static gchar *
2827 g_key_file_get_group_comment (GKeyFile *key_file,
2828 const gchar *group_name,
2829 GError **error)
2831 GList *group_node;
2832 GKeyFileGroup *group;
2834 group = g_key_file_lookup_group (key_file, group_name);
2835 if (!group)
2837 g_set_error (error, G_KEY_FILE_ERROR,
2838 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2839 _("Key file does not have group '%s'"),
2840 group_name ? group_name : "(null)");
2842 return NULL;
2845 if (group->comment)
2846 return g_strdup (group->comment->value);
2848 group_node = g_key_file_lookup_group_node (key_file, group_name);
2849 group_node = group_node->next;
2850 group = (GKeyFileGroup *)group_node->data;
2851 return get_group_comment (key_file, group, error);
2854 static gchar *
2855 g_key_file_get_top_comment (GKeyFile *key_file,
2856 GError **error)
2858 GList *group_node;
2859 GKeyFileGroup *group;
2861 /* The last group in the list should be the top (comments only)
2862 * group in the file
2864 g_warn_if_fail (key_file->groups != NULL);
2865 group_node = g_list_last (key_file->groups);
2866 group = (GKeyFileGroup *) group_node->data;
2867 g_warn_if_fail (group->name == NULL);
2869 return get_group_comment (key_file, group, error);
2873 * g_key_file_get_comment:
2874 * @key_file: a #GKeyFile
2875 * @group_name: a group name, or %NULL
2876 * @key: a key
2877 * @error: return location for a #GError
2879 * Retrieves a comment above @key from @group_name.
2880 * If @key is %NULL then @comment will be read from above
2881 * @group_name. If both @key and @group_name are %NULL, then
2882 * @comment will be read from above the first group in the file.
2884 * Returns: a comment that should be freed with g_free()
2886 * Since: 2.6
2888 gchar *
2889 g_key_file_get_comment (GKeyFile *key_file,
2890 const gchar *group_name,
2891 const gchar *key,
2892 GError **error)
2894 g_return_val_if_fail (key_file != NULL, NULL);
2896 if (group_name != NULL && key != NULL)
2897 return g_key_file_get_key_comment (key_file, group_name, key, error);
2898 else if (group_name != NULL)
2899 return g_key_file_get_group_comment (key_file, group_name, error);
2900 else
2901 return g_key_file_get_top_comment (key_file, error);
2905 * g_key_file_remove_comment:
2906 * @key_file: a #GKeyFile
2907 * @group_name: a group name, or %NULL
2908 * @key: a key
2909 * @error: return location for a #GError
2911 * Removes a comment above @key from @group_name.
2912 * If @key is %NULL then @comment will be removed above @group_name.
2913 * If both @key and @group_name are %NULL, then @comment will
2914 * be removed above the first group in the file.
2916 * Returns: %TRUE if the comment was removed, %FALSE otherwise
2918 * Since: 2.6
2921 gboolean
2922 g_key_file_remove_comment (GKeyFile *key_file,
2923 const gchar *group_name,
2924 const gchar *key,
2925 GError **error)
2927 g_return_val_if_fail (key_file != NULL, FALSE);
2929 if (group_name != NULL && key != NULL)
2930 return g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2931 else if (group_name != NULL)
2932 return g_key_file_set_group_comment (key_file, group_name, NULL, error);
2933 else
2934 return g_key_file_set_top_comment (key_file, NULL, error);
2938 * g_key_file_has_group:
2939 * @key_file: a #GKeyFile
2940 * @group_name: a group name
2942 * Looks whether the key file has the group @group_name.
2944 * Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2945 * otherwise.
2946 * Since: 2.6
2948 gboolean
2949 g_key_file_has_group (GKeyFile *key_file,
2950 const gchar *group_name)
2952 g_return_val_if_fail (key_file != NULL, FALSE);
2953 g_return_val_if_fail (group_name != NULL, FALSE);
2955 return g_key_file_lookup_group (key_file, group_name) != NULL;
2959 * g_key_file_has_key:
2960 * @key_file: a #GKeyFile
2961 * @group_name: a group name
2962 * @key: a key name
2963 * @error: return location for a #GError
2965 * Looks whether the key file has the key @key in the group
2966 * @group_name.
2968 * Return value: %TRUE if @key is a part of @group_name, %FALSE
2969 * otherwise.
2971 * Since: 2.6
2973 gboolean
2974 g_key_file_has_key (GKeyFile *key_file,
2975 const gchar *group_name,
2976 const gchar *key,
2977 GError **error)
2979 GKeyFileKeyValuePair *pair;
2980 GKeyFileGroup *group;
2982 g_return_val_if_fail (key_file != NULL, FALSE);
2983 g_return_val_if_fail (group_name != NULL, FALSE);
2984 g_return_val_if_fail (key != NULL, FALSE);
2986 group = g_key_file_lookup_group (key_file, group_name);
2988 if (!group)
2990 g_set_error (error, G_KEY_FILE_ERROR,
2991 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2992 _("Key file does not have group '%s'"),
2993 group_name ? group_name : "(null)");
2995 return FALSE;
2998 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3000 return pair != NULL;
3003 static void
3004 g_key_file_add_group (GKeyFile *key_file,
3005 const gchar *group_name)
3007 GKeyFileGroup *group;
3009 g_return_if_fail (key_file != NULL);
3010 g_return_if_fail (g_key_file_is_group_name (group_name));
3012 group = g_key_file_lookup_group (key_file, group_name);
3013 if (group != NULL)
3015 key_file->current_group = group;
3016 return;
3019 group = g_slice_new0 (GKeyFileGroup);
3020 group->name = g_strdup (group_name);
3021 group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
3022 key_file->groups = g_list_prepend (key_file->groups, group);
3023 key_file->approximate_size += strlen (group_name) + 3;
3024 key_file->current_group = group;
3026 if (key_file->start_group == NULL)
3027 key_file->start_group = group;
3029 g_hash_table_insert (key_file->group_hash, (gpointer)group->name, group);
3032 static void
3033 g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
3035 if (pair != NULL)
3037 g_free (pair->key);
3038 g_free (pair->value);
3039 g_slice_free (GKeyFileKeyValuePair, pair);
3043 /* Be careful not to call this function on a node with data in the
3044 * lookup map without removing it from the lookup map, first.
3046 * Some current cases where this warning is not a concern are
3047 * when:
3048 * - the node being removed is a comment node
3049 * - the entire lookup map is getting destroyed soon after
3050 * anyway.
3052 static void
3053 g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
3054 GKeyFileGroup *group,
3055 GList *pair_node)
3058 GKeyFileKeyValuePair *pair;
3060 pair = (GKeyFileKeyValuePair *) pair_node->data;
3062 group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
3064 if (pair->key != NULL)
3065 key_file->approximate_size -= strlen (pair->key) + 1;
3067 g_warn_if_fail (pair->value != NULL);
3068 key_file->approximate_size -= strlen (pair->value);
3070 g_key_file_key_value_pair_free (pair);
3072 g_list_free_1 (pair_node);
3075 static void
3076 g_key_file_remove_group_node (GKeyFile *key_file,
3077 GList *group_node)
3079 GKeyFileGroup *group;
3080 GList *tmp;
3082 group = (GKeyFileGroup *) group_node->data;
3084 if (group->name)
3085 g_hash_table_remove (key_file->group_hash, group->name);
3087 /* If the current group gets deleted make the current group the last
3088 * added group.
3090 if (key_file->current_group == group)
3092 /* groups should always contain at least the top comment group,
3093 * unless g_key_file_clear has been called
3095 if (key_file->groups)
3096 key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
3097 else
3098 key_file->current_group = NULL;
3101 /* If the start group gets deleted make the start group the first
3102 * added group.
3104 if (key_file->start_group == group)
3106 tmp = g_list_last (key_file->groups);
3107 while (tmp != NULL)
3109 if (tmp != group_node &&
3110 ((GKeyFileGroup *) tmp->data)->name != NULL)
3111 break;
3113 tmp = tmp->prev;
3116 if (tmp)
3117 key_file->start_group = (GKeyFileGroup *) tmp->data;
3118 else
3119 key_file->start_group = NULL;
3122 key_file->groups = g_list_remove_link (key_file->groups, group_node);
3124 if (group->name != NULL)
3125 key_file->approximate_size -= strlen (group->name) + 3;
3127 tmp = group->key_value_pairs;
3128 while (tmp != NULL)
3130 GList *pair_node;
3132 pair_node = tmp;
3133 tmp = tmp->next;
3134 g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3137 g_warn_if_fail (group->key_value_pairs == NULL);
3139 if (group->lookup_map)
3141 g_hash_table_destroy (group->lookup_map);
3142 group->lookup_map = NULL;
3145 g_free ((gchar *) group->name);
3146 g_slice_free (GKeyFileGroup, group);
3147 g_list_free_1 (group_node);
3151 * g_key_file_remove_group:
3152 * @key_file: a #GKeyFile
3153 * @group_name: a group name
3154 * @error: return location for a #GError or %NULL
3156 * Removes the specified group, @group_name,
3157 * from the key file.
3159 * Returns: %TRUE if the group was removed, %FALSE otherwise
3161 * Since: 2.6
3163 gboolean
3164 g_key_file_remove_group (GKeyFile *key_file,
3165 const gchar *group_name,
3166 GError **error)
3168 GList *group_node;
3170 g_return_val_if_fail (key_file != NULL, FALSE);
3171 g_return_val_if_fail (group_name != NULL, FALSE);
3173 group_node = g_key_file_lookup_group_node (key_file, group_name);
3175 if (!group_node)
3177 g_set_error (error, G_KEY_FILE_ERROR,
3178 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3179 _("Key file does not have group '%s'"),
3180 group_name);
3181 return FALSE;
3184 g_key_file_remove_group_node (key_file, group_node);
3186 return TRUE;
3189 static void
3190 g_key_file_add_key (GKeyFile *key_file,
3191 GKeyFileGroup *group,
3192 const gchar *key,
3193 const gchar *value)
3195 GKeyFileKeyValuePair *pair;
3197 pair = g_slice_new (GKeyFileKeyValuePair);
3198 pair->key = g_strdup (key);
3199 pair->value = g_strdup (value);
3201 g_hash_table_replace (group->lookup_map, pair->key, pair);
3202 group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3203 group->has_trailing_blank_line = FALSE;
3204 key_file->approximate_size += strlen (key) + strlen (value) + 2;
3208 * g_key_file_remove_key:
3209 * @key_file: a #GKeyFile
3210 * @group_name: a group name
3211 * @key: a key name to remove
3212 * @error: return location for a #GError or %NULL
3214 * Removes @key in @group_name from the key file.
3216 * Returns: %TRUE if the key was removed, %FALSE otherwise
3218 * Since: 2.6
3220 gboolean
3221 g_key_file_remove_key (GKeyFile *key_file,
3222 const gchar *group_name,
3223 const gchar *key,
3224 GError **error)
3226 GKeyFileGroup *group;
3227 GKeyFileKeyValuePair *pair;
3229 g_return_val_if_fail (key_file != NULL, FALSE);
3230 g_return_val_if_fail (group_name != NULL, FALSE);
3231 g_return_val_if_fail (key != NULL, FALSE);
3233 pair = NULL;
3235 group = g_key_file_lookup_group (key_file, group_name);
3236 if (!group)
3238 g_set_error (error, G_KEY_FILE_ERROR,
3239 G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3240 _("Key file does not have group '%s'"),
3241 group_name ? group_name : "(null)");
3242 return FALSE;
3245 pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3247 if (!pair)
3249 g_set_error (error, G_KEY_FILE_ERROR,
3250 G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3251 _("Key file does not have key '%s' in group '%s'"),
3252 key, group->name);
3253 return FALSE;
3256 key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3258 group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3259 g_hash_table_remove (group->lookup_map, pair->key);
3260 g_key_file_key_value_pair_free (pair);
3262 return TRUE;
3265 static GList *
3266 g_key_file_lookup_group_node (GKeyFile *key_file,
3267 const gchar *group_name)
3269 GKeyFileGroup *group;
3270 GList *tmp;
3272 for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3274 group = (GKeyFileGroup *) tmp->data;
3276 if (group && group->name && strcmp (group->name, group_name) == 0)
3277 break;
3280 return tmp;
3283 static GKeyFileGroup *
3284 g_key_file_lookup_group (GKeyFile *key_file,
3285 const gchar *group_name)
3287 return (GKeyFileGroup *)g_hash_table_lookup (key_file->group_hash, group_name);
3290 static GList *
3291 g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3292 GKeyFileGroup *group,
3293 const gchar *key)
3295 GList *key_node;
3297 for (key_node = group->key_value_pairs;
3298 key_node != NULL;
3299 key_node = key_node->next)
3301 GKeyFileKeyValuePair *pair;
3303 pair = (GKeyFileKeyValuePair *) key_node->data;
3305 if (pair->key && strcmp (pair->key, key) == 0)
3306 break;
3309 return key_node;
3312 static GKeyFileKeyValuePair *
3313 g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3314 GKeyFileGroup *group,
3315 const gchar *key)
3317 return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3320 /* Lines starting with # or consisting entirely of whitespace are merely
3321 * recorded, not parsed. This function assumes all leading whitespace
3322 * has been stripped.
3324 static gboolean
3325 g_key_file_line_is_comment (const gchar *line)
3327 return (*line == '#' || *line == '\0' || *line == '\n');
3330 static gboolean
3331 g_key_file_is_group_name (const gchar *name)
3333 gchar *p, *q;
3335 if (name == NULL)
3336 return FALSE;
3338 p = q = (gchar *) name;
3339 while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3340 q = g_utf8_find_next_char (q, NULL);
3342 if (*q != '\0' || q == p)
3343 return FALSE;
3345 return TRUE;
3348 static gboolean
3349 g_key_file_is_key_name (const gchar *name)
3351 gchar *p, *q;
3353 if (name == NULL)
3354 return FALSE;
3356 p = q = (gchar *) name;
3357 /* We accept a little more than the desktop entry spec says,
3358 * since gnome-vfs uses mime-types as keys in its cache.
3360 while (*q && *q != '=' && *q != '[' && *q != ']')
3361 q = g_utf8_find_next_char (q, NULL);
3363 /* No empty keys, please */
3364 if (q == p)
3365 return FALSE;
3367 /* We accept spaces in the middle of keys to not break
3368 * existing apps, but we don't tolerate initial or final
3369 * spaces, which would lead to silent corruption when
3370 * rereading the file.
3372 if (*p == ' ' || q[-1] == ' ')
3373 return FALSE;
3375 if (*q == '[')
3377 q++;
3378 while (*q && (g_unichar_isalnum (g_utf8_get_char_validated (q, -1)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3379 q = g_utf8_find_next_char (q, NULL);
3381 if (*q != ']')
3382 return FALSE;
3384 q++;
3387 if (*q != '\0')
3388 return FALSE;
3390 return TRUE;
3393 /* A group in a key file is made up of a starting '[' followed by one
3394 * or more letters making up the group name followed by ']'.
3396 static gboolean
3397 g_key_file_line_is_group (const gchar *line)
3399 gchar *p;
3401 p = (gchar *) line;
3402 if (*p != '[')
3403 return FALSE;
3405 p++;
3407 while (*p && *p != ']')
3408 p = g_utf8_find_next_char (p, NULL);
3410 if (*p != ']')
3411 return FALSE;
3413 /* silently accept whitespace after the ] */
3414 p = g_utf8_find_next_char (p, NULL);
3415 while (*p == ' ' || *p == '\t')
3416 p = g_utf8_find_next_char (p, NULL);
3418 if (*p)
3419 return FALSE;
3421 return TRUE;
3424 static gboolean
3425 g_key_file_line_is_key_value_pair (const gchar *line)
3427 gchar *p;
3429 p = (gchar *) g_utf8_strchr (line, -1, '=');
3431 if (!p)
3432 return FALSE;
3434 /* Key must be non-empty
3436 if (*p == line[0])
3437 return FALSE;
3439 return TRUE;
3442 static gchar *
3443 g_key_file_parse_value_as_string (GKeyFile *key_file,
3444 const gchar *value,
3445 GSList **pieces,
3446 GError **error)
3448 gchar *string_value, *p, *q0, *q;
3450 string_value = g_new (gchar, strlen (value) + 1);
3452 p = (gchar *) value;
3453 q0 = q = string_value;
3454 while (*p)
3456 if (*p == '\\')
3458 p++;
3460 switch (*p)
3462 case 's':
3463 *q = ' ';
3464 break;
3466 case 'n':
3467 *q = '\n';
3468 break;
3470 case 't':
3471 *q = '\t';
3472 break;
3474 case 'r':
3475 *q = '\r';
3476 break;
3478 case '\\':
3479 *q = '\\';
3480 break;
3482 case '\0':
3483 g_set_error_literal (error, G_KEY_FILE_ERROR,
3484 G_KEY_FILE_ERROR_INVALID_VALUE,
3485 _("Key file contains escape character "
3486 "at end of line"));
3487 break;
3489 default:
3490 if (pieces && *p == key_file->list_separator)
3491 *q = key_file->list_separator;
3492 else
3494 *q++ = '\\';
3495 *q = *p;
3497 if (*error == NULL)
3499 gchar sequence[3];
3501 sequence[0] = '\\';
3502 sequence[1] = *p;
3503 sequence[2] = '\0';
3505 g_set_error (error, G_KEY_FILE_ERROR,
3506 G_KEY_FILE_ERROR_INVALID_VALUE,
3507 _("Key file contains invalid escape "
3508 "sequence '%s'"), sequence);
3511 break;
3514 else
3516 *q = *p;
3517 if (pieces && (*p == key_file->list_separator))
3519 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3520 q0 = q + 1;
3524 if (*p == '\0')
3525 break;
3527 q++;
3528 p++;
3531 *q = '\0';
3532 if (pieces)
3534 if (q0 < q)
3535 *pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3536 *pieces = g_slist_reverse (*pieces);
3539 return string_value;
3542 static gchar *
3543 g_key_file_parse_string_as_value (GKeyFile *key_file,
3544 const gchar *string,
3545 gboolean escape_separator)
3547 gchar *value, *p, *q;
3548 gsize length;
3549 gboolean parsing_leading_space;
3551 length = strlen (string) + 1;
3553 /* Worst case would be that every character needs to be escaped.
3554 * In other words every character turns to two characters
3556 value = g_new (gchar, 2 * length);
3558 p = (gchar *) string;
3559 q = value;
3560 parsing_leading_space = TRUE;
3561 while (p < (string + length - 1))
3563 gchar escaped_character[3] = { '\\', 0, 0 };
3565 switch (*p)
3567 case ' ':
3568 if (parsing_leading_space)
3570 escaped_character[1] = 's';
3571 strcpy (q, escaped_character);
3572 q += 2;
3574 else
3576 *q = *p;
3577 q++;
3579 break;
3580 case '\t':
3581 if (parsing_leading_space)
3583 escaped_character[1] = 't';
3584 strcpy (q, escaped_character);
3585 q += 2;
3587 else
3589 *q = *p;
3590 q++;
3592 break;
3593 case '\n':
3594 escaped_character[1] = 'n';
3595 strcpy (q, escaped_character);
3596 q += 2;
3597 break;
3598 case '\r':
3599 escaped_character[1] = 'r';
3600 strcpy (q, escaped_character);
3601 q += 2;
3602 break;
3603 case '\\':
3604 escaped_character[1] = '\\';
3605 strcpy (q, escaped_character);
3606 q += 2;
3607 parsing_leading_space = FALSE;
3608 break;
3609 default:
3610 if (escape_separator && *p == key_file->list_separator)
3612 escaped_character[1] = key_file->list_separator;
3613 strcpy (q, escaped_character);
3614 q += 2;
3615 parsing_leading_space = TRUE;
3617 else
3619 *q = *p;
3620 q++;
3621 parsing_leading_space = FALSE;
3623 break;
3625 p++;
3627 *q = '\0';
3629 return value;
3632 static gint
3633 g_key_file_parse_value_as_integer (GKeyFile *key_file,
3634 const gchar *value,
3635 GError **error)
3637 gchar *end_of_valid_int;
3638 glong long_value;
3639 gint int_value;
3641 errno = 0;
3642 long_value = strtol (value, &end_of_valid_int, 10);
3644 if (*value == '\0' || *end_of_valid_int != '\0')
3646 gchar *value_utf8 = _g_utf8_make_valid (value);
3647 g_set_error (error, G_KEY_FILE_ERROR,
3648 G_KEY_FILE_ERROR_INVALID_VALUE,
3649 _("Value '%s' cannot be interpreted "
3650 "as a number."), value_utf8);
3651 g_free (value_utf8);
3653 return 0;
3656 int_value = long_value;
3657 if (int_value != long_value || errno == ERANGE)
3659 gchar *value_utf8 = _g_utf8_make_valid (value);
3660 g_set_error (error,
3661 G_KEY_FILE_ERROR,
3662 G_KEY_FILE_ERROR_INVALID_VALUE,
3663 _("Integer value '%s' out of range"),
3664 value_utf8);
3665 g_free (value_utf8);
3667 return 0;
3670 return int_value;
3673 static gchar *
3674 g_key_file_parse_integer_as_value (GKeyFile *key_file,
3675 gint value)
3678 return g_strdup_printf ("%d", value);
3681 static gdouble
3682 g_key_file_parse_value_as_double (GKeyFile *key_file,
3683 const gchar *value,
3684 GError **error)
3686 gchar *end_of_valid_d;
3687 gdouble double_value = 0;
3689 double_value = g_ascii_strtod (value, &end_of_valid_d);
3691 if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3693 gchar *value_utf8 = _g_utf8_make_valid (value);
3694 g_set_error (error, G_KEY_FILE_ERROR,
3695 G_KEY_FILE_ERROR_INVALID_VALUE,
3696 _("Value '%s' cannot be interpreted "
3697 "as a float number."),
3698 value_utf8);
3699 g_free (value_utf8);
3702 return double_value;
3705 static gboolean
3706 g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3707 const gchar *value,
3708 GError **error)
3710 gchar *value_utf8;
3712 if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3713 return TRUE;
3714 else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3715 return FALSE;
3717 value_utf8 = _g_utf8_make_valid (value);
3718 g_set_error (error, G_KEY_FILE_ERROR,
3719 G_KEY_FILE_ERROR_INVALID_VALUE,
3720 _("Value '%s' cannot be interpreted "
3721 "as a boolean."), value_utf8);
3722 g_free (value_utf8);
3724 return FALSE;
3727 static gchar *
3728 g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3729 gboolean value)
3731 if (value)
3732 return g_strdup ("true");
3733 else
3734 return g_strdup ("false");
3737 static gchar *
3738 g_key_file_parse_value_as_comment (GKeyFile *key_file,
3739 const gchar *value)
3741 GString *string;
3742 gchar **lines;
3743 gsize i;
3745 string = g_string_sized_new (512);
3747 lines = g_strsplit (value, "\n", 0);
3749 for (i = 0; lines[i] != NULL; i++)
3751 if (lines[i][0] != '#')
3752 g_string_append_printf (string, "%s\n", lines[i]);
3753 else
3754 g_string_append_printf (string, "%s\n", lines[i] + 1);
3756 g_strfreev (lines);
3758 return g_string_free (string, FALSE);
3761 static gchar *
3762 g_key_file_parse_comment_as_value (GKeyFile *key_file,
3763 const gchar *comment)
3765 GString *string;
3766 gchar **lines;
3767 gsize i;
3769 string = g_string_sized_new (512);
3771 lines = g_strsplit (comment, "\n", 0);
3773 for (i = 0; lines[i] != NULL; i++)
3774 g_string_append_printf (string, "#%s%s", lines[i],
3775 lines[i + 1] == NULL? "" : "\n");
3776 g_strfreev (lines);
3778 return g_string_free (string, FALSE);
3781 #define __G_KEY_FILE_C__
3782 #include "galiasdef.c"