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.
34
#include <sys/types.h>
43
#define S_ISREG(mode) ((mode)&_S_IFREG)
46
#endif /* G_OS_WIN23 */
51
#include "gfileutils.h"
57
#include "gmessages.h"
60
#include "gstrfuncs.h"
65
typedef struct _GKeyFileGroup GKeyFileGroup;
71
GKeyFileGroup *start_group;
72
GKeyFileGroup *current_group;
74
GString *parse_buffer; /* Holds up to one line of not-yet-parsed data */
76
/* Used for sizing the output buffer during serialization
78
gsize approximate_size;
85
typedef struct _GKeyFileKeyValuePair GKeyFileKeyValuePair;
89
const gchar *name; /* NULL for above first group (which will be comments) */
91
GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
92
gboolean has_trailing_blank_line;
94
GList *key_value_pairs;
96
/* Used in parallel with key_value_pairs for
97
* increased lookup performance
99
GHashTable *lookup_map;
102
struct _GKeyFileKeyValuePair
104
gchar *key; /* NULL for comments */
108
static gint find_file_in_data_dirs (const gchar *file,
112
static gboolean g_key_file_load_from_fd (GKeyFile *key_file,
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);
121
static GList *g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
122
GKeyFileGroup *group,
124
static GKeyFileKeyValuePair *g_key_file_lookup_key_value_pair (GKeyFile *key_file,
125
GKeyFileGroup *group,
128
static void g_key_file_remove_group_node (GKeyFile *key_file,
130
static void g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
131
GKeyFileGroup *group,
134
static void g_key_file_add_key (GKeyFile *key_file,
135
GKeyFileGroup *group,
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,
150
static gchar *g_key_file_parse_string_as_value (GKeyFile *key_file,
152
gboolean escape_separator);
153
static gint g_key_file_parse_value_as_integer (GKeyFile *key_file,
156
static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
158
static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
161
static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
164
static gchar *g_key_file_parse_boolean_as_value (GKeyFile *key_file,
166
static gchar *g_key_file_parse_value_as_comment (GKeyFile *key_file,
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,
174
static void g_key_file_parse_comment (GKeyFile *key_file,
178
static void g_key_file_parse_group (GKeyFile *key_file,
182
static gchar *key_get_locale (const gchar *key);
183
static void g_key_file_parse_data (GKeyFile *key_file,
187
static void g_key_file_flush_parse_buffer (GKeyFile *key_file,
192
g_key_file_error_quark (void)
194
return g_quark_from_static_string ("g-key-file-error-quark");
198
g_key_file_init (GKeyFile *key_file)
200
key_file->current_group = g_new0 (GKeyFileGroup, 1);
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 = ';';
210
g_key_file_clear (GKeyFile *key_file)
212
GList *tmp, *group_node;
214
if (key_file->parse_buffer)
215
g_string_free (key_file->parse_buffer, TRUE);
217
tmp = key_file->groups;
222
g_key_file_remove_group_node (key_file, group_node);
225
g_assert (key_file->groups == NULL);
232
* Creates a new empty #GKeyFile object. Use g_key_file_load_from_file(),
233
* g_key_file_load_from_data() or g_key_file_load_from_data_dirs() to
234
* read an existing key file.
236
* Return value: an empty #GKeyFile.
241
g_key_file_new (void)
245
key_file = g_new0 (GKeyFile, 1);
246
g_key_file_init (key_file);
252
* g_key_file_set_list_separator:
253
* @key_file: a #GKeyFile
254
* @separator: the separator
256
* Sets the character which is used to separate
257
* values in lists. Typically ';' or ',' are used
258
* as separators. The default list separator is ';'.
263
g_key_file_set_list_separator (GKeyFile *key_file,
266
g_return_if_fail (key_file != NULL);
268
key_file->list_separator = separator;
272
/* Iterates through all the directories in *dirs trying to
273
* open file. When it successfully locates and opens a file it
274
* returns the file descriptor to the open file. It also
275
* outputs the absolute path of the file in output_file and
276
* leaves the unchecked directories in *dirs.
279
find_file_in_data_dirs (const gchar *file,
284
gchar **data_dirs, *data_dir, *path;
295
while (data_dirs && (data_dir = *data_dirs) && fd < 0)
297
gchar *candidate_file, *sub_dir;
299
candidate_file = (gchar *) file;
300
sub_dir = g_strdup ("");
301
while (candidate_file != NULL && fd < 0)
305
path = g_build_filename (data_dir, sub_dir,
306
candidate_file, NULL);
308
fd = g_open (path, O_RDONLY, 0);
316
candidate_file = strchr (candidate_file, '-');
318
if (candidate_file == NULL)
324
sub_dir = g_strndup (file, candidate_file - file - 1);
326
for (p = sub_dir; *p != '\0'; p++)
329
*p = G_DIR_SEPARATOR;
340
g_set_error (error, G_KEY_FILE_ERROR,
341
G_KEY_FILE_ERROR_NOT_FOUND,
342
_("Valid key file could not be "
343
"found in data dirs"));
346
if (output_file != NULL && fd > 0)
347
*output_file = g_strdup (path);
355
g_key_file_load_from_fd (GKeyFile *key_file,
360
GError *key_file_error = NULL;
362
struct stat stat_buf;
363
gchar read_buf[4096];
365
if (fstat (fd, &stat_buf) < 0)
367
g_set_error (error, G_FILE_ERROR,
368
g_file_error_from_errno (errno),
369
"%s", g_strerror (errno));
373
if (!S_ISREG (stat_buf.st_mode))
375
g_set_error (error, G_KEY_FILE_ERROR,
376
G_KEY_FILE_ERROR_PARSE,
377
_("Not a regular file"));
381
if (stat_buf.st_size == 0)
383
g_set_error (error, G_KEY_FILE_ERROR,
384
G_KEY_FILE_ERROR_PARSE,
389
if (key_file->approximate_size > 0)
391
g_key_file_clear (key_file);
392
g_key_file_init (key_file);
394
key_file->flags = flags;
399
bytes_read = read (fd, read_buf, 4096);
401
if (bytes_read == 0) /* End of File */
406
if (errno == EINTR || errno == EAGAIN)
409
g_set_error (error, G_FILE_ERROR,
410
g_file_error_from_errno (errno),
411
"%s", g_strerror (errno));
415
g_key_file_parse_data (key_file,
416
read_buf, bytes_read,
419
while (!key_file_error);
423
g_propagate_error (error, key_file_error);
427
g_key_file_flush_parse_buffer (key_file, &key_file_error);
431
g_propagate_error (error, key_file_error);
439
* g_key_file_load_from_file:
440
* @key_file: an empty #GKeyFile struct
441
* @file: the path of a filename to load, in the GLib file name encoding
442
* @flags: flags from #GKeyFileFlags
443
* @error: return location for a #GError, or %NULL
445
* Loads a key file into an empty #GKeyFile structure.
446
* If the file could not be loaded then %error is set to
447
* either a #GFileError or #GKeyFileError.
449
* Return value: %TRUE if a key file could be loaded, %FALSE othewise
453
g_key_file_load_from_file (GKeyFile *key_file,
458
GError *key_file_error = NULL;
461
g_return_val_if_fail (key_file != NULL, FALSE);
462
g_return_val_if_fail (file != NULL, FALSE);
464
fd = g_open (file, O_RDONLY, 0);
468
g_set_error (error, G_FILE_ERROR,
469
g_file_error_from_errno (errno),
470
"%s", g_strerror (errno));
474
g_key_file_load_from_fd (key_file, fd, flags, &key_file_error);
479
g_propagate_error (error, key_file_error);
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
494
* Loads a key file from memory into an empty #GKeyFile structure. If
495
* the object cannot be created then %error is set to a
498
* Return value: %TRUE if a key file could be loaded, %FALSE othewise
502
g_key_file_load_from_data (GKeyFile *key_file,
508
GError *key_file_error = NULL;
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);
514
if (length == (gsize)-1)
515
length = strlen (data);
517
if (key_file->approximate_size > 0)
519
g_key_file_clear (key_file);
520
g_key_file_init (key_file);
522
key_file->flags = flags;
524
g_key_file_parse_data (key_file, data, length, &key_file_error);
528
g_propagate_error (error, key_file_error);
532
g_key_file_flush_parse_buffer (key_file, &key_file_error);
536
g_propagate_error (error, key_file_error);
544
* g_key_file_load_from_data_dirs:
545
* @key_file: an empty #GKeyFile struct
546
* @file: a relative path to a filename to open and parse
547
* @full_path: return location for a string containing the full path
548
* of the file, or %NULL
549
* @flags: flags from #GKeyFileFlags
550
* @error: return location for a #GError, or %NULL
552
* This function looks for a key file named @file in the paths
553
* returned from g_get_user_data_dir() and g_get_system_data_dirs(),
554
* loads the file into @key_file and returns the file's full path in
555
* @full_path. If the file could not be loaded then an %error is
556
* set to either a #GFileError or #GKeyFileError.
558
* Return value: %TRUE if a key file could be loaded, %FALSE othewise
562
g_key_file_load_from_data_dirs (GKeyFile *key_file,
568
GError *key_file_error = NULL;
569
gchar **all_data_dirs, **data_dirs;
570
const gchar * user_data_dir;
571
const gchar * const * system_data_dirs;
577
g_return_val_if_fail (key_file != NULL, FALSE);
578
g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
580
user_data_dir = g_get_user_data_dir ();
581
system_data_dirs = g_get_system_data_dirs ();
582
all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
585
all_data_dirs[i++] = g_strdup (user_data_dir);
588
while (system_data_dirs[j] != NULL)
589
all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
592
data_dirs = all_data_dirs;
594
while (*data_dirs != NULL && !found_file)
596
g_free (output_path);
598
fd = find_file_in_data_dirs (file, &output_path, &data_dirs,
604
g_propagate_error (error, key_file_error);
608
found_file = g_key_file_load_from_fd (key_file, fd, flags,
614
g_propagate_error (error, key_file_error);
619
if (found_file && full_path)
620
*full_path = output_path;
622
g_free (output_path);
624
g_strfreev (all_data_dirs);
631
* @key_file: a #GKeyFile
638
g_key_file_free (GKeyFile *key_file)
640
g_return_if_fail (key_file != NULL);
642
g_key_file_clear (key_file);
646
/* If G_KEY_FILE_KEEP_TRANSLATIONS is not set, only returns
647
* true for locales that match those in g_get_language_names().
650
g_key_file_locale_is_interesting (GKeyFile *key_file,
653
const gchar * const * current_locales;
656
if (key_file->flags & G_KEY_FILE_KEEP_TRANSLATIONS)
659
current_locales = g_get_language_names ();
661
for (i = 0; current_locales[i] != NULL; i++)
663
if (g_ascii_strcasecmp (current_locales[i], locale) == 0)
671
g_key_file_parse_line (GKeyFile *key_file,
676
GError *parse_error = NULL;
679
g_return_if_fail (key_file != NULL);
680
g_return_if_fail (line != NULL);
682
line_start = (gchar *) line;
683
while (g_ascii_isspace (*line_start))
686
if (g_key_file_line_is_comment (line_start))
687
g_key_file_parse_comment (key_file, line, length, &parse_error);
688
else if (g_key_file_line_is_group (line_start))
689
g_key_file_parse_group (key_file, line_start,
690
length - (line_start - line),
692
else if (g_key_file_line_is_key_value_pair (line_start))
693
g_key_file_parse_key_value_pair (key_file, line_start,
694
length - (line_start - line),
698
gchar *line_utf8 = _g_utf8_make_valid (line);
699
g_set_error (error, G_KEY_FILE_ERROR,
700
G_KEY_FILE_ERROR_PARSE,
701
_("Key file contains line '%s' which is not "
702
"a key-value pair, group, or comment"),
710
g_propagate_error (error, parse_error);
714
g_key_file_parse_comment (GKeyFile *key_file,
719
GKeyFileKeyValuePair *pair;
721
if (!(key_file->flags & G_KEY_FILE_KEEP_COMMENTS))
724
g_assert (key_file->current_group != NULL);
726
pair = g_new0 (GKeyFileKeyValuePair, 1);
729
pair->value = g_strndup (line, length);
731
key_file->current_group->key_value_pairs =
732
g_list_prepend (key_file->current_group->key_value_pairs, pair);
734
if (length == 0 || line[0] != '#')
735
key_file->current_group->has_trailing_blank_line = TRUE;
739
g_key_file_parse_group (GKeyFile *key_file,
745
const gchar *group_name_start, *group_name_end;
747
/* advance past opening '['
749
group_name_start = line + 1;
750
group_name_end = line + length - 1;
752
while (*group_name_end != ']')
755
group_name = g_strndup (group_name_start,
756
group_name_end - group_name_start);
758
if (!g_key_file_is_group_name (group_name))
760
g_set_error (error, G_KEY_FILE_ERROR,
761
G_KEY_FILE_ERROR_PARSE,
762
_("Invalid group name: %s"), group_name);
767
g_key_file_add_group (key_file, group_name);
772
g_key_file_parse_key_value_pair (GKeyFile *key_file,
777
gchar *key, *value, *key_end, *value_start, *locale;
778
gsize key_len, value_len;
780
if (key_file->current_group == NULL || key_file->current_group->name == NULL)
782
g_set_error (error, G_KEY_FILE_ERROR,
783
G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
784
_("Key file does not start with a group"));
788
key_end = value_start = strchr (line, '=');
790
g_assert (key_end != NULL);
795
/* Pull the key name from the line (chomping trailing whitespace)
797
while (g_ascii_isspace (*key_end))
800
key_len = key_end - line + 2;
802
g_assert (key_len <= length);
804
key = g_strndup (line, key_len - 1);
806
if (!g_key_file_is_key_name (key))
808
g_set_error (error, G_KEY_FILE_ERROR,
809
G_KEY_FILE_ERROR_PARSE,
810
_("Invalid key name: %s"), key);
815
/* Pull the value from the line (chugging leading whitespace)
817
while (g_ascii_isspace (*value_start))
820
value_len = line + length - value_start + 1;
822
value = g_strndup (value_start, value_len);
824
g_assert (key_file->start_group != NULL);
826
if (key_file->current_group
827
&& key_file->current_group->name
828
&& strcmp (key_file->start_group->name,
829
key_file->current_group->name) == 0
830
&& strcmp (key, "Encoding") == 0)
832
if (g_ascii_strcasecmp (value, "UTF-8") != 0)
834
gchar *value_utf8 = _g_utf8_make_valid (value);
835
g_set_error (error, G_KEY_FILE_ERROR,
836
G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
837
_("Key file contains unsupported "
838
"encoding '%s'"), value_utf8);
847
/* Is this key a translation? If so, is it one that we care about?
849
locale = key_get_locale (key);
851
if (locale == NULL || g_key_file_locale_is_interesting (key_file, locale))
852
g_key_file_add_key (key_file, key_file->current_group, key, value);
860
key_get_locale (const gchar *key)
864
locale = g_strrstr (key, "[");
866
if (locale && strlen (locale) <= 2)
870
locale = g_strndup (locale + 1, strlen (locale) - 2);
876
g_key_file_parse_data (GKeyFile *key_file,
884
g_return_if_fail (key_file != NULL);
885
g_return_if_fail (data != NULL);
889
for (i = 0; i < length; i++)
893
if (i > 0 && data[i - 1] == '\r')
894
g_string_erase (key_file->parse_buffer,
895
key_file->parse_buffer->len - 1,
898
/* When a newline is encountered flush the parse buffer so that the
899
* line can be parsed. Note that completely blank lines won't show
900
* up in the parse buffer, so they get parsed directly.
902
if (key_file->parse_buffer->len > 0)
903
g_key_file_flush_parse_buffer (key_file, &parse_error);
905
g_key_file_parse_comment (key_file, "", 1, &parse_error);
909
g_propagate_error (error, parse_error);
914
g_string_append_c (key_file->parse_buffer, data[i]);
917
key_file->approximate_size += length;
921
g_key_file_flush_parse_buffer (GKeyFile *key_file,
924
GError *file_error = NULL;
926
g_return_if_fail (key_file != NULL);
930
if (key_file->parse_buffer->len > 0)
932
g_key_file_parse_line (key_file, key_file->parse_buffer->str,
933
key_file->parse_buffer->len,
935
g_string_erase (key_file->parse_buffer, 0, -1);
939
g_propagate_error (error, file_error);
946
* g_key_file_to_data:
947
* @key_file: a #GKeyFile
948
* @length: return location for the length of the
949
* returned string, or %NULL
950
* @error: return location for a #GError, or %NULL
952
* This function outputs @key_file as a string.
954
* Return value: a newly allocated string holding
955
* the contents of the #GKeyFile
960
g_key_file_to_data (GKeyFile *key_file,
964
GString *data_string;
965
GList *group_node, *key_file_node;
966
gboolean has_blank_line = TRUE;
968
g_return_val_if_fail (key_file != NULL, NULL);
970
data_string = g_string_sized_new (2 * key_file->approximate_size);
972
for (group_node = g_list_last (key_file->groups);
974
group_node = group_node->prev)
976
GKeyFileGroup *group;
978
group = (GKeyFileGroup *) group_node->data;
980
/* separate groups by at least an empty line */
982
g_string_append_c (data_string, '\n');
983
has_blank_line = group->has_trailing_blank_line;
985
if (group->comment != NULL)
986
g_string_append_printf (data_string, "%s\n", group->comment->value);
988
if (group->name != NULL)
989
g_string_append_printf (data_string, "[%s]\n", group->name);
991
for (key_file_node = g_list_last (group->key_value_pairs);
992
key_file_node != NULL;
993
key_file_node = key_file_node->prev)
995
GKeyFileKeyValuePair *pair;
997
pair = (GKeyFileKeyValuePair *) key_file_node->data;
999
if (pair->key != NULL)
1000
g_string_append_printf (data_string, "%s=%s\n", pair->key, pair->value);
1002
g_string_append_printf (data_string, "%s\n", pair->value);
1007
*length = data_string->len;
1009
return g_string_free (data_string, FALSE);
1013
* g_key_file_get_keys:
1014
* @key_file: a #GKeyFile
1015
* @group_name: a group name
1016
* @length: return location for the number of keys returned, or %NULL
1017
* @error: return location for a #GError, or %NULL
1019
* Returns all keys for the group name @group_name. The array of
1020
* returned keys will be %NULL-terminated, so @length may
1021
* optionally be %NULL. In the event that the @group_name cannot
1022
* be found, %NULL is returned and @error is set to
1023
* #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1025
* Return value: a newly-allocated %NULL-terminated array of
1026
* strings. Use g_strfreev() to free it.
1031
g_key_file_get_keys (GKeyFile *key_file,
1032
const gchar *group_name,
1036
GKeyFileGroup *group;
1041
g_return_val_if_fail (key_file != NULL, NULL);
1042
g_return_val_if_fail (group_name != NULL, NULL);
1044
group = g_key_file_lookup_group (key_file, group_name);
1048
g_set_error (error, G_KEY_FILE_ERROR,
1049
G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1050
_("Key file does not have group '%s'"),
1051
group_name ? group_name : "(null)");
1056
for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1058
GKeyFileKeyValuePair *pair;
1060
pair = (GKeyFileKeyValuePair *) tmp->data;
1066
keys = g_new0 (gchar *, num_keys + 1);
1069
for (tmp = group->key_value_pairs; tmp; tmp = tmp->next)
1071
GKeyFileKeyValuePair *pair;
1073
pair = (GKeyFileKeyValuePair *) tmp->data;
1077
keys[i] = g_strdup (pair->key);
1082
keys[num_keys] = NULL;
1091
* g_key_file_get_start_group:
1092
* @key_file: a #GKeyFile
1094
* Returns the name of the start group of the file.
1096
* Return value: The start group of the key file.
1101
g_key_file_get_start_group (GKeyFile *key_file)
1103
g_return_val_if_fail (key_file != NULL, NULL);
1105
if (key_file->start_group)
1106
return g_strdup (key_file->start_group->name);
1112
* g_key_file_get_groups:
1113
* @key_file: a #GKeyFile
1114
* @length: return location for the number of returned groups, or %NULL
1116
* Returns all groups in the key file loaded with @key_file. The
1117
* array of returned groups will be %NULL-terminated, so @length may
1118
* optionally be %NULL.
1120
* Return value: a newly-allocated %NULL-terminated array of strings.
1121
* Use g_strfreev() to free it.
1125
g_key_file_get_groups (GKeyFile *key_file,
1130
gsize i, num_groups;
1132
g_return_val_if_fail (key_file != NULL, NULL);
1134
num_groups = g_list_length (key_file->groups);
1136
g_assert (num_groups > 0);
1138
/* Only need num_groups instead of num_groups + 1
1139
* because the first group of the file (last in the
1140
* list) is always the comment group at the top,
1143
groups = g_new0 (gchar *, num_groups);
1145
group_node = g_list_last (key_file->groups);
1147
g_assert (((GKeyFileGroup *) group_node->data)->name == NULL);
1150
for (group_node = group_node->prev;
1152
group_node = group_node->prev)
1154
GKeyFileGroup *group;
1156
group = (GKeyFileGroup *) group_node->data;
1158
g_assert (group->name != NULL);
1160
groups[i++] = g_strdup (group->name);
1171
* g_key_file_get_value:
1172
* @key_file: a #GKeyFile
1173
* @group_name: a group name
1175
* @error: return location for a #GError, or %NULL
1177
* Returns the value associated with @key under @group_name.
1179
* In the event the key cannot be found, %NULL is returned and
1180
* @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1181
* event that the @group_name cannot be found, %NULL is returned
1182
* and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1184
* Return value: a newly allocated string or %NULL if the specified
1185
* key cannot be found.
1190
g_key_file_get_value (GKeyFile *key_file,
1191
const gchar *group_name,
1195
GKeyFileGroup *group;
1196
GKeyFileKeyValuePair *pair;
1197
gchar *value = NULL;
1199
g_return_val_if_fail (key_file != NULL, NULL);
1200
g_return_val_if_fail (group_name != NULL, NULL);
1201
g_return_val_if_fail (key != NULL, NULL);
1203
group = g_key_file_lookup_group (key_file, group_name);
1207
g_set_error (error, G_KEY_FILE_ERROR,
1208
G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
1209
_("Key file does not have group '%s'"),
1210
group_name ? group_name : "(null)");
1214
pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1217
value = g_strdup (pair->value);
1219
g_set_error (error, G_KEY_FILE_ERROR,
1220
G_KEY_FILE_ERROR_KEY_NOT_FOUND,
1221
_("Key file does not have key '%s'"), key);
1227
* g_key_file_set_value:
1228
* @key_file: a #GKeyFile
1229
* @group_name: a group name
1233
* Associates a new value with @key under @group_name. If @key
1234
* cannot be found then it is created. If @group_name cannot be
1235
* found then it is created.
1240
g_key_file_set_value (GKeyFile *key_file,
1241
const gchar *group_name,
1245
GKeyFileGroup *group;
1246
GKeyFileKeyValuePair *pair;
1248
g_return_if_fail (key_file != NULL);
1249
g_return_if_fail (g_key_file_is_group_name (group_name));
1250
g_return_if_fail (g_key_file_is_key_name (key));
1251
g_return_if_fail (value != NULL);
1253
group = g_key_file_lookup_group (key_file, group_name);
1257
g_key_file_add_group (key_file, group_name);
1258
group = (GKeyFileGroup *) key_file->groups->data;
1260
g_key_file_add_key (key_file, group, key, value);
1264
pair = g_key_file_lookup_key_value_pair (key_file, group, key);
1267
g_key_file_add_key (key_file, group, key, value);
1270
g_free (pair->value);
1271
pair->value = g_strdup (value);
1277
* g_key_file_get_string:
1278
* @key_file: a #GKeyFile
1279
* @group_name: a group name
1281
* @error: return location for a #GError, or %NULL
1283
* Returns the value associated with @key under @group_name.
1285
* In the event the key cannot be found, %NULL is returned and
1286
* @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1287
* event that the @group_name cannot be found, %NULL is returned
1288
* and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1290
* Return value: a newly allocated string or %NULL if the specified
1291
* key cannot be found.
1296
g_key_file_get_string (GKeyFile *key_file,
1297
const gchar *group_name,
1301
gchar *value, *string_value;
1302
GError *key_file_error;
1304
g_return_val_if_fail (key_file != NULL, NULL);
1305
g_return_val_if_fail (group_name != NULL, NULL);
1306
g_return_val_if_fail (key != NULL, NULL);
1308
key_file_error = NULL;
1310
value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1314
g_propagate_error (error, key_file_error);
1318
if (!g_utf8_validate (value, -1, NULL))
1320
gchar *value_utf8 = _g_utf8_make_valid (value);
1321
g_set_error (error, G_KEY_FILE_ERROR,
1322
G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1323
_("Key file contains key '%s' with value '%s' "
1324
"which is not UTF-8"), key, value_utf8);
1325
g_free (value_utf8);
1331
string_value = g_key_file_parse_value_as_string (key_file, value, NULL,
1337
if (g_error_matches (key_file_error,
1339
G_KEY_FILE_ERROR_INVALID_VALUE))
1341
g_set_error (error, G_KEY_FILE_ERROR,
1342
G_KEY_FILE_ERROR_INVALID_VALUE,
1343
_("Key file contains key '%s' "
1344
"which has value that cannot be interpreted."),
1346
g_error_free (key_file_error);
1349
g_propagate_error (error, key_file_error);
1352
return string_value;
1356
* g_key_file_set_string:
1357
* @key_file: a #GKeyFile
1358
* @group_name: a group name
1362
* Associates a new string value with @key under @group_name. If
1363
* @key cannot be found then it is created. If @group_name
1364
* cannot be found then it is created.
1369
g_key_file_set_string (GKeyFile *key_file,
1370
const gchar *group_name,
1372
const gchar *string)
1376
g_return_if_fail (key_file != NULL);
1377
g_return_if_fail (string != NULL);
1379
value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1380
g_key_file_set_value (key_file, group_name, key, value);
1385
* g_key_file_get_string_list:
1386
* @key_file: a #GKeyFile
1387
* @group_name: a group name
1389
* @length: return location for the number of returned strings, or %NULL
1390
* @error: return location for a #GError, or %NULL
1392
* Returns the values associated with @key under @group_name.
1394
* In the event the key cannot be found, %NULL is returned and
1395
* @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the
1396
* event that the @group_name cannot be found, %NULL is returned
1397
* and @error is set to #G_KEY_FILE_ERROR_GROUP_NOT_FOUND.
1399
* Return value: a %NULL-terminated string array or %NULL if the specified
1400
* key cannot be found. The array should be freed with g_strfreev().
1405
g_key_file_get_string_list (GKeyFile *key_file,
1406
const gchar *group_name,
1411
GError *key_file_error = NULL;
1412
gchar *value, *string_value, **values;
1414
GSList *p, *pieces = NULL;
1416
g_return_val_if_fail (key_file != NULL, NULL);
1417
g_return_val_if_fail (group_name != NULL, NULL);
1418
g_return_val_if_fail (key != NULL, NULL);
1420
value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1424
g_propagate_error (error, key_file_error);
1428
if (!g_utf8_validate (value, -1, NULL))
1430
gchar *value_utf8 = _g_utf8_make_valid (value);
1431
g_set_error (error, G_KEY_FILE_ERROR,
1432
G_KEY_FILE_ERROR_UNKNOWN_ENCODING,
1433
_("Key file contains key '%s' with value '%s' "
1434
"which is not UTF-8"), key, value_utf8);
1435
g_free (value_utf8);
1441
string_value = g_key_file_parse_value_as_string (key_file, value, &pieces, &key_file_error);
1443
g_free (string_value);
1447
if (g_error_matches (key_file_error,
1449
G_KEY_FILE_ERROR_INVALID_VALUE))
1451
g_set_error (error, G_KEY_FILE_ERROR,
1452
G_KEY_FILE_ERROR_INVALID_VALUE,
1453
_("Key file contains key '%s' "
1454
"which has value that cannot be interpreted."),
1456
g_error_free (key_file_error);
1459
g_propagate_error (error, key_file_error);
1462
len = g_slist_length (pieces);
1463
values = g_new0 (gchar *, len + 1);
1464
for (p = pieces, i = 0; p; p = p->next)
1465
values[i++] = p->data;
1468
g_slist_free (pieces);
1477
* g_key_file_set_string_list:
1478
* @key_file: a #GKeyFile
1479
* @group_name: a group name
1481
* @list: an array of locale string values
1482
* @length: number of locale string values in @list
1484
* Associates a list of string values for @key under @group_name.
1485
* If @key cannot be found then it is created. If @group_name
1486
* cannot be found then it is created.
1491
g_key_file_set_string_list (GKeyFile *key_file,
1492
const gchar *group_name,
1494
const gchar * const list[],
1497
GString *value_list;
1500
g_return_if_fail (key_file != NULL);
1501
g_return_if_fail (list != NULL);
1503
value_list = g_string_sized_new (length * 128);
1504
for (i = 0; i < length && list[i] != NULL; i++)
1508
value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1509
g_string_append (value_list, value);
1510
g_string_append_c (value_list, key_file->list_separator);
1515
g_key_file_set_value (key_file, group_name, key, value_list->str);
1516
g_string_free (value_list, TRUE);
1520
* g_key_file_set_locale_string:
1521
* @key_file: a #GKeyFile
1522
* @group_name: a group name
1527
* Associates a string value for @key and @locale under
1528
* @group_name. If the translation for @key cannot be found
1529
* then it is created.
1534
g_key_file_set_locale_string (GKeyFile *key_file,
1535
const gchar *group_name,
1537
const gchar *locale,
1538
const gchar *string)
1540
gchar *full_key, *value;
1542
g_return_if_fail (key_file != NULL);
1543
g_return_if_fail (key != NULL);
1544
g_return_if_fail (locale != NULL);
1545
g_return_if_fail (string != NULL);
1547
value = g_key_file_parse_string_as_value (key_file, string, FALSE);
1548
full_key = g_strdup_printf ("%s[%s]", key, locale);
1549
g_key_file_set_value (key_file, group_name, full_key, value);
1554
extern GSList *_g_compute_locale_variants (const gchar *locale);
1557
* g_key_file_get_locale_string:
1558
* @key_file: a #GKeyFile
1559
* @group_name: a group name
1561
* @locale: a locale or %NULL
1562
* @error: return location for a #GError, or %NULL
1564
* Returns the value associated with @key under @group_name
1565
* translated in the given @locale if available. If @locale is
1566
* %NULL then the current locale is assumed.
1568
* If @key cannot be found then %NULL is returned and @error is set to
1569
* #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value associated
1570
* with @key cannot be interpreted or no suitable translation can
1571
* be found then the untranslated value is returned.
1573
* Return value: a newly allocated string or %NULL if the specified
1574
* key cannot be found.
1579
g_key_file_get_locale_string (GKeyFile *key_file,
1580
const gchar *group_name,
1582
const gchar *locale,
1585
gchar *candidate_key, *translated_value;
1586
GError *key_file_error;
1588
gboolean free_languages = FALSE;
1591
g_return_val_if_fail (key_file != NULL, NULL);
1592
g_return_val_if_fail (group_name != NULL, NULL);
1593
g_return_val_if_fail (key != NULL, NULL);
1595
candidate_key = NULL;
1596
translated_value = NULL;
1597
key_file_error = NULL;
1603
list = _g_compute_locale_variants (locale);
1605
languages = g_new0 (gchar *, g_slist_length (list) + 1);
1606
for (l = list, i = 0; l; l = l->next, i++)
1607
languages[i] = l->data;
1608
languages[i] = NULL;
1610
g_slist_free (list);
1611
free_languages = TRUE;
1615
languages = (gchar **) g_get_language_names ();
1616
free_languages = FALSE;
1619
for (i = 0; languages[i]; i++)
1621
candidate_key = g_strdup_printf ("%s[%s]", key, languages[i]);
1623
translated_value = g_key_file_get_string (key_file,
1625
candidate_key, NULL);
1626
g_free (candidate_key);
1628
if (translated_value)
1631
g_free (translated_value);
1632
translated_value = NULL;
1635
/* Fallback to untranslated key
1637
if (!translated_value)
1639
translated_value = g_key_file_get_string (key_file, group_name, key,
1642
if (!translated_value)
1643
g_propagate_error (error, key_file_error);
1647
g_strfreev (languages);
1649
return translated_value;
1653
* g_key_file_get_locale_string_list:
1654
* @key_file: a #GKeyFile
1655
* @group_name: a group name
1658
* @length: return location for the number of returned strings or %NULL
1659
* @error: return location for a #GError or %NULL
1661
* Returns the values associated with @key under @group_name
1662
* translated in the given @locale if available. If @locale is
1663
* %NULL then the current locale is assumed.
1665
* If @key cannot be found then %NULL is returned and @error is set to
1666
* #G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated
1667
* with @key cannot be interpreted or no suitable translations
1668
* can be found then the untranslated values are returned.
1669
* The returned array is %NULL-terminated, so @length may optionally be %NULL.
1671
* Return value: a newly allocated %NULL-terminated string array
1672
* or %NULL if the key isn't found. The string array should be freed
1673
* with g_strfreev().
1678
g_key_file_get_locale_string_list (GKeyFile *key_file,
1679
const gchar *group_name,
1681
const gchar *locale,
1685
GError *key_file_error;
1686
gchar **values, *value;
1688
g_return_val_if_fail (key_file != NULL, NULL);
1689
g_return_val_if_fail (group_name != NULL, NULL);
1690
g_return_val_if_fail (key != NULL, NULL);
1692
key_file_error = NULL;
1694
value = g_key_file_get_locale_string (key_file, group_name,
1699
g_propagate_error (error, key_file_error);
1704
if (value[strlen (value) - 1] == ';')
1705
value[strlen (value) - 1] = '\0';
1707
values = g_strsplit (value, ";", 0);
1712
*length = g_strv_length (values);
1718
* g_key_file_set_locale_string_list:
1719
* @key_file: a #GKeyFile
1720
* @group_name: a group name
1723
* @list: a %NULL-terminated array of locale string values
1724
* @length: the length of @list
1726
* Associates a list of string values for @key and @locale under
1727
* @group_name. If the translation for @key cannot be found then
1733
g_key_file_set_locale_string_list (GKeyFile *key_file,
1734
const gchar *group_name,
1736
const gchar *locale,
1737
const gchar * const list[],
1740
GString *value_list;
1744
g_return_if_fail (key_file != NULL);
1745
g_return_if_fail (key != NULL);
1746
g_return_if_fail (locale != NULL);
1747
g_return_if_fail (length != 0);
1749
value_list = g_string_sized_new (length * 128);
1750
for (i = 0; i < length && list[i] != NULL; i++)
1754
value = g_key_file_parse_string_as_value (key_file, list[i], TRUE);
1756
g_string_append (value_list, value);
1757
g_string_append_c (value_list, ';');
1762
full_key = g_strdup_printf ("%s[%s]", key, locale);
1763
g_key_file_set_value (key_file, group_name, full_key, value_list->str);
1765
g_string_free (value_list, TRUE);
1769
* g_key_file_get_boolean:
1770
* @key_file: a #GKeyFile
1771
* @group_name: a group name
1773
* @error: return location for a #GError
1775
* Returns the value associated with @key under @group_name as a
1778
* If @key cannot be found then the return value is undefined and
1779
* @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1780
* the value associated with @key cannot be interpreted as a boolean
1781
* then the return value is also undefined and @error is set to
1782
* #G_KEY_FILE_ERROR_INVALID_VALUE.
1784
* Return value: the value associated with the key as a boolean
1788
g_key_file_get_boolean (GKeyFile *key_file,
1789
const gchar *group_name,
1793
GError *key_file_error = NULL;
1795
gboolean bool_value;
1797
g_return_val_if_fail (key_file != NULL, FALSE);
1798
g_return_val_if_fail (group_name != NULL, FALSE);
1799
g_return_val_if_fail (key != NULL, FALSE);
1801
value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
1805
g_propagate_error (error, key_file_error);
1809
bool_value = g_key_file_parse_value_as_boolean (key_file, value,
1815
if (g_error_matches (key_file_error,
1817
G_KEY_FILE_ERROR_INVALID_VALUE))
1819
g_set_error (error, G_KEY_FILE_ERROR,
1820
G_KEY_FILE_ERROR_INVALID_VALUE,
1821
_("Key file contains key '%s' "
1822
"which has value that cannot be interpreted."),
1824
g_error_free (key_file_error);
1827
g_propagate_error (error, key_file_error);
1834
* g_key_file_set_boolean:
1835
* @key_file: a #GKeyFile
1836
* @group_name: a group name
1838
* @value: %TRUE or %FALSE
1840
* Associates a new boolean value with @key under @group_name.
1841
* If @key cannot be found then it is created.
1846
g_key_file_set_boolean (GKeyFile *key_file,
1847
const gchar *group_name,
1853
g_return_if_fail (key_file != NULL);
1855
result = g_key_file_parse_boolean_as_value (key_file, value);
1856
g_key_file_set_value (key_file, group_name, key, result);
1861
* g_key_file_get_boolean_list:
1862
* @key_file: a #GKeyFile
1863
* @group_name: a group name
1865
* @length: the number of booleans returned
1866
* @error: return location for a #GError
1868
* Returns the values associated with @key under @group_name as
1869
* booleans. If @group_name is %NULL, the start_group is used.
1871
* If @key cannot be found then the return value is undefined and
1872
* @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1873
* the values associated with @key cannot be interpreted as booleans
1874
* then the return value is also undefined and @error is set to
1875
* #G_KEY_FILE_ERROR_INVALID_VALUE.
1877
* Return value: the values associated with the key as a boolean
1882
g_key_file_get_boolean_list (GKeyFile *key_file,
1883
const gchar *group_name,
1888
GError *key_file_error;
1890
gboolean *bool_values;
1893
g_return_val_if_fail (key_file != NULL, NULL);
1894
g_return_val_if_fail (group_name != NULL, NULL);
1895
g_return_val_if_fail (key != NULL, NULL);
1897
key_file_error = NULL;
1899
values = g_key_file_get_string_list (key_file, group_name, key,
1900
&num_bools, &key_file_error);
1903
g_propagate_error (error, key_file_error);
1908
bool_values = g_new0 (gboolean, num_bools);
1910
for (i = 0; i < num_bools; i++)
1912
bool_values[i] = g_key_file_parse_value_as_boolean (key_file,
1918
g_propagate_error (error, key_file_error);
1919
g_strfreev (values);
1920
g_free (bool_values);
1925
g_strfreev (values);
1928
*length = num_bools;
1934
* g_key_file_set_boolean_list:
1935
* @key_file: a #GKeyFile
1936
* @group_name: a group name
1938
* @list: an array of boolean values
1939
* @length: length of @list
1941
* Associates a list of boolean values with @key under
1942
* @group_name. If @key cannot be found then it is created.
1943
* If @group_name is %NULL, the start_group is used.
1948
g_key_file_set_boolean_list (GKeyFile *key_file,
1949
const gchar *group_name,
1954
GString *value_list;
1957
g_return_if_fail (key_file != NULL);
1958
g_return_if_fail (list != NULL);
1960
value_list = g_string_sized_new (length * 8);
1961
for (i = 0; i < length; i++)
1965
value = g_key_file_parse_boolean_as_value (key_file, list[i]);
1967
g_string_append (value_list, value);
1968
g_string_append_c (value_list, key_file->list_separator);
1973
g_key_file_set_value (key_file, group_name, key, value_list->str);
1974
g_string_free (value_list, TRUE);
1978
* g_key_file_get_integer:
1979
* @key_file: a #GKeyFile
1980
* @group_name: a group name
1982
* @error: return location for a #GError
1984
* Returns the value associated with @key under @group_name as an
1985
* integer. If @group_name is %NULL, the start_group is used.
1987
* If @key cannot be found then the return value is undefined and
1988
* @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
1989
* the value associated with @key cannot be interpreted as an integer
1990
* then the return value is also undefined and @error is set to
1991
* #G_KEY_FILE_ERROR_INVALID_VALUE.
1993
* Return value: the value associated with the key as an integer.
1998
g_key_file_get_integer (GKeyFile *key_file,
1999
const gchar *group_name,
2003
GError *key_file_error;
2007
g_return_val_if_fail (key_file != NULL, -1);
2008
g_return_val_if_fail (group_name != NULL, -1);
2009
g_return_val_if_fail (key != NULL, -1);
2011
key_file_error = NULL;
2013
value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2017
g_propagate_error (error, key_file_error);
2021
int_value = g_key_file_parse_value_as_integer (key_file, value,
2027
if (g_error_matches (key_file_error,
2029
G_KEY_FILE_ERROR_INVALID_VALUE))
2031
g_set_error (error, G_KEY_FILE_ERROR,
2032
G_KEY_FILE_ERROR_INVALID_VALUE,
2033
_("Key file contains key '%s' in group '%s' "
2034
"which has value that cannot be interpreted."), key,
2036
g_error_free (key_file_error);
2039
g_propagate_error (error, key_file_error);
2046
* g_key_file_set_integer:
2047
* @key_file: a #GKeyFile
2048
* @group_name: a group name
2050
* @value: an integer value
2052
* Associates a new integer value with @key under @group_name.
2053
* If @key cannot be found then it is created.
2058
g_key_file_set_integer (GKeyFile *key_file,
2059
const gchar *group_name,
2065
g_return_if_fail (key_file != NULL);
2067
result = g_key_file_parse_integer_as_value (key_file, value);
2068
g_key_file_set_value (key_file, group_name, key, result);
2073
* g_key_file_get_integer_list:
2074
* @key_file: a #GKeyFile
2075
* @group_name: a group name
2077
* @length: the number of integers returned
2078
* @error: return location for a #GError
2080
* Returns the values associated with @key under @group_name as
2083
* If @key cannot be found then the return value is undefined and
2084
* @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2085
* the values associated with @key cannot be interpreted as integers
2086
* then the return value is also undefined and @error is set to
2087
* #G_KEY_FILE_ERROR_INVALID_VALUE.
2089
* Return value: the values associated with the key as a integer
2094
g_key_file_get_integer_list (GKeyFile *key_file,
2095
const gchar *group_name,
2100
GError *key_file_error = NULL;
2105
g_return_val_if_fail (key_file != NULL, NULL);
2106
g_return_val_if_fail (group_name != NULL, NULL);
2107
g_return_val_if_fail (key != NULL, NULL);
2109
values = g_key_file_get_string_list (key_file, group_name, key,
2110
&num_ints, &key_file_error);
2113
g_propagate_error (error, key_file_error);
2118
int_values = g_new0 (gint, num_ints);
2120
for (i = 0; i < num_ints; i++)
2122
int_values[i] = g_key_file_parse_value_as_integer (key_file,
2128
g_propagate_error (error, key_file_error);
2129
g_strfreev (values);
2130
g_free (int_values);
2135
g_strfreev (values);
2144
* g_key_file_set_integer_list:
2145
* @key_file: a #GKeyFile
2146
* @group_name: a group name
2148
* @list: an array of integer values
2149
* @length: number of integer values in @list
2151
* Associates a list of integer values with @key under
2152
* @group_name. If @key cannot be found then it is created.
2157
g_key_file_set_integer_list (GKeyFile *key_file,
2158
const gchar *group_name,
2166
g_return_if_fail (key_file != NULL);
2167
g_return_if_fail (list != NULL);
2169
values = g_string_sized_new (length * 16);
2170
for (i = 0; i < length; i++)
2174
value = g_key_file_parse_integer_as_value (key_file, list[i]);
2176
g_string_append (values, value);
2177
g_string_append_c (values, ';');
2182
g_key_file_set_value (key_file, group_name, key, values->str);
2183
g_string_free (values, TRUE);
2187
* g_key_file_get_double:
2188
* @key_file: a #GKeyFile
2189
* @group_name: a group name
2191
* @error: return location for a #GError
2193
* Returns the value associated with @key under @group_name as an
2194
* integer. If @group_name is %NULL, the start_group is used.
2196
* If @key cannot be found then the return value is undefined and
2197
* @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2198
* the value associated with @key cannot be interpreted as a double
2199
* then the return value is also undefined and @error is set to
2200
* #G_KEY_FILE_ERROR_INVALID_VALUE.
2202
* Return value: the value associated with the key as a double.
2207
g_key_file_get_double (GKeyFile *key_file,
2208
const gchar *group_name,
2212
GError *key_file_error;
2214
gdouble double_value;
2216
g_return_val_if_fail (key_file != NULL, -1);
2217
g_return_val_if_fail (group_name != NULL, -1);
2218
g_return_val_if_fail (key != NULL, -1);
2220
key_file_error = NULL;
2222
value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
2226
g_propagate_error (error, key_file_error);
2230
double_value = g_key_file_parse_value_as_double (key_file, value,
2236
if (g_error_matches (key_file_error,
2238
G_KEY_FILE_ERROR_INVALID_VALUE))
2240
g_set_error (error, G_KEY_FILE_ERROR,
2241
G_KEY_FILE_ERROR_INVALID_VALUE,
2242
_("Key file contains key '%s' in group '%s' "
2243
"which has value that cannot be interpreted."), key,
2245
g_error_free (key_file_error);
2248
g_propagate_error (error, key_file_error);
2251
return double_value;
2255
* g_key_file_set_double:
2256
* @key_file: a #GKeyFile
2257
* @group_name: a group name
2259
* @value: an double value
2261
* Associates a new double value with @key under @group_name.
2262
* If @key cannot be found then it is created. If @group_name
2263
* is %NULL, the start group is used.
2268
g_key_file_set_double (GKeyFile *key_file,
2269
const gchar *group_name,
2273
gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2275
g_return_if_fail (key_file != NULL);
2277
g_ascii_dtostr (result, sizeof (result), value);
2278
g_key_file_set_value (key_file, group_name, key, result);
2282
* g_key_file_get_double_list:
2283
* @key_file: a #GKeyFile
2284
* @group_name: a group name
2286
* @length: the number of doubles returned
2287
* @error: return location for a #GError
2289
* Returns the values associated with @key under @group_name as
2290
* doubles. If @group_name is %NULL, the start group is used.
2292
* If @key cannot be found then the return value is undefined and
2293
* @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
2294
* the values associated with @key cannot be interpreted as doubles
2295
* then the return value is also undefined and @error is set to
2296
* #G_KEY_FILE_ERROR_INVALID_VALUE.
2298
* Return value: the values associated with the key as a double
2303
g_key_file_get_double_list (GKeyFile *key_file,
2304
const gchar *group_name,
2309
GError *key_file_error = NULL;
2311
gdouble *double_values;
2312
gsize i, num_doubles;
2314
g_return_val_if_fail (key_file != NULL, NULL);
2315
g_return_val_if_fail (group_name != NULL, NULL);
2316
g_return_val_if_fail (key != NULL, NULL);
2318
values = g_key_file_get_string_list (key_file, group_name, key,
2319
&num_doubles, &key_file_error);
2322
g_propagate_error (error, key_file_error);
2327
double_values = g_new0 (gdouble, num_doubles);
2329
for (i = 0; i < num_doubles; i++)
2331
double_values[i] = g_key_file_parse_value_as_double (key_file,
2337
g_propagate_error (error, key_file_error);
2338
g_strfreev (values);
2339
g_free (double_values);
2344
g_strfreev (values);
2347
*length = num_doubles;
2349
return double_values;
2353
* g_key_file_set_double_list:
2354
* @key_file: a #GKeyFile
2355
* @group_name: a group name
2357
* @list: an array of double values
2358
* @length: number of double values in @list
2360
* Associates a list of double values with @key under
2361
* @group_name. If @key cannot be found then it is created.
2362
* If @group_name is %NULL the start group is used.
2367
g_key_file_set_double_list (GKeyFile *key_file,
2368
const gchar *group_name,
2376
g_return_if_fail (key_file != NULL);
2377
g_return_if_fail (list != NULL);
2379
values = g_string_sized_new (length * 16);
2380
for (i = 0; i < length; i++)
2382
gchar result[G_ASCII_DTOSTR_BUF_SIZE];
2384
g_ascii_dtostr( result, sizeof (result), list[i] );
2386
g_string_append (values, result);
2387
g_string_append_c (values, ';');
2390
g_key_file_set_value (key_file, group_name, key, values->str);
2391
g_string_free (values, TRUE);
2395
g_key_file_set_key_comment (GKeyFile *key_file,
2396
const gchar *group_name,
2398
const gchar *comment,
2401
GKeyFileGroup *group;
2402
GKeyFileKeyValuePair *pair;
2403
GList *key_node, *comment_node, *tmp;
2405
group = g_key_file_lookup_group (key_file, group_name);
2408
g_set_error (error, G_KEY_FILE_ERROR,
2409
G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2410
_("Key file does not have group '%s'"),
2411
group_name ? group_name : "(null)");
2416
/* First find the key the comments are supposed to be
2419
key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2421
if (key_node == NULL)
2423
g_set_error (error, G_KEY_FILE_ERROR,
2424
G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2425
_("Key file does not have key '%s' in group '%s'"),
2430
/* Then find all the comments already associated with the
2433
tmp = key_node->next;
2436
GKeyFileKeyValuePair *pair;
2438
pair = (GKeyFileKeyValuePair *) tmp->data;
2440
if (pair->key != NULL)
2445
g_key_file_remove_key_value_pair_node (key_file, group,
2449
if (comment == NULL)
2452
/* Now we can add our new comment
2454
pair = g_new0 (GKeyFileKeyValuePair, 1);
2457
pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2459
key_node = g_list_insert (key_node, pair, 1);
2463
g_key_file_set_group_comment (GKeyFile *key_file,
2464
const gchar *group_name,
2465
const gchar *comment,
2468
GKeyFileGroup *group;
2470
g_return_if_fail (g_key_file_is_group_name (group_name));
2472
group = g_key_file_lookup_group (key_file, group_name);
2475
g_set_error (error, G_KEY_FILE_ERROR,
2476
G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2477
_("Key file does not have group '%s'"),
2478
group_name ? group_name : "(null)");
2483
/* First remove any existing comment
2487
g_key_file_key_value_pair_free (group->comment);
2488
group->comment = NULL;
2491
if (comment == NULL)
2494
/* Now we can add our new comment
2496
group->comment = g_new0 (GKeyFileKeyValuePair, 1);
2498
group->comment->key = NULL;
2499
group->comment->value = g_key_file_parse_comment_as_value (key_file, comment);
2503
g_key_file_set_top_comment (GKeyFile *key_file,
2504
const gchar *comment,
2508
GKeyFileGroup *group;
2509
GKeyFileKeyValuePair *pair;
2511
/* The last group in the list should be the top (comments only)
2514
g_assert (key_file->groups != NULL);
2515
group_node = g_list_last (key_file->groups);
2516
group = (GKeyFileGroup *) group_node->data;
2517
g_assert (group->name == NULL);
2519
/* Note all keys must be comments at the top of
2520
* the file, so we can just free it all.
2522
if (group->key_value_pairs != NULL)
2524
g_list_foreach (group->key_value_pairs,
2525
(GFunc) g_key_file_key_value_pair_free,
2527
g_list_free (group->key_value_pairs);
2528
group->key_value_pairs = NULL;
2531
if (comment == NULL)
2534
pair = g_new0 (GKeyFileKeyValuePair, 1);
2537
pair->value = g_key_file_parse_comment_as_value (key_file, comment);
2539
group->key_value_pairs =
2540
g_list_prepend (group->key_value_pairs, pair);
2544
* g_key_file_set_comment:
2545
* @key_file: a #GKeyFile
2546
* @group_name: a group name, or %NULL
2548
* @comment: a comment
2549
* @error: return location for a #GError
2551
* Places a comment above @key from @group_name.
2552
* @group_name. If @key is %NULL then @comment will
2553
* be written above @group_name. If both @key
2554
* and @group_name are NULL, then @comment will
2555
* be written above the first group in the file.
2560
g_key_file_set_comment (GKeyFile *key_file,
2561
const gchar *group_name,
2563
const gchar *comment,
2566
g_return_if_fail (key_file != NULL);
2568
if (group_name != NULL && key != NULL)
2569
g_key_file_set_key_comment (key_file, group_name, key, comment, error);
2570
else if (group_name != NULL)
2571
g_key_file_set_group_comment (key_file, group_name, comment, error);
2573
g_key_file_set_top_comment (key_file, comment, error);
2575
if (comment != NULL)
2576
key_file->approximate_size += strlen (comment);
2580
g_key_file_get_key_comment (GKeyFile *key_file,
2581
const gchar *group_name,
2585
GKeyFileGroup *group;
2586
GKeyFileKeyValuePair *pair;
2587
GList *key_node, *tmp;
2591
g_return_val_if_fail (g_key_file_is_group_name (group_name), NULL);
2593
group = g_key_file_lookup_group (key_file, group_name);
2596
g_set_error (error, G_KEY_FILE_ERROR,
2597
G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2598
_("Key file does not have group '%s'"),
2599
group_name ? group_name : "(null)");
2604
/* First find the key the comments are supposed to be
2607
key_node = g_key_file_lookup_key_value_pair_node (key_file, group, key);
2609
if (key_node == NULL)
2611
g_set_error (error, G_KEY_FILE_ERROR,
2612
G_KEY_FILE_ERROR_KEY_NOT_FOUND,
2613
_("Key file does not have key '%s' in group '%s'"),
2620
/* Then find all the comments already associated with the
2621
* key and concatentate them.
2623
tmp = key_node->next;
2624
if (!key_node->next)
2627
pair = (GKeyFileKeyValuePair *) tmp->data;
2628
if (pair->key != NULL)
2633
pair = (GKeyFileKeyValuePair *) tmp->next->data;
2635
if (pair->key != NULL)
2641
while (tmp != key_node)
2643
GKeyFileKeyValuePair *pair;
2645
pair = (GKeyFileKeyValuePair *) tmp->data;
2648
string = g_string_sized_new (512);
2650
comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2651
g_string_append (string, comment);
2659
comment = string->str;
2660
g_string_free (string, FALSE);
2669
get_group_comment (GKeyFile *key_file,
2670
GKeyFileGroup *group,
2679
tmp = group->key_value_pairs;
2682
GKeyFileKeyValuePair *pair;
2684
pair = (GKeyFileKeyValuePair *) tmp->data;
2686
if (pair->key != NULL)
2692
if (tmp->next == NULL)
2700
GKeyFileKeyValuePair *pair;
2702
pair = (GKeyFileKeyValuePair *) tmp->data;
2705
string = g_string_sized_new (512);
2707
comment = g_key_file_parse_value_as_comment (key_file, pair->value);
2708
g_string_append (string, comment);
2715
return g_string_free (string, FALSE);
2721
g_key_file_get_group_comment (GKeyFile *key_file,
2722
const gchar *group_name,
2726
GKeyFileGroup *group;
2728
group_node = g_key_file_lookup_group_node (key_file, group_name);
2731
g_set_error (error, G_KEY_FILE_ERROR,
2732
G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2733
_("Key file does not have group '%s'"),
2734
group_name ? group_name : "(null)");
2739
group = (GKeyFileGroup *)group_node->data;
2741
return g_strdup (group->comment->value);
2743
group_node = group_node->next;
2744
group = (GKeyFileGroup *)group_node->data;
2745
return get_group_comment (key_file, group, error);
2749
g_key_file_get_top_comment (GKeyFile *key_file,
2753
GKeyFileGroup *group;
2755
/* The last group in the list should be the top (comments only)
2758
g_assert (key_file->groups != NULL);
2759
group_node = g_list_last (key_file->groups);
2760
group = (GKeyFileGroup *) group_node->data;
2761
g_assert (group->name == NULL);
2763
return get_group_comment (key_file, group, error);
2767
* g_key_file_get_comment:
2768
* @key_file: a #GKeyFile
2769
* @group_name: a group name, or %NULL
2771
* @error: return location for a #GError
2773
* Retrieves a comment above @key from @group_name.
2774
* @group_name. If @key is %NULL then @comment will
2775
* be read from above @group_name. If both @key
2776
* and @group_name are NULL, then @comment will
2777
* be read from above the first group in the file.
2779
* Returns: a comment that should be freed with g_free()
2784
g_key_file_get_comment (GKeyFile *key_file,
2785
const gchar *group_name,
2789
g_return_val_if_fail (key_file != NULL, NULL);
2791
if (group_name != NULL && key != NULL)
2792
return g_key_file_get_key_comment (key_file, group_name, key, error);
2793
else if (group_name != NULL)
2794
return g_key_file_get_group_comment (key_file, group_name, error);
2796
return g_key_file_get_top_comment (key_file, error);
2800
* g_key_file_remove_comment:
2801
* @key_file: a #GKeyFile
2802
* @group_name: a group name, or %NULL
2804
* @error: return location for a #GError
2806
* Removes a comment above @key from @group_name.
2807
* @group_name. If @key is %NULL then @comment will
2808
* be written above @group_name. If both @key
2809
* and @group_name are NULL, then @comment will
2810
* be written above the first group in the file.
2816
g_key_file_remove_comment (GKeyFile *key_file,
2817
const gchar *group_name,
2821
g_return_if_fail (key_file != NULL);
2823
if (group_name != NULL && key != NULL)
2824
g_key_file_set_key_comment (key_file, group_name, key, NULL, error);
2825
else if (group_name != NULL)
2826
g_key_file_set_group_comment (key_file, group_name, NULL, error);
2828
g_key_file_set_top_comment (key_file, NULL, error);
2832
* g_key_file_has_group:
2833
* @key_file: a #GKeyFile
2834
* @group_name: a group name
2836
* Looks whether the key file has the group @group_name.
2838
* Return value: %TRUE if @group_name is a part of @key_file, %FALSE
2843
g_key_file_has_group (GKeyFile *key_file,
2844
const gchar *group_name)
2846
g_return_val_if_fail (key_file != NULL, FALSE);
2847
g_return_val_if_fail (group_name != NULL, FALSE);
2849
return g_key_file_lookup_group_node (key_file, group_name) != NULL;
2853
* g_key_file_has_key:
2854
* @key_file: a #GKeyFile
2855
* @group_name: a group name
2857
* @error: return location for a #GError
2859
* Looks whether the key file has the key @key in the group
2862
* Return value: %TRUE if @key is a part of @group_name, %FALSE
2868
g_key_file_has_key (GKeyFile *key_file,
2869
const gchar *group_name,
2873
GKeyFileKeyValuePair *pair;
2874
GKeyFileGroup *group;
2876
g_return_val_if_fail (key_file != NULL, FALSE);
2877
g_return_val_if_fail (group_name != NULL, FALSE);
2878
g_return_val_if_fail (key != NULL, FALSE);
2880
group = g_key_file_lookup_group (key_file, group_name);
2884
g_set_error (error, G_KEY_FILE_ERROR,
2885
G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
2886
_("Key file does not have group '%s'"),
2887
group_name ? group_name : "(null)");
2892
pair = g_key_file_lookup_key_value_pair (key_file, group, key);
2894
return pair != NULL;
2898
g_key_file_add_group (GKeyFile *key_file,
2899
const gchar *group_name)
2901
GKeyFileGroup *group;
2903
g_return_if_fail (key_file != NULL);
2904
g_return_if_fail (g_key_file_is_group_name (group_name));
2906
group = g_key_file_lookup_group (key_file, group_name);
2909
key_file->current_group = group;
2913
group = g_new0 (GKeyFileGroup, 1);
2914
group->name = g_strdup (group_name);
2915
group->lookup_map = g_hash_table_new (g_str_hash, g_str_equal);
2916
key_file->groups = g_list_prepend (key_file->groups, group);
2917
key_file->approximate_size += strlen (group_name) + 3;
2918
key_file->current_group = group;
2920
if (key_file->start_group == NULL)
2921
key_file->start_group = group;
2925
g_key_file_key_value_pair_free (GKeyFileKeyValuePair *pair)
2930
g_free (pair->value);
2935
/* Be careful not to call this function on a node with data in the
2936
* lookup map without removing it from the lookup map, first.
2938
* Some current cases where this warning is not a concern are
2940
* - the node being removed is a comment node
2941
* - the entire lookup map is getting destroyed soon after
2945
g_key_file_remove_key_value_pair_node (GKeyFile *key_file,
2946
GKeyFileGroup *group,
2950
GKeyFileKeyValuePair *pair;
2952
pair = (GKeyFileKeyValuePair *) pair_node->data;
2954
group->key_value_pairs = g_list_remove_link (group->key_value_pairs, pair_node);
2956
if (pair->key != NULL)
2957
key_file->approximate_size -= strlen (pair->key) + 1;
2959
g_assert (pair->value != NULL);
2960
key_file->approximate_size -= strlen (pair->value);
2962
g_key_file_key_value_pair_free (pair);
2964
g_list_free_1 (pair_node);
2968
g_key_file_remove_group_node (GKeyFile *key_file,
2971
GKeyFileGroup *group;
2974
group = (GKeyFileGroup *) group_node->data;
2976
/* If the current group gets deleted make the current group the last
2979
if (key_file->current_group == group)
2981
/* groups should always contain at least the top comment group,
2982
* unless g_key_file_clear has been called
2984
if (key_file->groups)
2985
key_file->current_group = (GKeyFileGroup *) key_file->groups->data;
2987
key_file->current_group = NULL;
2990
/* If the start group gets deleted make the start group the first
2993
if (key_file->start_group == group)
2995
tmp = g_list_last (key_file->groups);
2998
if (tmp != group_node &&
2999
((GKeyFileGroup *) tmp->data)->name != NULL)
3006
key_file->start_group = (GKeyFileGroup *) tmp->data;
3008
key_file->start_group = NULL;
3011
key_file->groups = g_list_remove_link (key_file->groups, group_node);
3013
if (group->name != NULL)
3014
key_file->approximate_size -= strlen (group->name) + 3;
3016
tmp = group->key_value_pairs;
3023
g_key_file_remove_key_value_pair_node (key_file, group, pair_node);
3026
g_assert (group->key_value_pairs == NULL);
3028
if (group->lookup_map)
3030
g_hash_table_destroy (group->lookup_map);
3031
group->lookup_map = NULL;
3034
g_free ((gchar *) group->name);
3036
g_list_free_1 (group_node);
3040
* g_key_file_remove_group:
3041
* @key_file: a #GKeyFile
3042
* @group_name: a group name
3043
* @error: return location for a #GError or %NULL
3045
* Removes the specified group, @group_name,
3046
* from the key file.
3051
g_key_file_remove_group (GKeyFile *key_file,
3052
const gchar *group_name,
3057
g_return_if_fail (key_file != NULL);
3058
g_return_if_fail (group_name != NULL);
3060
group_node = g_key_file_lookup_group_node (key_file, group_name);
3064
g_set_error (error, G_KEY_FILE_ERROR,
3065
G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3066
_("Key file does not have group '%s'"),
3071
g_key_file_remove_group_node (key_file, group_node);
3075
g_key_file_add_key (GKeyFile *key_file,
3076
GKeyFileGroup *group,
3080
GKeyFileKeyValuePair *pair;
3082
pair = g_new0 (GKeyFileKeyValuePair, 1);
3084
pair->key = g_strdup (key);
3085
pair->value = g_strdup (value);
3087
g_hash_table_replace (group->lookup_map, pair->key, pair);
3088
group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
3089
group->has_trailing_blank_line = FALSE;
3090
key_file->approximate_size += strlen (key) + strlen (value) + 2;
3094
* g_key_file_remove_key:
3095
* @key_file: a #GKeyFile
3096
* @group_name: a group name
3097
* @key: a key name to remove
3098
* @error: return location for a #GError or %NULL
3100
* Removes @key in @group_name from the key file.
3105
g_key_file_remove_key (GKeyFile *key_file,
3106
const gchar *group_name,
3110
GKeyFileGroup *group;
3111
GKeyFileKeyValuePair *pair;
3113
g_return_if_fail (key_file != NULL);
3114
g_return_if_fail (group_name != NULL);
3115
g_return_if_fail (key != NULL);
3119
group = g_key_file_lookup_group (key_file, group_name);
3122
g_set_error (error, G_KEY_FILE_ERROR,
3123
G_KEY_FILE_ERROR_GROUP_NOT_FOUND,
3124
_("Key file does not have group '%s'"),
3125
group_name ? group_name : "(null)");
3129
pair = g_key_file_lookup_key_value_pair (key_file, group, key);
3133
g_set_error (error, G_KEY_FILE_ERROR,
3134
G_KEY_FILE_ERROR_KEY_NOT_FOUND,
3135
_("Key file does not have key '%s' in group '%s'"),
3140
key_file->approximate_size -= strlen (pair->key) + strlen (pair->value) + 2;
3142
group->key_value_pairs = g_list_remove (group->key_value_pairs, pair);
3143
g_hash_table_remove (group->lookup_map, pair->key);
3144
g_key_file_key_value_pair_free (pair);
3148
g_key_file_lookup_group_node (GKeyFile *key_file,
3149
const gchar *group_name)
3151
GKeyFileGroup *group;
3154
for (tmp = key_file->groups; tmp != NULL; tmp = tmp->next)
3156
group = (GKeyFileGroup *) tmp->data;
3158
if (group && group->name && strcmp (group->name, group_name) == 0)
3165
static GKeyFileGroup *
3166
g_key_file_lookup_group (GKeyFile *key_file,
3167
const gchar *group_name)
3171
group_node = g_key_file_lookup_group_node (key_file, group_name);
3173
if (group_node != NULL)
3174
return (GKeyFileGroup *) group_node->data;
3180
g_key_file_lookup_key_value_pair_node (GKeyFile *key_file,
3181
GKeyFileGroup *group,
3186
for (key_node = group->key_value_pairs;
3188
key_node = key_node->next)
3190
GKeyFileKeyValuePair *pair;
3192
pair = (GKeyFileKeyValuePair *) key_node->data;
3194
if (pair->key && strcmp (pair->key, key) == 0)
3201
static GKeyFileKeyValuePair *
3202
g_key_file_lookup_key_value_pair (GKeyFile *key_file,
3203
GKeyFileGroup *group,
3206
return (GKeyFileKeyValuePair *) g_hash_table_lookup (group->lookup_map, key);
3209
/* Lines starting with # or consisting entirely of whitespace are merely
3210
* recorded, not parsed. This function assumes all leading whitespace
3211
* has been stripped.
3214
g_key_file_line_is_comment (const gchar *line)
3216
return (*line == '#' || *line == '\0' || *line == '\n');
3220
g_key_file_is_group_name (const gchar *name)
3227
p = q = (gchar *) name;
3228
while (*q && *q != ']' && *q != '[' && !g_ascii_iscntrl (*q))
3229
q = g_utf8_next_char (q);
3231
if (*q != '\0' || q == p)
3238
g_key_file_is_key_name (const gchar *name)
3245
p = q = (gchar *) name;
3246
/* We accept a little more than the desktop entry spec says,
3247
* since gnome-vfs uses mime-types as keys in its cache.
3249
while (*q && *q != '=' && *q != '[' && *q != ']')
3250
q = g_utf8_next_char (q);
3252
/* No empty keys, please */
3256
/* We accept spaces in the middle of keys to not break
3257
* existing apps, but we don't tolerate initial of final
3258
* spaces, which would lead to silent corruption when
3259
* rereading the file.
3261
if (*p == ' ' || q[-1] == ' ')
3267
while (*q && (g_unichar_isalnum (g_utf8_get_char (q)) || *q == '-' || *q == '_' || *q == '.' || *q == '@'))
3268
q = g_utf8_next_char (q);
3282
/* A group in a key file is made up of a starting '[' followed by one
3283
* or more letters making up the group name followed by ']'.
3286
g_key_file_line_is_group (const gchar *line)
3296
while (*p && *p != ']')
3297
p = g_utf8_next_char (p);
3302
/* silently accept whitespace after the ] */
3303
p = g_utf8_next_char (p);
3304
while (*p == ' ' || *p == '\t')
3305
p = g_utf8_next_char (p);
3314
g_key_file_line_is_key_value_pair (const gchar *line)
3318
p = (gchar *) g_utf8_strchr (line, -1, '=');
3323
/* Key must be non-empty
3332
g_key_file_parse_value_as_string (GKeyFile *key_file,
3337
gchar *string_value, *p, *q0, *q;
3339
string_value = g_new0 (gchar, strlen (value) + 1);
3341
p = (gchar *) value;
3342
q0 = q = string_value;
3372
g_set_error (error, G_KEY_FILE_ERROR,
3373
G_KEY_FILE_ERROR_INVALID_VALUE,
3374
_("Key file contains escape character "
3379
if (pieces && *p == key_file->list_separator)
3380
*q = key_file->list_separator;
3394
g_set_error (error, G_KEY_FILE_ERROR,
3395
G_KEY_FILE_ERROR_INVALID_VALUE,
3396
_("Key file contains invalid escape "
3397
"sequence '%s'"), sequence);
3406
if (pieces && (*p == key_file->list_separator))
3408
*pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3424
*pieces = g_slist_prepend (*pieces, g_strndup (q0, q - q0));
3425
*pieces = g_slist_reverse (*pieces);
3428
return string_value;
3432
g_key_file_parse_string_as_value (GKeyFile *key_file,
3433
const gchar *string,
3434
gboolean escape_separator)
3436
gchar *value, *p, *q;
3438
gboolean parsing_leading_space;
3440
length = strlen (string) + 1;
3442
/* Worst case would be that every character needs to be escaped.
3443
* In other words every character turns to two characters
3445
value = g_new0 (gchar, 2 * length);
3447
p = (gchar *) string;
3449
parsing_leading_space = TRUE;
3450
while (p < (string + length - 1))
3452
gchar escaped_character[3] = { '\\', 0, 0 };
3457
if (parsing_leading_space)
3459
escaped_character[1] = 's';
3460
strcpy (q, escaped_character);
3470
if (parsing_leading_space)
3472
escaped_character[1] = 't';
3473
strcpy (q, escaped_character);
3483
escaped_character[1] = 'n';
3484
strcpy (q, escaped_character);
3488
escaped_character[1] = 'r';
3489
strcpy (q, escaped_character);
3493
escaped_character[1] = '\\';
3494
strcpy (q, escaped_character);
3496
parsing_leading_space = FALSE;
3499
if (escape_separator && *p == key_file->list_separator)
3501
escaped_character[1] = key_file->list_separator;
3502
strcpy (q, escaped_character);
3504
parsing_leading_space = TRUE;
3510
parsing_leading_space = FALSE;
3522
g_key_file_parse_value_as_integer (GKeyFile *key_file,
3526
gchar *end_of_valid_int;
3531
long_value = strtol (value, &end_of_valid_int, 10);
3533
if (*value == '\0' || *end_of_valid_int != '\0')
3535
gchar *value_utf8 = _g_utf8_make_valid (value);
3536
g_set_error (error, G_KEY_FILE_ERROR,
3537
G_KEY_FILE_ERROR_INVALID_VALUE,
3538
_("Value '%s' cannot be interpreted "
3539
"as a number."), value_utf8);
3540
g_free (value_utf8);
3545
int_value = long_value;
3546
if (int_value != long_value || errno == ERANGE)
3548
gchar *value_utf8 = _g_utf8_make_valid (value);
3551
G_KEY_FILE_ERROR_INVALID_VALUE,
3552
_("Integer value '%s' out of range"),
3554
g_free (value_utf8);
3563
g_key_file_parse_integer_as_value (GKeyFile *key_file,
3567
return g_strdup_printf ("%d", value);
3571
g_key_file_parse_value_as_double (GKeyFile *key_file,
3575
gchar *end_of_valid_d;
3576
gdouble double_value = 0;
3578
double_value = g_ascii_strtod (value, &end_of_valid_d);
3580
if (*end_of_valid_d != '\0' || end_of_valid_d == value)
3582
gchar *value_utf8 = _g_utf8_make_valid (value);
3583
g_set_error (error, G_KEY_FILE_ERROR,
3584
G_KEY_FILE_ERROR_INVALID_VALUE,
3585
_("Value '%s' cannot be interpreted "
3586
"as a float number."),
3588
g_free (value_utf8);
3591
return double_value;
3595
g_key_file_parse_value_as_boolean (GKeyFile *key_file,
3603
if (strcmp (value, "true") == 0 || strcmp (value, "1") == 0)
3605
else if (strcmp (value, "false") == 0 || strcmp (value, "0") == 0)
3609
value_utf8 = _g_utf8_make_valid (value);
3610
g_set_error (error, G_KEY_FILE_ERROR,
3611
G_KEY_FILE_ERROR_INVALID_VALUE,
3612
_("Value '%s' cannot be interpreted "
3613
"as a boolean."), value_utf8);
3614
g_free (value_utf8);
3620
g_key_file_parse_boolean_as_value (GKeyFile *key_file,
3624
return g_strdup ("true");
3626
return g_strdup ("false");
3630
g_key_file_parse_value_as_comment (GKeyFile *key_file,
3637
string = g_string_sized_new (512);
3639
lines = g_strsplit (value, "\n", 0);
3641
for (i = 0; lines[i] != NULL; i++)
3643
if (lines[i][0] != '#')
3644
g_string_append_printf (string, "%s\n", lines[i]);
3646
g_string_append_printf (string, "%s\n", lines[i] + 1);
3650
return g_string_free (string, FALSE);
3654
g_key_file_parse_comment_as_value (GKeyFile *key_file,
3655
const gchar *comment)
3661
string = g_string_sized_new (512);
3663
lines = g_strsplit (comment, "\n", 0);
3665
for (i = 0; lines[i] != NULL; i++)
3666
g_string_append_printf (string, "#%s%s", lines[i],
3667
lines[i + 1] == NULL? "" : "\n");
3670
return g_string_free (string, FALSE);
3673
#define __G_KEY_FILE_C__
3674
#include "galiasdef.c"