提交 23ab2092 编写于 作者: P Peter Krempa

util: XML: Introduce automatic reset of XPath's current node

Quite a few parts modify the XPath context current node to shift the
scope and allow easier queries. This also means that the node needs
to be restored afterwards.

Introduce a macro based on 'VIR_AUTOCLEAN' which adds a local structure
on the stack remembering the original node along with a function which
will make sure that the node is reset when the local structure leaves
scope.
Signed-off-by: NPeter Krempa <pkrempa@redhat.com>
Reviewed-by: NJán Tomko <jtomko@redhat.com>
上级 786f4741
......@@ -3247,6 +3247,7 @@ virXMLValidatorFree;
virXMLValidatorInit;
virXMLValidatorValidate;
virXPathBoolean;
virXPathContextNodeRestore;
virXPathInt;
virXPathLong;
virXPathLongHex;
......
......@@ -1398,3 +1398,13 @@ virXMLFormatElement(virBufferPtr buf,
virBufferFreeAndReset(childBuf);
return ret;
}
void
virXPathContextNodeRestore(virXPathContextNodeSavePtr save)
{
if (!save->ctxt)
return;
save->ctxt->node = save->node;
}
......@@ -219,4 +219,28 @@ virXMLFormatElement(virBufferPtr buf,
virBufferPtr attrBuf,
virBufferPtr childBuf);
struct _virXPathContextNodeSave {
xmlXPathContextPtr ctxt;
xmlNodePtr node;
};
typedef struct _virXPathContextNodeSave virXPathContextNodeSave;
typedef virXPathContextNodeSave *virXPathContextNodeSavePtr;
void
virXPathContextNodeRestore(virXPathContextNodeSavePtr save);
VIR_DEFINE_AUTOCLEAN_FUNC(virXPathContextNodeSave, virXPathContextNodeRestore);
/**
* VIR_XPATH_NODE_AUTORESTORE:
* @ctxt: XML XPath context pointer
*
* This macro ensures that when the scope where it's used ends, @ctxt's current
* node pointer is reset to the original value when this macro was used.
*/
# define VIR_XPATH_NODE_AUTORESTORE(_ctxt) \
VIR_AUTOCLEAN(virXPathContextNodeSave) _ctxt ## CtxtSave = { .ctxt = _ctxt,\
.node = _ctxt->node}; \
ignore_value(&_ctxt ## CtxtSave)
#endif /* LIBVIRT_VIRXML_H */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册