GKeyFile: Deal better with blank lines

There is no need to store a has_trailing_blank_line boolean for
each group, we can just check this at the time we assemble the data.

This fixes a problem without roundtrips where we would sometimes
add an extra blank line between groups.

The testcase here is inspired by
https://bugzilla.gnome.org/show_bug.cgi?id=677817
This commit is contained in:
Matthias Clasen 2012-06-15 18:53:09 -04:00
parent d0c8895a07
commit defa25f3d0
2 changed files with 29 additions and 9 deletions

View File

@ -454,7 +454,6 @@ struct _GKeyFileGroup
const gchar *name; /* NULL for above first group (which will be comments) */ const gchar *name; /* NULL for above first group (which will be comments) */
GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */ GKeyFileKeyValuePair *comment; /* Special comment that is stuck to the top of a group */
gboolean has_trailing_blank_line;
GList *key_value_pairs; GList *key_value_pairs;
@ -1190,9 +1189,6 @@ g_key_file_parse_comment (GKeyFile *key_file,
key_file->current_group->key_value_pairs = key_file->current_group->key_value_pairs =
g_list_prepend (key_file->current_group->key_value_pairs, pair); g_list_prepend (key_file->current_group->key_value_pairs, pair);
if (length == 0 || line[0] != '#')
key_file->current_group->has_trailing_blank_line = TRUE;
} }
static void static void
@ -1454,7 +1450,6 @@ g_key_file_to_data (GKeyFile *key_file,
{ {
GString *data_string; GString *data_string;
GList *group_node, *key_file_node; GList *group_node, *key_file_node;
gboolean has_blank_line = TRUE;
g_return_val_if_fail (key_file != NULL, NULL); g_return_val_if_fail (key_file != NULL, NULL);
@ -1469,9 +1464,9 @@ g_key_file_to_data (GKeyFile *key_file,
group = (GKeyFileGroup *) group_node->data; group = (GKeyFileGroup *) group_node->data;
/* separate groups by at least an empty line */ /* separate groups by at least an empty line */
if (!has_blank_line) if (data_string->len >= 2 &&
g_string_append_c (data_string, '\n'); data_string->str[data_string->len - 2] != '\n')
has_blank_line = group->has_trailing_blank_line; g_string_append_c (data_string, '\n');
if (group->comment != NULL) if (group->comment != NULL)
g_string_append_printf (data_string, "%s\n", group->comment->value); g_string_append_printf (data_string, "%s\n", group->comment->value);
@ -3792,7 +3787,6 @@ g_key_file_add_key_value_pair (GKeyFile *key_file,
{ {
g_hash_table_replace (group->lookup_map, pair->key, pair); g_hash_table_replace (group->lookup_map, pair->key, pair);
group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair); group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
group->has_trailing_blank_line = FALSE;
} }
static void static void

View File

@ -1538,6 +1538,31 @@ test_utf8 (void)
g_clear_error (&error); g_clear_error (&error);
g_key_file_free (file); g_key_file_free (file);
} }
static void
test_roundtrip (void)
{
GKeyFile *kf;
const gchar orig[] =
"[Group1]\n"
"key1=value1\n"
"\n"
"[Group2]\n"
"key1=value1\n";
gsize len;
gchar *data;
kf = load_data (orig, G_KEY_FILE_KEEP_COMMENTS);
g_key_file_set_integer (kf, "Group1", "key2", 0);
g_key_file_remove_key (kf, "Group1", "key2", NULL);
data = g_key_file_to_data (kf, &len, NULL);
g_assert_cmpstr (data, ==, orig);
g_free (data);
g_key_file_free (kf);
}
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
@ -1576,6 +1601,7 @@ main (int argc, char *argv[])
g_test_add_func ("/keyfile/empty-string", test_empty_string); g_test_add_func ("/keyfile/empty-string", test_empty_string);
g_test_add_func ("/keyfile/limbo", test_limbo); g_test_add_func ("/keyfile/limbo", test_limbo);
g_test_add_func ("/keyfile/utf8", test_utf8); g_test_add_func ("/keyfile/utf8", test_utf8);
g_test_add_func ("/keyfile/roundtrip", test_roundtrip);
return g_test_run (); return g_test_run ();
} }