Skip to content

Commit

Permalink
rearranging methods to aid src reading
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanasisMattas committed May 3, 2019
1 parent a97831e commit 27eefaa
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 84 deletions.
24 changes: 12 additions & 12 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 @@ -310,13 +317,6 @@ class XML_Node
*/
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
144 changes: 72 additions & 72 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,19 +1035,6 @@ 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& phaseId)
{
Expand Down

0 comments on commit 27eefaa

Please sign in to comment.