]> saetta.ns0.it Git - libzakcgi/commitdiff
Form: rendering of attributes.
authorAndrea Zagli <azagli@libero.it>
Wed, 26 Aug 2015 15:33:08 +0000 (17:33 +0200)
committerAndrea Zagli <azagli@libero.it>
Wed, 26 Aug 2015 15:33:08 +0000 (17:33 +0200)
Added ZakCgiCommons::ghashtable_to_str_attrs.

src/commons.c
src/commons.h
src/form.c
src/form.h
src/formelementtext.c
tests/form.c

index 00018d01be5fe9a01be72bbff7387bbb8cd8dc11..93c5075571b5dc464e15720b7ca6541129d88666 100644 (file)
@@ -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;
+}
index 721cf7d7ea66ef464165d7e783565207ad3a6f13..9d214fd6f783033e62e72be925dc3cad1739ed76 100644 (file)
@@ -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__ */
index 7b1862ab4a77264f9aec444c73f36af0dae1d38b..0e154fdb038f4a6b37642f43887145b57f56b232 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <syslog.h>
 
+#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 ("<form>");
+       str = g_string_new ("<form");
+
+       tmp = zak_cgi_commons_ghashtable_to_str_attrs (priv->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, "</form>");
+       g_string_append (str, "\n</form>");
 
        return g_strdup (str->str);
 }
index 15952942e82e876fb43b3aa3ee29c3e5e37a786a..107a1c027431a4dbb86017db9fc33307a57445a7 100644 (file)
@@ -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);
 
index b376ad783031817032ee5bb0ca17c0029c2ec215..1d8b0ee2ff9155592170c1aa0a472b6a2bdcd019 100644 (file)
@@ -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);
 }
index 18d6ddea0ffd0db0425ecf3cb42b26b0d4ba7e20..9e1f48461e00a07ac1ecef3240e91ef7a8d94fce 100644 (file)
@@ -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 ("");