mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-02-22 11:22:23 +00:00
buf: Simplify virBufferEscapeString
We are about to copy this function, so clean it up before we do.
This commit is contained in:
parent
c8b2a23c02
commit
0af02cb2e8
@ -282,7 +282,7 @@ err:
|
||||
void
|
||||
virBufferEscapeString(const virBufferPtr buf, const char *format, const char *str)
|
||||
{
|
||||
int size, count, len, grow_size;
|
||||
int len;
|
||||
char *escaped, *out;
|
||||
const char *cur;
|
||||
|
||||
@ -293,6 +293,11 @@ virBufferEscapeString(const virBufferPtr buf, const char *format, const char *st
|
||||
return;
|
||||
|
||||
len = strlen(str);
|
||||
if (strcspn(str, "<>&'\"") == len) {
|
||||
virBufferVSprintf(buf, format, str);
|
||||
return;
|
||||
}
|
||||
|
||||
if (VIR_ALLOC_N(escaped, 6 * len + 1) < 0) {
|
||||
virBufferNoMemory(buf);
|
||||
return;
|
||||
@ -345,36 +350,7 @@ virBufferEscapeString(const virBufferPtr buf, const char *format, const char *st
|
||||
}
|
||||
*out = 0;
|
||||
|
||||
if ((buf->use >= buf->size) &&
|
||||
virBufferGrow(buf, 100) < 0) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
size = buf->size - buf->use;
|
||||
if ((count = snprintf(&buf->content[buf->use], size,
|
||||
format, (char *)escaped)) < 0) {
|
||||
buf->error = 1;
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Grow buffer if necessary and retry */
|
||||
if (count >= size) {
|
||||
buf->content[buf->use] = 0;
|
||||
grow_size = (count + 1 > 1000) ? count + 1 : 1000;
|
||||
if (virBufferGrow(buf, grow_size) < 0) {
|
||||
goto err;
|
||||
}
|
||||
size = buf->size - buf->use;
|
||||
|
||||
if ((count = snprintf(&buf->content[buf->use], size,
|
||||
format, (char *)escaped)) < 0) {
|
||||
buf->error = 1;
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
buf->use += count;
|
||||
|
||||
err:
|
||||
virBufferVSprintf(buf, format, escaped);
|
||||
VIR_FREE(escaped);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user