A helper function that takes a XML node with a "size" and "unit" attributes and converts it into a human-readable string. Reduce the size and number of variables in the parent function. Signed-off-by: Ján Tomko <jtomko@xxxxxxxxxx> --- tools/virsh-completer.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index 2b9749300a..5985f09272 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -615,27 +615,44 @@ virshSnapshotNameCompleter(vshControl *ctl, return ret; } +static char * +virshPagesizeNodeToString(xmlNodePtr node) +{ + VIR_AUTOFREE(char *) pagesize = NULL; + VIR_AUTOFREE(char *) unit = NULL; + unsigned long long byteval = 0; + const char *suffix = NULL; + double size = 0; + char *ret; + + pagesize = virXMLPropString(node, "size"); + unit = virXMLPropString(node, "unit"); + if (virStrToLong_ull(pagesize, NULL, 10, &byteval) < 0) + return NULL; + if (virScaleInteger(&byteval, unit, 1024, UINT_MAX) < 0) + return NULL; + size = vshPrettyCapacity(byteval, &suffix); + if (virAsprintf(&ret, "%.0f%s", size, suffix) < 0) + return NULL; + return ret; +} + char ** virshAllocpagesPagesizeCompleter(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED, unsigned int flags) { - unsigned long long byteval = 0; VIR_AUTOPTR(xmlXPathContext) ctxt = NULL; virshControlPtr priv = ctl->privData; unsigned int npages = 0; VIR_AUTOFREE(xmlNodePtr *) pages = NULL; VIR_AUTOPTR(xmlDoc) doc = NULL; - double size = 0; size_t i = 0; - const char *suffix = NULL; const char *cellnum = NULL; bool cellno = vshCommandOptBool(cmd, "cellno"); VIR_AUTOFREE(char *) path = NULL; - VIR_AUTOFREE(char *) pagesize = NULL; VIR_AUTOFREE(char *) cap_xml = NULL; char **ret = NULL; - VIR_AUTOFREE(char *) unit = NULL; VIR_AUTOSTRINGLIST tmp = NULL; virCheckFlags(0, NULL); @@ -667,16 +684,7 @@ virshAllocpagesPagesizeCompleter(vshControl *ctl, return NULL; for (i = 0; i < npages; i++) { - VIR_FREE(pagesize); - VIR_FREE(unit); - pagesize = virXMLPropString(pages[i], "size"); - unit = virXMLPropString(pages[i], "unit"); - if (virStrToLong_ull(pagesize, NULL, 10, &byteval) < 0) - return NULL; - if (virScaleInteger(&byteval, unit, 1024, UINT_MAX) < 0) - return NULL; - size = vshPrettyCapacity(byteval, &suffix); - if (virAsprintf(&tmp[i], "%.0f%s", size, suffix) < 0) + if (!(tmp[i] = virshPagesizeNodeToString(pages[i]))) return NULL; } -- 2.20.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list