From e36da1bd8ab58e6ebdba54e211da6ea47643a361 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Thu, 22 Sep 2011 12:00:45 -0600 Subject: [PATCH] snapshot: simplify indentation of network xml More simplifications possible due to auto-indent. Also, within was only using 6 instead of 8 spaces. * src/util/network.h (virVirtualPortProfileFormat) (virBandwidthDefFormat): Alter signature. * src/util/network.c (virVirtualPortProfileFormat) (virBandwidthDefFormat): Alter indentation. (virBandwidthChildDefFormat): Tweak to make use easier. * src/conf/network_conf.c (virPortGroupDefFormat) (virNetworkDefFormat): Adjust callers. * src/conf/domain_conf.c (virDomainNetDefFormat): Likewise. (virDomainActualNetDefFormat): Likewise, and fix bandwidth indentation. --- src/conf/domain_conf.c | 23 ++++++++++++------- src/conf/network_conf.c | 14 ++++++++---- src/util/network.c | 49 ++++++++++++++--------------------------- src/util/network.h | 11 +++++---- 4 files changed, 47 insertions(+), 50 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 304d1a82aa..a20ca84637 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -9491,15 +9491,18 @@ virDomainActualNetDefFormat(virBufferPtr buf, return ret; } virBufferAsprintf(buf, " mode='%s'/>\n", mode); - virVirtualPortProfileFormat(buf, def->data.direct.virtPortProfile, - " "); + virBufferAdjustIndent(buf, 8); + virVirtualPortProfileFormat(buf, def->data.direct.virtPortProfile); + virBufferAdjustIndent(buf, -8); break; default: break; } - if (virBandwidthDefFormat(buf, def->bandwidth, " ") < 0) + virBufferAdjustIndent(buf, 8); + if (virBandwidthDefFormat(buf, def->bandwidth) < 0) goto error; + virBufferAdjustIndent(buf, -8); virBufferAddLit(buf, " \n"); @@ -9538,8 +9541,9 @@ virDomainNetDefFormat(virBufferPtr buf, def->data.network.portgroup); } virBufferAddLit(buf, "/>\n"); - virVirtualPortProfileFormat(buf, def->data.network.virtPortProfile, - " "); + virBufferAdjustIndent(buf, 6); + virVirtualPortProfileFormat(buf, def->data.network.virtPortProfile); + virBufferAdjustIndent(buf, -6); if ((flags & VIR_DOMAIN_XML_INTERNAL_ACTUAL_NET) && (virDomainActualNetDefFormat(buf, def->data.network.actual) < 0)) return -1; @@ -9590,8 +9594,9 @@ virDomainNetDefFormat(virBufferPtr buf, virBufferAsprintf(buf, " mode='%s'", virMacvtapModeTypeToString(def->data.direct.mode)); virBufferAddLit(buf, "/>\n"); - virVirtualPortProfileFormat(buf, def->data.direct.virtPortProfile, - " "); + virBufferAdjustIndent(buf, 6); + virVirtualPortProfileFormat(buf, def->data.direct.virtPortProfile); + virBufferAdjustIndent(buf, -6); break; case VIR_DOMAIN_NET_TYPE_USER: @@ -9656,8 +9661,10 @@ virDomainNetDefFormat(virBufferPtr buf, virBufferAsprintf(buf, " \n", virDomainNetInterfaceLinkStateTypeToString(def->linkstate)); - if (virBandwidthDefFormat(buf, def->bandwidth, " ") < 0) + virBufferAdjustIndent(buf, 6); + if (virBandwidthDefFormat(buf, def->bandwidth) < 0) return -1; + virBufferAdjustIndent(buf, -6); if (virDomainDeviceInfoFormat(buf, &def->info, flags) < 0) return -1; diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index b98ffad0bf..7bc2e2cbe8 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -1267,8 +1267,10 @@ virPortGroupDefFormat(virBufferPtr buf, virBufferAddLit(buf, " default='yes'"); } virBufferAddLit(buf, ">\n"); - virVirtualPortProfileFormat(buf, def->virtPortProfile, " "); - virBandwidthDefFormat(buf, def->bandwidth, " "); + virBufferAdjustIndent(buf, 4); + virVirtualPortProfileFormat(buf, def->virtPortProfile); + virBandwidthDefFormat(buf, def->bandwidth); + virBufferAdjustIndent(buf, -4); virBufferAddLit(buf, " \n"); } @@ -1341,15 +1343,19 @@ char *virNetworkDefFormat(const virNetworkDefPtr def) if (virNetworkDNSDefFormat(&buf, def->dns) < 0) goto error; - if (virBandwidthDefFormat(&buf, def->bandwidth, " ") < 0) + virBufferAdjustIndent(&buf, 2); + if (virBandwidthDefFormat(&buf, def->bandwidth) < 0) goto error; + virBufferAdjustIndent(&buf, -2); for (ii = 0; ii < def->nips; ii++) { if (virNetworkIpDefFormat(&buf, &def->ips[ii]) < 0) goto error; } - virVirtualPortProfileFormat(&buf, def->virtPortProfile, " "); + virBufferAdjustIndent(&buf, 2); + virVirtualPortProfileFormat(&buf, def->virtPortProfile); + virBufferAdjustIndent(&buf, -2); for (ii = 0; ii < def->nPortGroups; ii++) virPortGroupDefFormat(&buf, &def->portGroups[ii]); diff --git a/src/util/network.c b/src/util/network.c index ee695575a9..edf9c50f30 100644 --- a/src/util/network.c +++ b/src/util/network.c @@ -881,16 +881,14 @@ virVirtualPortProfileEqual(virVirtualPortProfileParamsPtr a, virVirtualPortProfi void virVirtualPortProfileFormat(virBufferPtr buf, - virVirtualPortProfileParamsPtr virtPort, - const char *indent) + virVirtualPortProfileParamsPtr virtPort) { char uuidstr[VIR_UUID_STRING_BUFLEN]; if (!virtPort || virtPort->virtPortType == VIR_VIRTUALPORT_NONE) return; - virBufferAsprintf(buf, "%s\n", - indent, + virBufferAsprintf(buf, "\n", virVirtualPortTypeToString(virtPort->virtPortType)); switch (virtPort->virtPortType) { @@ -902,9 +900,8 @@ virVirtualPortProfileFormat(virBufferPtr buf, virUUIDFormat(virtPort->u.virtPort8021Qbg.instanceID, uuidstr); virBufferAsprintf(buf, - "%s \n", - indent, virtPort->u.virtPort8021Qbg.managerID, virtPort->u.virtPort8021Qbg.typeID, virtPort->u.virtPort8021Qbg.typeIDVersion, @@ -913,13 +910,12 @@ virVirtualPortProfileFormat(virBufferPtr buf, case VIR_VIRTUALPORT_8021QBH: virBufferAsprintf(buf, - "%s \n", - indent, + " \n", virtPort->u.virtPort8021Qbh.profileID); break; } - virBufferAsprintf(buf, "%s\n", indent); + virBufferAddLit(buf, "\n"); } static int @@ -1074,11 +1070,14 @@ virBandwidthChildDefFormat(virBufferPtr buf, virRatePtr def, const char *elem_name) { - if (!buf || !def || !elem_name) + if (!buf || !elem_name) return -1; + if (!def) + return 0; if (def->average) { - virBufferAsprintf(buf, "<%s average='%llu'", elem_name, def->average); + virBufferAsprintf(buf, " <%s average='%llu'", elem_name, + def->average); if (def->peak) virBufferAsprintf(buf, " peak='%llu'", def->peak); @@ -1095,17 +1094,15 @@ virBandwidthChildDefFormat(virBufferPtr buf, * virBandwidthDefFormat: * @buf: Buffer to print to * @def: Data source - * @indent: prepend all lines printed with this * * Formats bandwidth and prepend each line with @indent. - * Passing NULL to @indent is equivalent passing "". + * @buf may use auto-indentation. * * Returns 0 on success, else -1. */ int virBandwidthDefFormat(virBufferPtr buf, - virBandwidthPtr def, - const char *indent) + virBandwidthPtr def) { int ret = -1; @@ -1117,23 +1114,11 @@ virBandwidthDefFormat(virBufferPtr buf, goto cleanup; } - if (!indent) - indent = ""; - - virBufferAsprintf(buf, "%s\n", indent); - if (def->in) { - virBufferAsprintf(buf, "%s ", indent); - if (virBandwidthChildDefFormat(buf, def->in, "inbound") < 0) - goto cleanup; - } - - if (def->out) { - virBufferAsprintf(buf, "%s ", indent); - if (virBandwidthChildDefFormat(buf, def->out, "outbound") < 0) - goto cleanup; - } - - virBufferAsprintf(buf, "%s\n", indent); + virBufferAddLit(buf, "\n"); + if (virBandwidthChildDefFormat(buf, def->in, "inbound") < 0 || + virBandwidthChildDefFormat(buf, def->out, "outbound") < 0) + goto cleanup; + virBufferAddLit(buf, "\n"); ret = 0; diff --git a/src/util/network.h b/src/util/network.h index 4d195af90f..835934f0c1 100644 --- a/src/util/network.h +++ b/src/util/network.h @@ -1,7 +1,7 @@ /* * network.h: network helper APIs for libvirt * - * Copyright (C) 2009-2009 Red Hat, Inc. + * Copyright (C) 2009-2011 Red Hat, Inc. * * See COPYING.LIB for the License of this software * @@ -147,16 +147,15 @@ virVirtualPortProfileParseXML(xmlNodePtr node, virVirtualPortProfileParamsPtr *virtPort); void virVirtualPortProfileFormat(virBufferPtr buf, - virVirtualPortProfileParamsPtr virtPort, - const char *indent); + virVirtualPortProfileParamsPtr virtPort); -bool virVirtualPortProfileEqual(virVirtualPortProfileParamsPtr a, virVirtualPortProfileParamsPtr b); +bool virVirtualPortProfileEqual(virVirtualPortProfileParamsPtr a, + virVirtualPortProfileParamsPtr b); virBandwidthPtr virBandwidthDefParseNode(xmlNodePtr node); void virBandwidthDefFree(virBandwidthPtr def); int virBandwidthDefFormat(virBufferPtr buf, - virBandwidthPtr def, - const char *indent); + virBandwidthPtr def); int virBandwidthEnable(virBandwidthPtr bandwidth, const char *iface); int virBandwidthDisable(const char *iface, bool may_fail); -- GitLab