17static const char*
const SPACE =
" \t\r\n";
37 const std::string& name,
38 const std::string& value)
65 child->set_parent (
this);
135 throw ExcXMLCore (
"Out-of-range attribute index " + std::to_string(i) +
" in " +
m_name);
139 std::advance (it, i);
164 std::optional<T> ret;
169 std::string::size_type beg = it->second.find_first_not_of (
SPACE);
170 std::string::size_type end = it->second.find_last_not_of (
SPACE);
171 if (beg == std::string::npos || end == std::string::npos)
return ret;
172 const char* begp = it->second.c_str()+beg;
173 const char* endp = it->second.c_str()+end+1;
174 auto [ptr, ec] = std::from_chars (begp, endp, val);
175 if (ec == std::errc() && ptr == endp)
268 std::vector<const XMLCoreNode*> children;
270 if (!children.empty()) {
286std::vector<const XMLCoreNode*>
289 std::vector<const XMLCoreNode*> children;
314 const std::string&
header,
318 os <<
header << std::endl;
322 bool allspace =
m_value.find_last_of(
SPACE) == std::string::npos;
328 for (
int i = 0; i <
depth; i++) os <<
" ";
331 os <<
"<!--" <<
m_value <<
"-->" << std::endl;
338 os <<
" " << p.first <<
"='" << p.second <<
"'";
341 os <<
">" << std::endl;
344 child->print (os,
"",
depth+1);
347 for (
int i = 0; i <
depth; i++) os <<
" ";
348 os <<
"</" <<
m_name <<
">" << std::endl;
372 std::vector<const XMLCoreNode*>& children,
375 std::string::size_type pos = path.find (
'/');
376 std::string name = path.substr (0, pos);
378 if (pos != std::string::npos) {
379 tail = path.substr (pos+1);
383 if (name ==
"*" || child->get_name() == name) {
385 children.push_back (child.get());
388 child->collect_children (
tail, children, only_one);
391 if (only_one && !children.empty())
break;
static const char *const SPACE
Simple DOM-like node structure to hold the result of XML parsing.
Class for exceptions thrown from XMLCoreParser.
ExcXMLCore(const std::string &what)
Constructor.
std::string m_value
The value fo this node.
NodeType
Classify node types.
double get_double_attrib(const std::string &name) const
Retrieve the value of an attribute as a double.
std::string m_name
The name of this node.
const std::string & get_attrib(const std::string &name) const
Retrieve the value of an attribute.
XMLCoreNode * m_parent
The parent of this node, or null for a top-level node.
NodeType m_type
The type of this node.
XMLCoreNode(NodeType type, const std::string &name="", const std::string &value="")
Constructor.
std::string get_attrib_name(size_t i) const
Return the name of the i'th attribute.
bool has_attrib(const std::string &name) const
Test for presence of an attribute with a given name.
std::vector< const XMLCoreNode * > get_children(const std::string &path="*") const
Return all children matching a pattern.
void print(const std::string &header, int depth=0) const
XMLCoreNode * add_child(std::unique_ptr< XMLCoreNode > child)
Add a new child to this node.
void set_parent(XMLCoreNode *parent)
Set the parent for this node.
NodeType get_type() const
Return the type of this node.
std::optional< int > try_int_attrib(const std::string &name) const
Try to retrieve an integer attribute.
size_t n_attribs() const
Return the number of attributes for this node.
const std::string & get_value() const
Return the value of this node, or an empty string if no value.
XMLCoreNode * get_parent()
Get the parent of this node, or nullptr for a top-level node.
void set_attrib(const std::string &name, const std::string &value)
Set the value of an attribute for this node.
std::optional< double > try_double_attrib(const std::string &name) const
Try to retrieve an double attribute.
std::optional< T > try_attrib(const std::string &name) const
Try to retrieve an attribute of type T.
void collect_children(const std::string &path, std::vector< const XMLCoreNode * > &children, bool only_one) const
Collect children of this node matching a path.
const std::string & get_name() const
Return the name of this node, or an empty string if no name.
int get_int_attrib(const std::string &name) const
Retrieve the value of an attribute as an integer.
std::map< std::string, std::string > m_attribs
Attributes of this node.
std::vector< std::unique_ptr< XMLCoreNode > > m_children
Children of this node.
const XMLCoreNode * get_child(const std::string &path) const
Return the first child matching a pattern, or nullptr.
std::string tail(std::string s, const std::string &pattern)
tail of a string
std::string depth
tag string for intendation