| 1 |
#ifndef XMLOUTPUTSTREAM_HH |
| 2 |
#define XMLOUTPUTSTREAM_HH |
| 3 |
|
| 4 |
#include "XMLEscape.hh" |
| 5 |
|
| 6 |
#include "ranges.hh" |
| 7 |
|
| 8 |
#include <algorithm> |
| 9 |
#include <array> |
| 10 |
#include <concepts> |
| 11 |
#include <cstdint> |
| 12 |
#include <string> |
| 13 |
#include <string_view> |
| 14 |
#include <vector> |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
template<typename Operations> |
| 76 |
class XMLOutputStream |
| 77 |
{ |
| 78 |
public: |
| 79 |
explicit XMLOutputStream(Operations& ops_) |
| 80 |
: ops(ops_) {} |
| 81 |
|
| 82 |
void begin(std::string_view tag); |
| 83 |
void attribute(std::string_view name, std::string_view value); |
| 84 |
void data(std::string_view value); |
| 85 |
void dataRaw(std::string_view value); |
| 86 |
void end(std::string_view tag); |
| 87 |
|
| 88 |
void with_tag(std::string_view tag, std::invocable auto next); |
| 89 |
|
| 90 |
private: |
| 91 |
void writeSpaces(unsigned n); |
| 92 |
void writeChar(char c); |
| 93 |
void writeString(std::string_view s); |
| 94 |
void writeEscapedString(std::string_view s); |
| 95 |
|
| 96 |
private: |
| 97 |
Operations& ops; |
| 98 |
unsigned level = 0; |
| 99 |
enum State : uint8_t { |
| 100 |
INDENT, |
| 101 |
CLOSE, |
| 102 |
DATA, |
| 103 |
}; |
| 104 |
State state = INDENT; |
| 105 |
#ifdef DEBUG |
| 106 |
std::vector<std::string> stack; |
| 107 |
#endif |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
template<typename Writer> |
| 112 |
void XMLOutputStream<Writer>::begin(std::string_view tag) |
| 113 |
{ |
| 114 |
#ifdef DEBUG |
| 115 |
stack.emplace_back(tag); |
| 116 |
#endif |
| 117 |
ops.check(state != DATA); |
| 118 |
if (state == CLOSE) { |
| 119 |
writeString(">\n"); |
| 120 |
} |
| 121 |
writeSpaces(2 * level); |
| 122 |
writeChar('<'); |
| 123 |
writeString(tag); |
| 124 |
++level; |
| 125 |
state = CLOSE; |
| 126 |
} |
| 127 |
|
| 128 |
template<typename Writer> |
| 129 |
void XMLOutputStream<Writer>::attribute(std::string_view name, std::string_view value) |
| 130 |
{ |
| 131 |
ops.check(level > 0); |
| 132 |
ops.check(state == CLOSE); |
| 133 |
writeChar(' '); |
| 134 |
writeString(name); |
| 135 |
writeString("=\""); |
| 136 |
writeEscapedString(value); |
| 137 |
writeChar('"'); |
| 138 |
} |
| 139 |
|
| 140 |
template<typename Writer> |
| 141 |
void XMLOutputStream<Writer>::data(std::string_view value) |
| 142 |
{ |
| 143 |
ops.check(level > 0); |
| 144 |
ops.check(state == CLOSE); |
| 145 |
|
| 146 |
if (value.empty()) return; |
| 147 |
|
| 148 |
writeChar('>'); |
| 149 |
writeEscapedString(value); |
| 150 |
state = DATA; |
| 151 |
} |
| 152 |
|
| 153 |
template<typename Writer> |
| 154 |
void XMLOutputStream<Writer>::dataRaw(std::string_view value) |
| 155 |
{ |
| 156 |
ops.check(level > 0); |
| 157 |
ops.check(state == CLOSE); |
| 158 |
|
| 159 |
if (value.empty()) return; |
| 160 |
|
| 161 |
writeChar('>'); |
| 162 |
writeString(value); |
| 163 |
state = DATA; |
| 164 |
} |
| 165 |
|
| 166 |
template<typename Writer> |
| 167 |
void XMLOutputStream<Writer>::end(std::string_view tag) |
| 168 |
{ |
| 169 |
#ifdef DEBUG |
| 170 |
ops.check(stack.size() == level); |
| 171 |
ops.check(!stack.empty()); |
| 172 |
ops.check(stack.back() == tag); |
| 173 |
stack.pop_back(); |
| 174 |
#endif |
| 175 |
ops.check(level > 0); |
| 176 |
--level; |
| 177 |
if (state == CLOSE) { |
| 178 |
writeString("/>\n"); |
| 179 |
} else { |
| 180 |
if (state == INDENT) { |
| 181 |
writeSpaces(2 * level); |
| 182 |
} |
| 183 |
writeString("</"); |
| 184 |
writeString(tag); |
| 185 |
writeString(">\n"); |
| 186 |
} |
| 187 |
state = INDENT; |
| 188 |
} |
| 189 |
|
| 190 |
template<typename Writer> |
| 191 |
void XMLOutputStream<Writer>::with_tag(std::string_view tag, std::invocable auto next) |
| 192 |
{ |
| 193 |
begin(tag); |
| 194 |
next(); |
| 195 |
end(tag); |
| 196 |
} |
| 197 |
|
| 198 |
template<typename Writer> |
| 199 |
void XMLOutputStream<Writer>::writeSpaces(unsigned n) |
| 200 |
{ |
| 201 |
static constexpr std::array<char, 64> spaces = { |
| 202 |
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', |
| 203 |
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', |
| 204 |
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', |
| 205 |
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', |
| 206 |
}; |
| 207 |
while (n) { |
| 208 |
auto t = std::min(64u, n); |
| 209 |
ops.write(subspan(spaces, 0, t)); |
| 210 |
n -= t; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
template<typename Writer> |
| 215 |
void XMLOutputStream<Writer>::writeChar(char c) |
| 216 |
{ |
| 217 |
ops.write1(c); |
| 218 |
} |
| 219 |
|
| 220 |
template<typename Writer> |
| 221 |
void XMLOutputStream<Writer>::writeString(std::string_view s) |
| 222 |
{ |
| 223 |
ops.write(s); |
| 224 |
} |
| 225 |
|
| 226 |
template<typename Writer> |
| 227 |
void XMLOutputStream<Writer>::writeEscapedString(std::string_view s) |
| 228 |
{ |
| 229 |
XMLEscape(s, [&](std::string_view chunk) { writeString(chunk); }); |
| 230 |
} |
| 231 |
|
| 232 |
#endif |