gst/gststructure.c: Convert function to use gst_value_serialize().
authorDavid Schleef <ds@schleef.org>
Tue, 20 Jan 2004 09:14:25 +0000 (09:14 +0000)
committerDavid Schleef <ds@schleef.org>
Tue, 20 Jan 2004 09:14:25 +0000 (09:14 +0000)
Original commit message from CVS:
* gst/gststructure.c: (gst_structure_to_string):
Convert function to use gst_value_serialize().
* gst/gstvalue.c: (gst_value_serialize_list),
(gst_value_serialize_fourcc), (gst_value_serialize_int_range),
(gst_value_serialize_double_range), (gst_value_serialize_boolean),
(gst_value_serialize_int), (gst_value_serialize_double),
(gst_string_wrap), (gst_value_serialize_string),
(gst_value_serialize), (gst_value_deserialize):
* gst/gstvalue.h:
Add implementations for serialize.

ChangeLog
gst/gststructure.c
gst/gstvalue.c
gst/gstvalue.h

index 767fd2b..3df2848 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2004-01-20  David Schleef  <ds@schleef.org>
+
+       * gst/gststructure.c: (gst_structure_to_string):
+       Convert function to use gst_value_serialize().
+       * gst/gstvalue.c: (gst_value_serialize_list),
+       (gst_value_serialize_fourcc), (gst_value_serialize_int_range),
+       (gst_value_serialize_double_range), (gst_value_serialize_boolean),
+       (gst_value_serialize_int), (gst_value_serialize_double),
+       (gst_string_wrap), (gst_value_serialize_string),
+       (gst_value_serialize), (gst_value_deserialize):
+       * gst/gstvalue.h:
+       Add implementations for serialize.
+
 2004-01-20  Julien MOUTTE  <julien@moutte.net>
 
        * gst/gsterror.h: xvidenc.c needs GST_LIBRARY_ERROR_ENCODE. Dunno if
index 0fbf54e..8a48084 100644 (file)
@@ -955,55 +955,6 @@ static const char *_gst_structure_to_abbr(GType type)
       ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
       ((c) == '.'))
 
-static gchar *
-_gst_structure_wrap_string(gchar *s)
-{
-  gchar *t;
-  int len;
-  gchar *d, *e;
-  gboolean wrap = FALSE;
-
-  len = 0;
-  t = s;
-  while (*t) {
-    if(GST_ASCII_IS_STRING(*t)) {
-      len++;
-    } else if(*t < 0x20 || *t >= 0x7f) {
-      wrap = TRUE;
-      len += 4;
-    } else {
-      wrap = TRUE;
-      len += 2;
-    }
-    t++;
-  }
-
-  if (!wrap) return s;
-
-  e = d = g_malloc(len + 3);
-
-  *e++ = '\"';
-  t = s;
-  while (*t) {
-    if(GST_ASCII_IS_STRING(*t)) {
-      *e++ = *t++;
-    } else if(*t < 0x20 || *t >= 0x7f) {
-      *e++ = '\\';
-      *e++ = '0' + ((*t)>>6);
-      *e++ = '0' + (((*t)>>3)&0x7);
-      *e++ = '0' + ((*t++)&0x7);
-    } else {
-      *e++ = '\\';
-      *e++ = *t++;
-    }
-  }
-  *e++ = '\"';
-  *e = 0;
-
-  g_free(s);
-  return d;
-}
-
 /**
  * gst_structure_to_string:
  * @structure: a #GstStructure
@@ -1017,7 +968,6 @@ gst_structure_to_string(const GstStructure *structure)
 {
   GstStructureField *field;
   GString *s;
-  char *t;
   int i;
 
   g_return_val_if_fail(structure != NULL, NULL);
@@ -1026,14 +976,12 @@ gst_structure_to_string(const GstStructure *structure)
   /* FIXME this string may need to be escaped */
   g_string_append_printf(s, "%s", g_quark_to_string(structure->name));
   for(i=0;i<structure->fields->len;i++) {
-    GValue s_val = { 0 };
+    char *t;
     GType type;
 
     field = GST_STRUCTURE_FIELD(structure, i);
 
-    g_value_init(&s_val, G_TYPE_STRING);
-    g_value_transform (&field->value, &s_val);
-
+    t = gst_value_serialize (&field->value);
     type = G_VALUE_TYPE (&field->value);
 
     if (type == GST_TYPE_LIST) {
@@ -1045,21 +993,14 @@ gst_structure_to_string(const GstStructure *structure)
       } else {
        type = G_TYPE_INT;
       }
-      t = g_strdup(g_value_get_string(&s_val));
     } else if (G_VALUE_TYPE(&field->value) == GST_TYPE_INT_RANGE) {
       type = G_TYPE_INT;
-      t = g_strdup(g_value_get_string(&s_val));
     } else if (G_VALUE_TYPE(&field->value) == GST_TYPE_DOUBLE_RANGE) {
       type = G_TYPE_DOUBLE;
-      t = g_strdup(g_value_get_string(&s_val));
-    } else {
-      t = _gst_structure_wrap_string(g_strdup(g_value_get_string(&s_val)));
     }
