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:
Ján Tomko 2013-06-18 17:52:00 +02:00
parent 5379bb0f33
commit d0d0413e48
4 changed files with 29 additions and 42 deletions

View File

@ -660,27 +660,28 @@ virBufferStrcat(virBufferPtr buf, ...)
* further limits how much of the tail is trimmed. If @str is NULL, then
* @len must be non-negative.
*
* Returns -1 if @buf has previously encountered an error or if @len is
* invalid, 0 if there was nothing to trim (@buf was too short or @str
* didn't match), and 1 if the trim was successful.
* Sets error to -1 (usage) if str is NULL and len is less than zero.
*/
int
void
virBufferTrim(virBufferPtr buf, const char *str, int len)
{
size_t len2 = 0;
if (!buf || buf->error || (!str && len < 0))
return -1;
if (!buf || buf->error)
return;
if (!str && len < 0) {
virBufferSetError(buf, -1);
return;
}
if (len > 0 && len > buf->use)
return 0;
return;
if (str) {
len2 = strlen(str);
if (len2 > buf->use ||
memcmp(&buf->content[buf->use - len2], str, len2) != 0)
return 0;
return;
}
buf->use -= len < 0 ? len2 : len;
buf->content[buf->use] = '\0';
return 1;
}

View File

@ -77,6 +77,6 @@ void virBufferURIEncodeString(virBufferPtr buf, const char *str);
void virBufferAdjustIndent(virBufferPtr buf, int indent);
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__ */

View File

@ -148,38 +148,21 @@ static int testBufTrim(const void *data ATTRIBUTE_UNUSED)
char *result = NULL;
const char *expected = "a,b";
int ret = -1;
int i = 1;
#define ACT(str, len, result) \
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;
}
virBufferTrim(buf, "", 0);
buf = &bufinit;
if (virBufferTrim(buf, NULL, -1) != -1) {
TEST_ERROR("Wrong failure detection 2");
goto cleanup;
}
virBufferAddLit(buf, "a;");
ACT("", 0, 1);
ACT("", -1, 1);
ACT(NULL, 1, 1);
ACT(NULL, 5, 0);
ACT("a", 2, 0);
virBufferTrim(buf, "", 0);
virBufferTrim(buf, "", -1);
virBufferTrim(buf, NULL, 1);
virBufferTrim(buf, NULL, 5);
virBufferTrim(buf, "a", 2);
virBufferAddLit(buf, ",b,,");
ACT("b", -1, 0);
ACT("b,,", 1, 1);
ACT(",", -1, 1);
virBufferTrim(buf, "b", -1);
virBufferTrim(buf, "b,,", 1);
virBufferTrim(buf, ",", -1);
result = virBufferContentAndReset(buf);
if (!result || STRNEQ(result, expected)) {
@ -187,6 +170,12 @@ static int testBufTrim(const void *data ATTRIBUTE_UNUSED)
goto cleanup;
}
virBufferTrim(buf, NULL, -1);
if (virBufferError(buf) != -1) {
TEST_ERROR("Usage error not flagged");
goto cleanup;
}
ret = 0;
cleanup:

View File

@ -632,18 +632,15 @@ vshTreePrintInternal(vshControl *ctl,
false, indent) < 0)
goto cleanup;
}
if (virBufferTrim(indent, " ", -1) < 0)
goto cleanup;
virBufferTrim(indent, " ", -1);
/* If there was no child device, and we're the last in
* a list of devices, then print another blank line */
if (nextlastdev == -1 && devid == lastdev)
vshPrint(ctl, "%s\n", virBufferCurrentContent(indent));
if (!root) {
if (virBufferTrim(indent, NULL, 2) < 0)
goto cleanup;
}
if (!root)
virBufferTrim(indent, NULL, 2);
ret = 0;
cleanup:
return ret;