Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some documentation corrections at xml.h #636

Merged
merged 2 commits into from
Jun 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 25 additions & 32 deletions include/cantera/base/xml.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,12 @@ class XML_Node
XML_Node& operator=(const XML_Node& right);
virtual ~XML_Node();

//! Add a child node to the current node containing a comment
//! Clear the current node and everything under it
/*!
* Child node will have the name, comment.
*
* @param comment Content of the comment
* The value, attributes and children are all zeroed. The name and the
* parent information is kept.
*/
void addComment(const std::string& comment);
void clear();

//! Merge an existing node as a child node to the current node
/*!
Expand Down Expand Up @@ -190,6 +189,14 @@ class XML_Node
* isn't modified in any way.
*/
void removeChild(const XML_Node* const node);

//! Add a child node to the current node containing a comment
/*!
* Child node will have the name, comment.
*
* @param comment Content of the comment
*/
void addComment(const std::string& comment);

//! Modify the value for the current node
/*!
Expand Down Expand Up @@ -306,18 +313,10 @@ class XML_Node
*
* @param attr String containing the attribute to be searched for.
* @return If a match is found, the attribute value is returned as a
* string. If no match is found, the empty string is
* returned.
* string. If no match is found, the empty string is returned.
*/
std::string attrib(const std::string& attr) const;

//! Clear the current node and everything under it
/*!
* The value, attributes and children are all zeroed. The name and the
* parent information is kept.
*/
void clear();

private:
//! Returns a changeable value of the attributes map for the current node
/*!
Expand Down Expand Up @@ -364,9 +363,6 @@ class XML_Node
bool hasAttrib(const std::string& a) const;

//! Returns the name of the XML node
/*!
* The name is the XML node is the XML node name
*/
std::string name() const {
return m_name;
}
Expand All @@ -393,7 +389,7 @@ class XML_Node

//! Return an unchangeable reference to the vector of children of the current node
/*!
* Each of the individual XML_Node child pointers, however, is to a
* Each of the individual XML_Node child pointers, however, is pointing to a
* changeable XML node object.
*/
const std::vector<XML_Node*>& children() const;
Expand All @@ -408,9 +404,9 @@ class XML_Node
//! Boolean function indicating whether a comment
bool isComment() const;

//! Require that the current XML node have an attribute named by the first
//! argument, a, and that this attribute have the the string value listed
//! in the second argument, v.
//! Require that the current XML node has an attribute named by the first
//! argument, a, and that this attribute has the string value listed in
//! the second argument, v.
/*!
* @param a attribute name
* @param v required value of the attribute
Expand Down Expand Up @@ -440,15 +436,15 @@ class XML_Node
XML_Node* findNameID(const std::string& nameTarget,
const std::string& idTarget) const;

//! This routine carries out a search for an XML node based
//! on both the XML element name and the attribute ID and an integer index.
//! This routine carries out a search for an XML node based on the XML
//! element name, the attribute ID and an integer index.
/*!
* If exact matches are found for all fields, the pointer
* to the matching XML Node is returned. The search is only carried out on
* the current element and the child elements of the current element.
*
* The "id" attribute may be defaulted by setting it to "". In this case the
* pointer to the first XML element matching the name only is returned.
* pointer to the first XML element matching the name and the Index is returned.
*
* @param nameTarget Name of the XML Node that is being searched for
* @param idTarget "id" attribute of the XML Node that the routine
Expand All @@ -461,14 +457,11 @@ class XML_Node
const std::string& idTarget, const int index) const;

//! This routine carries out a recursive search for an XML node based
//! on the XML element attribute, "id"
//! on the XML element attribute "id"
/*!
* If exact match is found, the pointer to the matching XML Node is
* returned. If not, 0 is returned.
*
* The ID attribute may be defaulted by setting it to "". In this case the
* pointer to the first XML element matching the name only is returned.
*
* @param id "id" attribute of the XML Node that the routine looks for
* @param depth Depth of the search.
* @returns the pointer to the XML node that fits the criteria
Expand Down Expand Up @@ -704,14 +697,14 @@ class XML_Node

//! Search an XML_Node tree for a named phase XML_Node
/*!
* Search for a phase Node matching a name.
* Search for a phase Node matching an id.
*
* @param root Starting XML_Node* pointer for the search
* @param phaseName Name of the phase to search for
* @param phaseId id of the phase to search for
* @returns the XML_Node pointer if the phase is found. If the phase is not
* found, it returns 0
* found, it returns 0
*/
XML_Node* findXMLPhase(XML_Node* root, const std::string& phaseName);
XML_Node* findXMLPhase(XML_Node* root, const std::string& phaseId);

}

