From a8141c507b5ffa8b246315cdbc796ce02cffaf4e Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 30 Dec 2015 16:24:56 +0100 Subject: [PATCH] Added filter lower/upper/camel. --- src/Makefile.am | 2 + src/formelementfilterluc.c | 244 +++++++++++++++++++++++++++++++++++++ src/formelementfilterluc.h | 43 +++++++ src/libzakform.h | 2 + 4 files changed, 291 insertions(+) create mode 100644 src/formelementfilterluc.c create mode 100644 src/formelementfilterluc.h diff --git a/src/Makefile.am b/src/Makefile.am index 6922a3f..259e025 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,6 +11,7 @@ libzakform_la_SOURCES = \ form.c \ formelement.c \ formelementfilter.c \ + formelementfilterluc.c \ formelementfiltertrim.c \ formelementvalidator.c \ formelementvalidatordate.c \ @@ -25,6 +26,7 @@ libzakform_include_HEADERS = \ form.h \ formelement.h \ formelementfilter.h \ + formelementfilterluc.h \ formelementfiltertrim.h \ formelementvalidator.h \ formelementvalidatordate.h \ diff --git a/src/formelementfilterluc.c b/src/formelementfilterluc.c new file mode 100644 index 0000000..543043a --- /dev/null +++ b/src/formelementfilterluc.c @@ -0,0 +1,244 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * 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 +#endif + +#include + +#include + +#include "formelementfilter.h" +#include "formelementfilterluc.h" + +static void zak_form_element_filter_luc_class_init (ZakFormElementFilterLucClass *class); +static void zak_form_element_filter_luc_init (ZakFormElementFilterLuc *zak_form_element); + +static void zak_form_element_filter_luc_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void zak_form_element_filter_luc_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void zak_form_element_filter_luc_dispose (GObject *gobject); +static void zak_form_element_filter_luc_finalize (GObject *gobject); + +static gchar *zak_form_element_filter_luc_filter (ZakFormElementFilter *filter_luc, const gchar *value); + +enum + { + LOWER, + UPPER, + CAMEL + }; + +struct _ZakFormElementFilterLuc +{ + ZakFormElementFilter parent_instance; + + /* Other members, including private data. */ +}; + +#define ZAK_FORM_ELEMENT_FILTER_LUC_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_FORM_TYPE_ELEMENT_FILTER_LUC, ZakFormElementFilterLucPrivate)) + +typedef struct _ZakFormElementFilterLucPrivate ZakFormElementFilterLucPrivate; +struct _ZakFormElementFilterLucPrivate + { + guint type; + }; + +G_DEFINE_TYPE (ZakFormElementFilterLuc, zak_form_element_filter_luc, ZAK_FORM_TYPE_ELEMENT_FILTER) + +static void +zak_form_element_filter_luc_class_init (ZakFormElementFilterLucClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + ZakFormElementFilterClass *parent_class = ZAK_FORM_ELEMENT_FILTER_CLASS (class); + + object_class->set_property = zak_form_element_filter_luc_set_property; + object_class->get_property = zak_form_element_filter_luc_get_property; + object_class->dispose = zak_form_element_filter_luc_dispose; + object_class->finalize = zak_form_element_filter_luc_finalize; + + parent_class->filter = zak_form_element_filter_luc_filter; + + g_type_class_add_private (object_class, sizeof (ZakFormElementFilterLucPrivate)); +} + +static void +zak_form_element_filter_luc_init (ZakFormElementFilterLuc *zak_form_element) +{ + ZakFormElementFilterLucPrivate *priv = ZAK_FORM_ELEMENT_FILTER_LUC_GET_PRIVATE (zak_form_element); +} + +/** + * zak_form_element_filter_luc_new: + * + * Returns: the newly created #ZakFormElementFilterLuc object. + */ +ZakFormElementFilterLuc +*zak_form_element_filter_luc_new () +{ + ZakFormElementFilterLuc *zak_form_element_filter_luc; + + zak_form_element_filter_luc = ZAK_FORM_ELEMENT_FILTER_LUC (g_object_new (zak_form_element_filter_luc_get_type (), NULL)); + + return zak_form_element_filter_luc; +} + +/** + * zak_form_element_filter_luc_xml_parsing: + * @filter: + * @xnode: + * + */ +gboolean +zak_form_element_filter_luc_xml_parsing (ZakFormElementFilter *filter, xmlNode *xnode) +{ + gchar *content; + + ZakFormElementFilterLucPrivate *priv = ZAK_FORM_ELEMENT_FILTER_LUC_GET_PRIVATE (filter); + + content = (gchar *)xmlNodeGetContent (xnode); + if (g_strcmp0 (content, "lower") == 0) + { + priv->type = LOWER; + } + else if (g_strcmp0 (content, "upper") == 0) + { + priv->type = UPPER; + } + else if (g_strcmp0 (content, "camel") == 0) + { + priv->type = CAMEL; + } + + return TRUE; +} + +/* PRIVATE */ +static void +zak_form_element_filter_luc_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + ZakFormElementFilterLuc *zak_form_element = (ZakFormElementFilterLuc *)object; + ZakFormElementFilterLucPrivate *priv = ZAK_FORM_ELEMENT_FILTER_LUC_GET_PRIVATE (zak_form_element); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_element_filter_luc_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + ZakFormElementFilterLuc *zak_form_element = (ZakFormElementFilterLuc *)object; + ZakFormElementFilterLucPrivate *priv = ZAK_FORM_ELEMENT_FILTER_LUC_GET_PRIVATE (zak_form_element); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_element_filter_luc_dispose (GObject *gobject) +{ + ZakFormElementFilterLuc *zak_form_element = (ZakFormElementFilterLuc *)gobject; + ZakFormElementFilterLucPrivate *priv = ZAK_FORM_ELEMENT_FILTER_LUC_GET_PRIVATE (zak_form_element); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->dispose (gobject); +} + +static void +zak_form_element_filter_luc_finalize (GObject *gobject) +{ + ZakFormElementFilterLuc *zak_form_element = (ZakFormElementFilterLuc *)gobject; + ZakFormElementFilterLucPrivate *priv = ZAK_FORM_ELEMENT_FILTER_LUC_GET_PRIVATE (zak_form_element); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->finalize (gobject); +} + +static gchar +*zak_form_element_filter_luc_filter (ZakFormElementFilter *filter_luc, + const gchar *value) +{ + gchar *ret; + gchar *_value; + guint i; + guint l; + GString *str; + + ZakFormElementFilterLuc *zak_form_element = ZAK_FORM_ELEMENT_FILTER_LUC (filter_luc); + ZakFormElementFilterLucPrivate *priv = ZAK_FORM_ELEMENT_FILTER_LUC_GET_PRIVATE (zak_form_element); + + g_return_val_if_fail (value != NULL, g_strdup ("")); + + _value = g_strdup (value); + + l = strlen (_value); + switch (priv->type) + { + case LOWER: + for (i = 0; i < l; i++) + { + _value[i] = g_ascii_tolower (_value[i]); + } + break; + + case UPPER: + for (i = 0; i < l; i++) + { + _value[i] = g_ascii_toupper (_value[i]); + } + break; + + case CAMEL: + str = g_string_new (_value); + zak_utils_gstring_initial_capital (str); + g_free (_value); + _value = g_strdup (str->str); + g_string_free (str, TRUE); + break; + } + + ret = g_strdup (_value); + g_free (_value); + + return ret; +} diff --git a/src/formelementfilterluc.h b/src/formelementfilterluc.h new file mode 100644 index 0000000..eb00250 --- /dev/null +++ b/src/formelementfilterluc.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * 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_FORM_ELEMENT_FILTER_LUC_H__ +#define __ZAK_FORM_ELEMENT_FILTER_LUC_H__ + + +#include + +#include + +#include "formelementfilter.h" + + +G_BEGIN_DECLS + + +#define ZAK_FORM_TYPE_ELEMENT_FILTER_LUC zak_form_element_filter_luc_get_type () +G_DECLARE_FINAL_TYPE (ZakFormElementFilterLuc, zak_form_element_filter_luc, ZAK_FORM, ELEMENT_FILTER_LUC, ZakFormElementFilter) + +ZakFormElementFilterLuc *zak_form_element_filter_luc_new (void); +gboolean zak_form_element_filter_luc_xml_parsing (ZakFormElementFilter *filter, xmlNode *xnode); + + +G_END_DECLS + + +#endif /* __ZAK_FORM_ELEMENT_FILTER_LUC_H__ */ diff --git a/src/libzakform.h b/src/libzakform.h index 03753c2..8274501 100644 --- a/src/libzakform.h +++ b/src/libzakform.h @@ -25,9 +25,11 @@ #include #include +#include #include #include +#include #include #include -- 2.49.0