mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-28 11:31:16 +00:00
virXMLFormatElement: Introduce virXMLFormatElementInternal
The new function aggregates the internal working of virXMLFormatElement and virXMLFormatElementEmpty and also allows skipping the newline after the opening tag to allow using this helper also in cases where we don't format any child elements but directly a value. Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
parent
a8682ab791
commit
68d1056392
@ -3634,6 +3634,7 @@ virXMLCheckIllegalChars;
|
|||||||
virXMLExtractNamespaceXML;
|
virXMLExtractNamespaceXML;
|
||||||
virXMLFormatElement;
|
virXMLFormatElement;
|
||||||
virXMLFormatElementEmpty;
|
virXMLFormatElementEmpty;
|
||||||
|
virXMLFormatElementInternal;
|
||||||
virXMLFormatMetadata;
|
virXMLFormatMetadata;
|
||||||
virXMLNewNode;
|
virXMLNewNode;
|
||||||
virXMLNodeContentString;
|
virXMLNodeContentString;
|
||||||
|
@ -1609,21 +1609,48 @@ virXMLValidatorFree(virXMLValidator *validator)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* same as virXMLFormatElement but outputs an empty element if @attrBuf and
|
/**
|
||||||
* @childBuf are both empty */
|
* virXMLFormatElementInternal
|
||||||
|
* @buf: the parent buffer where the element will be placed
|
||||||
|
* @name: the name of the element
|
||||||
|
* @attrBuf: buffer with attributes for element, may be NULL
|
||||||
|
* @childBuf: buffer with child elements, may be NULL
|
||||||
|
* @allowEmpty: Format empty element if @attrBuf and @childBuf are empty
|
||||||
|
* @childNewline: Add a newline after the opening element before formatting @childBuf
|
||||||
|
*
|
||||||
|
* Helper to format element where attributes or child elements
|
||||||
|
* are optional and may not be formatted. If both @attrBuf and
|
||||||
|
* @childBuf are NULL or are empty buffers the element is not
|
||||||
|
* formatted.
|
||||||
|
*
|
||||||
|
* Passing false for @childNewline allows to format elements where we directly
|
||||||
|
* output a value without subelements.
|
||||||
|
*
|
||||||
|
* Both passed buffers are always consumed and freed.
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
virXMLFormatElementEmpty(virBuffer *buf,
|
virXMLFormatElementInternal(virBuffer *buf,
|
||||||
const char *name,
|
const char *name,
|
||||||
virBuffer *attrBuf,
|
virBuffer *attrBuf,
|
||||||
virBuffer *childBuf)
|
virBuffer *childBuf,
|
||||||
|
bool allowEmpty,
|
||||||
|
bool childNewline)
|
||||||
{
|
{
|
||||||
|
if (!allowEmpty) {
|
||||||
|
if ((!attrBuf || virBufferUse(attrBuf) == 0) &&
|
||||||
|
(!childBuf || virBufferUse(childBuf) == 0))
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
virBufferAsprintf(buf, "<%s", name);
|
virBufferAsprintf(buf, "<%s", name);
|
||||||
|
|
||||||
if (attrBuf && virBufferUse(attrBuf) > 0)
|
if (attrBuf && virBufferUse(attrBuf) > 0)
|
||||||
virBufferAddBuffer(buf, attrBuf);
|
virBufferAddBuffer(buf, attrBuf);
|
||||||
|
|
||||||
if (childBuf && virBufferUse(childBuf) > 0) {
|
if (childBuf && virBufferUse(childBuf) > 0) {
|
||||||
virBufferAddLit(buf, ">\n");
|
virBufferAddLit(buf, ">");
|
||||||
|
if (childNewline)
|
||||||
|
virBufferAddLit(buf, "\n");
|
||||||
virBufferAddBuffer(buf, childBuf);
|
virBufferAddBuffer(buf, childBuf);
|
||||||
virBufferAsprintf(buf, "</%s>\n", name);
|
virBufferAsprintf(buf, "</%s>\n", name);
|
||||||
} else {
|
} else {
|
||||||
@ -1634,6 +1661,17 @@ virXMLFormatElementEmpty(virBuffer *buf,
|
|||||||
virBufferFreeAndReset(childBuf);
|
virBufferFreeAndReset(childBuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* same as virXMLFormatElement but outputs an empty element if @attrBuf and
|
||||||
|
* @childBuf are both empty */
|
||||||
|
void
|
||||||
|
virXMLFormatElementEmpty(virBuffer *buf,
|
||||||
|
const char *name,
|
||||||
|
virBuffer *attrBuf,
|
||||||
|
virBuffer *childBuf)
|
||||||
|
{
|
||||||
|
virXMLFormatElementInternal(buf, name, attrBuf, childBuf, true, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* virXMLFormatElement
|
* virXMLFormatElement
|
||||||
@ -1655,11 +1693,7 @@ virXMLFormatElement(virBuffer *buf,
|
|||||||
virBuffer *attrBuf,
|
virBuffer *attrBuf,
|
||||||
virBuffer *childBuf)
|
virBuffer *childBuf)
|
||||||
{
|
{
|
||||||
if ((!attrBuf || virBufferUse(attrBuf) == 0) &&
|
virXMLFormatElementInternal(buf, name, attrBuf, childBuf, false, true);
|
||||||
(!childBuf || virBufferUse(childBuf) == 0))
|
|
||||||
return;
|
|
||||||
|
|
||||||
virXMLFormatElementEmpty(buf, name, attrBuf, childBuf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -322,6 +322,13 @@ void
|
|||||||
virXMLValidatorFree(virXMLValidator *validator);
|
virXMLValidatorFree(virXMLValidator *validator);
|
||||||
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virXMLValidator, virXMLValidatorFree);
|
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virXMLValidator, virXMLValidatorFree);
|
||||||
|
|
||||||
|
void
|
||||||
|
virXMLFormatElementInternal(virBuffer *buf,
|
||||||
|
const char *name,
|
||||||
|
virBuffer *attrBuf,
|
||||||
|
virBuffer *childBuf,
|
||||||
|
bool allowEmpty,
|
||||||
|
bool childNewline);
|
||||||
void
|
void
|
||||||
virXMLFormatElement(virBuffer *buf,
|
virXMLFormatElement(virBuffer *buf,
|
||||||
const char *name,
|
const char *name,
|
||||||
|
Loading…
Reference in New Issue
Block a user