]> saetta.ns0.it Git - libzakcgi/commitdiff
Implemented filtering on ZakCgiForm.
authorAndrea Zagli <azagli@libero.it>
Thu, 8 Oct 2015 20:49:00 +0000 (22:49 +0200)
committerAndrea Zagli <azagli@libero.it>
Thu, 8 Oct 2015 20:49:00 +0000 (22:49 +0200)
debugcgi.sh
src/form.c
src/formelement.c
src/formelement.h
src/formelementfiltertrim.c
src/formelementfiltertrim.h
src/formelementifilter.c
src/formelementifilter.h
src/formelementselect.c
src/libzakcgi.h
tests/form.c

index 29824edb77110803652ac6ef7a749e637584b9e1..17a20d3ba0d912782cfd79d43c0895e5e28f29be 100755 (executable)
@@ -1,4 +1,4 @@
 #!/bin/bash
 
-PID=`ps ax | grep $1 | grep -v grep | awk ' { print $1 }'`
-gdb --pid $PID --symbols=$2
+PID=`ps ax | grep $1 | awk '{ print $1 }'`
+su -c "gdb --pid $PID --symbols=$2"
index 3ce1bd806d9303af299d974e07bacdba8ec13c75..351fa503397e29c84f6083c003c6b142f3bba60a 100644 (file)
@@ -235,7 +235,7 @@ zak_cgi_form_is_valid (ZakCgiForm *zakcgiform)
 
        ret = TRUE;
 
-       for (i == 0; i < priv->ar_elems->len; i++)
+       for (i = 0; i < priv->ar_elems->len; i++)
                {
                        ZakCgiFormElement *element = (ZakCgiFormElement *)g_ptr_array_index (priv->ar_elems, i);
                        if (!ZAK_CGI_IS_FORM_ELEMENT_STRING (element))
@@ -243,7 +243,6 @@ zak_cgi_form_is_valid (ZakCgiForm *zakcgiform)
                                        if (!zak_cgi_form_element_is_valid (element))
                                                {
                                                        ret = FALSE;
-                                                       break;
                                                }
                                }
                }
index 977ac0dfed66fd95a58807930ad0a73b494a9ba1..ef9c37150733d5d676badc3ec536348bacc9fdcd 100644 (file)
@@ -62,6 +62,7 @@ struct _ZakCgiFormElementPrivate
                gchar *id;
                gchar *validation_regex;
                GHashTable *ht_attrs;
+               GPtrArray *pa_filters;
                GValue *value;
                GHashTable *ht_label_attrs;
        };
@@ -98,6 +99,7 @@ zak_cgi_form_element_init (ZakCgiFormElement *zak_cgi_form_element)
 
        priv->validation_regex = NULL;
        priv->ht_attrs = NULL;
+       priv->pa_filters = NULL;
        priv->value = NULL;
        priv->ht_label_attrs = NULL;
 }
@@ -154,6 +156,56 @@ gchar
        return ret;
 }
 
