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:
parent
33b55fd85a
commit
7f197559f2
1
AUTHORS
1
AUTHORS
@ -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....]
|
||||
|
||||
|
@ -25,6 +25,7 @@ virBufferAddChar;
|
||||
virBufferAsprintf;
|
||||
virBufferContentAndReset;
|
||||
virBufferError;
|
||||
virBufferEscape;
|
||||
virBufferEscapeSexpr;
|
||||
virBufferEscapeString;
|
||||
virBufferFreeAndReset;
|
||||
|
@ -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;
|
||||
|
@ -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_) \
|
||||
|
Loading…
Reference in New Issue
Block a user