summaryrefslogtreecommitdiff
path: root/src/generators.c
diff options
context:
space:
mode:
authorTomas Bzatek <tbzatek@users.sourceforge.net>2009-04-04 20:09:08 +0200
committerTomas Bzatek <tbzatek@users.sourceforge.net>2009-04-04 20:09:08 +0200
commitc572b8e7a250ecfba7836c55bb5eda4c9473280a (patch)
tree56e513682ede75e15dba8049240ff535ef183540 /src/generators.c
parentcad89a1f9b9e36d7490c3f8cd1449082a1285050 (diff)
downloadcataract-c572b8e7a250ecfba7836c55bb5eda4c9473280a.tar.xz
New block parser
Diffstat (limited to 'src/generators.c')
-rw-r--r--src/generators.c268
1 files changed, 80 insertions, 188 deletions
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;
}