@@ -81,72 +81,70 @@ XMLWriter::~XMLWriter()
8181 delete d;
8282}
8383
84- void XMLWriter::writeStartElement (const NS &ns, const std::string &name, const std::map<std::string, std::string> &attr)
84+ int64_t XMLWriter::writeStartElement (const NS &ns, const std::string &name, const std::map<std::string, std::string> &attr)
8585{
86+ if (!d->w )
87+ return WRONG_ARGUMENTS;
8688 std::map<std::string, int >::iterator pos = d->nsmap .find (ns.prefix );
8789 if (pos != d->nsmap .cend ())
8890 pos->second ++;
8991 else
9092 pos = d->nsmap .insert ({ns.prefix , 1 }).first ;
91- if (!d->w )
92- return ;
93- if (xmlTextWriterStartElementNS (d->w , ns.prefix .empty () ? nullptr : pcxmlChar (ns.prefix .c_str ()),
94- pcxmlChar (name.c_str ()), pos->second > 1 ? nullptr : pcxmlChar (ns.ns .c_str ())) < 0 )
95- return ;
93+ if (xmlTextWriterStartElementNS (d->w , ns.prefix .empty () ? nullptr : pcxmlChar (ns.prefix .c_str ()),
94+ pcxmlChar (name.c_str ()), pos->second > 1 ? nullptr : pcxmlChar (ns.ns .c_str ())) == -1 )
95+ return IO_ERROR;
9696 for (auto i = attr.cbegin (), end = attr.cend (); i != end; ++i)
9797 {
98- if (xmlTextWriterWriteAttribute (d->w , pcxmlChar (i->first .c_str ()), pcxmlChar (i->second .c_str ())) < 0 )
99- break ;
98+ if (xmlTextWriterWriteAttribute (d->w , pcxmlChar (i->first .c_str ()), pcxmlChar (i->second .c_str ())) == - 1 )
99+ return IO_ERROR ;
100100 }
101+ return OK;
101102}
102103
103- void XMLWriter::writeEndElement (const NS &ns)
104+ int64_t XMLWriter::writeEndElement (const NS &ns)
104105{
105- if (d->w )
106- xmlTextWriterEndElement (d->w );
107- std::map<std::string, int >::iterator pos = d->nsmap .find (ns.prefix );
108- if (pos != d->nsmap .cend ())
106+ if (!d->w )
107+ return WRONG_ARGUMENTS;
108+ if (xmlTextWriterEndElement (d->w ) == -1 )
109+ return IO_ERROR;
110+ if (std::map<std::string, int >::iterator pos = d->nsmap .find (ns.prefix );
111+ pos != d->nsmap .cend ())
109112 pos->second --;
113+ return OK;
110114}
111115
112- void XMLWriter::writeElement (const NS &ns, const std::string &name, const std::function<void ()> &f)
116+ int64_t XMLWriter::writeElement (const NS &ns, const std::string &name, const std::function<void ()> &f)
113117{
114- writeStartElement (ns, name, {});
118+ if (auto rv = writeStartElement (ns, name, {}); rv != OK)
119+ return rv;
115120 if (f)
116121 f ();
117- writeEndElement (ns);
122+ return writeEndElement (ns);
118123}
119124
120- void XMLWriter::writeElement (const NS &ns, const std::string &name, const std::map<std::string, std::string> &attr, const std::function<void ()> &f)
125+ int64_t XMLWriter::writeElement (const NS &ns, const std::string &name, const std::map<std::string, std::string> &attr, const std::function<void ()> &f)
121126{
122- writeStartElement (ns, name, attr);
127+ if (auto rv = writeStartElement (ns, name, attr); rv != OK)
128+ return rv;
123129 if (f)
124130 f ();
125- writeEndElement (ns);
131+ return writeEndElement (ns);
126132}
127133
128- void XMLWriter::writeBase64Element (const NS &ns, const std::string &name, const std::vector<xmlChar> &data, const std::map<std::string, std::string> &attr)
134+ int64_t XMLWriter::writeBase64Element (const NS &ns, const std::string &name, const std::vector<xmlChar> &data, const std::map<std::string, std::string> &attr)
129135{
130- if (!d->w )
131- return ;
132- static const size_t bufLen = 48 * 10240 ;
133- std::vector<xmlChar> result (((bufLen + 2 ) / 3 ) * 4 , 0 );
134- writeStartElement (ns, name, attr);
135- for (size_t i = 0 ; i < data.size (); i += bufLen)
136- {
137- int size = EVP_EncodeBlock (result.data (), &data[i], std::min (data.size () - i, bufLen));
138- if (size == 0 )
139- break ;
140- if (xmlTextWriterWriteRawLen (d->w , result.data (), size) < 0 )
141- break ;
142- }
143- writeEndElement (ns);
136+ if (auto rv = writeStartElement (ns, name, attr); rv != OK)
137+ return rv;
138+ if (xmlTextWriterWriteBase64 (d->w , reinterpret_cast <const char *>(data.data ()), 0 , data.size ()) == -1 )
139+ return IO_ERROR;
140+ return writeEndElement (ns);
144141}
145142
146- void XMLWriter::writeTextElement (const NS &ns, const std::string &name, const std::map<std::string, std::string> &attr, const std::string &data)
143+ int64_t XMLWriter::writeTextElement (const NS &ns, const std::string &name, const std::map<std::string, std::string> &attr, const std::string &data)
147144{
148- writeStartElement (ns, name, attr);
149- if (d->w )
150- (void )xmlTextWriterWriteString (d->w , pcxmlChar (data.c_str ()));
151- writeEndElement (ns);
145+ if (auto rv = writeStartElement (ns, name, attr); rv != OK)
146+ return rv;
147+ if (xmlTextWriterWriteString (d->w , pcxmlChar (data.c_str ())) == -1 )
148+ return IO_ERROR;
149+ return writeEndElement (ns);
152150}
0 commit comments