#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");
#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 ) {
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;
}
return NULL;
}
- result = malloc( sizeof( struct writer ) );
- if ( ! result ) {
+ result = malloc(sizeof(struct writer));
+ if (!result) {
fatalx("lldpctl", "out of memory");
return NULL;
}
return result;
}
-