From: Andrea Zagli Date: Thu, 8 Oct 2015 20:49:00 +0000 (+0200) Subject: Implemented filtering on ZakCgiForm. X-Git-Tag: v0.1.0~3^2~33 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=1805b30c1ce73998d0630fade4d82c693f9414c6;p=libzakcgi Implemented filtering on ZakCgiForm. --- diff --git a/debugcgi.sh b/debugcgi.sh index 29824ed..17a20d3 100755 --- a/debugcgi.sh +++ b/debugcgi.sh @@ -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" diff --git a/src/form.c b/src/form.c index 3ce1bd8..351fa50 100644 --- a/src/form.c +++ b/src/form.c @@ -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; } } } diff --git a/src/formelement.c b/src/formelement.c index 977ac0d..ef9c371 100644 --- a/src/formelement.c +++ b/src/formelement.c @@ -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); } diff --git a/src/formelement.h b/src/formelement.h index 709d32c..a233fc5 100644 --- a/src/formelement.h +++ b/src/formelement.h @@ -23,6 +23,7 @@ #include #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); diff --git a/src/formelementfiltertrim.c b/src/formelementfiltertrim.c index d8928ca..d079967 100644 --- a/src/formelementfiltertrim.c +++ b/src/formelementfiltertrim.c @@ -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; } diff --git a/src/formelementfiltertrim.h b/src/formelementfiltertrim.h index e9a4152..7b84a0d 100644 --- a/src/formelementfiltertrim.h +++ b/src/formelementfiltertrim.h @@ -26,15 +26,9 @@ 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); diff --git a/src/formelementifilter.c b/src/formelementifilter.c index 0948e41..b518632 100644 --- a/src/formelementifilter.c +++ b/src/formelementifilter.c @@ -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)); diff --git a/src/formelementifilter.h b/src/formelementifilter.h index fcd4ef7..fb8a09f 100644 --- a/src/formelementifilter.h +++ b/src/formelementifilter.h @@ -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 diff --git a/src/formelementselect.c b/src/formelementselect.c index 94dadce..4a6f955 100644 --- a/src/formelementselect.c +++ b/src/formelementselect.c @@ -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; } diff --git a/src/libzakcgi.h b/src/libzakcgi.h index d7d750c..23c62bf 100644 --- a/src/libzakcgi.h +++ b/src/libzakcgi.h @@ -21,6 +21,9 @@ #include #include +#include +#include +#include #include #include #include diff --git a/tests/form.c b/tests/form.c index 3bb4a36..b436fcb 100644 --- a/tests/form.c +++ b/tests/form.c @@ -16,6 +16,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include + #include #include #include @@ -25,6 +27,8 @@ #include #include #include +#include +#include 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))