summaryrefslogtreecommitdiff
path: root/libpiano
diff options
context:
space:
mode:
Diffstat (limited to 'libpiano')
-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;
}