*.csv
tests/cookies
tests/env
+tests/form
tests/querystring
tests/redirect
tests/session
}
else
{
- g_hash_table_insert (priv->ht_elems, g_strdup (id), element);
+ g_hash_table_insert (priv->ht_elems, g_strdup (id), g_object_ref (element));
ret = TRUE;
}
return ret;
}
+/**
+ * zak_cgi_form_render:
+ * @zakcgiform:
+ *
+ * Returns:
+ */
+gchar
+*zak_cgi_form_render (ZakCgiForm *zakcgiform)
+{
+ GString *str;
+
+ GHashTableIter iter;
+ gpointer key;
+ gpointer value;
+
+ ZakCgiFormPrivate *priv;
+
+ priv = ZAK_CGI_FORM_GET_PRIVATE (zakcgiform);
+
+ str = g_string_new ("<form>");
+
+ 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));
+ }
+
+ g_string_append (str, "</form>");
+
+ return g_strdup (str->str);
+}
+
/* PRIVATE */
static void
zak_cgi_form_set_property (GObject *object,
gboolean zak_cgi_form_add_element (ZakCgiForm *zakcgiform, ZakCgiFormElement *element);
+gchar *zak_cgi_form_render (ZakCgiForm *zakcgiform);
+
G_END_DECLS
static void zak_cgi_form_element_class_init (ZakCgiFormElementClass *class);
static void zak_cgi_form_element_init (ZakCgiFormElement *zak_cgi_form_element);
+static void zak_cgi_form_element_construct (ZakCgiFormElement *element,
+ const gchar *id,
+ const gchar *validation_regex,
+ GHashTable *ht_attrs);
+
+static GHashTable *zak_cgi_form_element_get_ht_attrs (ZakCgiFormElement *element);
+
static void zak_cgi_form_element_set_property (GObject *object,
guint property_id,
const GValue *value,
"Validation regex",
"",
G_PARAM_READWRITE));
+
+ class->construct = zak_cgi_form_element_construct;
+ class->get_ht_attrs = zak_cgi_form_element_get_ht_attrs;
}
static void
priv->ht_attrs = NULL;
}
-/**
- * zak_cgi_form_element_new:
- * @id:
- * @validation_regex:
- * @...:
- *
- * Returns: the newly created #ZakCgiFormElement object.
- */
-ZakCgiFormElement
-*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);
- priv->ht_attrs = zak_cgi_commons_valist_to_ghashtable (ap);
-
- g_hash_table_insert (priv->ht_attrs, "id", g_strdup (id));
-
- 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;
-}
-
gchar
*zak_cgi_form_element_get_id (ZakCgiFormElement *element)
{
if (ZAK_CGI_IS_FORM_ELEMENT (element) && ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->render != NULL)
{
- ret = ZAK_CGI_FORM_ELEMENT_CLASS (element)->render (element);
+ ret = ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->render (element);
}
return ret;
if (ZAK_CGI_IS_FORM_ELEMENT (element) && ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->is_valid != NULL)
{
- ret = ZAK_CGI_FORM_ELEMENT_CLASS (element)->is_valid (element);
+ ret = ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->is_valid (element);
}
return ret;
}
/* PRIVATE */
+static void
+zak_cgi_form_element_construct (ZakCgiFormElement *element,
+ const gchar *id,
+ const gchar *validation_regex,
+ GHashTable *ht_attrs)
+{
+ ZakCgiFormElementPrivate *priv;
+
+ g_return_val_if_fail (id != NULL, NULL);
+
+ priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element);
+
+ priv->id = g_strdup (id);
+
+ zak_cgi_form_element_set_validation_regex (element, validation_regex);
+
+ priv->ht_attrs = ht_attrs;
+
+ if (g_hash_table_lookup (priv->ht_attrs, "name") == NULL)
+ {
+ g_hash_table_insert (priv->ht_attrs, "name", g_strdup (id));
+ }
+
+ return;
+}
+
+static GHashTable
+*zak_cgi_form_element_get_ht_attrs (ZakCgiFormElement *element)
+{
+ ZakCgiFormElementPrivate *priv;
+
+ priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element);
+
+ return priv->ht_attrs;
+}
+
static void
zak_cgi_form_element_set_property (GObject *object,
guint property_id,
{
GObjectClass parent_class;
+ void (*construct) (ZakCgiFormElement *element, const gchar *id, const gchar *validation_regex, GHashTable *ht_attrs);
+ GHashTable *(*get_ht_attrs) (ZakCgiFormElement *element);
+
gchar *(*render) (ZakCgiFormElement *element);
gboolean (*is_valid) (ZakCgiFormElement *element);
};
GType zak_cgi_form_element_get_type (void);
-ZakCgiFormElement *zak_cgi_form_element_new (const gchar *id, const gchar *validation_regex, ...);
-
gchar *zak_cgi_form_element_get_id (ZakCgiFormElement *element);
void zak_cgi_form_element_set_validation_regex (ZakCgiFormElement *element, const gchar *validation_regex);
#include <syslog.h>
+#include "commons.h"
+#include "tag.h"
#include "formelementtext.h"
static void zak_cgi_form_element_text_class_init (ZakCgiFormElementTextClass *class);
static void zak_cgi_form_element_text_init (ZakCgiFormElementText *zak_cgi_form_element_text);
+static gchar *zak_cgi_form_element_text_render (ZakCgiFormElement *element);
+
+static gboolean zak_cgi_form_element_text_is_valid (ZakCgiFormElement *element);
+
static void zak_cgi_form_element_text_set_property (GObject *object,
guint property_id,
const GValue *value,
typedef struct _ZakCgiFormElementTextPrivate ZakCgiFormElementTextPrivate;
struct _ZakCgiFormElementTextPrivate
{
+ gpointer empty;
};
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)
+zak_cgi_form_element_text_class_init (ZakCgiFormElementTextClass *klass)
{
- GObjectClass *object_class = G_OBJECT_CLASS (class);
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ ZakCgiFormElementClass *elem_class = ZAK_CGI_FORM_ELEMENT_CLASS (klass);
object_class->set_property = zak_cgi_form_element_text_set_property;
object_class->get_property = zak_cgi_form_element_text_get_property;
object_class->dispose = zak_cgi_form_element_text_dispose;
object_class->finalize = zak_cgi_form_element_text_finalize;
+ elem_class->render = zak_cgi_form_element_text_render;
+ elem_class->is_valid = zak_cgi_form_element_text_is_valid;
+
g_type_class_add_private (object_class, sizeof (ZakCgiFormElementTextPrivate));
}
/**
* zak_cgi_form_element_text_new:
+ * @id:
* @validation_regex:
+ * @...:
*
* Returns: the newly created #ZakCgiFormElementText object.
*/
-ZakCgiFormElementText
-*zak_cgi_form_element_text_new (const gchar *validation_regex)
+ZakCgiFormElement
+*zak_cgi_form_element_text_new (const gchar *id,
+ const gchar *validation_regex,
+ ...)
{
+ va_list ap;
+
ZakCgiFormElementText *zak_cgi_form_element_text;
- ZakCgiFormElementTextPrivate *priv;
zak_cgi_form_element_text = ZAK_CGI_FORM_ELEMENT_TEXT (g_object_new (zak_cgi_form_element_text_get_type (), NULL));
- priv = ZAK_CGI_FORM_ELEMENT_TEXT_GET_PRIVATE (zak_cgi_form_element_text);
+ va_start (ap, validation_regex);
- if (validation_regex != NULL)
- {
- }
+ 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));
- return zak_cgi_form_element_text;
+ return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text);
}
-/**
- * zak_cgi_form_element_text_render:
- * @element_text:
- *
- */
-gchar
-*zak_cgi_form_element_text_render (ZakCgiFormElementText *element_text)
+static gchar
+*zak_cgi_form_element_text_render (ZakCgiFormElement *element)
{
gchar *ret;
+ GHashTable *ht_attrs;
+
+ ZakCgiFormElementClass *klass;
+
+ klass = (ZakCgiFormElementClass *)g_type_class_peek_parent (ZAK_CGI_FORM_ELEMENT_TEXT_GET_CLASS (ZAK_CGI_FORM_ELEMENT_TEXT (element)));
+
+ ht_attrs = klass->get_ht_attrs (element);
+
+ ret = zak_cgi_tag_text_ht (zak_cgi_form_element_get_id (element), ht_attrs);
return ret;
}
-/**
- * zak_cgi_form_element_text_is_valid:
- * @element_text:
- *
- */
-gboolean
-zak_cgi_form_element_text_is_valid (ZakCgiFormElementText *element_text)
+static gboolean
+zak_cgi_form_element_text_is_valid (ZakCgiFormElement *element)
{
gboolean ret;
GType zak_cgi_form_element_text_get_type (void);
-ZakCgiFormElementText *zak_cgi_form_element_text_new (const gchar *validation_regex);
-
-gchar *zak_cgi_form_element_text_render (ZakCgiFormElementText *element_text);
-
-gboolean zak_cgi_form_element_text_is_valid (ZakCgiFormElementText *element_text);
+ZakCgiFormElement *zak_cgi_form_element_text_new (const gchar *id, const gchar *validation_regex, ...);
G_END_DECLS
noinst_PROGRAMS = \
cookies \
env \
+ form \
querystring \
redirect \
session \
--- /dev/null
+/*
+ * Copyright (C) 2015 Andrea Zagli <azagli@libero.it>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <form.h>
+#include <formelement.h>
+#include <formelementtext.h>
+
+int
+main (int argc, char *argv[])
+{
+ GString *str;
+
+ ZakCgiForm *form;
+ ZakCgiFormElement *element;
+
+ form = zak_cgi_form_new (NULL);
+
+ str = g_string_new ("");
+
+ element = zak_cgi_form_element_text_new ("first", "aaa", NULL);
+ zak_cgi_form_add_element (form, element);
+
+ g_string_append (str, zak_cgi_form_render (form));
+
+ zak_cgi_main_out (NULL, str->str);
+
+ return 0;
+}