-
     g_string_append_printf(s, ", %s=(%s)%s", g_quark_to_string(field->name),
        _gst_structure_to_abbr(type), t);
     g_free(t);
-    g_value_unset (&s_val);
   }
   return g_string_free(s, FALSE);
 }
index b4506bb..7035f82 100644 (file)
@@ -283,8 +283,24 @@ gst_value_compare_list (const GValue *value1, const GValue *value2)
 static char *
 gst_value_serialize_list (const GValue *value)
 {
-  g_warning("unimplemented");
-  return NULL;
+  int i;
+  GArray *array = value->data[0].v_pointer;
+  GString *s;
+  GValue *v;
+  gchar *s_val;
+
+  s = g_string_new("{ ");
+  for(i=0;i<array->len;i++){
+    v = &g_array_index (array, GValue, i);
+    s_val = gst_value_serialize (v);
+    g_string_append (s, s_val);
+    g_free (s_val);
+    if (i<array->len - 1) {
+      g_string_append (s, ", ");
+    }
+  }
+  g_string_append (s, " }");
+  return g_string_free (s, FALSE);
 }
 
 static gboolean
@@ -384,8 +400,16 @@ gst_value_compare_fourcc (const GValue *value1, const GValue *value2)
 static char *
 gst_value_serialize_fourcc (const GValue *value)
 {
-  g_warning("unimplemented");
-  return NULL;
+  guint32 fourcc = value->data[0].v_int;
+
+  if (g_ascii_isalnum ((fourcc>>0) & 0xff) &&
+      g_ascii_isalnum ((fourcc>>8) & 0xff) &&
+      g_ascii_isalnum ((fourcc>>16) & 0xff) &&
+      g_ascii_isalnum ((fourcc>>24) & 0xff)){
+    return g_strdup_printf(GST_FOURCC_FORMAT, GST_FOURCC_ARGS(fourcc));
+  } else {
+    return g_strdup_printf("0x%08x", fourcc);
+  }
 }
 
 static gboolean
@@ -499,8 +523,8 @@ gst_value_compare_int_range (const GValue *value1, const GValue *value2)
 static char *
 gst_value_serialize_int_range (const GValue *value)
 {
-  g_warning("unimplemented");
-  return NULL;
+  return g_strdup_printf ("[ %d, %d ]", value->data[0].v_int,
+      value->data[1].v_int);
 }
 
 static gboolean
