summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars-Dominik Braun <PromyLOPh@gmail.com>2008-06-10 21:42:21 +0200
committerLars-Dominik Braun <PromyLOPh@gmail.com>2008-06-10 21:42:21 +0200
commita83e76cc7021ef475f1b8a2f52f20f690d65fe4f (patch)
treeb11b3ae909bda2c58c7828a647cbb3c62a360d83
parentf531df2b04097bf24563ed4becdfca13df00de0d (diff)
downloadpianobar-windows-a83e76cc7021ef475f1b8a2f52f20f690d65fe4f.tar.gz
pianobar-windows-a83e76cc7021ef475f1b8a2f52f20f690d65fe4f.tar.bz2
pianobar-windows-a83e76cc7021ef475f1b8a2f52f20f690d65fe4f.zip
Add xml encode function
Could be tuned, but was the only one I found on my harddisk. Originally written for shell-fm.
-rw-r--r--libpiano/xml.c38
-rw-r--r--libpiano/xml.h2
2 files changed, 40 insertions, 0 deletions
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[] = {"&&amp;", "'&apos;", "\"&quot;", "<&lt;", ">&gt;"};
+ 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 */