diff options
| -rw-r--r-- | gallery-utils.c | 149 | ||||
| -rw-r--r-- | gallery-utils.h | 12 | ||||
| -rw-r--r-- | generators.c | 186 |
3 files changed, 252 insertions, 95 deletions
diff --git a/gallery-utils.c b/gallery-utils.c index 6de0224..ce80a4f 100644 --- a/gallery-utils.c +++ b/gallery-utils.c @@ -23,39 +23,83 @@ #include "gallery-utils.h" + +static char * +strstr_exclude (const char *haystack, const char *needle, const char *exclude_when) +{ + const char *src; + char *found; + + src = haystack; + + while (src && strstr (src, needle)) { + found = strstr (src, needle); + + /* skip if found 'exclude_when' at the same place as 'search' */ + if (exclude_when && (found == strstr (haystack, exclude_when))) { + src += strlen (exclude_when); + continue; + } + else + return found; + } + + return NULL; +} + /* * str_replace: replace substring 'search' with a 'replace' string - * - only one occurence of the string is replaced - * - reallocates the original string + * - multiple occurences of the string are replaced + * - specify 'exclude_when' if you want to skip replace when a string found at the place of 'search' + * - reallocates the original string */ void -str_replace (char **dst, const char *search, const char *replace) +str_replace (char **dst, const char *search, const char *replace, const char *exclude_when) { - char *res; - char *tms; - int new_str_len; + #define REPLACE_MAX_LENGTH 32768 + static char d[REPLACE_MAX_LENGTH]; + char *src; + char *found; + int i; - /* is 'search' present in 'dst'? */ - res = strstr (*dst, search); - if (res == NULL) - return; + /* TODO: add range checking */ - new_str_len = strlen (*dst) - strlen (search) + strlen (replace) + 1; + if (strstr (*dst, search) == NULL || strlen (*dst) == 0 || + strlen (search) == 0 || strlen (replace) == 0) + return; - tms = malloc (new_str_len); - memset (tms, 0, new_str_len); + i = 0; + src = *dst; + while (strstr_exclude (src, search, exclude_when)) { + found = strstr_exclude (src, search, exclude_when); + + /* copy the data between search string */ + if (found > src) { + memcpy (&d[i], src, found - src); + i += found - src; + } + + /* copy replace string instead */ + memcpy (&d[i], replace, strlen (replace)); + i += strlen (replace); + src = found + strlen (search); + } - memcpy (tms, *dst, res - *dst); - memcpy ((void*) ((long int) tms + (long int) res - (long int) *dst), - replace, - strlen (replace)); - memcpy ((void*) ((long int) tms + (long int) res - (long int) *dst + strlen (replace)), - (void*) ((long int) res + strlen (search)), - strlen (res) - strlen (search)); + /* copy the rest */ + if (src) { + memcpy (&d[i], src, strlen (src)); + i += strlen (src); + } + d[i] = 0x0; + +#ifdef __DEBUG_ALL__ + printf ("str_replace('%s', '%s') = '%s' --> '%s'\n", search, replace, *dst, &d[0]); +#endif + /* return fixed string */ free (*dst); - *dst = tms; + *dst = g_strdup (&d[0]); } @@ -120,3 +164,66 @@ make_string (const char* substr, const int count) } +/* + * fix_entities: replace all invalid & entities with & + * - returns newly allocated string + */ +void +fix_entities (char **str) +{ + static char d[REPLACE_MAX_LENGTH]; + char *src; + char *found; + char *scan; + int i; + + /* TODO: add range checking */ + + if (! *str || strstr (*str, "&") == NULL) + return; + + i = 0; + src = *str; + while (strstr (src, "&")) { + found = strstr (src, "&"); + + /* copy the data between search string */ + memcpy (&d[i], src, found - src + 1); + i += found - src + 1; + + /* scroll to next whitespace */ + scan = found + 1; + while (scan && ( + (*scan >= 0x41 && *scan <= 0x5a) || (*scan >= 0x61 && *scan <= 0x7a) || /* A-Z, a-z */ + (*scan >= 0x30 && *scan <= 0x39) || (*scan == 0x23) /* 0-9, # */ + )) + scan++; + + if (scan && (*scan == 0x3b)) { + /* this is semi-colon, correctly closed entity */ + /* -- ignore */ + } + else { + /* replace with & */ + memcpy (&d[i], "amp;", 4); + i += 4; + } + src = found + 1; + } + + /* copy the rest */ + if (src) { + memcpy (&d[i], src, strlen (src)); + i += strlen (src); + } + d[i] = 0x0; + + +#ifdef __DEBUG_ALL__ + printf ("fix_entities: '%s' --> '%s'\n", *str, &d[0]); +#endif + + /* return fixed string */ + free (*str); + *str = g_strdup (&d[0]); +} diff --git a/gallery-utils.h b/gallery-utils.h index 1547c90..95dbcff 100644 --- a/gallery-utils.h +++ b/gallery-utils.h @@ -23,10 +23,11 @@ /* * str_replace: replace substring 'search' with a 'replace' string - * - only one occurence of the string is replaced - * - reallocates the original string + * - multiple occurences of the string are replaced + * - specify 'exclude_when' if you want to skip replace when a string found at the place of 'search' + * - reallocates the original string */ -void str_replace (char **dst, const char *search, const char *replace); +void str_replace (char **dst, const char *search, const char *replace, const char *exclude_when); /* * copy_file: copy file from src to dst @@ -39,3 +40,8 @@ gboolean copy_file (const char *src, const char *dst); */ char *make_string (const char* substr, const int count); +/* + * fix_entities: replace all invalid & entities with & + * - returns newly allocated string + */ +void fix_entities (char **str);
\ No newline at end of file diff --git a/generators.c b/generators.c index a478389..18fa25f 100644 --- a/generators.c +++ b/generators.c @@ -36,11 +36,11 @@ */ void generate_image (TGallerySetup *setup, - TAlbum *items, - TIndexItem *item, + TAlbum *items, + TIndexItem *item, const char *dst, - unsigned long *img_w, unsigned long *img_h, - const char **img_src) + unsigned long *img_w, unsigned long *img_h, + const char **img_src) { unsigned long new_w, new_h; char *thumb_dst; @@ -188,7 +188,7 @@ write_html_album (TGallerySetup *setup, gboolean in_img_list_portrait; gboolean in_go_up_string; char *b; - char *s1, *s2, *s3; + char *s1, *s2, *s3, *s4; TAlbum *parent; TIndexItem *item; int level; @@ -285,33 +285,53 @@ write_html_album (TGallerySetup *setup, /* Simple placeholders */ - if (strstr (b, "<!-- $(ID) -->") && items->ID) - str_replace (&b, "<!-- $(ID) -->", items->ID); - if (strstr (b, "<!-- $(TITLE) -->") && items->title) - str_replace (&b, "<!-- $(TITLE) -->", items->title); - if (strstr (b, "<!-- $(DESCRIPTION) -->") && items->desc) - str_replace (&b, "<!-- $(DESCRIPTION) -->", items->desc); - if (strstr (b, "<!-- $(FOOTER) -->") && setup->footer) - str_replace (&b, "<!-- $(FOOTER) -->", setup->footer); + if (strstr (b, "<!-- $(ID) -->") && items->ID) { + s1 = g_strdup (items->ID); + fix_entities (&s1); + str_replace (&b, "<!-- $(ID) -->", s1, NULL); + g_free (s1); + } + if (strstr (b, "<!-- $(TITLE) -->") && items->title) { + s1 = g_strdup (items->title); + fix_entities (&s1); + str_replace (&b, "<!-- $(TITLE) -->", s1, NULL); + g_free (s1); + } + if (strstr (b, "<!-- $(DESCRIPTION) -->") && items->desc) { + s1 = g_strdup (items->desc); + fix_entities (&s1); + str_replace (&b, "<!-- $(DESCRIPTION) -->", s1, NULL); + g_free (s1); + } + if (strstr (b, "<!-- $(FOOTER) -->") && setup->footer) { + s1 = g_strdup (setup->footer); + fix_entities (&s1); + str_replace (&b, "<!-- $(FOOTER) -->", s1, NULL); + g_free (s1); + } if (strstr (b, "<!-- $(TOTAL_ITEMS) -->")) { s1 = g_strdup_printf ("%d", items->items->len); - str_replace (&b, "<!-- $(TOTAL_ITEMS) -->", s1); + str_replace (&b, "<!-- $(TOTAL_ITEMS) -->", s1, NULL); g_free (s1); } if (strstr (b, "<!-- $(NAV_BAR) -->")) { s1 = g_strdup (items->ID); + fix_entities (&s1); parent = items->parent_index; level = 1; while (parent) { - s3 = make_string ("../", level); - s2 = g_strconcat ("<a href=\"", s3, "index.html\">", parent->ID, "</a> > ", s1, NULL); + s3 = make_string ("../", level); + s4 = g_strdup (parent->ID); + fix_entities (&s4); + s2 = g_strconcat ("<a href=\"", s3, "index.html\">", s4, "</a> > ", s1, NULL); free (s3); g_free (s1); + g_free (s4); s1 = s2; parent = parent->parent_index; level++; } - str_replace (&b, "<!-- $(NAV_BAR) -->", s1); + str_replace (&b, "<!-- $(NAV_BAR) -->", s1, NULL); g_free (s1); } @@ -353,36 +373,44 @@ write_html_album (TGallerySetup *setup, else s1 = strdup (buf_img_list_portrait); - while (strstr (s1, "<!-- $(ALBUM_SUBPATH) -->")) { + if (strstr (s1, "<!-- $(ALBUM_SUBPATH) -->")) { s2 = g_strconcat (item->path, "/index.html", NULL); - str_replace (&s1, "<!-- $(ALBUM_SUBPATH) -->", s2); + str_replace (&s1, "<!-- $(ALBUM_SUBPATH) -->", s2, NULL); g_free (s2); } - while (strstr (s1, "<!-- $(IMG_SUBPAGE) -->")) { + if (strstr (s1, "<!-- $(IMG_SUBPAGE) -->")) { s2 = g_strconcat (img_src, ".html", NULL); - str_replace (&s1, "<!-- $(IMG_SUBPAGE) -->", s2); + str_replace (&s1, "<!-- $(IMG_SUBPAGE) -->", s2, NULL); g_free (s2); } - while (strstr (s1, "<!-- $(IMG_TITLE) -->") && item->title) - str_replace (&s1, "<!-- $(IMG_TITLE) -->", item->title); - while (strstr (s1, "<!-- $(IMG_DESCRIPTION) -->") && item->title_description) - str_replace (&s1, "<!-- $(IMG_DESCRIPTION) -->", item->title_description); - while (strstr(s1, "<!-- $(ALBUM_NUM_ITEMS) -->")) { + if (strstr (s1, "<!-- $(IMG_TITLE) -->") && item->title) { + s2 = g_strdup (item->title); + fix_entities (&s2); + str_replace (&s1, "<!-- $(IMG_TITLE) -->", s2, NULL); + g_free (s2); + } + if (strstr (s1, "<!-- $(IMG_DESCRIPTION) -->") && item->title_description) { + s2 = g_strdup (item->title_description); + fix_entities (&s2); + str_replace (&s1, "<!-- $(IMG_DESCRIPTION) -->", s2, NULL); + g_free (s2); + } + if (strstr(s1, "<!-- $(ALBUM_NUM_ITEMS) -->")) { s3 = g_strconcat (items->base_dir, "/", item->path, "/index.xml", NULL); - s2 = g_strdup_printf ("%d", get_album_objects_count(s3)); - str_replace (&s1, "<!-- $(ALBUM_NUM_ITEMS) -->", s2); + s2 = g_strdup_printf ("%d", get_album_objects_count(s3)); + str_replace (&s1, "<!-- $(ALBUM_NUM_ITEMS) -->", s2, NULL); g_free (s2); g_free (s3); } - while (strstr (s1, "<!-- $(IMG_THUMBNAIL) -->")) { + if (strstr (s1, "<!-- $(IMG_THUMBNAIL) -->")) { s3 = g_path_get_basename (img_src); s2 = g_strconcat (THUMBNAIL_DIR, "/", s3, NULL); - str_replace (&s1, "<!-- $(IMG_THUMBNAIL) -->", s2); + str_replace (&s1, "<!-- $(IMG_THUMBNAIL) -->", s2, NULL); g_free (s2); g_free (s3); } - while (strstr (s1, "<!-- $(IMG_FILENAME) -->")) - str_replace (&s1, "<!-- $(IMG_FILENAME) -->", img_src); + if (strstr (s1, "<!-- $(IMG_FILENAME) -->")) + str_replace (&s1, "<!-- $(IMG_FILENAME) -->", img_src, NULL); #ifdef __DEBUG_ALL__ @@ -454,7 +482,7 @@ write_html_image (TGallerySetup *setup, TIndexItem *next_item; TAlbum *parent; int i; - char *s1, *s2, *s3; + char *s1, *s2, *s3, *s4; char *imgname; char *b; gboolean res; @@ -541,145 +569,161 @@ write_html_image (TGallerySetup *setup, /* Simple placeholders */ if (strstr (b, "<!-- $(FILE_NAME) -->")) - str_replace (&b, "<!-- $(FILE_NAME) -->", imgname); - if (strstr (b, "<!-- $(TITLE) -->") && item->title) - str_replace (&b, "<!-- $(TITLE) -->", item->title); - if (strstr (b, "<!-- $(DESCRIPTION) -->") && item->title_description) - str_replace (&b, "<!-- $(DESCRIPTION) -->", item->title_description); + str_replace (&b, "<!-- $(FILE_NAME) -->", imgname, NULL); + if (strstr (b, "<!-- $(TITLE) -->") && item->title) { + s1 = g_strdup (item->title); + fix_entities (&s1); + str_replace (&b, "<!-- $(TITLE) -->", s1, NULL); + g_free (s1); + } + if (strstr (b, "<!-- $(DESCRIPTION) -->") && item->title_description) { + s1 = g_strdup (item->title_description); + fix_entities (&s1); + str_replace (&b, "<!-- $(DESCRIPTION) -->", s1, NULL); + g_free (s1); + } if (strstr (b, "<!-- $(TOTAL_ITEMS) -->")) { s1 = g_strdup_printf ("%d", parent_items->items->len); - str_replace (&b, "<!-- $(TOTAL_ITEMS) -->", s1); + str_replace (&b, "<!-- $(TOTAL_ITEMS) -->", s1, NULL); g_free (s1); } if (strstr (b, "<!-- $(FILE_NO) -->")) { s1 = g_strdup_printf ("%d", item_index); - str_replace(&b, "<!-- $(FILE_NO) -->", s1); + str_replace(&b, "<!-- $(FILE_NO) -->", s1, NULL); g_free (s1); } if (strstr (b, "<!-- $(NAV_BAR) -->")) { - s1 = g_strconcat (item->title, " (", imgname, ")", NULL); +// s1 = g_strconcat (item->title, " (", imgname, ")", NULL); + s1 = g_strdup (imgname); parent = parent_items; level = 0; while (parent) { s3 = make_string ("../", level); - s2 = g_strconcat ("<a href=\"", s3, "index.html\">", parent->ID, "</a> > ", s1, NULL); + s4 = g_strdup (parent->ID); + fix_entities (&s4); + s2 = g_strconcat ("<a href=\"", s3, "index.html\">", s4, "</a> > ", s1, NULL); free (s3); g_free (s1); + g_free (s4); s1 = s2; parent = parent->parent_index; level++; } - str_replace (&b, "<!-- $(NAV_BAR) -->", s1); + str_replace (&b, "<!-- $(NAV_BAR) -->", s1, NULL); g_free (s1); } if (strstr (b, "<!-- $(IMG_SRC_BIG) -->")) { s1 = g_strconcat (IMG_BIG_DIR, "/", imgname, NULL); - str_replace (&b, "<!-- $(IMG_SRC_BIG) -->", s1); + str_replace (&b, "<!-- $(IMG_SRC_BIG) -->", s1, NULL); g_free (s1); } if (strstr(b, "<!-- $(IMG_SRC_FULL) -->")) { s1 = g_strconcat (IMG_ORIG_DIR, "/", imgname, NULL); - str_replace (&b, "<!-- $(IMG_SRC_FULL) -->", s1); + str_replace (&b, "<!-- $(IMG_SRC_FULL) -->", s1, NULL); g_free (s1); } if (strstr(b, "<!-- $(IMG_SIZE_BIG_W) -->")) { s1 = g_strdup_printf ("%lu", img_big_w); - str_replace (&b, "<!-- $(IMG_SIZE_BIG_W) -->", s1); + str_replace (&b, "<!-- $(IMG_SIZE_BIG_W) -->", s1, NULL); g_free (s1); } if (strstr(b, "<!-- $(IMG_SIZE_BIG_H) -->")) { s1 = g_strdup_printf ("%lu", img_big_h); - str_replace (&b, "<!-- $(IMG_SIZE_BIG_H) -->", s1); + str_replace (&b, "<!-- $(IMG_SIZE_BIG_H) -->", s1, NULL); g_free (s1); } if (strstr(b, "<!-- $(IMG_SIZE_ORIG_W) -->")) { s1 = g_strdup_printf ("%lu", img_orig_w); - str_replace (&b, "<!-- $(IMG_SIZE_ORIG_W) -->", s1); + str_replace (&b, "<!-- $(IMG_SIZE_ORIG_W) -->", s1, NULL); g_free (s1); } if (strstr(b, "<!-- $(IMG_SIZE_ORIG_H) -->")) { s1 = g_strdup_printf ("%lu", img_orig_h); - str_replace (&b, "<!-- $(IMG_SIZE_ORIG_H) -->", s1); + str_replace (&b, "<!-- $(IMG_SIZE_ORIG_H) -->", s1, NULL); g_free (s1); } if (strstr (b, "<!-- $(EXIF_ISO) -->")) { if (exif->iso) - str_replace (&b, "<!-- $(EXIF_ISO) -->", exif->iso); + str_replace (&b, "<!-- $(EXIF_ISO) -->", exif->iso, NULL); else - str_replace (&b, "<!-- $(EXIF_ISO) -->", "??"); + str_replace (&b, "<!-- $(EXIF_ISO) -->", "??", NULL); } if (strstr (b, "<!-- $(EXIF_TIME) -->")) { if (exif->exposure) - str_replace (&b, "<!-- $(EXIF_TIME) -->", exif->exposure); + str_replace (&b, "<!-- $(EXIF_TIME) -->", exif->exposure, NULL); else - str_replace (&b, "<!-- $(EXIF_TIME) -->", "??"); + str_replace (&b, "<!-- $(EXIF_TIME) -->", "??", NULL); } if (strstr (b, "<!-- $(EXIF_APERTURE) -->")) { if (exif->aperture) - str_replace (&b, "<!-- $(EXIF_APERTURE) -->", exif->aperture); + str_replace (&b, "<!-- $(EXIF_APERTURE) -->", exif->aperture, NULL); else - str_replace (&b, "<!-- $(EXIF_APERTURE) -->", "??"); + str_replace (&b, "<!-- $(EXIF_APERTURE) -->", "??", NULL); } if (strstr (b, "<!-- $(EXIF_FOCAL_LENGTH) -->")) { if (exif->focal_length) - str_replace (&b, "<!-- $(EXIF_FOCAL_LENGTH) -->", exif->focal_length); + str_replace (&b, "<!-- $(EXIF_FOCAL_LENGTH) -->", exif->focal_length, NULL); else - str_replace (&b, "<!-- $(EXIF_FOCAL_LENGTH) -->", "??"); + str_replace (&b, "<!-- $(EXIF_FOCAL_LENGTH) -->", "??", NULL); } if (strstr (b, "<!-- $(EXIF_FLASH) -->")) { if (exif->flash) - str_replace (&b, "<!-- $(EXIF_FLASH) -->", exif->flash); + str_replace (&b, "<!-- $(EXIF_FLASH) -->", exif->flash, NULL); else - str_replace (&b, "<!-- $(EXIF_FLASH) -->", "??"); + str_replace (&b, "<!-- $(EXIF_FLASH) -->", "??", NULL); } if (strstr (b, "<!-- $(EXIF_DATE) -->")) { if (exif->datetime) - str_replace (&b, "<!-- $(EXIF_DATE) -->", exif->datetime); + str_replace (&b, "<!-- $(EXIF_DATE) -->", exif->datetime, NULL); else - str_replace (&b, "<!-- $(EXIF_DATE) -->", "??"); + str_replace (&b, "<!-- $(EXIF_DATE) -->", "??", NULL); } if (strstr (b, "<!-- $(EXIF_CAMERA_MODEL) -->")) { if (exif->camera_model) - str_replace (&b, "<!-- $(EXIF_CAMERA_MODEL) -->", exif->camera_model); + str_replace (&b, "<!-- $(EXIF_CAMERA_MODEL) -->", exif->camera_model, NULL); else - str_replace (&b, "<!-- $(EXIF_CAMERA_MODEL) -->", "??"); + str_replace (&b, "<!-- $(EXIF_CAMERA_MODEL) -->", "??", NULL); } if (strstr (b, "<!-- $(EXIF_FOCAL_35) -->")) { if (exif->focal_length_35mm) { s1 = g_strconcat ("(", exif->focal_length_35mm, ")", NULL); - str_replace (&b, "<!-- $(EXIF_FOCAL_35) -->", s1); + str_replace (&b, "<!-- $(EXIF_FOCAL_35) -->", s1, NULL); g_free (s1); } else - str_replace (&b, "<!-- $(EXIF_FOCAL_35) -->", ""); + str_replace (&b, "<!-- $(EXIF_FOCAL_35) -->", "", NULL); } if (strstr (b, "<!-- $(LINK_NEXT) -->")) { if (next_item) { s2 = (next_item->path == NULL && next_item->preview) ? g_path_get_basename (next_item->preview) : g_strdup (next_item->path); s1 = g_strconcat (s2, ".html", NULL); - str_replace (&b, "<!-- $(LINK_NEXT) -->", s1); + str_replace (&b, "<!-- $(LINK_NEXT) -->", s1, NULL); g_free (s1); g_free (s2); } else - str_replace (&b, "<!-- $(LINK_NEXT) -->", "index.html"); + str_replace (&b, "<!-- $(LINK_NEXT) -->", "index.html", NULL); } if (strstr(b, "<!-- $(LINK_PREV) -->")) { if (previous_item) { s2 = (previous_item->path == NULL && previous_item->preview) ? g_path_get_basename (previous_item->preview) : g_strdup (previous_item->path); s1 = g_strconcat (s2, ".html", NULL); - str_replace (&b, "<!-- $(LINK_PREV) -->", s1); + str_replace (&b, "<!-- $(LINK_PREV) -->", s1, NULL); g_free (s1); g_free (s2); } else - str_replace (&b, "<!-- $(LINK_PREV) -->", "index.html"); + str_replace (&b, "<!-- $(LINK_PREV) -->", "index.html", NULL); } - if (strstr (b, "<!-- $(FOOTER) -->")) - str_replace (&b, "<!-- $(FOOTER) -->", setup->footer); + if (strstr (b, "<!-- $(FOOTER) -->")) { + s1 = g_strdup (setup->footer); + fix_entities (&s1); + str_replace (&b, "<!-- $(FOOTER) -->", s1, NULL); + g_free (s1); + } if (! fputs (b, fout)) { fprintf (stderr, "write_html_image: error writing to file \"%s\": %s\n", dst, strerror (errno)); |
