]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
client: ensure XML is written to the provided file handle
authorVincent Bernat <vincent@bernat.im>
Sat, 23 Jan 2016 15:46:26 +0000 (16:46 +0100)
committerVincent Bernat <vincent@bernat.im>
Sat, 23 Jan 2016 15:46:26 +0000 (16:46 +0100)
Currently, this is just stdout but the interface provides a file handle
and we should use it.

src/client/xml_writer.c

index 83232f159137a79bcefaf610c5bfe53c4f6e1e49..a53c6d66d823a0f296268c72f573fe54fc25d7d8 100644 (file)
 #include "../log.h"
 
 struct xml_writer_private {
+       FILE *fh;
        xmlTextWriterPtr xw;
        xmlDocPtr doc;
 };
 
-void xml_start(struct writer * w , const char * tag, const char * descr ) {
-       struct xml_writer_private * p = w->priv;
+void xml_start(struct writer *w , const char *tag, const char *descr ) {
+       struct xml_writer_private *p = w->priv;
 
        if (xmlTextWriterStartElement(p->xw, BAD_CAST tag) < 0)
                log_warnx("lldpctl", "cannot start '%s' element", tag);
 
-       if ( descr && (strlen(descr) > 0) ) {
+       if (descr && (strlen(descr) > 0)) {
                if (xmlTextWriterWriteFormatAttribute(p->xw, BAD_CAST "label", "%s", descr) < 0)
                        log_warnx("lldpctl", "cannot add attribute 'label' to element %s", tag);
        }
 }
 
-void xml_attr(struct writer * w, const char * tag, const char * descr, const char * value ) {
-       struct xml_writer_private * p = w->priv;
+void xml_attr(struct writer *w, const char *tag, const char *descr, const char *value ) {
+       struct xml_writer_private *p = w->priv;
 
        if (xmlTextWriterWriteFormatAttribute(p->xw, BAD_CAST tag, "%s", value?value:"") < 0)
                log_warnx("lldpctl", "cannot add attribute %s with value %s", tag, value?value:"(none)");
 }
 
-void xml_data(struct writer * w, const char * data) {
-       struct xml_writer_private * p = w->priv;
+void xml_data(struct writer *w, const char *data) {
+       struct xml_writer_private *p = w->priv;
        if (xmlTextWriterWriteString(p->xw, BAD_CAST (data?data:"")) < 0 )
                log_warnx("lldpctl", "cannot add '%s' as data to element", data?data:"(none)");
 }
 
-void xml_end(struct writer * w) {
-       struct xml_writer_private * p = w->priv;
+void xml_end(struct writer *w) {
+       struct xml_writer_private *p = w->priv;
 
        if (xmlTextWriterEndElement(p->xw) < 0 )
                log_warnx("lldpctl", "cannot end element");
@@ -71,8 +72,8 @@ void xml_end(struct writer * w) {
 
 #define MY_ENCODING "UTF-8"
 
-void xml_finish(struct writer * w) {
-       struct xml_writer_private * p = w->priv;
+void xml_finish(struct writer *w) {
+       struct xml_writer_private *p = w->priv;
        int failed = 0;
 
        if (xmlTextWriterEndDocument(p->xw) < 0 ) {
@@ -82,28 +83,29 @@ void xml_finish(struct writer * w) {
 
        xmlFreeTextWriter(p->xw);
 
-       if ( ! failed )
-               xmlSaveFileEnc("-", p->doc, MY_ENCODING);
+       if (!failed)
+               xmlDocDump(p->fh, p->doc);
 
        xmlFreeDoc(p->doc);
 
-       free( w->priv );
-       free( w );
+       free(w->priv);
+       free(w);
 }
 
-struct writer * xml_init(FILE * fh) {
+struct writer *xml_init(FILE *fh) {
 
-       struct writer * result;
-       struct xml_writer_private * priv;
+       struct writer *result;
+       struct xml_writer_private *priv;
 
-       priv = malloc( sizeof( *priv ) );
-       if ( ! priv ) {
+       priv = malloc(sizeof(*priv));
+       priv->fh = fh;
+       if (!priv) {
                fatalx("lldpctl", "out of memory");
                return NULL;
        }
 
        priv->xw = xmlNewTextWriterDoc(&(priv->doc), 0);
-       if ( ! priv->xw ) {
+       if (!priv->xw) {
                fatalx("lldpctl", "cannot create xml writer");
                return NULL;
        }
@@ -115,8 +117,8 @@ struct writer * xml_init(FILE * fh) {
                return NULL;
        }
 
-       result = malloc( sizeof( struct writer ) );
-       if ( ! result ) {
+       result = malloc(sizeof(struct writer));
+       if (!result) {
                fatalx("lldpctl", "out of memory");
                return NULL;
        }
@@ -130,4 +132,3 @@ struct writer * xml_init(FILE * fh) {
 
        return result;
 }
-