mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-03 19:45:21 +00:00
util: switch virBufferTrim to void
We don't care whether the trim was succesful or not anywhere except the tests. Switch it to void and set the buffer error on wrong usage.
This commit is contained in:
parent
5379bb0f33
commit
d0d0413e48
@ -660,27 +660,28 @@ virBufferStrcat(virBufferPtr buf, ...)
|
|||||||
* further limits how much of the tail is trimmed. If @str is NULL, then
|
* further limits how much of the tail is trimmed. If @str is NULL, then
|
||||||
* @len must be non-negative.
|
* @len must be non-negative.
|
||||||
*
|
*
|
||||||
* Returns -1 if @buf has previously encountered an error or if @len is
|
* Sets error to -1 (usage) if str is NULL and len is less than zero.
|
||||||
* invalid, 0 if there was nothing to trim (@buf was too short or @str
|
|
||||||
* didn't match), and 1 if the trim was successful.
|
|
||||||
*/
|
*/
|
||||||
int
|
void
|
||||||
virBufferTrim(virBufferPtr buf, const char *str, int len)
|
virBufferTrim(virBufferPtr buf, const char *str, int len)
|
||||||
{
|
{
|
||||||
size_t len2 = 0;
|
size_t len2 = 0;
|
||||||
|
|
||||||
if (!buf || buf->error || (!str && len < 0))
|
if (!buf || buf->error)
|
||||||
return -1;
|
return;
|
||||||
|
if (!str && len < 0) {
|
||||||
|
virBufferSetError(buf, -1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (len > 0 && len > buf->use)
|
if (len > 0 && len > buf->use)
|
||||||
return 0;
|
return;
|
||||||
if (str) {
|
if (str) {
|
||||||
len2 = strlen(str);
|
len2 = strlen(str);
|
||||||
if (len2 > buf->use ||
|
if (len2 > buf->use ||
|
||||||
memcmp(&buf->content[buf->use - len2], str, len2) != 0)
|
memcmp(&buf->content[buf->use - len2], str, len2) != 0)
|
||||||
return 0;
|
return;
|
||||||
}
|
}
|
||||||
buf->use -= len < 0 ? len2 : len;
|
buf->use -= len < 0 ? len2 : len;
|
||||||
buf->content[buf->use] = '\0';
|
buf->content[buf->use] = '\0';
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
@ -77,6 +77,6 @@ void virBufferURIEncodeString(virBufferPtr buf, const char *str);
|
|||||||
void virBufferAdjustIndent(virBufferPtr buf, int indent);
|
void virBufferAdjustIndent(virBufferPtr buf, int indent);
|
||||||
int virBufferGetIndent(const virBufferPtr buf, bool dynamic);
|
int virBufferGetIndent(const virBufferPtr buf, bool dynamic);
|
||||||
|
|
||||||
int virBufferTrim(virBufferPtr buf, const char *trim, int len);
|
void virBufferTrim(virBufferPtr buf, const char *trim, int len);
|
||||||
|
|
||||||
#endif /* __VIR_BUFFER_H__ */
|
#endif /* __VIR_BUFFER_H__ */
|
||||||
|
@ -148,38 +148,21 @@ static int testBufTrim(const void *data ATTRIBUTE_UNUSED)
|
|||||||
char *result = NULL;
|
char *result = NULL;
|
||||||
const char *expected = "a,b";
|
const char *expected = "a,b";
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
int i = 1;
|
|
||||||
|
|
||||||
#define ACT(str, len, result) \
|
virBufferTrim(buf, "", 0);
|
||||||
do { \
|
|
||||||
if (virBufferTrim(buf, str, len) != result) { \
|
|
||||||
TEST_ERROR("trim %d failed", i); \
|
|
||||||
goto cleanup; \
|
|
||||||
} \
|
|
||||||
i++; \
|
|
||||||
} while (0);
|
|
||||||
|
|
||||||
if (virBufferTrim(buf, "", 0) != -1) {
|
|
||||||
TEST_ERROR("Wrong failure detection 1");
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
buf = &bufinit;
|
buf = &bufinit;
|
||||||
if (virBufferTrim(buf, NULL, -1) != -1) {
|
|
||||||
TEST_ERROR("Wrong failure detection 2");
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
virBufferAddLit(buf, "a;");
|
virBufferAddLit(buf, "a;");
|
||||||
ACT("", 0, 1);
|
virBufferTrim(buf, "", 0);
|
||||||
ACT("", -1, 1);
|
virBufferTrim(buf, "", -1);
|
||||||
ACT(NULL, 1, 1);
|
virBufferTrim(buf, NULL, 1);
|
||||||
ACT(NULL, 5, 0);
|
virBufferTrim(buf, NULL, 5);
|
||||||
ACT("a", 2, 0);
|
virBufferTrim(buf, "a", 2);
|
||||||
|
|
||||||
virBufferAddLit(buf, ",b,,");
|
virBufferAddLit(buf, ",b,,");
|
||||||
ACT("b", -1, 0);
|
virBufferTrim(buf, "b", -1);
|
||||||
ACT("b,,", 1, 1);
|
virBufferTrim(buf, "b,,", 1);
|
||||||
ACT(",", -1, 1);
|
virBufferTrim(buf, ",", -1);
|
||||||
|
|
||||||
result = virBufferContentAndReset(buf);
|
result = virBufferContentAndReset(buf);
|
||||||
if (!result || STRNEQ(result, expected)) {
|
if (!result || STRNEQ(result, expected)) {
|
||||||
@ -187,6 +170,12 @@ static int testBufTrim(const void *data ATTRIBUTE_UNUSED)
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virBufferTrim(buf, NULL, -1);
|
||||||
|
if (virBufferError(buf) != -1) {
|
||||||
|
TEST_ERROR("Usage error not flagged");
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
|
@ -632,18 +632,15 @@ vshTreePrintInternal(vshControl *ctl,
|
|||||||
false, indent) < 0)
|
false, indent) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
if (virBufferTrim(indent, " ", -1) < 0)
|
virBufferTrim(indent, " ", -1);
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
/* If there was no child device, and we're the last in
|
/* If there was no child device, and we're the last in
|
||||||
* a list of devices, then print another blank line */
|
* a list of devices, then print another blank line */
|
||||||
if (nextlastdev == -1 && devid == lastdev)
|
if (nextlastdev == -1 && devid == lastdev)
|
||||||
vshPrint(ctl, "%s\n", virBufferCurrentContent(indent));
|
vshPrint(ctl, "%s\n", virBufferCurrentContent(indent));
|
||||||
|
|
||||||
if (!root) {
|
if (!root)
|
||||||
if (virBufferTrim(indent, NULL, 2) < 0)
|
virBufferTrim(indent, NULL, 2);
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
cleanup:
|
cleanup:
|
||||||
return ret;
|
return ret;
|
||||||
|
Loading…
Reference in New Issue
Block a user