summaryrefslogtreecommitdiff
path: root/generators-replace-table.c
diff options
context:
space:
mode:
Diffstat (limited to 'generators-replace-table.c')
-rw-r--r--generators-replace-table.c140
1 files changed, 140 insertions, 0 deletions
diff --git a/generators-replace-table.c b/generators-replace-table.c
new file mode 100644
index 0000000..562f9af
--- /dev/null
+++ b/generators-replace-table.c
@@ -0,0 +1,140 @@
+/* Cataract - Static web photo gallery generator
+ * Copyright (C) 2008 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 "generators-replace-table.h"
+#include "gallery-utils.h"
+
+
+
+
+
+static void
+replace_table_destroy_notify (gpointer data)
+{
+ g_print ("replace_table_destroy_notify: %s\n", (gchar *) data);
+
+ if ((gchar *) data)
+ g_free ((gchar *) data);
+}
+
+ReplaceTable *
+replace_table_new ()
+{
+ return (ReplaceTable *) g_hash_table_new_full (g_str_hash, g_str_equal, replace_table_destroy_notify, replace_table_destroy_notify);
+}
+
+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));
+}
+
+
+
+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);
+
+// g_print ("replace_table_print: key = '%s', value = '%s'\n", (gchar *) key, (gchar *) value);
+
+ /* replace <!-- $(xxx) --> tags */
+ tag = g_strdup_printf ("<!-- $(%s) -->", (gchar *) key);
+ tag_value = g_strdup ((gchar *) value);
+ /* TODO: do something with tag_value? */
+/* fix_entities (&s1); */
+/* s1 = g_markup_escape_text (s4, -1); */
+ while (strstr (*buffer, tag)) {
+ str_replace (buffer, tag, tag_value, NULL);
+ }
+ g_free (tag_value);
+ g_free (tag);
+
+ /* replace $(xxx) tags */
+ tag = g_strdup_printf ("$(%s)", (gchar *) key);
+ tag_value = g_strdup ((gchar *) value);
+ /* TODO: do something with tag_value? */
+/* fix_entities (&s1); */
+/* s1 = g_markup_escape_text (s4, -1); */
+/* remove all tags as we're tag value */
+ while (strstr (*buffer, tag)) {
+ str_replace (buffer, tag, tag_value, NULL);
+ }
+ 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);
+*/
+}