2626
2727using namespace libcdoc ;
2828
29- typedef const xmlChar *pcxmlChar ;
29+ using pcxmlChar = xmlChar *;
3030
3131struct XMLWriter ::Private
3232{
33- unique_ptr_t <xmlFreeTextWriter> w = make_unique_ptr<xmlFreeTextWriter>(xmlNewTextWriter(
34- xmlOutputBufferCreateIO (xmlOutputWriteCallback, xmlOutputCloseCallback, this , nullptr ))) ;
35- std::map<std::string, int > nsmap;
33+ Private (libcdoc::DataConsumer &_dst): dst(_dst) {}
34+ libcdoc::DataConsumer &dst ;
35+ std::map<std::string, int > nsmap;
3636
37- libcdoc::DataConsumer* dst = nullptr ;
38- bool dst_owned = false ;
37+ unique_ptr_t <xmlFreeTextWriter> w = make_unique_ptr<xmlFreeTextWriter>(xmlNewTextWriter(
38+ xmlOutputBufferCreateIO (xmlOutputWriteCallback, nullptr , this , nullptr ))) ;
3939
40- static int xmlOutputWriteCallback (void *context, const char *buffer, int len);
41- static int xmlOutputCloseCallback (void *context);
40+ static int xmlOutputWriteCallback (void *context, const char *buffer, int len);
4241};
4342
4443int
4544XMLWriter::Private::xmlOutputWriteCallback (void *context, const char *buffer, int len)
4645{
47- auto *d = reinterpret_cast <XMLWriter::Private *>(context);
48- return d->dst ->write ((uint8_t *) buffer, len);
49- }
50-
51- int
52- XMLWriter::Private::xmlOutputCloseCallback (void *context)
53- {
54- auto *d = reinterpret_cast <XMLWriter::Private *>(context);
55- return d->dst ->close ();
56- }
57-
58- XMLWriter::XMLWriter (libcdoc::DataConsumer* dst)
59- : d(new Private)
60- {
61- d->dst = dst;
62- xmlTextWriterStartDocument (d->w .get (), nullptr , " UTF-8" , nullptr );
46+ auto *d = reinterpret_cast <XMLWriter::Private *>(context);
47+ return d->dst .write ((uint8_t *) buffer, len);
6348}
6449
65- XMLWriter::XMLWriter (std::vector< uint8_t >& vec )
66- : XMLWriter( new libcdoc::VectorConsumer(vec ))
50+ XMLWriter::XMLWriter (libcdoc::DataConsumer &dst )
51+ : d(std::make_unique<Private>(dst ))
6752{
68- d->dst_owned = true ;
53+ if (d->w )
54+ xmlTextWriterStartDocument (d->w .get (), nullptr , " UTF-8" , nullptr );
6955}
7056
71- XMLWriter::~XMLWriter ()
57+ XMLWriter::~XMLWriter () noexcept
7258{
73- xmlTextWriterEndDocument (d->w .get ());
74- // Force XmlTextWriter to finish before deleting consumer
75- d->w .reset ();
76- if (d->dst && d->dst_owned ) delete d->dst ;
77- delete d;
59+ if (d->w )
60+ xmlTextWriterEndDocument (d->w .get ());
7861}
7962
8063int64_t XMLWriter::writeStartElement (const NS &ns, const std::string &name, const std::map<std::string, std::string> &attr)
8164{
8265 if (!d->w )
8366 return WRONG_ARGUMENTS;
84- std::map<std::string, int >::iterator pos = d->nsmap .find (ns.prefix );
85- if (pos != d->nsmap .cend ())
86- pos->second ++;
87- else
88- pos = d->nsmap .insert ({ns.prefix , 1 }).first ;
89- if (xmlTextWriterStartElementNS (d->w .get (), ns.prefix .empty () ? nullptr : pcxmlChar (ns.prefix .c_str ()),
90- pcxmlChar (name.c_str ()), pos->second > 1 ? nullptr : pcxmlChar (ns.ns .c_str ())) == -1 )
67+ auto pos = d->nsmap .find (ns.prefix );
68+ if (pos != d->nsmap .cend ())
69+ pos->second ++;
70+ else
71+ pos = d->nsmap .insert ({ns.prefix , 1 }).first ;
72+ if (xmlTextWriterStartElementNS (d->w .get (),
73+ ns.prefix .empty () ? nullptr : pcxmlChar (ns.prefix .c_str ()),
74+ pcxmlChar (name.c_str ()),
75+ pos->second > 1 ? nullptr : pcxmlChar (ns.ns .c_str ())) == -1 )
9176 return IO_ERROR;
92- for ( auto i = attr. cbegin (), end = attr. cend (); i != end; ++i )
93- {
94- if (xmlTextWriterWriteAttribute (d->w .get (), pcxmlChar (i-> first .c_str ()), pcxmlChar (i-> second .c_str ())) == -1 )
77+ for ( const auto &[name, content]: attr )
78+ {
79+ if (xmlTextWriterWriteAttribute (d->w .get (), pcxmlChar (name .c_str ()), pcxmlChar (content .c_str ())) == -1 )
9580 return IO_ERROR;
96- }
81+ }
9782 return OK;
9883}
9984
@@ -103,26 +88,25 @@ int64_t XMLWriter::writeEndElement(const NS &ns)
10388 return WRONG_ARGUMENTS;
10489 if (xmlTextWriterEndElement (d->w .get ()) == -1 )
10590 return IO_ERROR;
106- if (std::map<std::string, int >::iterator pos = d->nsmap .find (ns.prefix );
107- pos != d->nsmap .cend ())
108- pos->second --;
91+ if (auto pos = d->nsmap .find (ns.prefix ); pos != d->nsmap .cend ())
92+ pos->second --;
10993 return OK;
11094}
11195
112- int64_t XMLWriter::writeElement (const NS &ns, const std::string &name, const std::function<uint64_t ()> &f)
96+ int64_t XMLWriter::writeElement (const NS &ns, const std::string &name, const std::function<int64_t ()> &f)
11397{
11498 if (auto rv = writeStartElement (ns, name, {}); rv != OK)
11599 return rv;
116- if (uint64_t rv = OK; f && (rv = f ()) != OK)
100+ if (int64_t rv = OK; f && (rv = f ()) != OK)
117101 return rv;
118102 return writeEndElement (ns);
119103}
120104
121- int64_t XMLWriter::writeElement (const NS &ns, const std::string &name, const std::map<std::string, std::string> &attr, const std::function<uint64_t ()> &f)
105+ int64_t XMLWriter::writeElement (const NS &ns, const std::string &name, const std::map<std::string, std::string> &attr, const std::function<int64_t ()> &f)
122106{
123107 if (auto rv = writeStartElement (ns, name, attr); rv != OK)
124108 return rv;
125- if (uint64_t rv = OK; f && (rv = f ()) != OK)
109+ if (int64_t rv = OK; f && (rv = f ()) != OK)
126110 return rv;
127111 return writeEndElement (ns);
128112}
0 commit comments