From: Vincent Bernat Date: Sat, 23 Jan 2016 15:46:26 +0000 (+0100) Subject: client: ensure XML is written to the provided file handle X-Git-Tag: 0.9.1~31 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9cff0cc82aae7f26953d92460d66c435fb1f2c95;p=thirdparty%2Flldpd.git client: ensure XML is written to the provided file handle Currently, this is just stdout but the interface provides a file handle and we should use it. --- diff --git a/src/client/xml_writer.c b/src/client/xml_writer.c index 83232f15..a53c6d66 100644 --- a/src/client/xml_writer.c +++ b/src/client/xml_writer.c @@ -33,37 +33,38 @@ #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; } -