diff options
| author | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2009-02-25 21:25:18 +0100 |
|---|---|---|
| committer | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2009-02-25 21:25:18 +0100 |
| commit | 10a77c7a1c4648693ded958d6ac8641afcdf1d34 (patch) | |
| tree | 14d5ee0080288dd08545e5348fbb402f5e144abc /gallery-utils.c | |
| parent | 9caacb6270bd4b4836c751cf86a25073f776986a (diff) | |
| download | cataract-10a77c7a1c4648693ded958d6ac8641afcdf1d34.tar.xz | |
The Ultimate Entity Solution (tm)
Diffstat (limited to 'gallery-utils.c')
| -rw-r--r-- | gallery-utils.c | 69 |
1 files changed, 41 insertions, 28 deletions
diff --git a/gallery-utils.c b/gallery-utils.c index 9642116..6378ae5 100644 --- a/gallery-utils.c +++ b/gallery-utils.c @@ -27,38 +27,13 @@ #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 * - multiple occurrences 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, const char *exclude_when) +str_replace (char **dst, const char *search, const char *replace) { #define REPLACE_MAX_LENGTH 32768 static char d[REPLACE_MAX_LENGTH]; @@ -73,8 +48,8 @@ str_replace (char **dst, const char *search, const char *replace, const char *ex i = 0; src = *dst; - while (strstr_exclude (src, search, exclude_when)) { - found = strstr_exclude (src, search, exclude_when); + while (strstr (src, search)) { + found = strstr (src, search); /* copy the data between search string */ if (found > src) { @@ -247,3 +222,41 @@ fix_entities (char **str) free (*str); *str = g_strdup (&d[0]); } + + +/* + * remove_tags: remove all occurences of tags beginning with tag_begin and ending with tag_end + * - e.g. remove_tags (&x, "<!--", "-->") will remove all comments + * - returns newly allocated string + */ +void +remove_tags (char **str, const char *tag_begin, const char *tag_end) +{ + char *src; + char *found; + char *found2; + char *dest; + + if (! *str || ! tag_begin || ! tag_end || strlen (*str) == 0 || strlen (tag_begin) == 0 || strlen (tag_end) == 0) + return; + + src = *str; + + while ((found = strstr (src, tag_begin)) != NULL) { + found2 = strstr (found, tag_end); + if (found2) { + found2 += strlen (tag_end); + dest = malloc (strlen (src) - (found2 - found) + 1); + 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 ("orig = %s, new = %s, strlen = %d\n", src, dest, strlen (dest)); +#endif + g_free (src); + src = g_strdup (dest); + free (dest); + } + } + *str = src; +} |
