On Thu, Apr 08, 2021 at 10:57:05AM +0200, Tim Wiederhake wrote: > Convenience function to return the value of an integer XML attribute. > > Signed-off-by: Tim Wiederhake <twiederh@xxxxxxxxxx> > --- > src/libvirt_private.syms | 1 + > src/util/virxml.c | 51 ++++++++++++++++++++++++++++++++++++++++ > src/util/virxml.h | 6 +++++ > 3 files changed, 58 insertions(+) > > diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms > index 776387e6b3..bdb63349ee 100644 > --- a/src/libvirt_private.syms > +++ b/src/libvirt_private.syms > @@ -3544,6 +3544,7 @@ virXMLNodeSanitizeNamespaces; > virXMLNodeToString; > virXMLParseHelper; > virXMLPickShellSafeComment; > +virXMLPropInt; > virXMLPropString; > virXMLPropStringLimit; > virXMLPropTristateBool; > diff --git a/src/util/virxml.c b/src/util/virxml.c > index f62c5c39c4..807cb7dae9 100644 > --- a/src/util/virxml.c > +++ b/src/util/virxml.c > @@ -656,6 +656,57 @@ virXMLPropTristateSwitch(xmlNodePtr node, const char* name, > } > > > +/** > + * virXMLPropInt: > + * @node: XML dom node pointer > + * @name: Name of the property (attribute) to get > + * @base: Number base, see strtol > + * @flags: Bitwise or of virXMLPropFlags > + * @result: The returned value > + * > + * Convenience function to return value of an integer attribute. > + * > + * Returns 1 in case of success in which case @result is set, > + * or 0 if the attribute is not present, > + * or -1 and reports an error on failure. > + */ > +int > +virXMLPropInt(xmlNodePtr node, const char *name, int base, > + virXMLPropFlags flags, int *result) > +{ > + g_autofree char *tmp = NULL; > + int ret; > + > + if (!node || !name || !result) { > + virReportError(VIR_ERR_INTERNAL_ERROR, > + _("Invalid argument to %s"), > + __FUNCTION__); > + return -1; > + } > + > + if (!(tmp = virXMLPropString(node, name))) { > + if ((flags & VIR_XML_PROP_REQUIRED) != VIR_XML_PROP_REQUIRED) > + return 0; > + > + virReportError(VIR_ERR_XML_ERROR, > + _("Missing required attribute '%s' in element '%s'"), > + name, node->name); > + return -1; > + } > + > + ret = virStrToLong_i(tmp, NULL, base, result); > + > + if ((ret < 0) || ((flags & VIR_XML_PROP_NONZERO) && (*result == 0))) { > + virReportError(VIR_ERR_XML_ERROR, > + _("Invalid value for attribute '%s' in element '%s': '%s'. Expected integer value"), > + name, node->name, tmp); > + return -1; > + } 0 is an integer value, so this message is not especially helpful in the case VIR_XML_PROP_NONZERO is set. Separate the two tests and give them separate error messages which are more directly relevant. "Unable to parse '%s' for property '%s' as an integer value" "Zero is not a permitted value for property '%s'" Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|