| version 1.1, 2003/04/21 23:47:39 |
version 1.2, 2003/04/22 14:21:34 |
|
|
| #include <stdlib.h> |
#include <stdlib.h> |
| #include <string.h> |
#include <string.h> |
| #include <ctype.h> |
|
| |
|
| #include <platon/html/escape.h> |
#include <platon/html/escape.h> |
| |
|
| struct html_escape_table html_escape_space[] = { |
#define HTML_ESCAPE_TABLE_TERMINATOR { '\0', NULL } |
| |
|
| |
static struct html_escape_table html_escape_space[] = { /* {{{ */ |
| { ' ', " " }, |
{ ' ', " " }, |
| { '\0', NULL } |
HTML_ESCAPE_TABLE_TERMINATOR |
| }; |
}; /* }}} */ |
| |
|
| struct html_escape_table html_escape_special_chars[] = { |
static struct html_escape_table html_escape_special_chars[] = { /* {{{ */ |
| { '\x3C', "<" }, |
{ '\x3C', "<" }, |
| { '\x3E', ">" }, |
{ '\x3E', ">" }, |
| { '\x26', "&" }, |
{ '\x26', "&" }, |
| { '\0', NULL } |
HTML_ESCAPE_TABLE_TERMINATOR |
| }; |
}; /* }}} */ |
| |
|
| struct html_escape_table html_escape_quotes[] = { |
static struct html_escape_table html_escape_quotes[] = { /* {{{ */ |
| { '\x22', """ }, |
{ '\"', """ }, |
| { '\'', "'" }, |
{ '\'', "'" }, |
| { '\0', NULL } |
HTML_ESCAPE_TABLE_TERMINATOR |
| }; |
}; /* }}} */ |
| |
|
| struct html_escape_table html_escape_iso8859_1[] = { |
static struct html_escape_table html_escape_iso8859_1[] = { /* {{{ */ |
| { '\0', NULL } |
HTML_ESCAPE_TABLE_TERMINATOR |
| }; |
}; /* }}} */ |
| |
|
| struct html_escape_table html_escape_iso8859_2[] = { |
static struct html_escape_table html_escape_iso8859_2[] = { /* {{{ */ |
| { '\0', NULL } |
HTML_ESCAPE_TABLE_TERMINATOR |
| }; |
}; /* }}} */ |
| |
|
| struct html_escape_table html_escape_iso8859_15[] = { |
static struct html_escape_table html_escape_iso8859_15[] = { /* {{{ */ |
| { '\xE4', "ä" }, |
{ '\xE4', "ä" }, |
| { '\xC4', "Ä" }, |
{ '\xC4', "Ä" }, |
| { '\xF6', "ö" }, |
{ '\xF6', "ö" }, |
| Line 39 struct html_escape_table html_escape_iso |
|
| Line 40 struct html_escape_table html_escape_iso |
|
| { '\xDC', "Ü" }, |
{ '\xDC', "Ü" }, |
| { '\xDF', "ß" }, |
{ '\xDF', "ß" }, |
| { '\xA4', "€" }, |
{ '\xA4', "€" }, |
| { '\0', NULL } |
HTML_ESCAPE_TABLE_TERMINATOR |
| }; |
}; /* }}} */ |
| |
|
| struct html_escape_table *html_escape_tables[] = { |
static struct html_escape_table *html_escape_tables[] = { /* {{{ */ |
| html_escape_space, |
html_escape_space, |
| html_escape_special_chars, |
html_escape_special_chars, |
| html_escape_quotes, |
html_escape_quotes, |
| Line 50 struct html_escape_table *html_escape_ta |
|
| Line 51 struct html_escape_table *html_escape_ta |
|
| html_escape_iso8859_2, |
html_escape_iso8859_2, |
| html_escape_iso8859_15, |
html_escape_iso8859_15, |
| NULL |
NULL |
| }; |
}; /* }}} */ |
| |
|
| char * |
char * |
| PLATON_FUNC(html_escape)(str, types) |
PLATON_FUNC(html_escape)(str, types) |
| const char *str; |
const char *str; |
| const int types; |
const int types; |
| { |
{ /* {{{ */ |
| register int i, ii, j, len = 1; |
register int i, ii, j, len = 1; |
| struct html_escape_table *current_table; |
struct html_escape_table *current_table; |
| char *html; |
char *html; |
| Line 85 PLATON_FUNC(html_escape)(str, types) |
|
| Line 86 PLATON_FUNC(html_escape)(str, types) |
|
| |
|
| /* Substitute character with appropriate entity */ |
/* Substitute character with appropriate entity */ |
| len += strlen(current_table[j].html); |
len += strlen(current_table[j].html); |
| if ((html = realloc(html, len)) == NULL) |
if ((html = (char *) realloc(html, len)) == NULL) |
| return NULL; |
return NULL; |
| strcat(html, current_table[j].html); |
strcat(html, current_table[j].html); |
| break; |
break; |
| Line 99 PLATON_FUNC(html_escape)(str, types) |
|
| Line 100 PLATON_FUNC(html_escape)(str, types) |
|
| continue; |
continue; |
| |
|
| /* Just copy character */ |
/* Just copy character */ |
| if ((html = realloc(html, len + 1)) == NULL) |
|
| return NULL; |
|
| html[len - 1] = *str; |
|
| len++; |
len++; |
| |
if ((html = (char *) realloc(html, len)) == NULL) |
| |
return NULL; |
| |
html[len - 2] = *str; |
| |
html[len - 1] = '\0'; |
| } |
} |
| html[len - 1] = '\0'; |
|
| |
|
| return html; |
return html; |
| } |
} /* }}} */ |
| |
|
| |
char * |
| |
PLATON_FUNC(html_unescape)(html, types) |
| |
const char *html; |
| |
const int types; |
| |
{ /* {{{ */ |
| |
register int i, ii, j, len; |
| |
struct html_escape_table *current_table; |
| |
char *str, *ptr; |
| |
|
| |
/* String duplication */ |
| |
if ((str = strdup(html)) == NULL) |
| |
return NULL; |
| |
|
| |
/* Loop through every HTML escape table */ |
| |
for (i = 0, ii = 1; ; i++, ii *= 2) { /* ii == 2^i */ |
| |
if ((current_table = html_escape_tables[i]) == NULL) |
| |
break; |
| |
|
| #if defined(SELF) || defined(SELFTEST) || defined(SELF_HTML_ESCAPE) |
/* Check if table is selected */ |
| |
if (! types & ii) |
| |
continue; |
| |
|
| |
/* Loop though every entity of HTML escape table */ |
| |
for (j = 0; current_table[j].html != NULL; j++) { |
| |
|
| |
while ((ptr = strstr(str, current_table[j].html)) != NULL) { |
| |
len = strlen(current_table[j].html); |
| |
if (len <= 0) |
| |
break; |
| |
*ptr = current_table[j].code; |
| |
memmove((void *) (ptr + 1), (void *) (ptr + len), |
| |
strlen(ptr + len) + 1); |
| |
} |
| |
|
| |
} |
| |
} |
| |
|
| |
return str; |
| |
} /* }}} */ |
| |
|
| |
#if defined(SELF) || defined(SELFTEST) || defined(SELF_HTML_ESCAPE) /* {{{ */ |
| |
|
| #include <stdio.h> |
#include <stdio.h> |
| |
|
| #define TEST_htmlentities "[<--//-->]" |
#define TEST_STRING "<<--'//'-->>" |
| |
|
| int |
int |
| main(argc, argv) |
main(argc, argv) |
| int argc; |
int argc; |
| char **argv; |
char **argv; |
| { |
{ |
| register char *s1; |
register char *s1, *s2; |
| |
|
| s1 = htmlentities(TEST_htmlentities); |
s1 = htmlspecialchars(TEST_STRING); |
| printf("htmlentities(\"%s\") = \"%s\"\n", TEST_htmlentities, s1); |
printf("htmlspecialchars(\"%s\") = \"%s\"\n", TEST_STRING, s1); |
| |
s2 = html_unescape(s1, HTML_ESCAPE_HTMLSPECIALCHARS); |
| |
printf("html_unescape(\"%s\", HTML_ESCAPE_HTMLSPECIALCHARS) = \"%s\"\n", s1, s2); |
| |
free(s2); |
| |
free(s1); |
| |
|
| |
s1 = htmlentities(TEST_STRING); |
| |
printf("htmlentities(\"%s\") = \"%s\"\n", TEST_STRING, s1); |
| |
s2 = html_unescape(s1, HTML_ESCAPE_HTMLENTITIES); |
| |
printf("html_unescape(\"%s\", HTML_ESCAPE_HTMLENTITIES) = \"%s\"\n", s1, s2); |
| |
free(s2); |
| free(s1); |
free(s1); |
| |
|
| return 0; |
return 0; |
| } |
} |
| |
|
| #endif /* #if defined(SELF) || defined(SELFTEST) || defined(SELF_STRCGI) */ |
#endif /* #if defined(SELF) || defined(SELFTEST) || defined(SELF_STRCGI) */ /* }}} */ |
| |
|
| |
/* Modeline for ViM {{{ |
| |
* vim: set ts=4: |
| |
* vim600: fdm=marker fdl=0 fdc=0: |
| |
* }}} */ |
| |
|