27 return source ? source->text(
"name").value_or(std::string_view {}) : std::string_view {};
33 for (
size_t index =
m_row + 1; index < end; ++index) {
34 const auto source =
m_rows->at(index);
35 if (!source || source->text(
"kind") != std::string_view(
"attribute"))
37 if (source->text(
"name") ==
name)
38 return source->text(
"value");
45 constexpr uintmax_t max_xml_file_bytes =
static_cast<const uintmax_t
>(64 * 1024 * 1024);
46 constexpr size_t max_xml_nodes = 1'000'000;
47 constexpr size_t max_xml_depth = 256;
49 std::string extension_of(
const std::string& filepath)
51 auto extension = std::filesystem::path(filepath).extension().string();
52 std::ranges::transform(extension, extension.begin(),
53 [](
unsigned char c) { return static_cast<char>(std::tolower(c)); });
58 std::string_view node_kind(pugi::xml_node_type type)
61 case pugi::node_element:
63 case pugi::node_pcdata:
65 case pugi::node_cdata:
67 case pugi::node_comment:
71 case pugi::node_declaration:
73 case pugi::node_doctype:
82 std::optional<uint64_t> parent;
86 void push_children(std::vector<PendingNode>& pending,
87 const pugi::xml_node& node, std::optional<uint64_t> parent,
size_t depth)
89 for (
auto child = node.last_child(); child; child = child.previous_sibling())
90 pending.push_back({ .node = child, .parent = parent, .depth = depth });
97 return extension_of(filepath) ==
".xml";
111 std::error_code error;
112 const auto file_size = std::filesystem::file_size(
m_filepath, error);
118 if (file_size > max_xml_file_bytes) {
119 m_last_error =
"XML file exceeds the 64 MiB input limit";
123 pugi::xml_document document;
124 const auto parsed = document.load_file(
m_filepath.c_str(),
125 pugi::parse_full | pugi::parse_ws_pcdata);
128 + std::to_string(parsed.offset) +
": " + parsed.description();
132 if (!document.document_element()) {
139 std::vector<PendingNode> pending;
140 push_children(pending, document, std::nullopt, 0);
142 while (!pending.empty()) {
143 auto entry = pending.back();
146 if (!entry.parent && entry.node.type() == pugi::node_pcdata) {
147 const std::string_view value(entry.node.value());
149 if (std::ranges::all_of(value, [](
unsigned char c) {
150 return std::isspace(c) != 0;
155 if (entry.depth > max_xml_depth || rows.
size() >= max_xml_nodes) {
156 m_last_error =
"XML document exceeds the node or depth limit";
160 const auto kind = node_kind(entry.node.type());
166 const auto row = insertion.
append();
167 if (!insertion.
set_text(row,
"kind", kind)
168 || (entry.parent && !insertion.
set(row,
"parent", *entry.parent))) {
173 const std::string_view name(entry.node.name());
174 const std::string_view value(entry.node.value());
175 const bool has_value = entry.node.type() == pugi::node_pcdata
176 || entry.node.type() == pugi::node_cdata
177 || entry.node.type() == pugi::node_comment
178 || entry.node.type() == pugi::node_pi
179 || entry.node.type() == pugi::node_doctype;
181 if ((!name.empty() && !insertion.
set_text(row,
"name", name))
182 || (has_value && !insertion.
set_text(row,
"value", value))) {
187 for (
auto attribute : entry.node.attributes()) {
188 if (rows.
size() >= max_xml_nodes) {
193 const auto attribute_row = insertion.
append();
194 if (!insertion.
set(attribute_row,
"parent",
static_cast<uint64_t
>(row))
195 || !insertion.
set_text(attribute_row,
"kind",
"attribute")
196 || !insertion.
set_text(attribute_row,
"name", attribute.name())
197 || !insertion.
set_text(attribute_row,
"value", attribute.value())) {
203 push_children(pending, entry.node,
static_cast<uint64_t
>(row), entry.depth + 1);
206 std::string forest_error;
208 if (!forest || !forest->preorder()) {
209 m_last_error =
"Could not index the XML node hierarchy: " + forest_error;
214 m_rows.emplace(std::move(rows));
230 std::optional<Kakshya::CompositeArray> rows = std::move(
m_rows);
244 std::error_code error;
254 return read_next(std::numeric_limits<size_t>::max());
257std::optional<Kakshya::CompositeArray>
267 const auto count = std::min(max_elements,
270 for (
size_t i = 0; i < count; ++i) {
272 if (!source || !insertion.
append(*source)) {
296 for (
size_t index = 0; index <
m_rows->size(); index +=
m_forest->subtree_size(index)) {
297 const auto source =
m_rows->at(index);
298 if (source && source->text(
"kind") == std::string_view(
"element"))
309 const auto source =
m_rows->at(
static_cast<size_t>(row));
310 if (!source || source->text(
"kind") != std::string_view(
"element"))
318 if (!
m_rows || position.size() != 1 || position[0] >
m_rows->size()) {
319 m_last_error =
"Seek requires a valid XML node position";
330 return m_rows ? std::vector<uint64_t> {
m_rows->size() }
331 : std::vector<uint64_t> {};
static std::string resolve_path(const std::string &filepath)
Resolve a filepath against the project source root if not found as-is.
const Kakshya::CompositeArray * m_rows
std::string_view name() const noexcept
const Kakshya::ForestIndex * m_forest
std::optional< std::string_view > attribute(std::string_view name) const noexcept
Borrowed XML element with named attributes and immediate child elements.
std::vector< uint64_t > get_dimension_sizes() const override
Total number of rows while open.
std::optional< Kakshya::CompositeArray > take_rows()
Move the parsed rows out and close the reader.
std::optional< XmlElementView > root_element() const noexcept
Borrow the document root element, independent of the row read position.
std::optional< FileMetadata > get_metadata() const override
XML format, MIME type, and source file size.
bool seek(const std::vector< uint64_t > &position) override
Seek to one row index, including the end position.
bool can_read(const std::string &filepath) const override
Accept paths with the .xml extension.
std::optional< Kakshya::CompositeArray > read_next(size_t max_elements) override
Copy at most max_elements rows and advance the position.
std::optional< XmlElementView > element_at(uint64_t row) const noexcept
Borrow an element by its global XML row index.
bool open(const std::string &filepath, FileReadOptions options=FileReadOptions::ALL) override
Parse an XML file and establish the fixed node layout.
void close() override
Release parsed rows and reset the read position.
std::optional< Kakshya::CompositeArray > m_rows
std::optional< Kakshya::ForestIndex > m_forest
std::optional< Kakshya::CompositeArray > read_composite() override
Copy all rows remaining after the current position.
std::optional< Kakshya::CompositeLayout > get_layout() const override
Copy the finalized node layout while open.
size_t size() const noexcept
Number of packed elements.
Owns a sequence of values that share a CompositeLayout.
bool set(size_t index, std::string_view field_name, T value)
Write an arithmetic or GLM field of an existing element.
bool set_text(size_t index, std::string_view field_name, std::string_view value)
Write UTF-8 bytes to a std::string field.
size_t append()
Append an empty element.
Mutable insertion adapter for a CompositeArray.
bool add_field(std::string name)
Declare a named arithmetic, GLM, text, or binary field.
Shared field description and byte layout for a CompositeArray.
FileReadOptions
Generic options for file reading behavior.
Kakshya::CompositeLayout xml_composite_layout()
Composite row layout used by the XML reader and writer.
std::optional< ForestIndex > index_composite_forest(const CompositeArray &rows, std::string_view parent_field, std::string &error, uint64_t *error_row)
Index the parent links of a Composite array.