From 207d2afd05796336bf4406d2063f6687391ec186 Mon Sep 17 00:00:00 2001 From: Cosimo Cecchi Date: Thu, 31 Jul 2008 16:16:36 +0200 Subject: [PATCH] Make serialize/unserialize funcs for GValue public. --- src/mm-string-utils.c | 172 +++++++++++++++++++++++++++++--------------------- src/mm-string-utils.h | 3 + 2 files changed, 102 insertions(+), 73 deletions(-) diff --git a/src/mm-string-utils.c b/src/mm-string-utils.c index 4b2c551..da0a9b2 100644 --- a/src/mm-string-utils.c +++ b/src/mm-string-utils.c @@ -120,37 +120,9 @@ serialize_value (xmlTextWriterPtr writer, GValue *v, GError **error) xmlChar *safe; int res; char *ret = NULL; - GType type = G_VALUE_TYPE (v); - /* guess the most used cases and handle those first */ - if (type == G_TYPE_STRING) { - ret = g_strdup (g_value_get_string (v)); - } else if (type == G_TYPE_BOOLEAN) { - ret = g_strdup_printf ("%d", g_value_get_boolean (v)); - } else if (type == G_TYPE_INT) { - ret = g_strdup_printf ("%d", g_value_get_int (v)); - } else if (type == G_TYPE_FLOAT) { - ret = g_strdup_printf ("%f", g_value_get_float (v)); - } else if (type == G_TYPE_DOUBLE) { - char double_buff[G_ASCII_DTOSTR_BUF_SIZE]; - ret = g_ascii_dtostr (double_buff, sizeof (double_buff), - g_value_get_double (v)); - } else if (type == G_TYPE_LONG) { - ret = g_strdup_printf ("%ld", g_value_get_long (v)); - } else if (type == G_TYPE_INT64) { - ret = g_strdup_printf ("%lld", g_value_get_int64 (v)); - } else if (type == G_TYPE_UINT) { - ret = g_strdup_printf ("%u", g_value_get_uint (v)); - } else if (type == G_TYPE_ULONG) { - ret = g_strdup_printf ("%lu", g_value_get_ulong (v)); - } else if (type == G_TYPE_UINT64) { - ret = g_strdup_printf ("%llu", g_value_get_uint64 (v)); - } else if (G_VALUE_HOLDS_CHAR (v)) { - ret = g_strdup_printf ("%c", g_value_get_char (v)); - } else if (G_VALUE_HOLDS_UCHAR (v)) { - ret = g_strdup_printf ("%c", g_value_get_uchar (v)); - } else { - g_warning ("Can't convert the value to string: unhandled type"); + ret = mm_gvalue_to_string (v); + if (!ret) { return; } @@ -331,6 +303,7 @@ unserialize_value (xmlTextReaderPtr reader, GValue *v, GError **error) int res; GType type; const char *val_string; + gboolean result; /* we should be on */ if (xmlStrcmp (xmlTextReaderConstName (reader), BAD_CAST ("value")) != 0) { @@ -350,49 +323,8 @@ unserialize_value (xmlTextReaderPtr reader, GValue *v, GError **error) type = G_VALUE_TYPE (v); val_string = (const char *) xmlTextReaderConstValue (reader); - if (type == G_TYPE_STRING) { - g_value_set_string (v, val_string); - } else if (type == G_TYPE_BOOLEAN) { - gboolean bval; - sscanf (val_string, "%d", &bval); - g_value_set_boolean (v, bval); - } else if (type == G_TYPE_INT) { - gint ival; - sscanf (val_string, "%d", &ival); - g_value_set_int (v, ival); - } else if (type == G_TYPE_FLOAT) { - gfloat fval; - sscanf (val_string, "%f", &fval); - g_value_set_float (v, fval); - } else if (type == G_TYPE_DOUBLE) { - gdouble dval; - dval = g_ascii_strtod (val_string, NULL); - g_value_set_double (v, dval); - } else if (type == G_TYPE_LONG) { - glong lval; - sscanf (val_string, "%ld", &lval); - g_value_set_long (v, lval); - } else if (type == G_TYPE_INT64) { - g_value_set_int64 (v, g_ascii_strtoll (val_string, NULL, 0)); - } else if (type == G_TYPE_UINT) { - guint uval; - sscanf (val_string, "%u", &uval); - g_value_set_uint (v, uval); - } else if (type == G_TYPE_ULONG) { - gulong ulval; - sscanf (val_string, "%lu", &ulval); - g_value_set_ulong (v, ulval); - } else if (type == G_TYPE_UINT64) { - g_value_set_uint64 (v, g_ascii_strtoull (val_string, NULL, 0)); - } else if (G_VALUE_HOLDS_CHAR (v)) { - gchar cval; - sscanf (val_string, "%c", &cval); - g_value_set_char (v, cval); - } else if (G_VALUE_HOLDS_UCHAR (v)) { - guchar ucval; - sscanf (val_string, "%c", &ucval); - g_value_set_uchar (v, ucval); - } else { + result = mm_gvalue_from_string (val_string, type, v); + if (!result) { g_set_error (error, MM_XML_ERROR_QUARK, MM_XML_ERROR_UNKNOWN_GTYPE, "Can't convert the string to a value: unhandled type"); return; @@ -877,3 +809,97 @@ out: return hc; } + +char * +mm_gvalue_to_string (GValue *v) +{ + char *ret = NULL; + GType type = G_VALUE_TYPE (v); + + /* guess the most used cases and handle those first */ + if (type == G_TYPE_STRING) { + ret = g_strdup (g_value_get_string (v)); + } else if (type == G_TYPE_BOOLEAN) { + ret = g_strdup_printf ("%d", g_value_get_boolean (v)); + } else if (type == G_TYPE_INT) { + ret = g_strdup_printf ("%d", g_value_get_int (v)); + } else if (type == G_TYPE_FLOAT) { + ret = g_strdup_printf ("%f", g_value_get_float (v)); + } else if (type == G_TYPE_DOUBLE) { + char double_buff[G_ASCII_DTOSTR_BUF_SIZE]; + ret = g_ascii_dtostr (double_buff, sizeof (double_buff), + g_value_get_double (v)); + } else if (type == G_TYPE_LONG) { + ret = g_strdup_printf ("%ld", g_value_get_long (v)); + } else if (type == G_TYPE_INT64) { + ret = g_strdup_printf ("%lld", g_value_get_int64 (v)); + } else if (type == G_TYPE_UINT) { + ret = g_strdup_printf ("%u", g_value_get_uint (v)); + } else if (type == G_TYPE_ULONG) { + ret = g_strdup_printf ("%lu", g_value_get_ulong (v)); + } else if (type == G_TYPE_UINT64) { + ret = g_strdup_printf ("%llu", g_value_get_uint64 (v)); + } else if (G_VALUE_HOLDS_CHAR (v)) { + ret = g_strdup_printf ("%c", g_value_get_char (v)); + } else if (G_VALUE_HOLDS_UCHAR (v)) { + ret = g_strdup_printf ("%c", g_value_get_uchar (v)); + } else { + g_warning ("Can't convert the value to string: unhandled type"); + } + + return ret; +} + +gboolean +mm_gvalue_from_string (const char *val_string, GType type, GValue *v) +{ + gboolean retval = TRUE; + + if (type == G_TYPE_STRING) { + g_value_set_string (v, val_string); + } else if (type == G_TYPE_BOOLEAN) { + gboolean bval; + sscanf (val_string, "%d", &bval); + g_value_set_boolean (v, bval); + } else if (type == G_TYPE_INT) { + gint ival; + sscanf (val_string, "%d", &ival); + g_value_set_int (v, ival); + } else if (type == G_TYPE_FLOAT) { + gfloat fval; + sscanf (val_string, "%f", &fval); + g_value_set_float (v, fval); + } else if (type == G_TYPE_DOUBLE) { + gdouble dval; + dval = g_ascii_strtod (val_string, NULL); + g_value_set_double (v, dval); + } else if (type == G_TYPE_LONG) { + glong lval; + sscanf (val_string, "%ld", &lval); + g_value_set_long (v, lval); + } else if (type == G_TYPE_INT64) { + g_value_set_int64 (v, g_ascii_strtoll (val_string, NULL, 0)); + } else if (type == G_TYPE_UINT) { + guint uval; + sscanf (val_string, "%u", &uval); + g_value_set_uint (v, uval); + } else if (type == G_TYPE_ULONG) { + gulong ulval; + sscanf (val_string, "%lu", &ulval); + g_value_set_ulong (v, ulval); + } else if (type == G_TYPE_UINT64) { + g_value_set_uint64 (v, g_ascii_strtoull (val_string, NULL, 0)); + } else if (G_VALUE_HOLDS_CHAR (v)) { + gchar cval; + sscanf (val_string, "%c", &cval); + g_value_set_char (v, cval); + } else if (G_VALUE_HOLDS_UCHAR (v)) { + guchar ucval; + sscanf (val_string, "%c", &ucval); + g_value_set_uchar (v, ucval); + } else { + retval = FALSE; + } + + return retval; +} diff --git a/src/mm-string-utils.h b/src/mm-string-utils.h index b019e3d..d1ef4e3 100644 --- a/src/mm-string-utils.h +++ b/src/mm-string-utils.h @@ -29,5 +29,8 @@ char * mm_filter_serialize (MMFilter *f, GError **error); MMFilter * mm_filter_unserialize (const char *serialized_filter, GError **error); char * mm_hit_collection_serialize (MMHitCollection *hc, GError **error); MMHitCollection * mm_hit_collection_unserialize (const char *serialized_coll, GError **error); +char * mm_gvalue_to_string (GValue *v); +gboolean mm_gvalue_from_string (const char *val_string, GType type, + GValue *v); #endif /* __MM_STRING_UTILS_H__ */ -- 2.11.4.GIT