]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
client: fix spurious warning in XML writer
authorVincent Bernat <vincent@bernat.im>
Thu, 19 May 2016 18:51:07 +0000 (20:51 +0200)
committerVincent Bernat <vincent@bernat.im>
Thu, 19 May 2016 18:51:07 +0000 (20:51 +0200)
It seems that libxml doesn't like generating an empty
document. Therefore, we initialize the writer only when we are sure we
will get content.

src/client/xml_writer.c

index c38fdfcf8c0de18439c9e14b741ede9a1177847a..3441866b03e2f5de6d14e47166e522576918f120 100644 (file)
@@ -39,9 +39,24 @@ struct xml_writer_private {
        xmlDocPtr doc;
 };
 
+void xml_new_writer(struct xml_writer_private *priv)
+{
+       priv->xw = xmlNewTextWriterDoc(&(priv->doc), 0);
+       if (!priv->xw)
+               fatalx("lldpctl", "cannot create xml writer");
+
+       xmlTextWriterSetIndent(priv->xw, 4);
+
+       if (xmlTextWriterStartDocument(priv->xw, NULL, "UTF-8", NULL) < 0 )
+               fatalx("lldpctl", "cannot start xml document");
+}
+
 void xml_start(struct writer *w , const char *tag, const char *descr ) {
        struct xml_writer_private *p = w->priv;
 
+       if (p->depth == 0)
+               xml_new_writer(p);
+
        if (xmlTextWriterStartElement(p->xw, BAD_CAST tag) < 0)
                log_warnx("lldpctl", "cannot start '%s' element", tag);
 
@@ -66,18 +81,6 @@ void xml_data(struct writer *w, const char *data) {
                log_warnx("lldpctl", "cannot add '%s' as data to element", data?data:"(none)");
 }
 
-void xml_new_writer(struct xml_writer_private *priv)
-{
-       priv->xw = xmlNewTextWriterDoc(&(priv->doc), 0);
-       if (!priv->xw)
-               fatalx("lldpctl", "cannot create xml writer");
-
-       xmlTextWriterSetIndent(priv->xw, 4);
-
-       if (xmlTextWriterStartDocument(priv->xw, NULL, "UTF-8", NULL) < 0 )
-               fatalx("lldpctl", "cannot start xml document");
-}
-
 void xml_end(struct writer *w) {
        struct xml_writer_private *p = w->priv;
 
@@ -93,13 +96,9 @@ void xml_end(struct writer *w) {
                }
 
                xmlFreeTextWriter(p->xw);
-
                if (!failed)
                        xmlDocDump(p->fh, p->doc);
-
                xmlFreeDoc(p->doc);
-
-               xml_new_writer(p);
        }
 }
 
@@ -110,9 +109,7 @@ void xml_finish(struct writer *w) {
                /* memory leak... */
        }
 
-       xmlFreeTextWriter(p->xw);
-       xmlFreeDoc(p->doc);
-       free(w->priv);
+       free(p);
        free(w);
 }
 
@@ -129,8 +126,6 @@ struct writer *xml_init(FILE *fh) {
        priv->fh = fh;
        priv->depth = 0;
 
-       xml_new_writer(priv);
-
        result = malloc(sizeof(struct writer));
        if (!result)
                fatalx("lldpctl", "out of memory");