/*
- * Copyright (C) 2017-2020 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2017-2021 Andrea Zagli <azagli@libero.it>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
return ret;
}
+/*
+ * zak_form_element_array_split_string:
+ * @string: string to split.
+ *
+ * Returns: a #GHashTable with form_element_name=>value.
+ */
+GHashTable
+*zak_form_element_array_split_string (const gchar *string)
+{
+ GHashTable *ret;
+
+ gchar **splitted;
+ guint i;
+ gchar *field;
+ gchar **field_splitted;
+ guint l;
+
+ gchar *field_name;
+ gchar *field_value;
+
+ ret = g_hash_table_new (g_str_hash, g_str_equal);
+
+ splitted = g_strsplit (string, "},{", -1);
+
+ for (i = 0; i < g_strv_length (splitted); i++)
+ {
+ if (i == 0)
+ {
+ field = g_strdup (splitted[0] + 1);
+ }
+ else if (i == (g_strv_length (splitted) - 1))
+ {
+ field = g_strndup (splitted[i], strlen (splitted[i]) - 1);
+ }
+ else
+ {
+ field = g_strdup (splitted [i]);
+ }
+
+ field_name = NULL;
+ field_value = NULL;
+
+ field_splitted = g_strsplit_set (field, "{\",}", -1);
+ for (l = 0; l < g_strv_length (field_splitted); l++)
+ {
+ if (strlen (field_splitted[l]) > 0)
+ {
+ if (field_name == NULL)
+ {
+ field_name = g_strdup (field_splitted[l]);
+ }
+ else
+ {
+ field_value = g_strdup (field_splitted[l]);
+ break;
+ }
+ }
+ }
+
+ if (field_name != NULL)
+ {
+ if (field_value == NULL)
+ {
+ field_value = g_strdup ("");
+ }
+ g_hash_table_insert (ret, g_strdup (field_name), g_strdup (field_value));
+ }
+
+ g_free (field_name);
+ g_free (field_value);
+
+ g_strfreev (field_splitted);
+
+ g_free (field);
+ }
+
+ g_strfreev (splitted);
+
+ return ret;
+}
+
/* PRIVATE */
static void
zak_form_element_array_set_property (GObject *object,
static gboolean
zak_form_element_array_set_value (ZakFormElementArray *element, GValue *value)
{
- gchar **splitted;
- guint i;
- gchar *field;
- gchar **field_splitted;
- guint l;
+ GHashTable *ht;
+ GHashTableIter htiter;
- gchar *field_name;
- gchar *field_value;
+ gpointer field_name;
+ gpointer field_value;
ZakFormElement *form_element;
ZakFormElementArrayPrivate *priv = zak_form_element_array_get_instance_private (element);
- splitted = g_strsplit (g_value_get_string (value), "},{", -1);
+ ht = zak_form_element_array_split_string (g_value_get_string (value));
+ g_hash_table_iter_init (&htiter, ht);
- for (i = 0; i < g_strv_length (splitted); i++)
+ while (g_hash_table_iter_next (&htiter, &field_name, &field_value))
{
- if (i == 0)
- {
- field = g_strdup (splitted[0] + 1);
- }
- else if (i == (g_strv_length (splitted) - 1))
- {
- field = g_strndup (splitted[i], strlen (splitted[i]) - 1);
- }
- else
- {
- field = g_strdup (splitted [i]);
- }
-
- field_name = NULL;
- field_value = NULL;
-
- field_splitted = g_strsplit_set (field, "{\",}", -1);
- for (l = 0; l < g_strv_length (field_splitted); l++)
+ form_element = zak_form_element_array_get_element_by_id (ZAK_FORM_ELEMENT (element), (gchar *)field_name);
+ if (form_element != NULL)
{
- if (strlen (field_splitted[l]) > 0)
- {
- if (field_name == NULL)
- {
- field_name = g_strdup (field_splitted[l]);
- }
- else
- {
- field_value = g_strdup (field_splitted[l]);
- break;
- }
- }
+ zak_form_element_set_value (ZAK_FORM_ELEMENT (form_element), (gchar *)field_value);
}
-
- /* assign value to the right FormElement */
- if (field_name != NULL)
- {
- form_element = zak_form_element_array_get_element_by_id (ZAK_FORM_ELEMENT (element), field_name);
- if (form_element != NULL)
- {
- if (field_value == NULL)
- {
- field_value = g_strdup ("");
- }
- zak_form_element_set_value (ZAK_FORM_ELEMENT (form_element), field_value);
- }
- }
-
- g_free (field_name);
- g_free (field_value);
-
- g_strfreev (field_splitted);
-
- g_free (field);
}
- g_strfreev (splitted);
-
return TRUE;
}