|
18 | 18 |
|
19 | 19 | #include "XmlWriter.h" |
20 | 20 |
|
21 | | -#include "Utils.h" |
| 21 | +#include "Io.h" |
22 | 22 |
|
23 | | -#include <openssl/evp.h> |
24 | 23 | #include <libxml/xmlwriter.h> |
25 | 24 |
|
26 | 25 | using namespace libcdoc; |
@@ -81,72 +80,70 @@ XMLWriter::~XMLWriter() |
81 | 80 | delete d; |
82 | 81 | } |
83 | 82 |
|
84 | | -void XMLWriter::writeStartElement(const NS &ns, const std::string &name, const std::map<std::string, std::string> &attr) |
| 83 | +int64_t XMLWriter::writeStartElement(const NS &ns, const std::string &name, const std::map<std::string, std::string> &attr) |
85 | 84 | { |
| 85 | + if(!d->w) |
| 86 | + return WRONG_ARGUMENTS; |
86 | 87 | std::map<std::string, int>::iterator pos = d->nsmap.find(ns.prefix); |
87 | 88 | if (pos != d->nsmap.cend()) |
88 | 89 | pos->second++; |
89 | 90 | else |
90 | 91 | 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; |
| 92 | + if(xmlTextWriterStartElementNS(d->w, ns.prefix.empty() ? nullptr : pcxmlChar(ns.prefix.c_str()), |
| 93 | + pcxmlChar(name.c_str()), pos->second > 1 ? nullptr : pcxmlChar(ns.ns.c_str())) == -1) |
| 94 | + return IO_ERROR; |
96 | 95 | for(auto i = attr.cbegin(), end = attr.cend(); i != end; ++i) |
97 | 96 | { |
98 | | - if(xmlTextWriterWriteAttribute(d->w, pcxmlChar(i->first.c_str()), pcxmlChar(i->second.c_str())) < 0) |
99 | | - break; |
| 97 | + if(xmlTextWriterWriteAttribute(d->w, pcxmlChar(i->first.c_str()), pcxmlChar(i->second.c_str())) == -1) |
| 98 | + return IO_ERROR; |
100 | 99 | } |
| 100 | + return OK; |
101 | 101 | } |
102 | 102 |
|
103 | | -void XMLWriter::writeEndElement(const NS &ns) |
| 103 | +int64_t XMLWriter::writeEndElement(const NS &ns) |
104 | 104 | { |
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()) |
| 105 | + if(!d->w) |
| 106 | + return WRONG_ARGUMENTS; |
| 107 | + if(xmlTextWriterEndElement(d->w) == -1) |
| 108 | + return IO_ERROR; |
| 109 | + if(std::map<std::string, int>::iterator pos = d->nsmap.find(ns.prefix); |
| 110 | + pos != d->nsmap.cend()) |
109 | 111 | pos->second--; |
| 112 | + return OK; |
110 | 113 | } |
111 | 114 |
|
112 | | -void XMLWriter::writeElement(const NS &ns, const std::string &name, const std::function<void()> &f) |
| 115 | +int64_t XMLWriter::writeElement(const NS &ns, const std::string &name, const std::function<void()> &f) |
113 | 116 | { |
114 | | - writeStartElement(ns, name, {}); |
| 117 | + if(auto rv = writeStartElement(ns, name, {}); rv != OK) |
| 118 | + return rv; |
115 | 119 | if(f) |
116 | 120 | f(); |
117 | | - writeEndElement(ns); |
| 121 | + return writeEndElement(ns); |
118 | 122 | } |
119 | 123 |
|
120 | | -void XMLWriter::writeElement(const NS &ns, const std::string &name, const std::map<std::string, std::string> &attr, const std::function<void()> &f) |
| 124 | +int64_t XMLWriter::writeElement(const NS &ns, const std::string &name, const std::map<std::string, std::string> &attr, const std::function<void()> &f) |
121 | 125 | { |
122 | | - writeStartElement(ns, name, attr); |
| 126 | + if(auto rv = writeStartElement(ns, name, attr); rv != OK) |
| 127 | + return rv; |
123 | 128 | if(f) |
124 | 129 | f(); |
125 | | - writeEndElement(ns); |
| 130 | + return writeEndElement(ns); |
126 | 131 | } |
127 | 132 |
|
128 | | -void XMLWriter::writeBase64Element(const NS &ns, const std::string &name, const std::vector<xmlChar> &data, const std::map<std::string, std::string> &attr) |
| 133 | +int64_t XMLWriter::writeBase64Element(const NS &ns, const std::string &name, const std::vector<xmlChar> &data, const std::map<std::string, std::string> &attr) |
129 | 134 | { |
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); |
| 135 | + if(auto rv = writeStartElement(ns, name, attr); rv != OK) |
| 136 | + return rv; |
| 137 | + if(xmlTextWriterWriteBase64(d->w, reinterpret_cast<const char*>(data.data()), 0, data.size()) == -1) |
| 138 | + return IO_ERROR; |
| 139 | + return writeEndElement(ns); |
144 | 140 | } |
145 | 141 |
|
146 | | -void XMLWriter::writeTextElement(const NS &ns, const std::string &name, const std::map<std::string, std::string> &attr, const std::string &data) |
| 142 | +int64_t XMLWriter::writeTextElement(const NS &ns, const std::string &name, const std::map<std::string, std::string> &attr, const std::string &data) |
147 | 143 | { |
148 | | - writeStartElement(ns, name, attr); |
149 | | - if(d->w) |
150 | | - (void)xmlTextWriterWriteString(d->w, pcxmlChar(data.c_str())); |
151 | | - writeEndElement(ns); |
| 144 | + if(auto rv = writeStartElement(ns, name, attr); rv != OK) |
| 145 | + return rv; |
| 146 | + if(xmlTextWriterWriteString(d->w, pcxmlChar(data.c_str())) == -1) |
| 147 | + return IO_ERROR; |
| 148 | + return writeEndElement(ns); |
152 | 149 | } |
0 commit comments