From 8dde879752a9028df65b95b041864e21da62173f Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Mon, 24 Aug 2015 18:59:44 +0200 Subject: [PATCH] Form: saved element attributes as hash table. --- src/formelement.c | 45 ++++++++++++++++++++++++++++++++++++++++++- src/formelement.h | 2 +- src/formelementtext.c | 2 +- src/main.c | 2 ++ 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/formelement.c b/src/formelement.c index eb6d03c..647e587 100644 --- a/src/formelement.c +++ b/src/formelement.c @@ -50,7 +50,9 @@ static void zak_cgi_form_element_finalize (GObject *gobject); typedef struct _ZakCgiFormElementPrivate ZakCgiFormElementPrivate; struct _ZakCgiFormElementPrivate { + gchar *id; gchar *validation_regex; + GHashTable *ht_attrs; }; G_DEFINE_TYPE (ZakCgiFormElement, zak_cgi_form_element, G_TYPE_OBJECT) @@ -81,26 +83,67 @@ zak_cgi_form_element_init (ZakCgiFormElement *zak_cgi_form_element) ZakCgiFormElementPrivate *priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (zak_cgi_form_element); priv->validation_regex = NULL; + priv->ht_attrs = g_hash_table_new (g_str_hash, g_str_equal); } /** * zak_cgi_form_element_new: + * @id: * @validation_regex: + * @...: * * Returns: the newly created #ZakCgiFormElement object. */ ZakCgiFormElement -*zak_cgi_form_element_new (const gchar *validation_regex) +*zak_cgi_form_element_new (const gchar *id, + const gchar *validation_regex, + ...) { + va_list ap; + ZakCgiFormElement *zak_cgi_form_element; ZakCgiFormElementPrivate *priv; + gchar *attr; + gchar *attr_value; + + g_return_val_if_fail (id != NULL, NULL); + zak_cgi_form_element = ZAK_CGI_FORM_ELEMENT (g_object_new (zak_cgi_form_element_get_type (), NULL)); priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (zak_cgi_form_element); + priv->id = g_strdup (id); + zak_cgi_form_element_set_validation_regex (zak_cgi_form_element, validation_regex); + va_start (ap, validation_regex); + do + { + attr = va_arg (ap, gchar *); + if (attr != NULL) + { + attr_value = va_arg (ap, gchar *); + if (attr_value != NULL) + { + g_hash_table_insert (priv->ht_attrs, g_strdup (attr), g_strdup (attr_value)); + } + else + { + break; + } + } + else + { + break; + } + } while (TRUE); + + if (g_hash_table_lookup (priv->ht_attrs, "name") == NULL) + { + g_hash_table_insert (priv->ht_attrs, "name", g_strdup (id)); + } + return zak_cgi_form_element; } diff --git a/src/formelement.h b/src/formelement.h index ac4e18c..e2ec6ee 100644 --- a/src/formelement.h +++ b/src/formelement.h @@ -54,7 +54,7 @@ struct _ZakCgiFormElementClass GType zak_cgi_form_element_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_new (const gchar *validation_regex); +ZakCgiFormElement *zak_cgi_form_element_new (const gchar *id, const gchar *validation_regex, ...); void zak_cgi_form_element_set_validation_regex (ZakCgiFormElement *element, const gchar *validation_regex); gchar *zak_cgi_form_element_get_validation_regex (ZakCgiFormElement *element); diff --git a/src/formelementtext.c b/src/formelementtext.c index eacc3df..0476b27 100644 --- a/src/formelementtext.c +++ b/src/formelementtext.c @@ -46,7 +46,7 @@ struct _ZakCgiFormElementTextPrivate { }; -G_DEFINE_TYPE (ZakCgiFormElementText, zak_cgi_form_element_text, G_TYPE_OBJECT) +G_DEFINE_TYPE (ZakCgiFormElementText, zak_cgi_form_element_text, ZAK_CGI_TYPE_FORM_ELEMENT) static void zak_cgi_form_element_text_class_init (ZakCgiFormElementTextClass *class) diff --git a/src/main.c b/src/main.c index 0065548..8d64bd9 100644 --- a/src/main.c +++ b/src/main.c @@ -21,11 +21,13 @@ #endif #include +#include #include #include #include +#include #include #include "main.h" -- 2.49.0