summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars-Dominik Braun <PromyLOPh@gmail.com>2008-07-03 16:33:30 +0200
committerLars-Dominik Braun <PromyLOPh@gmail.com>2008-07-03 16:33:30 +0200
commit701177ef5b4005d8f03c7f9f47081bb4e7c46517 (patch)
treea91db940178e2749b9cf46be1e4adc4a8c6a4b52
parent66ad24c2a61a79815e738244a16cc0f7edd6f35e (diff)
downloadpianobar-windows-701177ef5b4005d8f03c7f9f47081bb4e7c46517.tar.gz
pianobar-windows-701177ef5b4005d8f03c7f9f47081bb4e7c46517.tar.bz2
pianobar-windows-701177ef5b4005d8f03c7f9f47081bb4e7c46517.zip
piano: Replace XmlEncodeString with more efficient version
-rw-r--r--libpiano/src/xml.c34
1 files changed, 14 insertions, 20 deletions
diff --git a/libpiano/src/xml.c b/libpiano/src/xml.c
index a9be0a8..377affb 100644
--- a/libpiano/src/xml.c
+++ b/libpiano/src/xml.c
@@ -548,33 +548,27 @@ PianoReturn_t PianoXmlParseSearch (char *searchXml,
* @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) {
+ char *replacements[] = {"&&amp;", "'&apos;", "\"&quot;", "<&lt;",
+ ">&gt;", NULL};
+ char **r;
+ char *sOut = calloc (strlen (s) * 5 + 1, sizeof (*sOut));
+ char found;
+
+ while (*s != '\0') {
+ r = replacements;
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));
+ while (*r != NULL) {
+ if (*s == *r[0]) {
found = 1;
- /* no need to look for other entities */
+ strcat (sOut, (*r) + 1);
break;
}
+ r++;
}
if (!found) {
- strncat (s_new, s, 1);
+ strncat (sOut, s, 1);
}
s++;
}
- return s_new;
+ return sOut;
}