aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@lanedo.com>2012-02-11 19:09:01 +0100
committerAleksander Morgado <aleksander@lanedo.com>2012-03-15 14:15:04 +0100
commitf0b9f3954c886f4f73b1c2a39628d5d124195546 (patch)
tree3f055d00a51f4b73cccd5b0acbb9f52bd66c7b92
parentb7938448ea125827a7e54f11d6b548d26653cc37 (diff)
libmm-common: use the new key/value parser in the common SMS properties builder
-rw-r--r--libmm-common/mm-common-sms-properties.c70
1 files changed, 28 insertions, 42 deletions
diff --git a/libmm-common/mm-common-sms-properties.c b/libmm-common/mm-common-sms-properties.c
index bb047019..ccdc8c54 100644
--- a/libmm-common/mm-common-sms-properties.c
+++ b/libmm-common/mm-common-sms-properties.c
@@ -226,58 +226,44 @@ consume_string (MMCommonSmsProperties *self,
return TRUE;
}
+typedef struct {
+ MMCommonSmsProperties *properties;
+ GError *error;
+} ParseKeyValueContext;
+
+static gboolean
+key_value_foreach (const gchar *key,
+ const gchar *value,
+ ParseKeyValueContext *ctx)
+{
+ return consume_string (ctx->properties,
+ key,
+ value,
+ &ctx->error);
+}
+
MMCommonSmsProperties *
mm_common_sms_properties_new_from_string (const gchar *str,
GError **error)
{
- GError *inner_error = NULL;
- MMCommonSmsProperties *properties;
- gchar **words;
- gchar *key;
- gchar *value;
- guint i;
-
- properties = mm_common_sms_properties_new ();
+ ParseKeyValueContext ctx;
- /* Expecting input as:
- * key1=string,key2=true,key3=false...
- * */
+ ctx.properties = mm_common_sms_properties_new ();
+ ctx.error = NULL;
- words = g_strsplit_set (str, ",= ", -1);
- if (!words)
- return properties;
-
- i = 0;
- key = words[i];
- while (key) {
- value = words[++i];
-
- if (!value) {
- inner_error = g_error_new (MM_CORE_ERROR,
- MM_CORE_ERROR_INVALID_ARGS,
- "Invalid properties string, no value for key '%s'",
- key);
- break;
- }
-
- if (!consume_string (properties,
- key,
- value,
- &inner_error))
- break;
-
- key = words[++i];
- }
+ mm_common_parse_key_value_string (str,
+ &ctx.error,
+ (MMParseKeyValueForeachFn)key_value_foreach,
+ &ctx);
/* If error, destroy the object */
- if (inner_error) {
- g_propagate_error (error, inner_error);
- g_object_unref (properties);
- properties = NULL;
+ if (ctx.error) {
+ g_propagate_error (error, ctx.error);
+ g_object_unref (ctx.properties);
+ ctx.properties = NULL;
}
- g_strfreev (words);
- return properties;
+ return ctx.properties;
}
/*****************************************************************************/