From f9da823afec9abdde353190a51740c8b08376757 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Fri, 26 Aug 2022 12:41:50 +0200 Subject: [PATCH] virDomainControllerDefFormatPCI: Refactor formatting of '' subelement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rewrite the code to use virXMLFormat element so that we can avoid a bunch of unnecessary checks. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- src/conf/domain_conf.c | 56 ++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 37 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 53f6524bbe..c5970dde9c 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -22676,6 +22676,8 @@ virDomainControllerDefFormatPCI(virBuffer *buf, virDomainControllerDef *def, unsigned int flags) { + g_auto(virBuffer) targetAttrBuf = VIR_BUFFER_INITIALIZER; + g_auto(virBuffer) targetChildBuf = VIR_BUFFER_INIT_CHILD(buf); bool formatModelName = true; if (def->opts.pciopts.modelName == VIR_DOMAIN_CONTROLLER_PCI_MODEL_NAME_NONE) @@ -22710,45 +22712,25 @@ virDomainControllerDefFormatPCI(virBuffer *buf, virBufferAsprintf(buf, "\n", modelName); } - if (def->opts.pciopts.chassisNr != -1 || - def->opts.pciopts.chassis != -1 || - def->opts.pciopts.port != -1 || - def->opts.pciopts.busNr != -1 || - def->opts.pciopts.targetIndex != -1 || - def->opts.pciopts.numaNode != -1 || - def->opts.pciopts.hotplug != VIR_TRISTATE_SWITCH_ABSENT) { - virBufferAddLit(buf, "opts.pciopts.chassisNr != -1) - virBufferAsprintf(buf, " chassisNr='%d'", - def->opts.pciopts.chassisNr); - if (def->opts.pciopts.chassis != -1) - virBufferAsprintf(buf, " chassis='%d'", - def->opts.pciopts.chassis); - if (def->opts.pciopts.port != -1) - virBufferAsprintf(buf, " port='0x%x'", - def->opts.pciopts.port); - if (def->opts.pciopts.busNr != -1) - virBufferAsprintf(buf, " busNr='%d'", - def->opts.pciopts.busNr); - if (def->opts.pciopts.targetIndex != -1) - virBufferAsprintf(buf, " index='%d'", - def->opts.pciopts.targetIndex); - if (def->opts.pciopts.hotplug != VIR_TRISTATE_SWITCH_ABSENT) { - virBufferAsprintf(buf, " hotplug='%s'", - virTristateSwitchTypeToString(def->opts.pciopts.hotplug)); - } - if (def->opts.pciopts.numaNode == -1) { - virBufferAddLit(buf, "/>\n"); - } else { - virBufferAddLit(buf, ">\n"); - virBufferAdjustIndent(buf, 2); - virBufferAsprintf(buf, "%d\n", - def->opts.pciopts.numaNode); - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); - } + if (def->opts.pciopts.chassisNr != -1) + virBufferAsprintf(&targetAttrBuf, " chassisNr='%d'", def->opts.pciopts.chassisNr); + if (def->opts.pciopts.chassis != -1) + virBufferAsprintf(&targetAttrBuf, " chassis='%d'", def->opts.pciopts.chassis); + if (def->opts.pciopts.port != -1) + virBufferAsprintf(&targetAttrBuf, " port='0x%x'", def->opts.pciopts.port); + if (def->opts.pciopts.busNr != -1) + virBufferAsprintf(&targetAttrBuf, " busNr='%d'", def->opts.pciopts.busNr); + if (def->opts.pciopts.targetIndex != -1) + virBufferAsprintf(&targetAttrBuf, " index='%d'", def->opts.pciopts.targetIndex); + if (def->opts.pciopts.hotplug != VIR_TRISTATE_SWITCH_ABSENT) { + virBufferAsprintf(&targetAttrBuf, " hotplug='%s'", + virTristateSwitchTypeToString(def->opts.pciopts.hotplug)); } + if (def->opts.pciopts.numaNode != -1) + virBufferAsprintf(&targetChildBuf, "%d\n", def->opts.pciopts.numaNode); + + virXMLFormatElement(buf, "target", &targetAttrBuf, &targetChildBuf); return 0; }