Expand Down
156 changes: 78 additions & 78 deletions src/base/xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,6 @@ void XML_Node::clear()
m_linenum = 0;
}

void XML_Node::addComment(const std::string& comment)
{
addChild("comment", comment);
}

XML_Node& XML_Node::mergeAsChild(XML_Node& node)
{
m_children.push_back(&node);
Expand Down Expand Up @@ -425,12 +420,9 @@ void XML_Node::removeChild(const XML_Node* const node)
m_childindex.erase(node->name());
}

std::string XML_Node::id() const
void XML_Node::addComment(const std::string& comment)
{
if (hasAttrib("id")) {
return attrib("id");
}
return "";
addChild("comment", comment);
}

void XML_Node::addValue(const std::string& val)
Expand All @@ -451,24 +443,24 @@ std::string XML_Node::value() const
return m_value;
}

doublereal XML_Node::fp_value() const
std::string XML_Node::value(const std::string& cname) const
{
return fpValueCheck(m_value);
return child(cname).value();
}

integer XML_Node::int_value() const
std::string XML_Node::operator()(const std::string& cname) const
{
return std::atoi(m_value.c_str());
return value(cname);
}

std::string XML_Node::value(const std::string& cname) const
doublereal XML_Node::fp_value() const
{
return child(cname).value();
return fpValueCheck(m_value);
}

std::string XML_Node::operator()(const std::string& loc) const
integer XML_Node::int_value() const
{
return value(loc);
return std::atoi(m_value.c_str());
}

void XML_Node::addAttribute(const std::string& attrib, const std::string& value)
Expand Down Expand Up @@ -543,6 +535,14 @@ bool XML_Node::hasAttrib(const std::string& a) const
return (m_attribs.find(a) != m_attribs.end());
}

std::string XML_Node::id() const
{
if (hasAttrib("id")) {
return attrib("id");
}
return "";
}

