From: Sage Weil Date: Tue, 20 Sep 2011 04:13:42 +0000 (-0700) Subject: buf: implement generic virBufferEscape X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=7f197559f2546c5f65c4cbdb068dbb83bd1781ef;p=libvirt.git buf: implement generic virBufferEscape Implement a generic helper to escape a given set of characters with a leading '\'. Generalizes virBufferEscapeSexpr(). Signed-off-by: Sage Weil --- diff --git a/AUTHORS b/AUTHORS index fd4314f866..0b7f76afcc 100644 --- a/AUTHORS +++ b/AUTHORS @@ -197,6 +197,7 @@ Patches have also been contributed by: Matthias Witte Tang Chen Dan Horák + Sage Weil [....send patches to get your name here....] diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 11ff7053ff..4f965185d8 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -25,6 +25,7 @@ virBufferAddChar; virBufferAsprintf; virBufferContentAndReset; virBufferError; +virBufferEscape; virBufferEscapeSexpr; virBufferEscapeString; virBufferFreeAndReset; diff --git a/src/util/buf.c b/src/util/buf.c index 500248683e..fa12855a72 100644 --- a/src/util/buf.c +++ b/src/util/buf.c @@ -382,6 +382,25 @@ void virBufferEscapeSexpr(const virBufferPtr buf, const char *format, const char *str) +{ + virBufferEscape(buf, "\\'", format, str); +} + +/** + * virBufferEscape: + * @buf: the buffer to dump + * @toescape: NULL-terminated list of characters to escape + * @format: a printf like format string but with only one %s parameter + * @str: the string argument which need to be escaped + * + * Do a formatted print with a single string to a buffer. Any characters + * in the provided list are escaped with a preceeding \. + */ +void +virBufferEscape(const virBufferPtr buf, + const char *toescape, + const char *format, + const char *str) { int len; char *escaped, *out; @@ -394,7 +413,7 @@ virBufferEscapeSexpr(const virBufferPtr buf, return; len = strlen(str); - if (strcspn(str, "\\'") == len) { + if (strcspn(str, toescape) == len) { virBufferAsprintf(buf, format, str); return; } @@ -408,14 +427,9 @@ virBufferEscapeSexpr(const virBufferPtr buf, cur = str; out = escaped; while (*cur != 0) { - switch (*cur) { - case '\\': - case '\'': + if (strchr(toescape, *cur)) *out++ = '\\'; - /* fallthrough */ - default: - *out++ = *cur; - } + *out++ = *cur; cur++; } *out = 0; diff --git a/src/util/buf.h b/src/util/buf.h index 06d01baeef..e545ed902d 100644 --- a/src/util/buf.h +++ b/src/util/buf.h @@ -50,6 +50,7 @@ void virBufferStrcat(const virBufferPtr buf, ...) ATTRIBUTE_SENTINEL; void virBufferEscapeString(const virBufferPtr buf, const char *format, const char *str); void virBufferEscapeSexpr(const virBufferPtr buf, const char *format, const char *str); +void virBufferEscape(const virBufferPtr buf, const char *toescape, const char *format, const char *str); void virBufferURIEncodeString (const virBufferPtr buf, const char *str); # define virBufferAddLit(buf_, literal_string_) \