From e14cdeb44f1c902e9383327c828e799b452da1f6 Mon Sep 17 00:00:00 2001 From: Luyao Huang Date: Fri, 22 May 2015 17:26:28 +0800 Subject: [PATCH] util: improve the sysinfo element XML format If the ends up not formatting any sub-elements, then rather than formatting as: Just format it more cleanly as: Signed-off-by: Luyao Huang --- src/util/virsysinfo.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/util/virsysinfo.c b/src/util/virsysinfo.c index 40390abaa9..7b0d80d62f 100644 --- a/src/util/virsysinfo.c +++ b/src/util/virsysinfo.c @@ -1040,31 +1040,42 @@ virSysinfoMemoryFormat(virBufferPtr buf, virSysinfoDefPtr def) int virSysinfoFormat(virBufferPtr buf, virSysinfoDefPtr def) { + virBuffer childrenBuf = VIR_BUFFER_INITIALIZER; const char *type = virSysinfoTypeToString(def->type); + int indent = virBufferGetIndent(buf, false); + int ret = -1; if (!type) { virReportError(VIR_ERR_INTERNAL_ERROR, _("unexpected sysinfo type model %d"), def->type); virBufferFreeAndReset(buf); - return -1; + goto cleanup; } - virBufferAsprintf(buf, "\n", type); - virBufferAdjustIndent(buf, 2); + virBufferAdjustIndent(&childrenBuf, indent + 2); - virSysinfoBIOSFormat(buf, def); - virSysinfoSystemFormat(buf, def); - virSysinfoProcessorFormat(buf, def); - virSysinfoMemoryFormat(buf, def); + virSysinfoBIOSFormat(&childrenBuf, def); + virSysinfoSystemFormat(&childrenBuf, def); + virSysinfoProcessorFormat(&childrenBuf, def); + virSysinfoMemoryFormat(&childrenBuf, def); - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); + virBufferAsprintf(buf, "\n"); + virBufferAddBuffer(buf, &childrenBuf); + virBufferAddLit(buf, "\n"); + } else { + virBufferAddLit(buf, "/>\n"); + } if (virBufferCheckError(buf) < 0) - return -1; + goto cleanup; - return 0; + ret = 0; + cleanup: + virBufferFreeAndReset(&childrenBuf); + return ret; } bool virSysinfoIsEqual(virSysinfoDefPtr src, -- GitLab