]> saetta.ns0.it Git - zakform/libzakform/commitdiff
Added utility's function FormElementArray::split_string. develop
authorAndrea Zagli <azagli@libero.it>
Thu, 18 Mar 2021 20:36:32 +0000 (21:36 +0100)
committerAndrea Zagli <azagli@libero.it>
Thu, 18 Mar 2021 20:36:32 +0000 (21:36 +0100)
src/formelementarray.c
src/formelementarray.h

index 9f14e1e8659971e6a79a9a94659176551f871646..1a9683b7ecf61b47874d00c0b956eeb60c3db367 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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
@@ -269,6 +269,87 @@ ZakFormElement
        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,
@@ -362,80 +443,28 @@ static GValue
 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;
 }
 
index 7dac2225a6b3dbd2fa74975f8c605b38385d96f8..54073b4e0dacd20571d3c8668cecea59e37f9ac5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 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
@@ -63,6 +63,8 @@ GPtrArray *zak_form_element_array_get_elements (ZakFormElement *element);
 
 ZakFormElement *zak_form_element_array_get_element_by_id (ZakFormElement *element, const gchar *id);
 
+GHashTable *zak_form_element_array_split_string (const gchar *string);
+
 
 G_END_DECLS