14 std::vector<pugi::xml_node>
nodes;
19 std::string extension_of(
const std::string& filepath)
21 auto extension = std::filesystem::path(filepath).extension().string();
22 std::ranges::transform(extension, extension.begin(),
23 [](
unsigned char c) { return static_cast<char>(std::tolower(c)); });
27 bool valid_name(std::string_view name)
32 const auto first =
static_cast<unsigned char>(name.front());
33 const bool valid_first = std::isalpha(first) || first ==
'_'
34 || first ==
':' || first >= 0x80;
36 return valid_first && std::ranges::all_of(name, [](
unsigned char c) {
37 return std::isalnum(c) || c ==
'_' || c ==
':'
38 || c ==
'-' || c ==
'.' || c >= 0x80;
42 bool valid_xml_value(std::string_view value)
44 return std::ranges::all_of(value, [](
unsigned char c) {
45 return c >= 0x20 || c ==
'\t' || c ==
'\n' || c ==
'\r';
49 std::optional<pugi::xml_node_type> type_of(std::string_view kind)
51 if (kind ==
"element")
52 return pugi::node_element;
55 return pugi::node_pcdata;
58 return pugi::node_cdata;
60 if (kind ==
"comment")
61 return pugi::node_comment;
66 if (kind ==
"declaration")
67 return pugi::node_declaration;
69 if (kind ==
"doctype")
70 return pugi::node_doctype;
87 return extension_of(filepath) ==
".xml";
94 m_last_error =
"Close the current XML document before opening another";
105 m_last_error =
"XML output requires xml_composite_layout()";
109 m_xml = std::make_unique<Impl>();
114 std::ofstream output(
m_filepath, std::ios::binary | std::ios::trunc);
146 m_last_error =
"Composite slice layout differs from the XML layout";
151 for (
size_t i = 0; i < rows.
size(); ++i) {
152 const auto row = rows.
at(i);
168 const auto kind = row.
text(
"kind");
169 const auto name = row.
text(
"name");
170 const auto value = row.
text(
"value");
171 const auto parent = row.
get<uint64_t>(
"parent");
172 const auto index =
static_cast<uint64_t
>(
m_xml->nodes.size());
179 if (*kind ==
"attribute") {
180 if (!parent || *parent >= index
181 || !name || !valid_name(*name)
182 || !value || !valid_xml_value(*value)
186 m_last_error =
"XML attribute requires an open parent and must precede its content";
190 auto owner =
m_xml->nodes[*parent];
191 if (owner.type() != pugi::node_element
192 && owner.type() != pugi::node_declaration) {
193 m_last_error =
"XML attribute parent must be an element or declaration";
197 if (owner.attribute(std::string(*name).c_str())) {
198 m_last_error =
"XML attribute names must be unique within an element";
202 auto attribute = owner.append_attribute(std::string(*name).c_str());
203 const std::string output_value =
204 owner.type() == pugi::node_declaration && *name ==
"encoding"
206 : std::string(*value);
208 if (!attribute || !attribute.set_value(output_value.c_str())) {
213 m_xml->nodes.emplace_back();
219 const auto type = type_of(*kind);
225 pugi::xml_node owner =
m_xml->document.root();
227 if (*parent >= index) {
228 m_last_error =
"XML parent index must precede its child";
235 if (
m_ancestors.empty() ||
m_xml->nodes[*parent].type() != pugi::node_element) {
236 m_last_error =
"XML parent must be an open ancestor element";
240 owner =
m_xml->nodes[*parent];
243 if (*type == pugi::node_declaration || *type == pugi::node_doctype) {
244 m_last_error =
"XML declaration and doctype must be top-level";
249 if (*type == pugi::node_element) {
251 m_last_error =
"XML document may contain only one root element";
256 }
else if (*type == pugi::node_declaration) {
258 m_last_error =
"XML declaration must be the first document node";
263 }
else if (*type == pugi::node_doctype) {
265 m_last_error =
"XML doctype must occur once before the root";
270 }
else if (*type != pugi::node_comment && *type != pugi::node_pi) {
271 m_last_error =
"XML text and CDATA require a parent element";
276 const bool needs_name = *type == pugi::node_element
277 || *type == pugi::node_pi || *type == pugi::node_declaration;
279 const bool needs_value = *type == pugi::node_pcdata
280 || *type == pugi::node_cdata || *type == pugi::node_comment
281 || *type == pugi::node_pi || *type == pugi::node_doctype;
283 if ((needs_name && (!name || !valid_name(*name)))
284 || (*type == pugi::node_declaration && (!name || *name !=
"xml"))
285 || (*type == pugi::node_pi && name && *name ==
"xml")
286 || (!needs_name && name)
287 || (needs_value && (!value || !valid_xml_value(*value)))
288 || (!needs_value && value)
289 || (*type == pugi::node_comment
290 && (value->find(
"--") != std::string_view::npos || value->ends_with(
'-')))
291 || (*type == pugi::node_cdata && value->find(
"]]>") != std::string_view::npos)
292 || (*type == pugi::node_pi && value->find(
"?>") != std::string_view::npos)) {
293 m_last_error =
"XML row has invalid name or value fields";
297 auto node = owner.append_child(*type);
298 if (!node || (name && !node.set_name(std::string(*name).c_str()))
299 || (value && !node.set_value(std::string(*value).c_str()))) {
304 m_xml->nodes.push_back(node);
307 if (*type == pugi::node_element || *type == pugi::node_declaration)
324 m_last_error =
"XML document must contain one root element before saving";
329 pugi::format_raw | pugi::format_no_declaration, pugi::encoding_utf8);
std::vector< bool > m_content_started
bool close() override
Save and release the document, or return false after failure.
std::unique_ptr< Impl > m_xml
bool can_write(const std::string &filepath) const override
Accept paths with the .xml extension.
bool write_rows(const Kakshya::CompositeSlice &rows) override
Append preorder rows whose parent indices are global.
bool open(const std::string &filepath, const Kakshya::CompositeLayout &layout) override
Start a document using xml_composite_layout().
std::vector< uint64_t > m_ancestors
bool flush() override
Save the current document, which must contain one root.
bool write_row(const Kakshya::Composite &row)
bool same_structure(const CompositeLayout &other) const noexcept
Compare declared field names, kinds, and nested layouts.
Shared field description and byte layout for a CompositeArray.
std::optional< Composite > at(size_t index) const noexcept
Borrow a selected element by slice-relative index.
size_t size() const noexcept
Number of selected elements.
const CompositeLayout & layout() const noexcept
Shared layout of all selected elements.
Borrowed consecutive elements, optionally selected by a Region.
std::optional< std::string_view > text(std::string_view field_name) const noexcept
Read a present std::string field as borrowed UTF-8 bytes.
std::optional< T > get(std::string_view field_name) const noexcept
Read a present arithmetic or GLM field by exact value type.
Read view of one value in a CompositeArray.
std::string resolve_write_path(const std::string &filepath, std::string_view default_directory=Config::OUT_DIR)
Resolve a writable path against the configured output directory.
Kakshya::CompositeLayout xml_composite_layout()
Composite row layout used by the XML reader and writer.
pugi::xml_document document
std::vector< pugi::xml_node > nodes