From 099af8b36080fbdbf328c5269bf237b7b31799a7 Mon Sep 17 00:00:00 2001
From: Andrea Zagli <azagli@libero.it>
Date: Mon, 27 Jul 2015 18:04:32 +0200
Subject: [PATCH] Added ZakCgiTag::tag (refs #945).

---
 .gitignore        |   1 +
 src/Makefile.am   |   6 +-
 src/libzakcgi.h   |   1 +
 src/tag.c         | 138 ++++++++++++++++++++++++++++++++++++++++++++++
 src/tag.h         |  33 +++++++++++
 tests/Makefile.am |   1 +
 tests/tag.c       |  31 +++++++++++
 7 files changed, 209 insertions(+), 2 deletions(-)
 create mode 100644 src/tag.c
 create mode 100644 src/tag.h
 create mode 100644 tests/tag.c

diff --git a/.gitignore b/.gitignore
index bb5048c..162ccb3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -55,4 +55,5 @@ tests/env
 tests/querystring
 tests/redirect
 tests/session
+tests/tag
 tests/url
diff --git a/src/Makefile.am b/src/Makefile.am
index a15bb40..8dcaea6 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -6,14 +6,16 @@ AM_CPPFLAGS = $(ZAKCGI_CFLAGS) \
 lib_LTLIBRARIES = libzakcgi.la
 
 libzakcgi_la_SOURCES = main.c \
-                        session.c \
-                        url.c
+                       session.c \
+                       tag.c \
+                       url.c
 
 libzakcgi_la_LDFLAGS = -no-undefined
 
 libzakcgi_include_HEADERS = libzakcgi.h \
                             main.h \
                             session.h \
+                            tag.h \
                             url.h
 
 libzakcgi_includedir = $(includedir)/libzakcgi
diff --git a/src/libzakcgi.h b/src/libzakcgi.h
index 2b6c435..b33731e 100644
--- a/src/libzakcgi.h
+++ b/src/libzakcgi.h
@@ -21,6 +21,7 @@
 
 #include <libzakcgi/main.h>
 #include <libzakcgi/session.h>
+#include <libzakcgi/tag.h>
 #include <libzakcgi/url.h>
 
 #endif /* __LIBZAKCGI_H__ */
diff --git a/src/tag.c b/src/tag.c
new file mode 100644
index 0000000..b8f14a9
--- /dev/null
+++ b/src/tag.c
@@ -0,0 +1,138 @@
+/*
+ * 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 Lesser 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 Lesser 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
+ */
+
+#ifdef HAVE_CONFIG_H
+	#include <config.h>
+#endif
+
+#include "tag.h"
+
+static gchar
+*zak_cgi_tag_tag_valist (const gchar *name,
+						 const gchar *id,
+						 va_list ap)
+{
+	gchar *ret;
+	GString *str;
+
+	gboolean with_name;
+	gboolean with_content;
+
+	gchar *attr;
+	gchar *attr_val;
+
+	gchar *content;
+
+	with_name = FALSE;
+	with_content = FALSE;
+
+	content = NULL;
+
+	str = g_string_new ("");
+	g_string_append_printf (str,
+							"<%s id=\"%s\"",
+							name,
+							id);
+
+	do
+		{
+			attr = va_arg (ap, gchar *);
+			if (attr != NULL)
+				{
+					attr_val = va_arg (ap, gchar *);
+					if (attr_val != NULL)
+						{
+							if (g_strcmp0 (attr, "content") == 0)
+								{
+									with_content = TRUE;
+									if (content != NULL)
+										{
+											g_free (content);
+										}
+									content = g_strdup (attr_val);
+								}
+							else
+								{
+									if (g_strcmp0 (attr, "name") == 0)
+										{
+											with_name = TRUE;
+										}
+
+									g_string_append_printf (str,
+															" %s=\"%s\"",
+															attr,
+															attr_val);
+								}
+						}
+					else
+						{
+							g_free (attr);
+							break;
+						}
+				}
+			else
+				{
+					break;
+				}
+		} while (TRUE);
+	va_end (ap);
+
+	if (!with_name)
+		{
+			g_string_append_printf (str,
+									" name=\"%s\"",
+									id);
+		}
+
+	if (!with_content)
+		{
+			g_string_append (str,
+							 " />");
+		}
+	else
+		{
+			g_string_append_printf (str,
+									">%s</%s>",
+									content,
+									name);
+		}
+
+	ret = g_strdup (str->str);
+	g_string_free (str, TRUE);
+
+	return ret;
+}
+
+/**
+ * zak_cgi_tag_tag:
+ * @name:
+ * @id:
+ *
+ * Returns:
+ */
+gchar
+*zak_cgi_tag_tag (const gchar *name,
+				  const gchar *id,
+				  ...)
+{
+	va_list ap;
+
+	va_start (ap, id);
+
+	return zak_cgi_tag_tag_valist (name, id, ap);
+}
diff --git a/src/tag.h b/src/tag.h
new file mode 100644
index 0000000..1d3ba7e
--- /dev/null
+++ b/src/tag.h
@@ -0,0 +1,33 @@
+/*
+ * 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 Lesser 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 Lesser 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
+ */
+
+#ifndef __ZAK_CGI_TAG_H__
+#define __ZAK_CGI_TAG_H__
+
+
+#include <glib-object.h>
+
+#include "main.h"
+
+
+gchar *zak_cgi_tag_tag (const gchar *name,
+						const gchar *id,
+						...);
+
+
+#endif /* __ZAK_CGI_TAG_H__ */
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b070e92..a5b823b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -14,4 +14,5 @@ noinst_PROGRAMS = \
                     querystring \
                     redirect \
                     session \
+                    tag \
                     url
diff --git a/tests/tag.c b/tests/tag.c
new file mode 100644
index 0000000..cfaf81a
--- /dev/null
+++ b/tests/tag.c
@@ -0,0 +1,31 @@
+/*
+ * 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 <tag.h>
+
+int
+main (int argc, char *argv[])
+{
+	g_message ("%s", zak_cgi_tag_tag ("input", "text",
+									  "type", "textarea",
+									  "name", "myname",
+									  "content", "the content of the text area",
+									  NULL));
+
+	return 0;
+}
-- 
2.49.0