From 9caacb6270bd4b4836c751cf86a25073f776986a Mon Sep 17 00:00:00 2001 From: Tomas Bzatek Date: Sun, 22 Feb 2009 13:55:03 +0100 Subject: Introduce tag replace table, part II. Things might be little faster in some cases and slower in others. Most of the CPU time is lost in image handling anyway. --- generators-replace-table.c | 27 +++- generators-replace-table.h | 1 + generators.c | 359 ++++++++++++++++++--------------------------- 3 files changed, 163 insertions(+), 224 deletions(-) diff --git a/generators-replace-table.c b/generators-replace-table.c index 562f9af..6a84c17 100644 --- a/generators-replace-table.c +++ b/generators-replace-table.c @@ -1,5 +1,5 @@ /* Cataract - Static web photo gallery generator - * Copyright (C) 2008 Tomas Bzatek + * Copyright (C) 2009 Tomas Bzatek * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -23,6 +23,7 @@ #include #include +#include #include "generators-replace-table.h" #include "gallery-utils.h" @@ -30,7 +31,7 @@ - +#if 0 static void replace_table_destroy_notify (gpointer data) { @@ -39,11 +40,12 @@ replace_table_destroy_notify (gpointer data) if ((gchar *) data) g_free ((gchar *) data); } +#endif 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); + return (ReplaceTable *) g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); } void @@ -51,7 +53,7 @@ replace_table_free (ReplaceTable *table) { g_return_if_fail (table != NULL); - g_hash_table_destroy (table); + g_hash_table_destroy (table); } @@ -79,6 +81,22 @@ replace_table_add_key_int (ReplaceTable *table, const gchar *tag, gint value) 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 @@ -112,6 +130,7 @@ replace_table_process_value (gpointer key, gpointer value, gpointer user_data) /* TODO: do something with tag_value? */ /* fix_entities (&s1); */ /* s1 = g_markup_escape_text (s4, -1); */ +/* line endings */ /* remove all tags as we're tag value */ while (strstr (*buffer, tag)) { str_replace (buffer, tag, tag_value, NULL); diff --git a/generators-replace-table.h b/generators-replace-table.h index 5646e5b..860a42b 100644 --- a/generators-replace-table.h +++ b/generators-replace-table.h @@ -42,6 +42,7 @@ void replace_table_free (ReplaceTable *table); */ 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 diff --git a/generators.c b/generators.c index 8ac90db..fd40490 100644 --- a/generators.c +++ b/generators.c @@ -222,12 +222,14 @@ write_html_album (TGallerySetup *setup, TIndexItem *item; TIndexItem *tmp_item; int level, old_parent_item_index; - gboolean res; + gboolean res, bb; int i; unsigned long img_w, img_h; char *img_src; char *thumb; unsigned int real_total_items; + ReplaceTable *global_replace_table; + ReplaceTable *local_replace_table; fin = fopen (template_src, "r"); @@ -254,6 +256,8 @@ write_html_album (TGallerySetup *setup, in_go_up_string = FALSE; res = TRUE; + global_replace_table = replace_table_new (); + /* Get number of real pictures in the list */ real_total_items = 0; for (i = 0; i < items->items->len; i++) { @@ -261,6 +265,75 @@ write_html_album (TGallerySetup *setup, if (tmp_item->type == INDEX_ITEM_TYPE_PICTURE) real_total_items++; } + replace_table_add_key_int (global_replace_table, "TOTAL_ITEMS", real_total_items); + + /* Page title */ + if (items->parent_index == NULL || setup->site_title == NULL) + s1 = g_strdup (setup->site_title ? setup->site_title : items->ID); + else + s1 = g_strdup_printf ("%s | %s", items->title, setup->site_title); + replace_table_add_key (global_replace_table, "PAGE_TITLE", s1); + g_free (s1); + + /* Simple placeholders */ + replace_table_add_key (global_replace_table, "ID", items->ID); + replace_table_add_key (global_replace_table, "TITLE", items->title); + replace_table_add_key (global_replace_table, "DESCRIPTION", items->desc); + replace_table_add_key (global_replace_table, "FOOTNOTE", items->footnote); + replace_table_add_key (global_replace_table, "FOOTER", setup->footer); + s1 = setup->use_inpage_links ? g_strdup_printf ("../index.html#i%d", items->parent_item_index + 1) : g_strdup ("../index.html"); + replace_table_add_key (global_replace_table, "GO_UP_LINK", s1); + g_free (s1); + + /* Navigation bar */ + s1 = g_strdup (items->ID); + /* TODO: check */ + fix_entities (&s1); + old_parent_item_index = items->parent_item_index + 1; + parent = items->parent_index; + level = 1; + while (parent) { + s3 = make_string ("../", level); + s4 = g_strdup (parent->ID); + /* TODO: check */ + fix_entities (&s4); + s5 = setup->use_inpage_links ? g_strdup_printf ("#i%d", old_parent_item_index) : g_strdup (""); + s2 = g_strdup_printf ("%s > %s", s3, s5, s4, s1); + free (s3); + g_free (s1); + g_free (s4); + g_free (s5); + s1 = s2; + old_parent_item_index = parent->parent_item_index + 1; + parent = parent->parent_index; + level++; + } + replace_table_add_key (global_replace_table, "NAV_BAR", s1); + g_free (s1); + + /* META tags */ + s1 = g_strdup_printf ("\t\n", APP_VERSION); + if (setup->meta_author || items->meta_author) { + s2 = g_strdup_printf ("%s\t\n", s1, + items->meta_author ? items->meta_author : setup->meta_author); + g_free (s1); + s1 = s2; + } + if (setup->meta_description || items->meta_description) { + s2 = g_strdup_printf ("%s\t\n", s1, + items->meta_description? items->meta_description : setup->meta_description); + g_free (s1); + s1 = s2; + } + if (setup->meta_keywords || items->meta_keywords) { + s2 = g_strdup_printf ("%s\t\n", s1, + items->meta_keywords ? items->meta_keywords : setup->meta_keywords); + g_free (s1); + s1 = s2; + } + replace_table_add_key (global_replace_table, "CGG_META_TAGS", s1); + g_free (s1); + /* Read through the template and replace placeholders with real data */ while (! feof (fin)) @@ -268,177 +341,58 @@ write_html_album (TGallerySetup *setup, memset (buffer, 0, BUFFER_SIZE); if (! fgets (buffer, BUFFER_SIZE, fin)) break; - if (buffer == NULL) - continue; - b = strdup (buffer); - /* Block placeholders */ if (in_img_list && (strstr (buffer, ""))) { in_img_list_landscape = TRUE; - free (b); continue; } if (in_img_list && (strstr (buffer, ""))) { in_img_list_landscape = FALSE; - free (b); continue; } if (in_img_list && (strstr (buffer, ""))) { in_img_list_portrait = TRUE; - free (b); continue; } if (in_img_list && (strstr (buffer, ""))) { in_img_list_portrait = FALSE; - free (b); continue; } if (in_img_list && (strstr (buffer, ""))) { in_img_separator = TRUE; - free (b); continue; } if (in_img_list && (strstr (buffer, ""))) { in_img_separator = FALSE; - free (b); continue; } if (in_img_list && in_img_list_landscape) { - buf_img_list_landscape = strncat (buf_img_list_landscape, b, BUFFER_SIZE - strlen (buf_img_list_landscape) - 2); - free (b); + 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, b, BUFFER_SIZE - strlen (buf_img_list_portrait) - 2); - free (b); + 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, b, BUFFER_SIZE - strlen (buf_img_separator) - 2); - free (b); + buf_img_separator = strncat (buf_img_separator, buffer, BUFFER_SIZE - strlen (buf_img_separator) - 2); continue; } if (strstr (buffer, "")) { memset (buf_go_up_string, 0, BUFFER_SIZE); in_go_up_string = TRUE; - free (b); continue; } - if (in_go_up_string && (strstr (buffer, ""))) { + if (in_go_up_string && strstr (buffer, "")) { in_go_up_string = FALSE; - free (b); - /* print the "Go Up" string if not toplevel */ - if (items->parent_index) { - b = strdup (buf_go_up_string); - if (strstr (b, "$(GO_UP_LINK)")) { - s1 = setup->use_inpage_links ? g_strdup_printf ("../index.html#i%d", items->parent_item_index + 1) : g_strdup ("../index.html"); - str_replace (&b, "$(GO_UP_LINK)", s1, NULL); - g_free (s1); - } - } - else continue; + if (! items->parent_index) + continue; } if (in_go_up_string) { - buf_go_up_string = strncat (buf_go_up_string, b, BUFFER_SIZE - strlen (buf_go_up_string) - 2); - free (b); - continue; - } - - - /* Simple placeholders */ - if (strstr (b, "")) { - if (items->parent_index == NULL || setup->site_title == NULL) - s1 = g_strdup (setup->site_title ? setup->site_title : items->ID); - else - s1 = g_strdup_printf ("%s | %s", items->title, setup->site_title); - s2 = g_markup_escape_text (s1, -1); - str_replace (&b, "", s2, NULL); - g_free (s2); - g_free (s1); - } - if (strstr (b, "") && items->ID) { - s1 = g_strdup (items->ID); - fix_entities (&s1); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "") && items->title) { - s1 = g_strdup (items->title); - fix_entities (&s1); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "") && items->desc) { - s1 = g_strdup (items->desc); - fix_entities (&s1); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "") && items->footnote) { - s1 = g_strdup (items->footnote); - fix_entities (&s1); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "") && setup->footer) { - s1 = g_strdup (setup->footer); - fix_entities (&s1); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "")) { - s1 = g_strdup_printf ("\t\n", APP_VERSION); - if (setup->meta_author || items->meta_author) { - s2 = g_strdup_printf ("%s\t\n", s1, - items->meta_author ? items->meta_author : setup->meta_author); - g_free (s1); - s1 = s2; - } - if (setup->meta_description || items->meta_description) { - s2 = g_strdup_printf ("%s\t\n", s1, - items->meta_description? items->meta_description : setup->meta_description); - g_free (s1); - s1 = s2; - } - if (setup->meta_keywords || items->meta_keywords) { - s2 = g_strdup_printf ("%s\t\n", s1, - items->meta_keywords ? items->meta_keywords : setup->meta_keywords); - g_free (s1); - s1 = s2; - } - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "")) { - s1 = g_strdup_printf ("%d", real_total_items); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "")) { - s1 = g_strdup (items->ID); - fix_entities (&s1); - old_parent_item_index = items->parent_item_index + 1; - parent = items->parent_index; - level = 1; - while (parent) { - s3 = make_string ("../", level); - s4 = g_strdup (parent->ID); - fix_entities (&s4); - s5 = setup->use_inpage_links ? g_strdup_printf ("#i%d", old_parent_item_index) : g_strdup (""); - s2 = g_strdup_printf ("%s > %s", s3, s5, s4, s1); - free (s3); - g_free (s1); - g_free (s4); - g_free (s5); - s1 = s2; - old_parent_item_index = parent->parent_item_index + 1; - parent = parent->parent_index; - level++; - } - str_replace (&b, "", s1, NULL); - g_free (s1); + buf_go_up_string = strncat (buf_go_up_string, buffer, BUFFER_SIZE - strlen (buf_go_up_string) - 2); + continue; } /* Image list, nested placeholders */ @@ -450,10 +404,8 @@ write_html_album (TGallerySetup *setup, in_img_list_landscape = FALSE; in_img_list_portrait = FALSE; in_img_separator = FALSE; - free (b); continue; } - if (in_img_list && (strstr (buffer, ""))) { in_img_list = FALSE; in_img_list_landscape = FALSE; @@ -465,19 +417,19 @@ write_html_album (TGallerySetup *setup, item = g_ptr_array_index (items->items, i); if (item == NULL) { fprintf (stderr, "write_html_index: error getting item %d\n", i); - free (b); continue; } /* Generate the images (preview, original, thumbnail) */ + local_replace_table = replace_table_new (); img_w = 0; img_h = 0; - img_src = NULL; - thumb = NULL; s1 = NULL; switch (item->type) { case INDEX_ITEM_TYPE_PICTURE: + img_src = NULL; + thumb = NULL; generate_image (setup, items, item, i, dst, &img_w, &img_h, &img_src, &thumb); /* Skip HTML code generation if it's a hidden item */ if (! item->hidden) { @@ -486,95 +438,69 @@ write_html_album (TGallerySetup *setup, else s1 = strdup (buf_img_list_portrait); - if (strstr (s1, "")) { - s2 = g_strconcat (item->path, "/index.html", NULL); - str_replace (&s1, "", s2, NULL); - g_free (s2); - } - if (strstr (s1, "")) { - s2 = g_strconcat (img_src, ".html", NULL); - str_replace (&s1, "", s2, NULL); - g_free (s2); - } - if (strstr (s1, "") && item->title) { - s2 = g_strdup (item->title); - fix_entities (&s2); - str_replace (&s1, "", s2, NULL); - g_free (s2); - } - if (strstr (s1, "") && item->title_description) { - s2 = g_strdup (item->title_description); - fix_entities (&s2); - str_replace (&s1, "", s2, NULL); - g_free (s2); - } - if (strstr (s1, "")) { + replace_table_add_key_printf (local_replace_table, "ALBUM_SUBPATH", "%s/index.html", item->path); + replace_table_add_key_printf (local_replace_table, "IMG_SUBPAGE", "%s.html", img_src); + replace_table_add_key (local_replace_table, "IMG_TITLE", item->title); + replace_table_add_key (local_replace_table, "IMG_DESCRIPTION", item->title_description); + replace_table_add_key_printf (local_replace_table, "IMG_LIST_ID", "i%d", i + 1); + replace_table_add_key_printf (local_replace_table, "IMG_THUMBNAIL", "%s/%s", THUMBNAIL_DIR, thumb); + replace_table_add_key (local_replace_table, "IMG_FILENAME", img_src); + if (items->type == GALLERY_TYPE_INDEX) { s3 = g_strconcat (items->base_dir, "/", item->path, "/index.xml", NULL); - s2 = g_strdup_printf ("%d", get_album_objects_count (s3)); - str_replace (&s1, "", s2, NULL); - g_free (s2); + replace_table_add_key_int (local_replace_table, "ALBUM_NUM_ITEMS", get_album_objects_count (s3)); g_free (s3); } - if (strstr (s1, "")) { - s2 = g_strdup_printf ("i%d", i + 1); - str_replace (&s1, "", s2, NULL); - g_free (s2); - } - if (strstr (s1, "")) { - s2 = g_strconcat (THUMBNAIL_DIR, "/", thumb, NULL); - str_replace (&s1, "", s2, NULL); - g_free (s2); - } - if (strstr (s1, "")) - str_replace (&s1, "", img_src, NULL); } + if (img_src) + g_free (img_src); + if (thumb) + g_free (thumb); break; case INDEX_ITEM_TYPE_SEPARATOR: s1 = strdup (buf_img_separator); - if (strstr (s1, "") && item->title) { - s2 = g_strdup (item->title); - fix_entities (&s2); - str_replace (&s1, "", s2, NULL); - g_free (s2); - } + replace_table_add_key (local_replace_table, "LIST_SEPARATOR_TITLE", item->title); break; } - #ifdef __DEBUG_ALL__ - printf("***** %s ******\n", s1); - #endif + bb = TRUE; if (s1) { - if (! fputs (s1, fout)) { - fprintf (stderr, "write_html_index: error writing to file \"%s\": %s\n", dst, strerror (errno)); - res = FALSE; - free (s1); - break; - } + replace_table_process (&s1, local_replace_table); + bb = fputs (s1, fout); free (s1); } - if (img_src) - g_free (img_src); - if (thumb) - g_free (thumb); + 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; + } } - free (b); continue; /* no need to write anything */ } - /* Write the generated string to target file */ - if (! fputs (b, fout)) { + /* Select apropriate line buffer */ + if (strstr (buffer, "") && items->parent_index) { + b = strdup (buf_go_up_string); + } else + b = strdup (buffer); + + /* Replace all known tags */ + replace_table_process (&b, global_replace_table); + + /* Write to file */ + bb = fputs (b, fout); + free (b); + if (! bb) { fprintf (stderr, "write_html_index: error writing to file \"%s\": %s\n", dst, strerror (errno)); res = FALSE; - free (b); break; } - free (b); } fclose (fout); fclose (fin); - + replace_table_free (global_replace_table); free (buffer); free (buf_img_list_landscape); free (buf_img_list_portrait); @@ -622,7 +548,7 @@ write_html_image (TGallerySetup *setup, char *imgname, *preload_imgname; char *title, *title_desc; char *b; - gboolean res; + gboolean res, bb; int level, old_parent_item_index; gboolean override_title_meta; gboolean image_fullsize; @@ -758,12 +684,8 @@ write_html_image (TGallerySetup *setup, replace_table_add_key_int (replace_table, "IMG_SIZE_BIG_H", img_big_h); replace_table_add_key_int (replace_table, "IMG_SIZE_ORIG_W", img_orig_w); replace_table_add_key_int (replace_table, "IMG_SIZE_ORIG_H", img_orig_h); - s1 = g_strdup_printf ("%s/%s", IMG_BIG_DIR, imgname); - replace_table_add_key (replace_table, "IMG_SRC_BIG", s1); - g_free (s1); - s1 = g_strdup_printf ("%s/%s", IMG_ORIG_DIR, imgname); - replace_table_add_key (replace_table, "IMG_SRC_FULL", s1); - g_free (s1); + replace_table_add_key_printf (replace_table, "IMG_SRC_BIG", "%s/%s", IMG_BIG_DIR, imgname); + replace_table_add_key_printf (replace_table, "IMG_SRC_FULL", "%s/%s", IMG_ORIG_DIR, imgname); replace_table_add_key (replace_table, "IMG_SRC_PRELOAD", preload_imgname ? preload_imgname : ""); /* Navigation bar */ @@ -814,18 +736,14 @@ write_html_image (TGallerySetup *setup, /* Next/Previous links */ if (next_item) { s2 = g_path_get_basename ((next_item->path == NULL && next_item->preview) ? next_item->preview : next_item->path); - s1 = g_strconcat (s2, ".html", NULL); - replace_table_add_key (replace_table, "LINK_NEXT", s1); - g_free (s1); + replace_table_add_key_printf (replace_table, "LINK_NEXT", "%s.html", s2); g_free (s2); } else replace_table_add_key (replace_table, "LINK_NEXT", "index.html"); if (previous_item) { s2 = g_path_get_basename ((previous_item->path == NULL && previous_item->preview) ? previous_item->preview : previous_item->path); - s1 = g_strconcat (s2, ".html", NULL); - replace_table_add_key (replace_table, "LINK_PREV", s1); - g_free (s1); + replace_table_add_key_printf (replace_table, "LINK_PREV", "%s.html", s2); g_free (s2); } else @@ -863,38 +781,39 @@ write_html_image (TGallerySetup *setup, memset (buffer, 0, BUFFER_SIZE); if (! fgets (buffer, BUFFER_SIZE, fin)) break; - b = strdup (buffer); /* Block placeholders */ if (strstr (buffer, "")) { in_img_fullsize_link = TRUE; - free (b); continue; } if (strstr (buffer, "")) { in_img_fullsize_link = FALSE; - free (b); - if (image_fullsize) - b = strdup (buf_img_fullsize_link); - else continue; + if (! image_fullsize) /* write down the block later in this cycle */ + continue; } if (in_img_fullsize_link) { - buf_img_fullsize_link = strncat (buf_img_fullsize_link, b, BUFFER_SIZE - strlen (buf_img_fullsize_link) - 2); - free (b); + buf_img_fullsize_link = strncat (buf_img_fullsize_link, buffer, BUFFER_SIZE - strlen (buf_img_fullsize_link) - 2); continue; } + /* Select apropriate line buffer */ + if (strstr (buffer, "") && image_fullsize) { + b = strdup (buf_img_fullsize_link); + } else + b = strdup (buffer); + /* Replace all known tags */ replace_table_process (&b, replace_table); /* Write to file */ - if (! fputs (b, fout)) { + bb = fputs (b, fout); + free (b); + if (! bb) { fprintf (stderr, "write_html_image: error writing to file \"%s\": %s\n", dst, strerror (errno)); res = FALSE; - free (b); break; } - free (b); } fclose (fout); -- cgit v1.2.3