]> saetta.ns0.it Git - libzakcgi/commitdiff
Added ZakCgiForm::add_str.
authorAndrea Zagli <azagli@libero.it>
Wed, 26 Aug 2015 16:21:48 +0000 (18:21 +0200)
committerAndrea Zagli <azagli@libero.it>
Wed, 26 Aug 2015 16:21:48 +0000 (18:21 +0200)
src/form.c
src/form.h
tests/form.c

index b86047597a5b913ab0c445fd91b7cf873a4bae9e..da81760728c085ca751b93b11375bcb017d3018e 100644 (file)
@@ -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</form>");
index f14b14d4f840c19a4bf55c02f7faf05d0c4c6d8c..366afdb761752a410fb290320249bd3396d7b626 100644 (file)
@@ -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);
index ed3a85cde6054508e330114affd8a44486e5633f..e791862528d78b2c8d3c10a9082a4d5ed32fbefb 100644 (file)
@@ -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, "<h1>big big big</h1>");
+
        element = zak_cgi_form_element_submit_new ("submit", "aaa", NULL);
        zak_cgi_form_add_element (form, element);