@@ -619,8 +643,11 @@ gst_value_compare_double_range (const GValue *value1, const GValue *value2)
 static char *
 gst_value_serialize_double_range (const GValue *value)
 {
-  g_warning("unimplemented");
-  return NULL;
+  char d1[G_ASCII_DTOSTR_BUF_SIZE];
+  char d2[G_ASCII_DTOSTR_BUF_SIZE];
+  g_ascii_dtostr(d1, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
+  g_ascii_dtostr(d2, G_ASCII_DTOSTR_BUF_SIZE, value->data[1].v_double);
+  return g_strdup_printf ("[ %s, %s ]", d1, d2);
 }
 
 static gboolean
@@ -671,8 +698,10 @@ gst_value_compare_boolean (const GValue *value1, const GValue *value2)
 static char *
 gst_value_serialize_boolean (const GValue *value)
 {
-  g_warning("unimplemented");
-  return NULL;
+  if (value->data[0].v_int) {
+    return g_strdup ("true");
+  }
+  return g_strdup ("false");
 }
 
 static gboolean
@@ -698,8 +727,7 @@ gst_value_compare_int (const GValue *value1, const GValue *value2)
 static char *
 gst_value_serialize_int (const GValue *value)
 {
-  g_warning("unimplemented");
-  return NULL;
+  return g_strdup_printf ("%d", value->data[0].v_int);
 }
 
 static gboolean
@@ -727,8 +755,9 @@ gst_value_compare_double (const GValue *value1, const GValue *value2)
 static char *
 gst_value_serialize_double (const GValue *value)
 {
-  g_warning("unimplemented");
-  return NULL;
+  char d[G_ASCII_DTOSTR_BUF_SIZE];
+  g_ascii_dtostr(d, G_ASCII_DTOSTR_BUF_SIZE, value->data[0].v_double);
+  return g_strdup (d);
 }
 
 static gboolean
@@ -750,11 +779,62 @@ gst_value_compare_string (const GValue *value1, const GValue *value2)
   return GST_VALUE_EQUAL;
 }
 
+#define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
+    ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
+    ((c) == '.'))
+
+static gchar *
+gst_string_wrap (const char *s)
+{
+  const gchar *t;
+  int len;
+  gchar *d, *e;
+  gboolean wrap = FALSE;
+
+  len = 0;
+  t = s;
+  while (*t) {
+    if(GST_ASCII_IS_STRING(*t)) {
+      len++;
+    } else if(*t < 0x20 || *t >= 0x7f) {
+      wrap = TRUE;
+      len += 4;
+    } else {
+      wrap = TRUE;
+      len += 2;
+    }
+    t++;
+  }
+
+  if (!wrap) return strdup (s);
+
+  e = d = g_malloc(len + 3);
+
+  *e++ = '\"';
+  t = s;
+  while (*t) {
+    if(GST_ASCII_IS_STRING(*t)) {
+      *e++ = *t++;
+    } else if(*t < 0x20 || *t >= 0x7f) {
+      *e++ = '\\';
+      *e++ = '0' + ((*t)>>6);
+      *e++ = '0' + (((*t)>>3)&0x7);
+      *e++ = '0' + ((*t++)&0x7);
+    } else {
+      *e++ = '\\';
+      *e++ = *t++;
+    }
+  }
+  *e++ = '\"';
+  *e = 0;
+
+  return d;
+}
+
 static char *
 gst_value_serialize_string (const GValue *value)
 {
-  g_warning("unimplemented");
-  return NULL;
+  return gst_string_wrap (value->data[0].v_pointer);
 }
 
 static gboolean
@@ -1102,6 +1182,45 @@ gst_value_init_and_copy (GValue *dest, const GValue *src)
   g_value_copy (src, dest);
 }
 
+/**
+ * gst_value_serialize:
+ *
+ */
+gchar *
+gst_value_serialize (const GValue *value)
+{
+  int i;
+  GValue s_val = { 0 };
+  GstValueTable *table;
+  char *s;
+
+  for(i=0;i<gst_value_table->len;i++){
+    table = &g_array_index(gst_value_table, GstValueTable, i);
+    if(table->type != G_VALUE_TYPE(value) ||
+        table->serialize == NULL) continue;
+
+    return table->serialize(value);
+  }
+
+  g_value_init (&s_val, G_TYPE_STRING);
+  g_value_transform (value, &s_val);
+  s = gst_string_wrap (g_value_get_string (&s_val));
+  g_value_unset (&s_val);
+
+  return s;
+}
+
+/**
+ * gst_value_deserialize:
+ *
+ */
+gboolean
+gst_value_deserialize (GValue *dest, const gchar *src)
+{
+  g_warning("unimplemented");
+  return FALSE;
+}
+
 void
 _gst_value_initialize (void)
 {
index 36aa89d..27d8eef 100644 (file)
@@ -129,6 +129,10 @@ void gst_value_register (const GstValueTable *table);
 void gst_value_init_and_copy (GValue *dest, const GValue *src);
 void _gst_value_initialize (void);
 
+gchar * gst_value_serialize (const GValue *value);
+gboolean gst_value_deserialize (GValue *dest, const gchar *src);
+
+
 G_END_DECLS
 
 #endif