From a83e76cc7021ef475f1b8a2f52f20f690d65fe4f Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Tue, 10 Jun 2008 21:42:21 +0200 Subject: Add xml encode function Could be tuned, but was the only one I found on my harddisk. Originally written for shell-fm. --- libpiano/xml.c | 38 ++++++++++++++++++++++++++++++++++++++ libpiano/xml.h | 2 ++ 2 files changed, 40 insertions(+) diff --git a/libpiano/xml.c b/libpiano/xml.c index e59e4b4..7e5346a 100644 --- a/libpiano/xml.c +++ b/libpiano/xml.c @@ -313,3 +313,41 @@ PianoReturn_t PianoXmlParseSimple (char *xml) { return ret; } + +/* encode reserved chars + * @author PromyLOPh + * @added 2008-06-10 + * @param encode this + * @return encoded string + */ +char *PianoXmlEncodeString (const char *s) { + char *replacements[] = {"&&", "''", "\""", "<<", ">>"}; + char *s_new = NULL; + unsigned int s_new_n = 0; + unsigned int i; + char found = 0; + + s_new_n = strlen (s) + 1; + s_new = calloc (s_new_n, 1); + + while (*s) { + found = 0; + for (i = 0; i < sizeof (replacements) / sizeof (*replacements); i++) { + if (*s == replacements[i][0]) { + /* replacements[i]+1 is our replacement, and -1 'cause we + * "overwrite" one byte */ + s_new_n += strlen (replacements[i]+1)-1; + s_new = realloc (s_new, s_new_n); + strncat (s_new, replacements[i]+1, strlen (replacements[i]+1)); + found = 1; + /* no need to look for other entities */ + break; + } + } + if (!found) { + strncat (s_new, s, 1); + } + s++; + } + return s_new; +} diff --git a/libpiano/xml.h b/libpiano/xml.h index c4eede8..7d08367 100644 --- a/libpiano/xml.h +++ b/libpiano/xml.h @@ -27,4 +27,6 @@ void PianoXmlParseUserinfo (PianoHandle_t *ph, char *xml); void PianoXmlParseStations (PianoHandle_t *ph, char *xml); void PianoXmlParsePlaylist (PianoHandle_t *ph, char *xml); +char *PianoXmlEncodeString (const char *s); + #endif /* _XML_H */ -- cgit v1.2.3