]> saetta.ns0.it Git - libzakcgi/commitdiff
In ZakCgiForm replace internal GHashTable with GPtrArray.
authorAndrea Zagli <azagli@libero.it>
Mon, 14 Sep 2015 16:47:12 +0000 (18:47 +0200)
committerAndrea Zagli <azagli@libero.it>
Mon, 14 Sep 2015 16:51:17 +0000 (18:51 +0200)
Because GHashTable doesn't respect the inserting order.

src/Makefile.am
src/form.c
src/formelementstring.c [new file with mode: 0644]
src/formelementstring.h [new file with mode: 0644]
src/formelementsubmit.c

index 49d9a1c04ccd7defd5cee8f2f7d1c05206458255..62477160ab78d3ab3893a235f366fa9e4134c2bf 100644 (file)
@@ -13,6 +13,7 @@ libzakcgi_la_SOURCES = commons.c \
                        formelementhidden.c \
                        formelementpassword.c \
                        formelementselect.c \
+                       formelementstring.c \
                        formelementtext.c \
                        formelementtextarea.c \
                        formelementsubmit.c \
@@ -31,6 +32,7 @@ libzakcgi_include_HEADERS = \
                             formelementhidden.h \
                             formelementpassword.h \
                             formelementselect.h \
+                            formelementstring.h \
                             formelementtext.h \
                             formelementtextarea.h \
                             formelementsubmit.h \
index b061b03465dee550c53f2dc1d039c18f544cb327..3ce1bd806d9303af299d974e07bacdba8ec13c75 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "commons.h"
 #include "form.h"
+#include "formelementstring.h"
 
 static void zak_cgi_form_class_init (ZakCgiFormClass *class);
 static void zak_cgi_form_init (ZakCgiForm *zak_cgi_form);
@@ -47,8 +48,7 @@ struct _ZakCgiFormPrivate
        {
                ZakCgiMain *zakcgimain;
                GHashTable *ht_attrs;
-               guint elems;
-               GHashTable *ht_elems;
+               GPtrArray *ar_elems;
        };
 
 G_DEFINE_TYPE (ZakCgiForm, zak_cgi_form, G_TYPE_OBJECT)
@@ -73,8 +73,7 @@ zak_cgi_form_init (ZakCgiForm *zak_cgi_form)
 
        priv->zakcgimain = NULL;
        priv->ht_attrs = NULL;
