summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.am2
-rwxr-xr-xsample/gen.sh3
-rw-r--r--src/Makefile.am6
-rw-r--r--src/block-parser.c318
-rw-r--r--src/block-parser.h97
-rw-r--r--src/gallery-utils.c2
-rw-r--r--src/generators.c268
-rw-r--r--src/replace-table.c (renamed from src/generators-replace-table.c)13
-rw-r--r--src/replace-table.h (renamed from src/generators-replace-table.h)11
9 files changed, 506 insertions, 214 deletions
diff --git a/Makefile.am b/Makefile.am
index 76fd658..7c7a990 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -17,7 +17,7 @@ DISTCLEANFILES = \
# "make samples" target
# generate sample structure
samples:
- (cd sample && ./gen.sh)
+ (cd sample && ./gen.sh $@)
ACLOCAL_AMFLAGS = -I m4
diff --git a/sample/gen.sh b/sample/gen.sh
index fec6ce8..58c7710 100755
--- a/sample/gen.sh
+++ b/sample/gen.sh
@@ -1,4 +1,3 @@
#!/bin/sh
cd src
-../../src/cgg -s . -o ../dst -v
-
+../../src/cgg -s . -o ../dst -v $@
diff --git a/src/Makefile.am b/src/Makefile.am
index 1300d8d..9b7396c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -19,18 +19,20 @@ bin_SCRIPTS = \
cgg_SOURCES = \
cgg.c \
+ block-parser.c \
+ block-parser.h \
gallery-utils.c \
gallery-utils.h \
generators.c \
generators.h \
- generators-replace-table.c \
- generators-replace-table.h \
items.c \
items.h \
job-manager.h \
job-manager.c \
jpeg-utils.cpp \
jpeg-utils.h \
+ replace-table.c \
+ replace-table.h \
setup.c \
setup.h \
xml-parser.c \
diff --git a/src/block-parser.c b/src/block-parser.c
new file mode 100644
index 0000000..713f963
--- /dev/null
+++ b/src/block-parser.c
@@ -0,0 +1,318 @@
+/* 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 "block-parser.h"
+#include "gallery-utils.h"
+
+
+
+#define BUFFER_SIZE 65536 /* lines cannot be longer than this */
+
+
+struct BlockParser {
+ GHashTable *table;
+ GQueue *active_tree;
+ char *current_line;
+};
+
+typedef struct {
+ char *replace_key;
+ char *data;
+ gboolean used;
+ gboolean finished;
+} BlockData;
+
+
+
+static void
+block_parser_destroy_notify (gpointer data)
+{
+ BlockData *d = data;
+
+ if (data == NULL)
+ return;
+
+ g_free (d->replace_key);
+ g_free (d->data);
+ g_free (d);
+}
+
+BlockParser *
+block_parser_new ()
+{
+ BlockParser *parser;
+
+ parser = g_new0 (BlockParser, 1);
+ parser->table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, block_parser_destroy_notify);
+ parser->active_tree = g_queue_new ();
+
+ return parser;
+}
+
+void
+block_parser_free (BlockParser *parser)
+{
+ g_return_if_fail (parser != NULL);
+
+ g_hash_table_destroy (parser->table);
+ g_queue_free (parser->active_tree);
+ g_free (parser);
+}
+
+
+/*
+ * block_parser_register_key: tell parser to expect the key to catch
+ * key: part of the BEGIN_xxx placeholder, matched BEGIN_xxx and END_xxx as one block
+ * replace_key: placeholder to replace finished block with, for later use in replace_table,
+ * placeholder will be surrounded by "<!-- $(replace_key) -->"
+ *
+ */
+void
+block_parser_register_key (BlockParser *parser, const char *key, const char *replace_key)
+{
+ BlockData *data;
+
+ g_return_if_fail (parser != NULL);
+
+ data = g_new0 (BlockData, 1);
+ if (replace_key)
+ data->replace_key = g_strdup_printf ("<!-- $(%s) -->", replace_key);
+ data->used = TRUE; /* buffer is empty, mask it for block_parser_has_unused_data() */
+ g_hash_table_replace (parser->table, g_strdup (key), data);
+}
+
+
+/*
+ * block_parser_get_data: return retrieved data or NULL if none read yet
+ * returns newly allocated string, caller is responsible for freeing
+ *
+ */
+char *
+block_parser_get_data (BlockParser *parser, const char *key)
+{
+ BlockData *data = NULL;
+
+ g_return_val_if_fail (parser != NULL, NULL);
+
+ data = g_hash_table_lookup (parser->table, key);
+ if (data != NULL) {
+ data->used = TRUE;
+ return g_strdup (data->data);
+ }
+
+ return NULL;
+}
+
+/*
+ * block_parser_has_unused_data: indicates whether the data have already been read and used (by calling block_parser_get_data)
+ *
+ */
+gboolean
+block_parser_has_unused_data (BlockParser *parser, const char *key)
+{
+ BlockData *data = NULL;
+
+ g_return_val_if_fail (parser != NULL, FALSE);
+
+ data = g_hash_table_lookup (parser->table, key);
+ if (data != NULL) {
+ return (data->used == FALSE) && (data->finished == TRUE);
+ }
+
+ return FALSE;
+}
+
+
+
+
+/* -------------------------------------------------------------------------------------------------------- */
+
+/*
+ * get_next_token: retrieves first token (<!-- $(TOKEN) --> or $(TOKEN)) in the string
+ * - returns newly allocated token name, caller is responsible for freeing
+ * - start and end are positions of token in the source string 's'
+ *
+ */
+static char *
+get_next_token (const char *s, char **start, char **end)
+{
+ char *dollar;
+ char *end_brace;
+ char *b;
+
+ *start = NULL;
+ *end = NULL;
+
+ dollar = strstr (s, "$(");
+ if (dollar == NULL)
+ return NULL;
+ end_brace = strchr (dollar + 2, ')');
+ if (end_brace == NULL)
+ return NULL;
+
+ /* Go back and try to find placeholder beginning */
+ for (b = dollar; b >= s + 3; b--) {
+ if (*b == '-' && *(b-1) == '-' && *(b-2) == '!' && *(b-3) == '<') {
+ *start = b - 3;
+ break;
+ }
+ }
+ if (*start == NULL)
+ *start = dollar;
+
+ /* Go forth and try to find placeholder end */
+ for (b = end_brace; b <= end_brace + strlen (end_brace) - 3; b++) {
+ if (*b == '-' && *(b+1) == '-' && *(b+2) == '>') {
+ *end = b + 2;
+ break;
+ }
+ }
+ if (*end == NULL)
+ *end = end_brace;
+
+ return g_strndup (dollar + 2, end_brace - dollar - 2);
+}
+
+
+static void
+push_string (BlockParser *parser, const char *piece)
+{
+ BlockData *data;
+ char *s;
+
+ data = g_queue_peek_head (parser->active_tree);
+ if (data) {
+ if (data->data)
+ s = g_strdup_printf ("%s%s", data->data, piece);
+ else
+ s = g_strdup (piece);
+ g_free (data->data);
+ data->data = s;
+ data->used = FALSE;
+ }
+ else
+ {
+ s = g_strdup_printf ("%s%s", parser->current_line, piece);
+ g_free (parser->current_line);
+ parser->current_line = s;
+ }
+}
+
+/*
+ * block_parser_read_and_parse: reads input from the file and returns parsed line
+ * - if there's a multiline block, string before the opening placeholder and
+ * string after closing placeholder are returned on one line,
+ * separated by "replace_key" placeholder as specified in registered blocks
+ * - only the outmost "replace_key" placeholder will be used,
+ * no matter how many nested blocks were inside
+ * - returns newly allocated string, caller is responsible for freeing
+ *
+ */
+char *
+block_parser_read_and_parse (BlockParser *parser, FILE *stream)
+{
+ char *buffer;
+ char *b;
+ char *token;
+ char *start, *end;
+ char *s;
+ GList *keys;
+ GList *l;
+ BlockData *data;
+ gboolean handled;
+
+ g_return_val_if_fail (parser != NULL, NULL);
+
+ buffer = malloc (BUFFER_SIZE);
+ memset (buffer, 0, BUFFER_SIZE);
+ parser->current_line = g_strdup ("");
+ if (! fgets (buffer, BUFFER_SIZE, stream) || strlen (buffer) == 0)
+ return NULL;
+
+ keys = g_hash_table_get_keys (parser->table);
+
+ /* find tokens */
+ b = buffer;
+ while ((token = get_next_token (b, &start, &end))) {
+ handled = FALSE;
+
+ /* push the string before the token */
+ s = g_strndup (b, start - b);
+ push_string (parser, s);
+ g_free (s);
+
+ /* match known tags */
+ for (l = keys; l != NULL; l = l->next) {
+ /* test BEGIN_ tokens */
+ s = g_strdup_printf ("BEGIN_%s", (char *) l->data);
+ if (strcmp (s, token) == 0) {
+ data = g_hash_table_lookup (parser->table, (char *) l->data);
+ if (data) {
+ g_queue_push_head (parser->active_tree, data);
+ handled = TRUE;
+ }
+ }
+ g_free (s);
+
+ /* test END_ tokens */
+ s = g_strdup_printf ("END_%s", (char *) l->data);
+ if (strcmp (s, token) == 0) {
+ data = g_hash_table_lookup (parser->table, (char *) l->data);
+ if (data == NULL || data != g_queue_peek_head (parser->active_tree)) {
+ fprintf (stderr, "block_parser_read_and_parse: something is wrong with the parser table: expected '%s'\n", (char *) l->data);
+ }
+ else
+ {
+ g_queue_pop_head (parser->active_tree);
+ /* push the replacement placeholder */
+ if (data->replace_key)
+ push_string (parser, data->replace_key);
+ data->finished = TRUE;
+ handled = TRUE;
+ }
+ }
+ g_free (s);
+ }
+
+ /* push the tag if not matched above */
+ if (! handled) {
+ s = g_strndup (start, end - start + 1);
+ push_string (parser, s);
+ g_free (s);
+ }
+
+ g_free (token);
+ b = end + 1;
+ }
+
+ /* push rest of the buffer till the end of the line */
+ push_string (parser, b);
+
+ g_list_free (keys);
+ free (buffer);
+ return parser->current_line;
+}
diff --git a/src/block-parser.h b/src/block-parser.h
new file mode 100644
index 0000000..41db0b1
--- /dev/null
+++ b/src/block-parser.h
@@ -0,0 +1,97 @@
+/* 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 <glib.h>
+
+typedef struct BlockParser BlockParser;
+
+
+
+BlockParser *block_parser_new ();
+
+void block_parser_free (BlockParser *parser);
+
+
+
+/*
+ * block_parser_register_key: tell parser to expect the key to catch
+ * key: part of the BEGIN_xxx placeholder, matched BEGIN_xxx and END_xxx as one block
+ * replace_key: placeholder to replace finished block with, for later use in replace_table,
+ * placeholder will be surrounded by "<!-- $(replace_key) -->"
+ *
+ */
+void block_parser_register_key (BlockParser *parser, const char *key, const char *replace_key);
+
+/*
+ * block_parser_get_data: return retrieved data or NULL if none read yet
+ * returns newly allocated string, caller is responsible for freeing
+ *
+ */
+char * block_parser_get_data (BlockParser *parser, const char *key);
+
+/*
+ * block_parser_has_unused_data: indicates whether the data have already been read and used (by calling block_parser_get_data)
+ *
+ */
+gboolean block_parser_has_unused_data (BlockParser *parser, const char *key);
+
+/*
+ * block_parser_read_and_parse: reads input from the file and returns parsed line
+ * - if there's a multiline block, string before the opening placeholder and
+ * string after closing placeholder are returned on one line,
+ * separated by "replace_key" placeholder as specified in registered blocks
+ * - only the outmost "replace_key" placeholder will be used,
+ * no matter how many nested blocks were inside
+ * - returns newly allocated string, caller is responsible for freeing
+ *
+ */
+char * block_parser_read_and_parse (BlockParser *parser, FILE *stream);
+
+
+
+#if 0
+
+/*
+ * 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);
+void replace_table_add_key_int (ReplaceTable *table, const gchar *tag, gint value);
+void replace_table_add_key_printf (ReplaceTable *table, const gchar *tag, const gchar *format, ...) G_GNUC_PRINTF (3, 4);
+
+/*
+ * 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);
+
+
+/*
+ * 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);
+void adjust_tags_parameter (char **str);
+
+#endif
diff --git a/src/gallery-utils.c b/src/gallery-utils.c
index 2958c51..7f1222a 100644
--- a/src/gallery-utils.c
+++ b/src/gallery-utils.c
@@ -258,7 +258,7 @@ remove_tags (char **str, const char *tag_begin, const char *tag_end)
memcpy (dest, src, found - src);
memcpy (dest + (found - src), found2, strlen (found2) + 1);
#ifdef __DEBUG_ALL__
- printf ("found = %ld, found2 = %ld, strlen = %d, res = %d\n", (long int)found, (long int)found2, strlen (src), strlen (src) - (found2 - found) + 1);
+ printf ("found = %p, found2 = %p, strlen = %d, res = %d\n", found, found2, strlen (src), strlen (src) - (found2 - found) + 1);
printf ("orig = %s, new = %s, strlen = %d\n", src, dest, strlen (dest));
#endif
g_free (src);
diff --git a/src/generators.c b/src/generators.c
index 59de1f4..ff88a73 100644
--- a/src/generators.c
+++ b/src/generators.c
@@ -30,7 +30,8 @@
#include "items.h"
#include "jpeg-utils.h"
#include "gallery-utils.h"
-#include "generators-replace-table.h"
+#include "replace-table.h"
+#include "block-parser.h"
/*
@@ -162,10 +163,8 @@ generate_image (TGallerySetup *setup,
}
}
}
- if (img_src_full)
- g_free (img_src_full);
- if (thumb_src_full)
- g_free (thumb_src_full);
+ g_free (img_src_full);
+ g_free (thumb_src_full);
}
@@ -184,20 +183,10 @@ write_html_album (TGallerySetup *setup,
const char *dst,
TAlbum *items)
{
- #define BUFFER_SIZE 65536
FILE *fin;
FILE *fout;
- char *buffer;
- char *buf_img_list_landscape;
- char *buf_img_list_portrait;
- char *buf_img_separator;
- char *buf_go_up_string;
- gboolean in_img_list;
- gboolean in_img_list_landscape;
- gboolean in_img_list_portrait;
- gboolean in_img_separator;
- gboolean in_go_up_string;
- char *b;
+ char *line;
+ char *block;
char *s1, *s2, *s3, *s4, *s5;
TAlbum *parent;
TIndexItem *item;
@@ -208,6 +197,7 @@ write_html_album (TGallerySetup *setup,
unsigned int real_total_items;
ReplaceTable *global_replace_table;
ReplaceTable *local_replace_table;
+ BlockParser *block_parser;
fin = fopen (template_src, "r");
@@ -222,19 +212,10 @@ write_html_album (TGallerySetup *setup,
return FALSE;
}
- buffer = malloc (BUFFER_SIZE);
- buf_img_list_landscape = malloc (BUFFER_SIZE);
- buf_img_list_portrait = malloc (BUFFER_SIZE);
- buf_img_separator = malloc (BUFFER_SIZE);
- buf_go_up_string = malloc (BUFFER_SIZE);
- in_img_list = FALSE;
- in_img_list_landscape = FALSE;
- in_img_list_portrait = FALSE;
- in_img_separator = FALSE;
- in_go_up_string = FALSE;
res = TRUE;
global_replace_table = replace_table_new ();
+ block_parser = block_parser_new ();
/* Get number of real pictures in the list */
real_total_items = 0;
@@ -344,81 +325,32 @@ write_html_album (TGallerySetup *setup,
g_free (s1);
+ /* Setup block parser */
+ block_parser_register_key (block_parser, "IMG_LIST", "IMG_LIST");
+ block_parser_register_key (block_parser, "IMG_LIST_LANDSCAPE", NULL);
+ block_parser_register_key (block_parser, "IMG_LIST_PORTRAIT", NULL);
+ block_parser_register_key (block_parser, "LIST_SEPARATOR", NULL);
+ block_parser_register_key (block_parser, "GO_UP", "GO_UP");
+
+
/* Read through the template and replace placeholders with real data */
- while (! feof (fin))
- {
- memset (buffer, 0, BUFFER_SIZE);
- if (! fgets (buffer, BUFFER_SIZE, fin))
- break;
-
- /* Block placeholders */
- if (in_img_list && (strstr (buffer, "<!-- $(BEGIN_IMG_LIST_LANDSCAPE) -->"))) {
- in_img_list_landscape = TRUE;
- continue;
- }
- if (in_img_list && (strstr (buffer, "<!-- $(END_IMG_LIST_LANDSCAPE) -->"))) {
- in_img_list_landscape = FALSE;
- continue;
- }
- if (in_img_list && (strstr (buffer, "<!-- $(BEGIN_IMG_LIST_PORTRAIT) -->"))) {
- in_img_list_portrait = TRUE;
- continue;
- }
- if (in_img_list && (strstr (buffer, "<!-- $(END_IMG_LIST_PORTRAIT) -->"))) {
- in_img_list_portrait = FALSE;
- continue;
- }
- if (in_img_list && (strstr (buffer, "<!-- $(BEGIN_LIST_SEPARATOR) -->"))) {
- in_img_separator = TRUE;
- continue;
- }
- if (in_img_list && (strstr (buffer, "<!-- $(END_LIST_SEPARATOR) -->"))) {
- in_img_separator = FALSE;
- continue;
- }
- if (in_img_list && in_img_list_landscape) {
- buf_img_list_landscape = strncat (buf_img_list_landscape, buffer, BUFFER_SIZE - strlen (buf_img_list_landscape) - 2);
- continue;
- }
- if (in_img_list && in_img_list_portrait) {
- buf_img_list_portrait = strncat (buf_img_list_portrait, buffer, BUFFER_SIZE - strlen (buf_img_list_portrait) - 2);
- continue;
- }
- if (in_img_list && in_img_separator) {
- buf_img_separator = strncat (buf_img_separator, buffer, BUFFER_SIZE - strlen (buf_img_separator) - 2);
- continue;
- }
+ while (! feof (fin)) {
+ line = block_parser_read_and_parse (block_parser, fin);
+ if (line == NULL)
+ break;
- if (strstr (buffer, "<!-- $(BEGIN_GO_UP) -->")) {
- memset (buf_go_up_string, 0, BUFFER_SIZE);
- in_go_up_string = TRUE;
- continue;
- }
- if (in_go_up_string && strstr (buffer, "<!-- $(END_GO_UP) -->")) {
- in_go_up_string = FALSE;
- if (! items->parent_index || ! setup->show_go_up)
- continue;
- }
- if (in_go_up_string) {
- buf_go_up_string = strncat (buf_go_up_string, buffer, BUFFER_SIZE - strlen (buf_go_up_string) - 2);
- continue;
+ /* Blocks */
+ if (block_parser_has_unused_data (block_parser, "GO_UP")) {
+ block = block_parser_get_data (block_parser, "GO_UP");
+ if (block) {
+ replace_table_process (&block, global_replace_table);
+ replace_table_add_key (global_replace_table, "GO_UP", items->parent_index && setup->show_go_up ? block : "");
+ }
+ g_free (block);
}
- /* Image list, nested placeholders */
- if (strstr (buffer, "<!-- $(BEGIN_IMG_LIST) -->")) {
- memset (buf_img_list_landscape, 0, BUFFER_SIZE);
- memset (buf_img_list_portrait, 0, BUFFER_SIZE);
- memset (buf_img_separator, 0, BUFFER_SIZE);
- in_img_list = TRUE;
- in_img_list_landscape = FALSE;
- in_img_list_portrait = FALSE;
- in_img_separator = FALSE;
- continue;
- }
- if (in_img_list && (strstr (buffer, "<!-- $(END_IMG_LIST) -->"))) {
- in_img_list = FALSE;
- in_img_list_landscape = FALSE;
- in_img_list_portrait = FALSE;
+ if (block_parser_has_unused_data (block_parser, "IMG_LIST")) {
+ block = g_strdup ("");
/* Now we have all block placeholders read, generate the items: */
for (i = 0; i < items->items->len; i++)
@@ -429,7 +361,7 @@ write_html_album (TGallerySetup *setup,
continue;
}
- /* Generate the images (preview, original, thumbnail) */
+ /* Generate images (preview, original, thumbnail) */
local_replace_table = replace_table_new ();
s1 = NULL;
@@ -437,8 +369,7 @@ write_html_album (TGallerySetup *setup,
case INDEX_ITEM_TYPE_PICTURE:
/* Skip HTML code generation if it's a hidden item */
if (! item->hidden) {
- s1 = strdup (item->gen_portrait ? buf_img_list_portrait : buf_img_list_landscape);
-
+ s1 = block_parser_get_data (block_parser, item->gen_portrait ? "IMG_LIST_PORTRAIT" : "IMG_LIST_LANDSCAPE");
replace_table_add_key_printf (local_replace_table, "ALBUM_SUBPATH", "%s/%s", item->path, setup->index_file_name);
replace_table_add_key_printf (local_replace_table, "IMG_SUBPAGE", "%s%s", item->gen_img_src, GET_EXT (setup->index_file_name));
replace_table_add_key (local_replace_table, "IMG_TITLE", item->title);
@@ -456,39 +387,31 @@ write_html_album (TGallerySetup *setup,
break;
case INDEX_ITEM_TYPE_SEPARATOR:
- s1 = strdup (buf_img_separator);
+ s1 = block_parser_get_data (block_parser, "LIST_SEPARATOR");
replace_table_add_key (local_replace_table, "LIST_SEPARATOR_TITLE", item->title);
break;
}
- bb = TRUE;
if (s1) {
replace_table_process (&s1, local_replace_table);
- bb = fputs (s1, fout);
- free (s1);
+ s2 = g_strdup_printf ("%s%s", block, s1);
+ g_free (block);
+ block = s2;
+ g_free (s1);
}
replace_table_free (local_replace_table);
- if (! bb) {
- fprintf (stderr, "write_html_index: error writing to file \"%s\": %s\n", dst, strerror (errno));
- res = FALSE;
- break;
- }
}
- continue; /* no need to write anything */
+ replace_table_process (&block, global_replace_table);
+ replace_table_add_key (global_replace_table, "IMG_LIST", block);
+ g_free (block);
}
- /* Select apropriate line buffer */
- if (strstr (buffer, "<!-- $(END_GO_UP) -->") && items->parent_index && setup->show_go_up) {
- b = strdup (buf_go_up_string);
- } else
- b = strdup (buffer);
-
/* Replace all known tags */
- replace_table_process (&b, global_replace_table);
+ replace_table_process (&line, global_replace_table);
/* Write to file */
- bb = fputs (b, fout);
- free (b);
+ bb = fputs (line, fout);
+ g_free (line);
if (! bb) {
fprintf (stderr, "write_html_index: error writing to file \"%s\": %s\n", dst, strerror (errno));
res = FALSE;
@@ -499,11 +422,7 @@ write_html_album (TGallerySetup *setup,
fclose (fout);
fclose (fin);
replace_table_free (global_replace_table);
- free (buffer);
- free (buf_img_list_landscape);
- free (buf_img_list_portrait);
- free (buf_img_separator);
- free (buf_go_up_string);
+ block_parser_free (block_parser);
return res;
}
@@ -526,16 +445,10 @@ write_html_image (TGallerySetup *setup,
TIndexItem *item,
TAlbum *parent_items)
{
- #define BUFFER_SIZE 65536
FILE *fin;
FILE *fout;
- char *buffer;
char *big_dst;
char *orig_dst;
- char *buf_img_fullsize_link;
- char *buf_exif_table;
- gboolean in_img_fullsize_link;
- gboolean in_exif_table;
TExifData *exif;
unsigned long img_big_w, img_big_h, img_orig_w, img_orig_h;
unsigned int item_index, real_item_index, real_total_items;
@@ -544,15 +457,17 @@ write_html_image (TGallerySetup *setup,
TIndexItem *tmp_item;
TAlbum *parent;
int i;
+ char *line;
+ char *block;
char *s1, *s2, *s3, *s4, *s5;
char *imgname, *preload_imgname;
char *title, *title_desc;
- char *b;
gboolean res, bb;
int level, old_parent_item_index;
gboolean override_title_meta;
gboolean image_fullsize;
ReplaceTable *replace_table;
+ BlockParser *block_parser;
fin = fopen (template_src, "r");
@@ -567,10 +482,10 @@ write_html_image (TGallerySetup *setup,
return FALSE;
}
- buffer = malloc (BUFFER_SIZE);
preload_imgname = NULL;
replace_table = replace_table_new ();
+ block_parser = block_parser_new ();
/* Get our index in the album */
item_index = 0;
@@ -609,12 +524,6 @@ write_html_image (TGallerySetup *setup,
big_dst = g_strconcat (s1, "/", IMG_BIG_DIR, "/", imgname, NULL);
orig_dst = g_strconcat (s1, "/", IMG_ORIG_DIR, "/", imgname, NULL);
g_free (s1);
- buf_img_fullsize_link = malloc (BUFFER_SIZE);
- memset (buf_img_fullsize_link, 0, BUFFER_SIZE);
- in_img_fullsize_link = FALSE;
- buf_exif_table = malloc (BUFFER_SIZE);
- memset (buf_exif_table, 0, BUFFER_SIZE);
- in_exif_table = FALSE;
res = TRUE;
/* Get EXIF data from the original image */
@@ -671,9 +580,9 @@ write_html_image (TGallerySetup *setup,
s3 = setup->site_title ? g_strdup_printf(" | %s", setup->site_title) : NULL;
s4 = g_strconcat (s1 ? s1 : "", s2, s3 ? s3 : "", NULL);
replace_table_add_key (replace_table, "PAGE_TITLE", s4);
- if (s1) g_free (s1);
- if (s2) g_free (s2);
- if (s3) g_free (s3);
+ g_free (s1);
+ g_free (s2);
+ g_free (s3);
g_free (s4);
/* Simple placeholders */
@@ -801,55 +710,41 @@ write_html_image (TGallerySetup *setup,
g_free (s1);
+ /* Setup block parser */
+ block_parser_register_key (block_parser, "IMG_FULLSIZE_LINK", "IMG_FULLSIZE_LINK");
+ block_parser_register_key (block_parser, "EXIF_TABLE", "EXIF_TABLE");
+
+
/* Read through the template and replace placeholders with real data */
while (! feof (fin)) {
- memset (buffer, 0, BUFFER_SIZE);
- if (! fgets (buffer, BUFFER_SIZE, fin))
+ line = block_parser_read_and_parse (block_parser, fin);
+ if (line == NULL)
break;
- /* Block placeholders */
- if (strstr (buffer, "<!-- $(BEGIN_IMG_FULLSIZE_LINK) -->")) {
- in_img_fullsize_link = TRUE;
- continue;
- }
- if (strstr (buffer, "<!-- $(END_IMG_FULLSIZE_LINK) -->")) {
- in_img_fullsize_link = FALSE;
- if (! image_fullsize) /* write down the block later in this cycle */
- continue;
- }
- if (strstr (buffer, "<!-- $(BEGIN_EXIF_TABLE) -->")) {
- in_exif_table = TRUE;
- continue;
- }
- if (strstr (buffer, "<!-- $(END_EXIF_TABLE) -->")) {
- in_exif_table = FALSE;
- if (! setup->show_exif_table)
- continue;
- }
- if (in_img_fullsize_link) {
- buf_img_fullsize_link = strncat (buf_img_fullsize_link, buffer, BUFFER_SIZE - strlen (buf_img_fullsize_link) - 2);
- continue;
+ /* Blocks */
+ if (block_parser_has_unused_data (block_parser, "IMG_FULLSIZE_LINK")) {
+ block = block_parser_get_data (block_parser, "IMG_FULLSIZE_LINK");
+ if (block) {
+ replace_table_process (&block, replace_table);
+ replace_table_add_key (replace_table, "IMG_FULLSIZE_LINK", image_fullsize ? block : "");
+ }
+ g_free (block);
}
- if (in_exif_table) {
- buf_exif_table = strncat (buf_exif_table, buffer, BUFFER_SIZE - strlen (buf_exif_table) - 2);
- continue;
+ if (block_parser_has_unused_data (block_parser, "EXIF_TABLE")) {
+ block = block_parser_get_data (block_parser, "EXIF_TABLE");
+ if (block) {
+ replace_table_process (&block, replace_table);
+ replace_table_add_key (replace_table, "EXIF_TABLE", setup->show_exif_table ? block : "");
+ }
+ g_free (block);
}
- /* Select apropriate line buffer */
- if (strstr (buffer, "<!-- $(END_IMG_FULLSIZE_LINK) -->") && image_fullsize) {
- b = strdup (buf_img_fullsize_link);
- } else
- if (strstr (buffer, "<!-- $(END_EXIF_TABLE) -->") && setup->show_exif_table) {
- b = strdup (buf_exif_table);
- } else
- b = strdup (buffer);
-
/* Replace all known tags */
- replace_table_process (&b, replace_table);
+ replace_table_process (&line, replace_table);
/* Write to file */
- bb = fputs (b, fout);
- free (b);
+ bb = fputs (line, fout);
+ g_free (line);
if (! bb) {
fprintf (stderr, "write_html_image: error writing to file \"%s\": %s\n", dst, strerror (errno));
res = FALSE;
@@ -859,18 +754,15 @@ write_html_image (TGallerySetup *setup,
fclose (fout);
fclose (fin);
- if (title) g_free (title);
- if (title_desc) g_free (title_desc);
- free (buffer);
+ g_free (title);
+ g_free (title_desc);
free (big_dst);
free (orig_dst);
- free (buf_img_fullsize_link);
- free (buf_exif_table);
g_free (imgname);
- if (preload_imgname)
- g_free (preload_imgname);
+ g_free (preload_imgname);
free_exif_data (exif);
replace_table_free (replace_table);
+ block_parser_free (block_parser);
return res;
}
diff --git a/src/generators-replace-table.c b/src/replace-table.c
index 23771fc..d3503c4 100644
--- a/src/generators-replace-table.c
+++ b/src/replace-table.c
@@ -25,23 +25,12 @@
#include <glib.h>
#include <glib/gprintf.h>
-#include "generators-replace-table.h"
+#include "replace-table.h"
#include "gallery-utils.h"
-#if 0
-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);
-}
-#endif
-
ReplaceTable *
replace_table_new ()
{
diff --git a/src/generators-replace-table.h b/src/replace-table.h
index d72d150..5c2b35e 100644
--- a/src/generators-replace-table.h
+++ b/src/replace-table.h
@@ -21,14 +21,9 @@
typedef GHashTable ReplaceTable;
-/*
- * write_html_album: process album and index template files
- *
- * template_src = template file of the album/index
- * dst = save generated file as
- * items = array of items in the album/index
- *
- */
+
+
+
ReplaceTable *replace_table_new ();
void replace_table_free (ReplaceTable *table);