From 51219e11b8c57c7a9755eedd72986269b78ccfb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1n=20Tomko?= Date: Mon, 8 Aug 2016 15:38:20 +0200 Subject: [PATCH] Use a separate buffer for subelements Instead of figuring out upfront whether will be a single or a pair element, format the subelements into a separate buffer and close early if this buffer is empty. --- src/conf/domain_conf.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index fe9b7c7273..a57c395661 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -23036,6 +23036,7 @@ virDomainInputDefFormat(virBufferPtr buf, { const char *type = virDomainInputTypeToString(def->type); const char *bus = virDomainInputBusTypeToString(def->bus); + virBuffer childbuf = VIR_BUFFER_INITIALIZER; /* don't format keyboard into migratable XML for backward compatibility */ if (flags & VIR_DOMAIN_DEF_FORMAT_MIGRATABLE && @@ -23058,17 +23059,17 @@ virDomainInputDefFormat(virBufferPtr buf, virBufferAsprintf(buf, "info, flags) || - def->type == VIR_DOMAIN_INPUT_TYPE_PASSTHROUGH) { - virBufferAddLit(buf, ">\n"); - virBufferAdjustIndent(buf, 2); - virBufferEscapeString(buf, "\n", def->source.evdev); - if (virDomainDeviceInfoFormat(buf, &def->info, flags) < 0) - return -1; - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); - } else { + virBufferAdjustIndent(&childbuf, virBufferGetIndent(buf, false) + 2); + virBufferEscapeString(&childbuf, "\n", def->source.evdev); + if (virDomainDeviceInfoFormat(&childbuf, &def->info, flags) < 0) + return -1; + + if (!virBufferUse(&childbuf)) { virBufferAddLit(buf, "/>\n"); + } else { + virBufferAddLit(buf, ">\n"); + virBufferAddBuffer(buf, &childbuf); + virBufferAddLit(buf, "\n"); } return 0;