2020
2121#include " Io.h"
2222
23+ #include " utils/memory.h"
24+
2325#include < libxml/xmlwriter.h>
2426
2527using namespace libcdoc ;
@@ -28,8 +30,8 @@ typedef const xmlChar *pcxmlChar;
2830
2931struct XMLWriter ::Private
3032{
31- xmlTextWriterPtr w = xmlNewTextWriter(
32- xmlOutputBufferCreateIO (xmlOutputWriteCallback, xmlOutputCloseCallback, this , nullptr ));
33+ unique_ptr_t <xmlFreeTextWriter> w = make_unique_ptr<xmlFreeTextWriter>( xmlNewTextWriter(
34+ xmlOutputBufferCreateIO (xmlOutputWriteCallback, xmlOutputCloseCallback, this , nullptr ) ));
3335 std::map<std::string, int > nsmap;
3436
3537 libcdoc::DataConsumer* dst = nullptr ;
@@ -57,7 +59,7 @@ XMLWriter::XMLWriter(libcdoc::DataConsumer* dst)
5759 : d(new Private)
5860{
5961 d->dst = dst;
60- xmlTextWriterStartDocument (d->w , nullptr , " UTF-8" , nullptr );
62+ xmlTextWriterStartDocument (d->w . get () , nullptr , " UTF-8" , nullptr );
6163}
6264
6365XMLWriter::XMLWriter (const std::string& path)
@@ -74,8 +76,7 @@ XMLWriter::XMLWriter(std::vector<uint8_t>& vec)
7476
7577XMLWriter::~XMLWriter ()
7678{
77- xmlTextWriterEndDocument (d->w );
78- xmlFreeTextWriter (d->w );
79+ xmlTextWriterEndDocument (d->w .get ());
7980 if (d->dst && d->dst_owned ) delete d->dst ;
8081 delete d;
8182}
@@ -89,12 +90,12 @@ int64_t XMLWriter::writeStartElement(const NS &ns, const std::string &name, cons
8990 pos->second ++;
9091 else
9192 pos = d->nsmap .insert ({ns.prefix , 1 }).first ;
92- if (xmlTextWriterStartElementNS (d->w , ns.prefix .empty () ? nullptr : pcxmlChar (ns.prefix .c_str ()),
93+ if (xmlTextWriterStartElementNS (d->w . get () , ns.prefix .empty () ? nullptr : pcxmlChar (ns.prefix .c_str ()),
9394 pcxmlChar (name.c_str ()), pos->second > 1 ? nullptr : pcxmlChar (ns.ns .c_str ())) == -1 )
9495 return IO_ERROR;
9596 for (auto i = attr.cbegin (), end = attr.cend (); i != end; ++i)
9697 {
97- if (xmlTextWriterWriteAttribute (d->w , pcxmlChar (i->first .c_str ()), pcxmlChar (i->second .c_str ())) == -1 )
98+ if (xmlTextWriterWriteAttribute (d->w . get () , pcxmlChar (i->first .c_str ()), pcxmlChar (i->second .c_str ())) == -1 )
9899 return IO_ERROR;
99100 }
100101 return OK;
@@ -104,7 +105,7 @@ int64_t XMLWriter::writeEndElement(const NS &ns)
104105{
105106 if (!d->w )
106107 return WRONG_ARGUMENTS;
107- if (xmlTextWriterEndElement (d->w ) == -1 )
108+ if (xmlTextWriterEndElement (d->w . get () ) == -1 )
108109 return IO_ERROR;
109110 if (std::map<std::string, int >::iterator pos = d->nsmap .find (ns.prefix );
110111 pos != d->nsmap .cend ())
@@ -134,7 +135,7 @@ int64_t XMLWriter::writeBase64Element(const NS &ns, const std::string &name, con
134135{
135136 if (auto rv = writeStartElement (ns, name, attr); rv != OK)
136137 return rv;
137- if (xmlTextWriterWriteBase64 (d->w , reinterpret_cast <const char *>(data.data ()), 0 , data.size ()) == -1 )
138+ if (xmlTextWriterWriteBase64 (d->w . get () , reinterpret_cast <const char *>(data.data ()), 0 , data.size ()) == -1 )
138139 return IO_ERROR;
139140 return writeEndElement (ns);
140141}
@@ -143,7 +144,7 @@ int64_t XMLWriter::writeTextElement(const NS &ns, const std::string &name, const
143144{
144145 if (auto rv = writeStartElement (ns, name, attr); rv != OK)
145146 return rv;
146- if (xmlTextWriterWriteString (d->w , pcxmlChar (data.c_str ())) == -1 )
147+ if (xmlTextWriterWriteString (d->w . get () , pcxmlChar (data.c_str ())) == -1 )
147148 return IO_ERROR;
148149 return writeEndElement (ns);
149150}
0 commit comments