From 1ac74334daf980762167da0dae9d765c04f81a83 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 26 Aug 2015 18:21:48 +0200 Subject: [PATCH] Added ZakCgiForm::add_str. --- src/form.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- src/form.h | 1 + tests/form.c | 2 ++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/form.c b/src/form.c index b860475..da81760 100644 --- a/src/form.c +++ b/src/form.c @@ -47,6 +47,7 @@ struct _ZakCgiFormPrivate { ZakCgiMain *zakcgimain; GHashTable *ht_attrs; + guint elems; GHashTable *ht_elems; }; @@ -72,6 +73,7 @@ zak_cgi_form_init (ZakCgiForm *zak_cgi_form) priv->zakcgimain = NULL; priv->ht_attrs = NULL; + priv->elems = 0; priv->ht_elems = g_hash_table_new (g_str_hash, g_str_equal); } @@ -127,6 +129,7 @@ zak_cgi_form_add_element (ZakCgiForm *zakcgiform, ZakCgiFormElement *element) } else { + priv->elems++; g_hash_table_insert (priv->ht_elems, g_strdup (id), g_object_ref (element)); ret = TRUE; } @@ -136,6 +139,42 @@ zak_cgi_form_add_element (ZakCgiForm *zakcgiform, ZakCgiFormElement *element) return ret; } +/** + * zak_cgi_form_add_str: + * @zakcgiform: + * @str: + * + * Returns: #TRUE if @str is added; FALSE otherwise. + */ +gboolean +zak_cgi_form_add_str (ZakCgiForm *zakcgiform, const gchar *str) +{ + gboolean ret; + gchar *id; + + ZakCgiFormPrivate *priv; + + priv = ZAK_CGI_FORM_GET_PRIVATE (zakcgiform); + + id = g_strdup_printf ("{id_%d}", priv->elems++); + + if (g_hash_table_lookup (priv->ht_elems, id)) + { + priv->elems--; + g_warning ("You cannot add an element with id already present in the form."); + ret = FALSE; + } + else + { + g_hash_table_insert (priv->ht_elems, g_strdup (id), g_strdup (str)); + ret = TRUE; + } + + g_free (id); + + return ret; +} + /** * zak_cgi_form_render_start: * @zakcgiform: @@ -194,9 +233,16 @@ gchar g_hash_table_iter_init (&iter, priv->ht_elems); while (g_hash_table_iter_next (&iter, &key, &value)) { - tmp = zak_cgi_form_element_render ((ZakCgiFormElement *)value); - g_string_append_printf (str, "\n%s", tmp); - g_free (tmp); + if (g_str_has_prefix ((gchar *)key, "{id_")) + { + g_string_append_printf (str, "\n%s", (gchar *)value); + } + else + { + tmp = zak_cgi_form_element_render ((ZakCgiFormElement *)value); + g_string_append_printf (str, "\n%s", tmp); + g_free (tmp); + } } g_string_append (str, "\n"); diff --git a/src/form.h b/src/form.h index f14b14d..366afdb 100644 --- a/src/form.h +++ b/src/form.h @@ -55,6 +55,7 @@ GType zak_cgi_form_get_type (void); ZakCgiForm *zak_cgi_form_new (ZakCgiMain *zakcgimain, ...); gboolean zak_cgi_form_add_element (ZakCgiForm *zakcgiform, ZakCgiFormElement *element); +gboolean zak_cgi_form_add_str (ZakCgiForm *zakcgiform, const gchar *str); gchar *zak_cgi_form_render_start (ZakCgiForm *zakcgiform); gchar *zak_cgi_form_render (ZakCgiForm *zakcgiform); diff --git a/tests/form.c b/tests/form.c index ed3a85c..e791862 100644 --- a/tests/form.c +++ b/tests/form.c @@ -38,6 +38,8 @@ main (int argc, char *argv[]) element = zak_cgi_form_element_text_new ("first", "aaa", NULL); zak_cgi_form_add_element (form, element); + zak_cgi_form_add_str (form, "

big big big

"); + element = zak_cgi_form_element_submit_new ("submit", "aaa", NULL); zak_cgi_form_add_element (form, element); -- 2.49.0