gst/gststructure.c: Improve serialization and fix tests.
authorStefan Kost <ensonic@users.sourceforge.net>
Tue, 16 Oct 2007 06:32:07 +0000 (06:32 +0000)
committerStefan Kost <ensonic@users.sourceforge.net>
Tue, 16 Oct 2007 06:32:07 +0000 (06:32 +0000)
Original commit message from CVS:
* gst/gststructure.c:
Improve serialization and fix tests.
* tests/check/gst/gststructure.c:
Add another test that covers why I actually did the previous structure
change.

ChangeLog
gst/gststructure.c
tests/check/gst/gststructure.c

index 5053b1c..2449af2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2007-10-16  Stefan Kost  <ensonic@users.sf.net>
+
+       * gst/gststructure.c:
+         Improve serialization and fix tests.
+
+       * tests/check/gst/gststructure.c:
+         Add another test that covers why I actually did the previous structure
+         change.
+
 2007-10-15  Wim Taymans  <wim.taymans@gmail.com>
 
        * tools/gst-inspect.c: (print_element_info):
index c95845f..1b9d85c 100644 (file)
@@ -1484,6 +1484,8 @@ gst_structure_to_string (const GstStructure * structure)
   GstStructureField *field;
   GString *s;
   guint i;
+  const gchar *name;
+  gchar *name_esc;
 
   /* NOTE:  This function is potentially called by the debug system,
    * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
@@ -1494,8 +1496,15 @@ gst_structure_to_string (const GstStructure * structure)
   g_return_val_if_fail (structure != NULL, NULL);
 
   s = g_string_new ("");
-  /* FIXME this string may need to be escaped */
-  g_string_append_printf (s, "\"%s\"", g_quark_to_string (structure->name));
+  /* this string may need to be escaped */
+  name = g_quark_to_string (structure->name);
+  name_esc = g_strescape (name, NULL);
+  if ((strlen (name) < strlen (name_esc)) || strchr (name, ' ')) {
+    g_string_append_printf (s, "\"%s\"", name);
+  } else {
+    g_string_append_printf (s, "%s", name);
+  }
+  g_free (name_esc);
   for (i = 0; i < structure->fields->len; i++) {
     char *t;
     GType type;
@@ -1505,6 +1514,7 @@ gst_structure_to_string (const GstStructure * structure)
     t = gst_value_serialize (&field->value);
     type = gst_structure_value_get_generic_type (&field->value);
 
+    /* FIXME: do we need to escape fieldnames? */
     g_string_append_printf (s, ", %s=(%s)%s", g_quark_to_string (field->name),
         gst_structure_to_abbr (type), GST_STR_NULL (t));
     g_free (t);
index e9a7f27..fcf79b4 100644 (file)
@@ -119,11 +119,34 @@ GST_START_TEST (test_from_string)
 
 GST_END_TEST;
 
+
+GST_START_TEST (test_to_string)
+{
+  GstStructure *st1, *st2;
+  gchar *str;
+
+  /* use structure name and string with spaces, to test escaping/unescaping */
+  st1 = gst_structure_new ("Foo Bar\nwith newline", "num", G_TYPE_INT, 9173,
+      "string", G_TYPE_STRING, "Something Like Face/Off", NULL);
+  str = gst_structure_to_string (st1);
+  st2 = gst_structure_from_string (str, NULL);
+  g_free (str);
+
+  fail_unless (st2 != NULL);
+  fail_unless (!strcmp ("Foo Bar\nwith newline", gst_structure_get_name (st2)));
+
+  gst_structure_free (st2);
+  gst_structure_free (st1);
+}
+
+GST_END_TEST;
+
+
 GST_START_TEST (test_to_from_string)
 {
   GstCaps *caps1, *caps2;
   GstStructure *st1, *st2;
-  gchar *str, *res1, *res2;;
+  gchar *str, *res1, *res2;
 
   /* use structure name and string with spaces, to test escaping/unescaping */
   st1 = gst_structure_new ("Foo Bar", "num", G_TYPE_INT, 9173,
@@ -132,6 +155,8 @@ GST_START_TEST (test_to_from_string)
   st2 = gst_structure_from_string (str, NULL);
   g_free (str);
 
+  fail_unless (st2 != NULL);
+
   /* need to put stuctures into caps to compare */
   caps1 = gst_caps_new_empty ();
   gst_caps_append_structure (caps1, st1);
@@ -283,6 +308,7 @@ gst_structure_suite (void)
   suite_add_tcase (s, tc_chain);
   tcase_add_test (tc_chain, test_from_string_int);
   tcase_add_test (tc_chain, test_from_string);
+  tcase_add_test (tc_chain, test_to_string);
   tcase_add_test (tc_chain, test_to_from_string);
   tcase_add_test (tc_chain, test_complete_structure);
   tcase_add_test (tc_chain, test_structure_new);