-       priv->elems = 0;
-       priv->ht_elems = g_hash_table_new (g_str_hash, g_str_equal);
+       priv->ar_elems = g_ptr_array_new ();
 }
 
 /**
@@ -103,6 +102,33 @@ ZakCgiForm
        return zak_cgi_form;
 }
 
+static guint
+get_idx (ZakCgiForm *zakcgiform, const gchar *id)
+{
+       guint idx;
+       gchar *element_id;
+
+       ZakCgiFormPrivate *priv;
+
+       priv = ZAK_CGI_FORM_GET_PRIVATE (zakcgiform);
+
+       for (idx = 0; idx < priv->ar_elems->len; idx++)
+               {
+                       element_id = zak_cgi_form_element_get_id ((ZakCgiFormElement *)g_ptr_array_index (priv->ar_elems, idx));
+                       if (g_strcmp0 (element_id, id) == 0)
+                               {
+                                       break;
+                               }
+                       g_free (element_id);
+               }
+       if (idx == priv->ar_elems->len)
+               {
+                       idx = -1;
+               }
+
+       return idx;
+}
+
 /**
  * zak_cgi_form_add_element:
  * @zakcgiform:
@@ -122,15 +148,14 @@ zak_cgi_form_add_element (ZakCgiForm *zakcgiform, ZakCgiFormElement *element)
 
        id = zak_cgi_form_element_get_id (element);
 
-       if (g_hash_table_lookup (priv->ht_elems, id))
+       if (get_idx (zakcgiform, id) > -1)
                {
                        g_warning ("You cannot add an element with id already present in the form.");
                        ret = FALSE;
                }
        else
                {
-                       priv->elems++;
-                       g_hash_table_insert (priv->ht_elems, g_strdup (id), g_object_ref (element));
+                       g_ptr_array_add (priv->ar_elems, g_object_ref (element));
                        ret = TRUE;
                }
 
@@ -150,27 +175,11 @@ gboolean
 zak_cgi_form_add_str (ZakCgiForm *zakcgiform, const gchar *str)
 {
        gboolean ret;
-       gchar *id;
 
-       ZakCgiFormPrivate *priv;
+       ZakCgiFormElement *element;
 
-       priv = ZAK_CGI_FORM_GET_PRIVATE (zakcgiform);
-
-       id = g_strdup_printf ("{id_%d}", priv->elems++);
-
-       if (g_hash_table_lookup (priv->ht_elems, id))
-               {
-                       priv->elems--;
-                       g_warning ("You cannot add an element with id already present in the form.");
-                       ret = FALSE;
-               }
-       else
-               {
-                       g_hash_table_insert (priv->ht_elems, g_strdup (id), g_strdup (str));
-                       ret = TRUE;
-               }
-
-       g_free (id);
+       element = zak_cgi_form_element_string_new (str);
+       ret = zak_cgi_form_add_element (zakcgiform, element);
 
        return ret;
 }
@@ -183,9 +192,7 @@ zak_cgi_form_add_str (ZakCgiForm *zakcgiform, const gchar *str)
 void
 zak_cgi_form_bind (ZakCgiForm *zakcgiform)
 {
-       GHashTableIter iter;
-       gpointer key;
-       gpointer value;
+       guint i;
 
        GValue *gval;
 
@@ -195,15 +202,15 @@ zak_cgi_form_bind (ZakCgiForm *zakcgiform)
 
        priv = ZAK_CGI_FORM_GET_PRIVATE (zakcgiform);
 
-       g_hash_table_iter_init (&iter, priv->ht_elems);
-       while (g_hash_table_iter_next (&iter, &key, &value))
+       for (i == 0; i < priv->ar_elems->len; i++)
                {
-                       if (!g_str_has_prefix ((gchar *)key, "{id_"))
+                       ZakCgiFormElement *element = (ZakCgiFormElement *)g_ptr_array_index (priv->ar_elems, i);
+                       if (!ZAK_CGI_IS_FORM_ELEMENT_STRING (element))
                                {
-                                       gval = zak_cgi_main_get_stdin_field (priv->zakcgimain, (gchar *)key);
+                                       gval = zak_cgi_main_get_stdin_field (priv->zakcgimain, zak_cgi_form_element_get_id (element));
                                        if (gval != NULL)
                                                {
-                                                       zak_cgi_form_element_set_value ((ZakCgiFormElement *)value, gval);
+                                                       zak_cgi_form_element_set_value (element, gval);
                                                }
                                }
                }
@@ -218,9 +225,7 @@ zak_cgi_form_bind (ZakCgiForm *zakcgiform)
 gboolean
 zak_cgi_form_is_valid (ZakCgiForm *zakcgiform)
 {
-       GHashTableIter iter;
-       gpointer key;
-       gpointer value;
+       guint i;
 
        gboolean ret;
 
@@ -230,16 +235,16 @@ zak_cgi_form_is_valid (ZakCgiForm *zakcgiform)
 
        ret = TRUE;
 
-       g_hash_table_iter_init (&iter, priv->ht_elems);
-       while (g_hash_table_iter_next (&iter, &key, &value))
+       for (i == 0; i < priv->ar_elems->len; i++)
                {
-                       if (!g_str_has_prefix ((gchar *)key, "{id_"))
+                       ZakCgiFormElement *element = (ZakCgiFormElement *)g_ptr_array_index (priv->ar_elems, i);
+                       if (!ZAK_CGI_IS_FORM_ELEMENT_STRING (element))
                                {
-                                       if (!zak_cgi_form_element_is_valid ((ZakCgiFormElement *)value))
+                                       if (!zak_cgi_form_element_is_valid (element))
                                                {
                                                        ret = FALSE;
                                                        break;
-                                               };
+                                               }
                                }
                }
 
@@ -287,9 +292,7 @@ gchar
 {
        GString *str;
 
-       GHashTableIter iter;
-       gpointer key;
-       gpointer value;
+       guint i;
 
        gchar *tmp;
 
@@ -303,19 +306,12 @@ gchar
        g_string_append (str, tmp);
        g_free (tmp);
 
-       g_hash_table_iter_init (&iter, priv->ht_elems);
-       while (g_hash_table_iter_next (&iter, &key, &value))
+       for (i == 0; i < priv->ar_elems->len; i++)
                {
-                       if (g_str_has_prefix ((gchar *)key, "{id_"))
-                               {
-                                       g_string_append_printf (str, "\n%s", (gchar *)value);
-                               }
-                       else
-                               {
-                                       tmp = zak_cgi_form_element_render ((ZakCgiFormElement *)value);
-                                       g_string_append_printf (str, "\n%s", tmp);
-                                       g_free (tmp);
-                               }
+                       ZakCgiFormElement *element = (ZakCgiFormElement *)g_ptr_array_index (priv->ar_elems, i);
+                       tmp = zak_cgi_form_element_render (element);
+                       g_string_append_printf (str, "\n%s", tmp);
+                       g_free (tmp);
                }
 
        g_string_append (str, "\n</form>");
diff --git a/src/formelementstring.c b/src/formelementstring.c
new file mode 100644 (file)
index 0000000..59fb663
--- /dev/null
@@ -0,0 +1,184 @@
+/*
+ * Copyright (C) 2015 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
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifdef HAVE_CONFIG_H
+       #include <config.h>
+#endif
+
+#include <syslog.h>
+
+#include "commons.h"
+#include "tag.h"
+#include "formelementstring.h"
+
+static void zak_cgi_form_element_string_class_init (ZakCgiFormElementStringClass *class);
+static void zak_cgi_form_element_string_init (ZakCgiFormElementString *zak_cgi_form_element_string);
+
+static gchar *zak_cgi_form_element_string_render (ZakCgiFormElement *element);
+
+static gboolean zak_cgi_form_element_string_is_valid (ZakCgiFormElement *element);
+
+static void zak_cgi_form_element_string_set_property (GObject *object,
+                               guint property_id,
+                               const GValue *value,
+                               GParamSpec *pspec);
+static void zak_cgi_form_element_string_get_property (GObject *object,
+                               guint property_id,
+                               GValue *value,
+                               GParamSpec *pspec);
+
+static void zak_cgi_form_element_string_dispose (GObject *gobject);
+static void zak_cgi_form_element_string_finalize (GObject *gobject);
+
+#define ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_STRING, ZakCgiFormElementStringPrivate))
+
+typedef struct _ZakCgiFormElementStringPrivate ZakCgiFormElementStringPrivate;
+struct _ZakCgiFormElementStringPrivate
+       {
+               gchar *str;
+       };
+
+G_DEFINE_TYPE (ZakCgiFormElementString, zak_cgi_form_element_string, ZAK_CGI_TYPE_FORM_ELEMENT)
+
+static void
+zak_cgi_form_element_string_class_init (ZakCgiFormElementStringClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       ZakCgiFormElementClass *elem_class = ZAK_CGI_FORM_ELEMENT_CLASS (klass);
+
+       object_class->set_property = zak_cgi_form_element_string_set_property;
+       object_class->get_property = zak_cgi_form_element_string_get_property;
+       object_class->dispose = zak_cgi_form_element_string_dispose;
+       object_class->finalize = zak_cgi_form_element_string_finalize;
+
+       elem_class->render = zak_cgi_form_element_string_render;
+       elem_class->is_valid = zak_cgi_form_element_string_is_valid;
+
+       g_type_class_add_private (object_class, sizeof (ZakCgiFormElementStringPrivate));
+}
+
+static void
+zak_cgi_form_element_string_init (ZakCgiFormElementString *zak_cgi_form_element_string)
+{
+       ZakCgiFormElementStringPrivate *priv = ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE (zak_cgi_form_element_string);
+
+       priv->str = NULL;
+}
+
+/**
+ * zak_cgi_form_element_string_new:
+ * @str:
+ *
+ * Returns: the newly created #ZakCgiFormElementString object.
+ */
+ZakCgiFormElement
+*zak_cgi_form_element_string_new (const gchar *str)
+{
+       ZakCgiFormElementString *zak_cgi_form_element_string;
+
+       zak_cgi_form_element_string = ZAK_CGI_FORM_ELEMENT_STRING (g_object_new (zak_cgi_form_element_string_get_type (), NULL));
+
+       ZakCgiFormElementStringPrivate *priv = ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE (zak_cgi_form_element_string);
+
+       priv->str = g_strdup (str);
+
+       return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_string);
+}
+
+static gchar
+*zak_cgi_form_element_string_render (ZakCgiFormElement *element)
+{
+       gchar *ret;
+
+       ZakCgiFormElementStringPrivate *priv = ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE (element);
+
+       ret = g_strdup (priv->str);
+
+       return ret;
+}
+
+static gboolean
+zak_cgi_form_element_string_check_value (const gchar *validation_regex, GValue *value)
+{
+       return TRUE;
+}
+
+static gboolean
+zak_cgi_form_element_string_is_valid (ZakCgiFormElement *element)
+{
+       return TRUE;
+}
+
+/* PRIVATE */
+static void
+zak_cgi_form_element_string_set_property (GObject *object,
+                   guint property_id,
+                   const GValue *value,
+                   GParamSpec *pspec)
+{
+       ZakCgiFormElementString *zak_cgi_form_element_string = (ZakCgiFormElementString *)object;
+       ZakCgiFormElementStringPrivate *priv = ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE (zak_cgi_form_element_string);
+
+       switch (property_id)
+               {
+                       default:
+                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                               break;
+               }
+}
+
+static void
+zak_cgi_form_element_string_get_property (GObject *object,
+                   guint property_id,
+                   GValue *value,
+                   GParamSpec *pspec)
+{
+       ZakCgiFormElementString *zak_cgi_form_element_string = (ZakCgiFormElementString *)object;
+       ZakCgiFormElementStringPrivate *priv = ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE (zak_cgi_form_element_string);
+
+       switch (property_id)
+               {
+                       default:
+                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                               break;
+               }
+}
+
+static void
+zak_cgi_form_element_string_dispose (GObject *gobject)
+{
+       ZakCgiFormElementString *zak_cgi_form_element_string = (ZakCgiFormElementString *)gobject;
+       ZakCgiFormElementStringPrivate *priv = ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE (zak_cgi_form_element_string);
+
+
+
+       GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject));
+       parent_class->dispose (gobject);
+}
+
+static void
+zak_cgi_form_element_string_finalize (GObject *gobject)
+{
+       ZakCgiFormElementString *zak_cgi_form_element_string = (ZakCgiFormElementString *)gobject;
+       ZakCgiFormElementStringPrivate *priv = ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE (zak_cgi_form_element_string);
+
+
+
+       GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject));
+       parent_class->finalize (gobject);
+}
diff --git a/src/formelementstring.h b/src/formelementstring.h
new file mode 100644 (file)
index 0000000..a9db7c3
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2015 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
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef __ZAK_CGI_FORM_ELEMENT_STRING_H__
+#define __ZAK_CGI_FORM_ELEMENT_STRING_H__
+
+
+#include <glib-object.h>
+
+#include "formelement.h"
+
+
+G_BEGIN_DECLS
+
+
+#define ZAK_CGI_TYPE_FORM_ELEMENT_STRING                 (zak_cgi_form_element_string_get_type ())
+#define ZAK_CGI_FORM_ELEMENT_STRING(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_STRING, ZakCgiFormElementString))
+#define ZAK_CGI_FORM_ELEMENT_STRING_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), ZAK_CGI_TYPE_FORM_ELEMENT_STRING, ZakCgiFormElementStringClass))
+#define ZAK_CGI_IS_FORM_ELEMENT_STRING(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_STRING))
+#define ZAK_CGI_IS_FORM_ELEMENT_STRING_CLASS(klass)      (G_TYPE_CHECK_CLASS_TYPE ((klass), ZAK_CGI_TYPE_FORM_ELEMENT_STRING))
+#define ZAK_CGI_FORM_ELEMENT_STRING_GET_CLASS(obj)       (G_TYPE_INSTANCE_GET_CLASS ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_STRING, ZakCgiFormElementStringClass))
+
+typedef struct _ZakCgiFormElementString ZakCgiFormElementString;
+typedef struct _ZakCgiFormElementStringClass ZakCgiFormElementStringClass;
+
+struct _ZakCgiFormElementString
+       {
+               ZakCgiFormElement parent_instance;
+       };
+
+struct _ZakCgiFormElementStringClass
+       {
+               ZakCgiFormElementClass parent_class;
+       };
+
+
+GType zak_cgi_form_element_string_get_type (void);
+
+ZakCgiFormElement *zak_cgi_form_element_string_new (const gchar *str);
+
+
+G_END_DECLS
+
+
+#endif /* __ZAK_CGI_FORM_ELEMENT_STRING_H__ */
index 1bcbcdf4166ac0dac1a048a365ef28a95777438f..44dca469c9ad068e9ec0276184dc1681330c7a46 100644 (file)
@@ -134,7 +134,6 @@ static gchar
                                        /* no form-control for submit button */
                                        g_string_erase (str_attr_class, form_control - str_attr_class->str, 12);
                                }
-                       g_string_free (str_attr_class, TRUE);
 
                        if (g_strstr_len (attr_class, -1, "btn btn-default") == NULL)
                                {
@@ -142,6 +141,7 @@ static gchar
                                }
 
                        g_free (attr_class);
+                       g_string_free (str_attr_class, TRUE);
                }
        else
                {