summaryrefslogtreecommitdiff
path: root/src/replace-table.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/replace-table.c')
-rw-r--r--src/replace-table.c163
1 files changed, 163 insertions, 0 deletions
diff --git a/src/replace-table.c b/src/replace-table.c
new file mode 100644
index 0000000..d3503c4
--- /dev/null
+++ b/src/replace-table.c
@@ -0,0 +1,163 @@
+/* Cataract - Static web photo gallery generator
+ * Copyright (C) 2009 Tomas Bzatek <tbzatek@users.sourceforge.net>
+ *
+ * This program 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
+ * of the License, or any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <glib.h>
+#include <glib/gprintf.h>
+
+#include "replace-table.h"
+#include "gallery-utils.h"
+
+
+
+
+ReplaceTable *
+replace_table_new ()
+{
+ return (ReplaceTable *) g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+}
+
+void
+replace_table_free (ReplaceTable *table)
+{
+ g_return_if_fail (table != NULL);
+
+ g_hash_table_destroy (table);
+}
+
+
+/*
+ * replace_table_add_key: add tag/value pair to replace
+ *
+ * tag, value will be referenced inside
+ *
+ */
+void
+replace_table_add_key (ReplaceTable *table, const gchar *tag, const gchar *value)
+{
+ g_return_if_fail (table != NULL);
+
+ if (tag != NULL && value != NULL)
+ g_hash_table_replace (table, g_strdup (tag), g_strdup (value));
+}
+
+void
+replace_table_add_key_int (ReplaceTable *table, const gchar *tag, gint value)
+{
+ g_return_if_fail (table != NULL);
+
+ if (tag != NULL)
+ g_hash_table_replace (table, g_strdup (tag), g_strdup_printf ("%d", value));
+}
+
+void
+replace_table_add_key_printf (ReplaceTable *table, const gchar *tag, const gchar *format, ...)
+{
+ va_list args;
+ gchar *s = NULL;
+
+ g_return_if_fail (table != NULL);
+
+ if (tag != NULL && format != NULL) {
+ va_start (args, format);
+ g_vasprintf (&s, format, args);
+ g_hash_table_replace (table, g_strdup (tag), s);
+ va_end (args);
+ }
+}
+
+
+
+static void
+replace_table_process_value (gpointer key, gpointer value, gpointer user_data)
+{
+ gchar **buffer = user_data;
+ gchar *tag;
+ gchar *tag_value;
+
+ g_return_if_fail (key != NULL);
+ g_return_if_fail (value != NULL);
+ g_return_if_fail (user_data != NULL);
+
+ /* replace <!-- $(xxx) --> tags */
+ tag = g_strdup_printf ("<!-- $(%s) -->", (gchar *) key);
+ tag_value = g_strdup ((gchar *) value);
+ adjust_tags_normal (&tag_value);
+ while (strstr (*buffer, tag)) {
+ str_replace (buffer, tag, tag_value);
+ }
+ g_free (tag_value);
+ g_free (tag);
+
+ /* replace $(xxx) tags */
+ tag = g_strdup_printf ("$(%s)", (gchar *) key);
+ tag_value = g_strdup ((gchar *) value);
+ adjust_tags_parameter (&tag_value);
+ while (strstr (*buffer, tag)) {
+ str_replace (buffer, tag, tag_value);
+ }
+ g_free (tag_value);
+ g_free (tag);
+}
+
+/*
+ * replace_table_process: process buffer and replace all tags filled in the replace table
+ *
+ * - reallocates source buffer
+ *
+ */
+void
+replace_table_process (gchar **buffer, ReplaceTable *table)
+{
+ g_return_if_fail (table != NULL);
+
+ g_hash_table_foreach (table, replace_table_process_value, buffer);
+
+/*
+ gchar *val = g_hash_table_lookup (table, "TAG");
+ g_print ("replace_table_process: val = '%s'\n", val);
+*/
+}
+
+
+/*
+ * adjust_tags_normal: adjust string for normal HTML use
+ * adjust_tags_parameter: adjust string for use as tag parameter value
+ * - both funtions return newly allocated string
+ */
+void
+adjust_tags_normal (char **str)
+{
+ fix_entities (str);
+}
+
+void
+adjust_tags_parameter (char **str)
+{
+ /* TODO: replace line endings with single space? */
+ remove_tags (str, "<!--", "-->"); /* comments */
+ remove_tags (str, "<", ">"); /* tags */
+ fix_entities (str); /* entities */
+ str_replace (str, "\"", "&quot;"); /* " */
+ str_replace (str, "'", "&apos;"); /* ' */
+}