XML_Node& XML_Node::child(const size_t n) const
{
return *m_children[n];
Expand Down Expand Up @@ -676,10 +676,10 @@ XML_Node* XML_Node::findByAttr(const std::string& attr,
return 0;
}

XML_Node* XML_Node::findByName(const std::string& nm, int depth)
const XML_Node* XML_Node::findByName(const std::string& nm, int depth) const
{
if (name() == nm) {
return this;
return const_cast<XML_Node*>(this);
}
if (depth > 0) {
for (size_t i = 0; i < nChildren(); i++) {
Expand All @@ -692,10 +692,10 @@ XML_Node* XML_Node::findByName(const std::string& nm, int depth)
return 0;
}

const XML_Node* XML_Node::findByName(const std::string& nm, int depth) const
XML_Node* XML_Node::findByName(const std::string& nm, int depth)
{
if (name() == nm) {
return const_cast<XML_Node*>(this);
return this;
}
if (depth > 0) {
for (size_t i = 0; i < nChildren(); i++) {
Expand All @@ -708,9 +708,55 @@ const XML_Node* XML_Node::findByName(const std::string& nm, int depth) const
return 0;
}

void XML_Node::writeHeader(std::ostream& s)
std::vector<XML_Node*> XML_Node::getChildren(const std::string& nm) const
{
s << "<?xml version=\"1.0\"?>" << endl;
std::vector<XML_Node*> children_;
for (size_t i = 0; i < nChildren(); i++) {
if (caseInsensitiveEquals(child(i).name(), nm)) {
children_.push_back(&child(i));
}
}
return children_;
}

XML_Node& XML_Node::child(const std::string& aloc) const
{
string loc = aloc;
while (true) {
size_t iloc = loc.find('/');
if (iloc != string::npos) {
string cname = loc.substr(0,iloc);
loc = loc.substr(iloc+1, loc.size());
auto i = m_childindex.find(cname);
if (i != m_childindex.end()) {
return i->second->child(loc);
} else {
throw XML_NoChild(this, m_name, cname, root().m_filename,
lineNumber());
}
} else {
auto i = m_childindex.find(loc);
if (i != m_childindex.end()) {
return *(i->second);
} else {
throw XML_NoChild(this, m_name, loc, root().m_filename,
lineNumber());
}
}
}
}

XML_Node& XML_Node::root() const
{
return *m_root;
}

void XML_Node::setRoot(const XML_Node& newRoot)
{
m_root = const_cast<XML_Node*>(&newRoot);
for (size_t i = 0; i < m_children.size(); i++) {
m_children[i]->setRoot(newRoot);
}
}

void XML_Node::build(const std::string& filename)
Expand Down Expand Up @@ -861,42 +907,9 @@ void XML_Node::unlock()
}
}

std::vector<XML_Node*> XML_Node::getChildren(const std::string& nm) const
{
std::vector<XML_Node*> children_;
for (size_t i = 0; i < nChildren(); i++) {
if (caseInsensitiveEquals(child(i).name(), nm)) {
children_.push_back(&child(i));
}
}
return children_;
}

XML_Node& XML_Node::child(const std::string& aloc) const
void XML_Node::writeHeader(std::ostream& s)
{
string loc = aloc;
while (true) {
size_t iloc = loc.find('/');
if (iloc != string::npos) {
string cname = loc.substr(0,iloc);
loc = loc.substr(iloc+1, loc.size());
auto i = m_childindex.find(cname);
if (i != m_childindex.end()) {
return i->second->child(loc);
} else {
throw XML_NoChild(this, m_name, cname, root().m_filename,
lineNumber());
}
} else {
auto i = m_childindex.find(loc);
if (i != m_childindex.end()) {
return *(i->second);
} else {
throw XML_NoChild(this, m_name, loc, root().m_filename,
lineNumber());
}
}
}
s << "<?xml version=\"1.0\"?>" << endl;
}

void XML_Node::write_int(std::ostream& s, int level, int numRecursivesAllowed) const
Expand Down Expand Up @@ -1022,31 +1035,18 @@ void XML_Node::write(std::ostream& s, const int level, int numRecursivesAllowed)
s << endl;
}

XML_Node& XML_Node::root() const
{
return *m_root;
}

void XML_Node::setRoot(const XML_Node& newRoot)
{
m_root = const_cast<XML_Node*>(&newRoot);
for (size_t i = 0; i < m_children.size(); i++) {
m_children[i]->setRoot(newRoot);
}
}

XML_Node* findXMLPhase(XML_Node* root,
const std::string& idtarget)
const std::string& phaseId)
{
XML_Node* scResult = 0;
if (!root) {
return 0;
}
if (root->name() == "phase") {
if (idtarget == "") {
if (phaseId == "") {
return root;
}
if (idtarget == root->id()) {
if (phaseId == root->id()) {
return root;
}
}
Expand All @@ -1055,17 +1055,17 @@ XML_Node* findXMLPhase(XML_Node* root,
for (size_t n = 0; n < root->nChildren(); n++) {
XML_Node* sc = vsc[n];
if (sc->name() == "phase") {
if (idtarget == "") {
if (phaseId == "") {
return sc;
}
if (idtarget == sc->id()) {
if (phaseId == sc->id()) {
return sc;
}
}
}
for (size_t n = 0; n < root->nChildren(); n++) {
XML_Node* sc = vsc[n];
scResult = findXMLPhase(sc, idtarget);
scResult = findXMLPhase(sc, phaseId);
if (scResult) {
return scResult;
}
Expand Down