[glib] GKeyFile: Deal better with blank lines



commit defa25f3d00619947720be0bcda2357bd43e718f
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Jun 15 18:53:09 2012 -0400

    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

 glib/gkeyfile.c      |   12 +++---------
 glib/tests/keyfile.c |   26 ++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 9 deletions(-)
---
diff --git a/glib/gkeyfile.c b/glib/gkeyfile.c
index 50ba2d9..98adf99 100644
--- a/glib/gkeyfile.c
+++ b/glib/gkeyfile.c
@@ -454,7 +454,6 @@ struct _GKeyFileGroup
   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;
 
@@ -1190,9 +1189,6 @@ g_key_file_parse_comment (GKeyFile     *key_file,
   
   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
@@ -1454,7 +1450,6 @@ g_key_file_to_data (GKeyFile  *key_file,
 {
   GString *data_string;
   GList *group_node, *key_file_node;
-  gboolean has_blank_line = TRUE;
 
   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;
 
       /* 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);
@@ -3792,7 +3787,6 @@ g_key_file_add_key_value_pair (GKeyFile             *key_file,
 {
   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
diff --git a/glib/tests/keyfile.c b/glib/tests/keyfile.c
index 8b05fcf..4c12b57 100644
--- a/glib/tests/keyfile.c
+++ b/glib/tests/keyfile.c
@@ -1538,6 +1538,31 @@ test_utf8 (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[])
 {
@@ -1576,6 +1601,7 @@ 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 ();
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]