From: Andrea Zagli Date: Wed, 26 Aug 2015 15:33:08 +0000 (+0200) Subject: Form: rendering of attributes. X-Git-Tag: v0.1.0~3^2~49 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=0d0c75999e7803ff0329e83f874a8a8a6ce526eb;p=libzakcgi Form: rendering of attributes. Added ZakCgiCommons::ghashtable_to_str_attrs. --- diff --git a/src/commons.c b/src/commons.c index 00018d0..93c5075 100644 --- a/src/commons.c +++ b/src/commons.c @@ -57,3 +57,31 @@ GHashTable return ret; } + +gchar +*zak_cgi_commons_ghashtable_to_str_attrs (GHashTable *ht) +{ + GHashTableIter iter; + gpointer key; + gpointer value; + + GString *str; + gchar *ret; + + g_return_val_if_fail (ht != NULL, ""); + + str = g_string_new (""); + + g_hash_table_iter_init (&iter, ht); + while (g_hash_table_iter_next (&iter, &key, &value)) + { + g_string_append_printf (str, + " \"%s\"=\"%s\"", + (gchar *)key, + (gchar *)value); + } + + ret = g_strdup (str->str); + + return ret; +} diff --git a/src/commons.h b/src/commons.h index 721cf7d..9d214fd 100644 --- a/src/commons.h +++ b/src/commons.h @@ -27,5 +27,7 @@ GHashTable *zak_cgi_commons_valist_to_ghashtable (va_list ap); +gchar *zak_cgi_commons_ghashtable_to_str_attrs (GHashTable *ht); + #endif /* __ZAK_CGI_COMMONS_H__ */ diff --git a/src/form.c b/src/form.c index 7b1862a..0e154fd 100644 --- a/src/form.c +++ b/src/form.c @@ -22,6 +22,7 @@ #include +#include "commons.h" #include "form.h" static void zak_cgi_form_class_init (ZakCgiFormClass *class); @@ -45,6 +46,7 @@ typedef struct _ZakCgiFormPrivate ZakCgiFormPrivate; struct _ZakCgiFormPrivate { ZakCgiMain *zakcgimain; + GHashTable *ht_attrs; GHashTable *ht_elems; }; @@ -69,26 +71,33 @@ zak_cgi_form_init (ZakCgiForm *zak_cgi_form) ZakCgiFormPrivate *priv = ZAK_CGI_FORM_GET_PRIVATE (zak_cgi_form); priv->zakcgimain = NULL; + priv->ht_attrs = NULL; priv->ht_elems = g_hash_table_new (g_str_hash, g_str_equal); } /** * zak_cgi_form_new: * @zakcgimain: + * @...: * * Returns: the newly created #ZakCgiForm object. */ ZakCgiForm -*zak_cgi_form_new (ZakCgiMain *zakcgimain) +*zak_cgi_form_new (ZakCgiMain *zakcgimain, ...) { ZakCgiForm *zak_cgi_form; ZakCgiFormPrivate *priv; + va_list ap; + zak_cgi_form = ZAK_CGI_FORM (g_object_new (zak_cgi_form_get_type (), NULL)); priv = ZAK_CGI_FORM_GET_PRIVATE (zak_cgi_form); priv->zakcgimain = zakcgimain; + va_start (ap, zakcgimain); + priv->ht_attrs = zak_cgi_commons_valist_to_ghashtable (ap); + return zak_cgi_form; } @@ -142,19 +151,27 @@ gchar gpointer key; gpointer value; + gchar *tmp; + ZakCgiFormPrivate *priv; priv = ZAK_CGI_FORM_GET_PRIVATE (zakcgiform); - str = g_string_new ("
"); + str = g_string_new ("ht_attrs); + g_string_append_printf (str, "%s>", tmp); + g_free (tmp); g_hash_table_iter_init (&iter, priv->ht_elems); while (g_hash_table_iter_next (&iter, &key, &value)) { - g_string_append_printf (str, "\n%s", zak_cgi_form_element_render ((ZakCgiFormElement *)value)); + tmp = zak_cgi_form_element_render ((ZakCgiFormElement *)value); + g_string_append_printf (str, "\n%s", tmp); + g_free (tmp); } - g_string_append (str, ""); + g_string_append (str, "\n"); return g_strdup (str->str); } diff --git a/src/form.h b/src/form.h index 1595294..107a1c0 100644 --- a/src/form.h +++ b/src/form.h @@ -52,7 +52,7 @@ struct _ZakCgiFormClass GType zak_cgi_form_get_type (void); -ZakCgiForm *zak_cgi_form_new (ZakCgiMain *zakcgimain); +ZakCgiForm *zak_cgi_form_new (ZakCgiMain *zakcgimain, ...); gboolean zak_cgi_form_add_element (ZakCgiForm *zakcgiform, ZakCgiFormElement *element); diff --git a/src/formelementtext.c b/src/formelementtext.c index b376ad7..1d8b0ee 100644 --- a/src/formelementtext.c +++ b/src/formelementtext.c @@ -102,7 +102,8 @@ ZakCgiFormElement ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_text_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text), id, - validation_regex, zak_cgi_commons_valist_to_ghashtable (ap)); + validation_regex, + zak_cgi_commons_valist_to_ghashtable (ap)); return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text); } diff --git a/tests/form.c b/tests/form.c index 18d6dde..9e1f484 100644 --- a/tests/form.c +++ b/tests/form.c @@ -28,7 +28,9 @@ main (int argc, char *argv[]) ZakCgiForm *form; ZakCgiFormElement *element; - form = zak_cgi_form_new (NULL); + form = zak_cgi_form_new (NULL, + "method", "post", + NULL); str = g_string_new ("");