1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-25 01:34:11 +03:00

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 <sage@newdream.net>
This commit is contained in:
Sage Weil 2011-09-19 21:13:42 -07:00 committed by Eric Blake
parent 33b55fd85a
commit 7f197559f2
4 changed files with 25 additions and 8 deletions

View File

@ -197,6 +197,7 @@ Patches have also been contributed by:
Matthias Witte <witte@netzquadrat.de>
Tang Chen <tangchen@cn.fujitsu.com>
Dan Horák <dan@danny.cz>
Sage Weil <sage@newdream.net>
[....send patches to get your name here....]

View File

@ -25,6 +25,7 @@ virBufferAddChar;
virBufferAsprintf;
virBufferContentAndReset;
virBufferError;
virBufferEscape;
virBufferEscapeSexpr;
virBufferEscapeString;
virBufferFreeAndReset;

View File

@ -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;

View File

@ -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_) \