Re: [PATCH 1/2] virsh.c: new xPathULong() to parse XML returning ulong value of selected node.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2011/2/17 Michal Privoznik <mprivozn@xxxxxxxxxx>:
> Parse XML from given buffer, select appropiate XPath node and return its
> value as unsigned long.
> ---
> Âtools/virsh.c | Â 49 +++++++++++++++++++++++++++++++++++++++++++++++++
> Â1 files changed, 49 insertions(+), 0 deletions(-)
>
> diff --git a/tools/virsh.c b/tools/virsh.c
> index 50d5e33..8804fc3 100644
> --- a/tools/virsh.c
> +++ b/tools/virsh.c
> @@ -2270,6 +2270,55 @@ static const vshCmdInfo info_freecell[] = {
> Â Â {NULL, NULL}
> Â};
>
> +static int
> +xPathULong(char *xml, char *xpath, unsigned long *value) {
> +
> + Â ÂxmlDocPtr doc = NULL;
> + Â ÂxmlXPathContextPtr ctxt = NULL;
> + Â ÂxmlXPathObjectPtr obj = NULL;
> + Â Âint ret = -1;
> +
> + Â Âdoc = xmlReadDoc((const xmlChar *) xml, "domain.xml", NULL,
> + Â Â Â Â Â Â Â Â Â Â XML_PARSE_NOENT | XML_PARSE_NONET | XML_PARSE_NOWARNING);
> + Â Âif (!doc)
> + Â Â Â Âgoto cleanup;
> +
> + Â Âctxt = xmlXPathNewContext(doc);
> + Â Âif (!ctxt)
> + Â Â Â Âgoto cleanup;
> +
> + Â Âobj = xmlXPathEval(BAD_CAST xpath, ctxt);
> + Â Âif (!obj)
> + Â Â Â Âgoto cleanup;
> +
> + Â Âif (!value)
> + Â Â Â Âgoto cleanup;
> +
> + Â Â*value = 0;
> + Â Âif ((obj->type == XPATH_STRING) &&
> + Â Â Â Â(obj->stringval != NULL) && (obj->stringval[0] != 0)) {
> + Â Â Â Â Â Âchar *conv = NULL;
> + Â Â Â Â Â Âunsigned long val;
> +
> + Â Â Â Â Â Âval = strtoul((const char *) obj->stringval, &conv, 10);
> + Â Â Â Â Â Âif (*conv =='\0') {
> + Â Â Â Â Â Â Â Â*value = val;
> + Â Â Â Â Â Â Â Âret = 0;
> + Â Â Â Â Â Â}
> + Â Â} else if ((obj != NULL) && (obj->type == XPATH_NUMBER)) {
> + Â Â Â Â*value = (unsigned long) obj->floatval;
> + Â Â Â Âif (*value == obj->floatval) {
> + Â Â Â Â Â Âret = 0;
> + Â Â Â Â}
> + Â Â}
> +
> +cleanup:
> + Â ÂxmlXPathFreeObject(obj);
> + Â ÂxmlXPathFreeContext(ctxt);
> + Â ÂxmlFreeDoc(doc);
> + Â Âreturn ret;
> +}
> +
> Âstatic const vshCmdOptDef opts_freecell[] = {
> Â Â {"cellno", VSH_OT_INT, 0, N_("NUMA cell number")},
> Â Â {"all", VSH_OT_BOOL, 0, N_("show free memory for all NUMA cells")},
> --
> 1.7.3.5
>

NACK, there already is a virXPathULong function. Look at how other
virXPath* functions are used in virsh.

Matthias

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]