+/**
+ * zak_cgi_form_element_add_filter:
+ * @element:
+ * @filter:
+ *
+ */
+void
+zak_cgi_form_element_add_filter (ZakCgiFormElement *element, ZakCgiFormElementIFilter *filter)
+{
+       ZakCgiFormElementPrivate *priv;
+
+       priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element);
+
+       if (priv->pa_filters == NULL)
+               {
+                       priv->pa_filters = g_ptr_array_new ();
+               }
+
+       g_ptr_array_add (priv->pa_filters, filter);
+}
+
+/**
+ * zak_cgi_form_element_filter:
+ * @element:
+ *
+ */
+void
+zak_cgi_form_element_filter (ZakCgiFormElement *element)
+{
+       guint i;
+
+       ZakCgiFormElementPrivate *priv;
+
+       priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element);
+
+       if (priv->pa_filters == NULL)
+               {
+                       return;
+               }
+
+       for (i = 0; i < priv->pa_filters->len; i++)
+               {
+                       GValue *val;
+
+                       val = zak_cgi_form_element_ifilter_filter ((ZakCgiFormElementIFilter *)g_ptr_array_index (priv->pa_filters, i),
+                                                                                                          zak_cgi_form_element_get_value (element));
+                       zak_cgi_form_element_set_value (element, val);
+               }
+}
+
 /**
  * zak_cgi_form_element_set_value:
  * @element:
@@ -282,6 +334,7 @@ zak_cgi_form_element_is_valid (ZakCgiFormElement *element)
 
        if (ZAK_CGI_IS_FORM_ELEMENT (element) && ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->is_valid != NULL)
                {
+                       zak_cgi_form_element_filter (element);
                        ret = ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->is_valid (element);
                }
 
index 709d32c867191d2bfc39b6dfa7467ae6459d3446..a233fc5cf29df61dd825032a5c2cace857bd2bea 100644 (file)
@@ -23,6 +23,7 @@
 #include <glib-object.h>
 
 #include "main.h"
+#include "formelementifilter.h"
 
 
 G_BEGIN_DECLS
@@ -62,6 +63,9 @@ gchar *zak_cgi_form_element_get_id (ZakCgiFormElement *element);
 void zak_cgi_form_element_set_validation_regex (ZakCgiFormElement *element, const gchar *validation_regex);
 gchar *zak_cgi_form_element_get_validation_regex (ZakCgiFormElement *element);
 
+void zak_cgi_form_element_add_filter (ZakCgiFormElement *element, ZakCgiFormElementIFilter *filter);
+void zak_cgi_form_element_filter (ZakCgiFormElement *element);
+
 void zak_cgi_form_element_set_value (ZakCgiFormElement *element, GValue *value);
 GValue *zak_cgi_form_element_get_value (ZakCgiFormElement *element);
 
index d8928caec58f3768e6c1b4602341f4303e835777..d0799677d83d25e71997bbba93baad0b3ec7607b 100644 (file)
@@ -39,7 +39,7 @@ static void zak_cgi_form_element_filter_trim_get_property (GObject *object,
 static void zak_cgi_form_element_filter_trim_dispose (GObject *gobject);
 static void zak_cgi_form_element_filter_trim_finalize (GObject *gobject);
 
-static gchar *zak_cgi_form_element_filter_trim_filter (ZakCgiFormElementFilterTrim *filter_trim, const gchar *value);
+static GValue *zak_cgi_form_element_filter_trim_filter (ZakCgiFormElementIFilter *filter_trim, GValue *value);
 
 struct _ZakCgiFormElementFilterTrim
 {
@@ -85,6 +85,21 @@ zak_cgi_form_element_filter_trim_init (ZakCgiFormElementFilterTrim *zak_cgi_form
        ZakCgiFormElementFilterTrimPrivate *priv = ZAK_CGI_FORM_ELEMENT_FILTER_TRIM_GET_PRIVATE (zak_cgi_form_element);
 }
 
+/**
+ * zak_cgi_form_element_filter_trim_new:
+ *
+ * Returns: the newly created #ZakCgiFormElementFilterTrim object.
+ */
+ZakCgiFormElementFilterTrim
+*zak_cgi_form_element_filter_trim_new ()
+{
+       ZakCgiFormElementFilterTrim *zak_cgi_form_element_filter_trim;
+
+       zak_cgi_form_element_filter_trim = ZAK_CGI_FORM_ELEMENT_FILTER_TRIM (g_object_new (zak_cgi_form_element_filter_trim_get_type (), NULL));
+
+       return zak_cgi_form_element_filter_trim;
+}
+
 /* PRIVATE */
 static void
 zak_cgi_form_element_filter_trim_set_property (GObject *object,
@@ -144,16 +159,20 @@ zak_cgi_form_element_filter_trim_finalize (GObject *gobject)
        parent_class->finalize (gobject);
 }
 
-static gchar
-*zak_cgi_form_element_filter_trim_filter (ZakCgiFormElementFilterTrim *filter_trim,
-                                                                                 const gchar *value)
+static GValue
+*zak_cgi_form_element_filter_trim_filter (ZakCgiFormElementIFilter *filter_trim,
+                                                                                 GValue *value)
 {
-       gchar *ret;
+       GValue *ret;
+       gchar *_value;
 
        g_return_val_if_fail (value != NULL, g_strdup (""));
 
-       ret = g_strdup (value);
-       g_strstrip (ret);
+       _value = g_strdup (g_value_get_string (value));
+
+       ret = g_new0 (GValue, 1);
+       g_value_init (ret, G_TYPE_STRING);
+       g_value_set_string (ret, g_strdup (g_strstrip (_value)));
 
-       return ret;;
+       return ret;
 }
index e9a41525f76cd19366672d9dcfd4d30cabdb990f..7b84a0da9faa663504b2d1682b3cff26f3f45f00 100644 (file)
 G_BEGIN_DECLS
 
 
