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 */
- gboolean has_trailing_blank_line;
GList *key_value_pairs;
key_file->current_group->key_value_pairs =
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
{
GString *data_string;
GList *group_node, *key_file_node;
- gboolean has_blank_line = TRUE;
g_return_val_if_fail (key_file != NULL, NULL);
group = (GKeyFileGroup *) group_node->data;
/* separate groups by at least an empty line */
- if (!has_blank_line)
- g_string_append_c (data_string, '\n');
- has_blank_line = group->has_trailing_blank_line;
+ if (data_string->len >= 2 &&
+ data_string->str[data_string->len - 2] != '\n')
+ g_string_append_c (data_string, '\n');
if (group->comment != NULL)
g_string_append_printf (data_string, "%s\n", group->comment->value);
{
g_hash_table_replace (group->lookup_map, pair->key, pair);
group->key_value_pairs = g_list_prepend (group->key_value_pairs, pair);
- group->has_trailing_blank_line = FALSE;
}
static void
g_clear_error (&error);
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
main (int argc, char *argv[])
{
g_test_add_func ("/keyfile/empty-string", test_empty_string);
g_test_add_func ("/keyfile/limbo", test_limbo);
g_test_add_func ("/keyfile/utf8", test_utf8);
+ g_test_add_func ("/keyfile/roundtrip", test_roundtrip);
return g_test_run ();
}