-/*
- * Type declaration.
- */
 #define ZAK_CGI_TYPE_FORM_ELEMENT_FILTER_TRIM zak_cgi_form_element_filter_trim_get_type ()
 G_DECLARE_FINAL_TYPE (ZakCgiFormElementFilterTrim, zak_cgi_form_element_filter_trim, ZAK_CGI, FORM_ELEMENT_FILTER_TRIM, GObject)
 
-/*
- * Method definitions.
- */
 ZakCgiFormElementFilterTrim *zak_cgi_form_element_filter_trim_new (void);
 
 
index 0948e412873936c73b5b1561cd6fe3c940524ff5..b518632671e8b1a2603205702b71001f4ea40584 100644 (file)
@@ -26,8 +26,8 @@ zak_cgi_form_element_ifilter_default_init (ZakCgiFormElementIFilterInterface *if
     /* add properties and signals to the interface here */
 }
 
-gchar
-*zak_cgi_form_element_ifilter_filter (ZakCgiFormElementIFilter *self, const gchar *value)
+GValue
+*zak_cgi_form_element_ifilter_filter (ZakCgiFormElementIFilter *self, GValue *value)
 {
        g_return_if_fail (ZAK_CGI_IS_FORM_ELEMENT_IFILTER (self));
 
index fcd4ef72387b5b678ce8ac8ddb182637cff476d9..fb8a09fa2ddb8a33e05172e66f3e5d9921956a80 100644 (file)
@@ -32,10 +32,10 @@ struct _ZakCgiFormElementIFilterInterface
 {
        GTypeInterface parent_iface;
 
-       gchar *(*filter) (ZakCgiFormElementIFilter *self, const gchar *value);
+       GValue *(*filter) (ZakCgiFormElementIFilter *self, GValue *value);
 };
 
-gchar *zak_cgi_form_element_ifilter_filter (ZakCgiFormElementIFilter *self, const gchar *value);
+GValue *zak_cgi_form_element_ifilter_filter (ZakCgiFormElementIFilter *self, GValue *value);
 
 
 G_END_DECLS
index 94dadcec68cc4756b59785d0a08f5cd0d088c390..4a6f955cbf77edeed78511c9d5add4d3c66a2d43 100644 (file)
@@ -209,7 +209,7 @@ zak_cgi_form_element_select_check_value (const gchar *validation_regex, GValue *
                                || error != NULL)
                                {
                                        syslog (LOG_MAKEPRI(LOG_SYSLOG, LOG_DEBUG), "Error on creating regex: %s.",
-                                                       error->message != NULL ? error->message : "no details");
+                                                       error != NULL && error->message != NULL ? error->message : "no details");
                                        return FALSE;
                                }
 
index d7d750c1f5bcd03149022a7b3f7278a7c42ed6c7..23c62bf08e4605c48d6ad0e7725480b3a2896b77 100644 (file)
@@ -21,6 +21,9 @@
 
 #include <libzakcgi/main.h>
 #include <libzakcgi/form.h>
+#include <libzakcgi/formelement.h>
+#include <libzakcgi/formelementifilter.h>
+#include <libzakcgi/formelementfiltertrim.h>
 #include <libzakcgi/session.h>
 #include <libzakcgi/tag.h>
 #include <libzakcgi/url.h>
index 3bb4a361bb2fb06a5e0a4459f1a30af9c146ba65..b436fcb164b71e5fc2d24e84d7ed8877cf0b24da 100644 (file)
@@ -16,6 +16,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#include <unistd.h>
+
 #include <form.h>
 #include <formelement.h>
 #include <formelementcheck.h>
@@ -25,6 +27,8 @@
 #include <formelementtext.h>
 #include <formelementtextarea.h>
 #include <formelementsubmit.h>
+#include <formelementifilter.h>
+#include <formelementfiltertrim.h>
 
 int
 main (int argc, char *argv[])
@@ -59,6 +63,8 @@ main (int argc, char *argv[])
 
        element = zak_cgi_form_element_text_new ("first", "aaa", NULL);
        zak_cgi_form_element_set_label (element, "The Label for first", NULL);
+       zak_cgi_form_element_add_filter (element,
+                                                                        ZAK_CGI_FORM_ELEMENT_IFILTER (zak_cgi_form_element_filter_trim_new ()));
        zak_cgi_form_add_element (form, element);
 
        element = zak_cgi_form_element_check_new ("chk", NULL, NULL);
@@ -89,6 +95,7 @@ main (int argc, char *argv[])
 
        if (zak_cgi_main_is_post (zakcgimain))
                {
+                       sleep(10);
                        /* validating the form */
                        zak_cgi_form_bind (form);
                        if (zak_cgi_form_is_valid (form))