From 51aa98675e0687964db33ae62c5ed2ba56ecf1c2 Mon Sep 17 00:00:00 2001 From: Harry Moffat Date: Fri, 2 Jan 2009 22:34:41 +0000 Subject: [PATCH] Changed one of the getString() functions to getChildValue(), to get rid of some unnecessary overloading. --- Cantera/src/base/ctml.cpp | 94 +++++++++++++++++++++------ Cantera/src/base/ctml.h | 65 +++++++++++++++++- Cantera/src/thermo/DebyeHuckel.cpp | 2 +- Cantera/src/thermo/HMWSoln_input.cpp | 2 +- Cantera/src/thermo/LatticePhase.cpp | 2 +- Cantera/src/thermo/MolalityVPSSTP.cpp | 2 +- Cantera/src/thermo/SurfPhase.cpp | 2 +- Cantera/src/thermo/ThermoPhase.cpp | 4 +- 8 files changed, 144 insertions(+), 29 deletions(-) diff --git a/Cantera/src/base/ctml.cpp b/Cantera/src/base/ctml.cpp index 015d3d38b..55a040ecc 100755 --- a/Cantera/src/base/ctml.cpp +++ b/Cantera/src/base/ctml.cpp @@ -30,7 +30,7 @@ using namespace Cantera; namespace ctml { - static doublereal fpValue(string val) { + static doublereal fpValue(std::string val) { return atof(stripws(val).c_str()); } @@ -161,26 +161,82 @@ namespace ctml { if (s->parent() == &node) return s; else return 0; } - - string getString(const XML_Node& parent, string name) { - if (!parent.hasChild(name)) return ""; - return parent(name); + + // This function reads a child node with the name string and returns + // its xml value as the return string + /* + * If the child XML_node named "name" doesn't exist, the empty string is returned. + * + * Code snipet: + * @verbatum + const XML_Node &parent; + string name = "vacency_species"; + string valueString = getChildValue(parent, name + std::string typeString); + @endverbatum + * + * returns valueString = "O(V)" + * + * from the following the snippet in the XML file: + * + * @verbatum + + O(V) + <\vancencySpecies> + @endverbatum + * + * @param parent parent reference to the XML_Node object of the parent XML element + * @param name Name of the childe XML_Node to read the value from. + * + * @return String value of the child XML_Node + */ + std::string getChildValue(const XML_Node& parent, const std::string &nameString) { + if (!parent.hasChild(nameString)) return ""; + return parent(nameString); } - void getString(XML_Node& node, string title, string& val, - string& type) { - val = ""; - type = ""; - XML_Node* s = getByTitle(node, title); - if (s) - if (s->name() == "string") { - val = (*s).value(); - type = (*s)["type"]; - return; - } - } - - void getIntegers(const XML_Node& node, map& v) { + // This function reads a child node with the name, "string", with a specific + // title attribute named "titleString" + /* + * This function will read a child node to the current XML node, with the + * name "string". It must have a title attribute, named titleString, and the body + * of the XML node will be read into the valueString output argument. + * + * Example: + * + * Code snipet: + * @verbatum + const XML_Node &node; + getString(XML_Node& node, std::string titleString, std::string valueString, + std::string typeString); + @endverbatum + * + * Reads the following the snippet in the XML file: + * @verbatum + + valueString + <\string> + @endverbatum + * + * @param node reference to the XML_Node object of the parent XML element + * @param titleString String name of the title attribute of the child node + * @param valueString Value string that is found in the child node. output variable + * @param typeString String type. This is an optional output variable + */ + void getString(XML_Node& node, const std::string &titleString, std::string& valueString, + std::string& typeString) { + valueString = ""; + typeString = ""; + XML_Node* s = getByTitle(node, titleString); + if (s) + if (s->name() == "string") { + valueString = (*s).value(); + typeString = (*s)["type"]; + return; + } + } + + void getIntegers(const XML_Node& node, map& v) { vector f; node.getChildren("integer",f); int n = static_cast(f.size()); diff --git a/Cantera/src/base/ctml.h b/Cantera/src/base/ctml.h index 8aa6c2a76..d618953c2 100755 --- a/Cantera/src/base/ctml.h +++ b/Cantera/src/base/ctml.h @@ -246,10 +246,69 @@ namespace ctml { void getFunction(const Cantera::XML_Node& node, std::string& type, doublereal& xmin, doublereal& xmax, Cantera::vector_fp& coeffs); Cantera::XML_Node* getByTitle(Cantera::XML_Node& node, std::string title); - void getString(Cantera::XML_Node& node, std::string title, - std::string& val, std::string& type); - std::string getString(const Cantera::XML_Node& parent, std::string name); + //! This function reads a child node with the name string with a specific + //! title attribute named titleString + /*! + * This function will read a child node to the current XML node, with the + * name "string". It must have a title attribute, named titleString, and the body + * of the XML node will be read into the valueString output argument. + * + * If the child node is not found then the empty string is returned. + * + * Example: + * + * Code snipet: + * @verbatum + const XML_Node &node; + getString(XML_Node& node, std::string titleString, std::string valueString, + std::string typeString); + @endverbatum + * + * Reads the following the snippet in the XML file: + * @verbatum + + valueString + <\string> + @endverbatum + * + * @param node reference to the XML_Node object of the parent XML element + * @param titleString String name of the title attribute of the child node + * @param valueString Value string that is found in the child node. output variable + * @param typeString String type. This is an optional output variable + */ + void getString(Cantera::XML_Node& node, const std::string &titleString, + std::string& valueString, std::string& typeString); + + //! This function reads a child node with the name, nameString, and returns + //! its xml value as the return string + /*! + * If the child XML_node named "name" doesn't exist, the empty string is returned. + * + * Code snipet: + * @verbatum + const XML_Node &parent; + string nameString = "vacency_species"; + string valueString = getChildValue(parent, nameString + std::string typeString); + @endverbatum + * + * returns valueString = "O(V)" + * + * from the following the snippet in the XML file: + * + * @verbatum + + O(V) + <\vancencySpecies> + @endverbatum + * + * @param parent parent reference to the XML_Node object of the parent XML element + * @param nameString Name of the childe XML_Node to read the value from. + * + * @return String value of the child XML_Node + */ + std::string getChildValue(const Cantera::XML_Node& parent, const std::string &nameString); // these are defined in ct2ctml.cpp void get_CTML_Tree(Cantera::XML_Node* node, std::string file, int debug = 0); diff --git a/Cantera/src/thermo/DebyeHuckel.cpp b/Cantera/src/thermo/DebyeHuckel.cpp index e2a050846..a4ac54c98 100644 --- a/Cantera/src/thermo/DebyeHuckel.cpp +++ b/Cantera/src/thermo/DebyeHuckel.cpp @@ -1563,7 +1563,7 @@ namespace Cantera { spPtr = xspecies[k]; if (!spPtr) { if (spPtr->hasChild("electrolyteSpeciesType")) { - std::string est = getString(*spPtr, "electrolyteSpeciesType"); + std::string est = getChildValue(*spPtr, "electrolyteSpeciesType"); if ((m_electrolyteSpeciesType[k] = interp_est(est)) == -1) { throw CanteraError("DebyeHuckel:initThermoXML", "Bad electrolyte type: " + est); diff --git a/Cantera/src/thermo/HMWSoln_input.cpp b/Cantera/src/thermo/HMWSoln_input.cpp index b50d7b6c5..2f96f6f04 100644 --- a/Cantera/src/thermo/HMWSoln_input.cpp +++ b/Cantera/src/thermo/HMWSoln_input.cpp @@ -1476,7 +1476,7 @@ namespace Cantera { spPtr = xspecies[k]; if (!spPtr) { if (spPtr->hasChild("electrolyteSpeciesType")) { - string est = getString(*spPtr, "electrolyteSpeciesType"); + string est = getChildValue(*spPtr, "electrolyteSpeciesType"); if ((m_electrolyteSpeciesType[k] = interp_est(est)) == -1) { throw CanteraError("HMWSoln::initThermoXML", "Bad electrolyte type: " + est); diff --git a/Cantera/src/thermo/LatticePhase.cpp b/Cantera/src/thermo/LatticePhase.cpp index cba39e8e8..00942edfb 100644 --- a/Cantera/src/thermo/LatticePhase.cpp +++ b/Cantera/src/thermo/LatticePhase.cpp @@ -249,7 +249,7 @@ namespace Cantera { void LatticePhase::setParametersFromXML(const XML_Node& eosdata) { eosdata._require("model","Lattice"); m_molar_density = getFloat(eosdata, "site_density", "toSI"); - m_vacancy = getString(eosdata, "vacancy_species"); + m_vacancy = getChildValue(eosdata, "vacancy_species"); } } diff --git a/Cantera/src/thermo/MolalityVPSSTP.cpp b/Cantera/src/thermo/MolalityVPSSTP.cpp index 86d3c6d37..633f158bb 100644 --- a/Cantera/src/thermo/MolalityVPSSTP.cpp +++ b/Cantera/src/thermo/MolalityVPSSTP.cpp @@ -594,7 +594,7 @@ namespace Cantera { */ void MolalityVPSSTP::setStateFromXML(const XML_Node& state) { VPStandardStateTP::setStateFromXML(state); - string comp = getString(state,"soluteMolalities"); + string comp = getChildValue(state,"soluteMolalities"); if (comp != "") { setMolalitiesByName(comp); } diff --git a/Cantera/src/thermo/SurfPhase.cpp b/Cantera/src/thermo/SurfPhase.cpp index de3b9e0c2..6585d2f42 100644 --- a/Cantera/src/thermo/SurfPhase.cpp +++ b/Cantera/src/thermo/SurfPhase.cpp @@ -475,7 +475,7 @@ namespace Cantera { } if (state.hasChild("coverages")) { - string comp = getString(state,"coverages"); + string comp = getChildValue(state,"coverages"); setCoveragesByName(comp); } } diff --git a/Cantera/src/thermo/ThermoPhase.cpp b/Cantera/src/thermo/ThermoPhase.cpp index 61a04230b..ef8bc00ca 100644 --- a/Cantera/src/thermo/ThermoPhase.cpp +++ b/Cantera/src/thermo/ThermoPhase.cpp @@ -904,11 +904,11 @@ namespace Cantera { */ void ThermoPhase::setStateFromXML(const XML_Node& state) { - string comp = getString(state,"moleFractions"); + string comp = getChildValue(state,"moleFractions"); if (comp != "") setMoleFractionsByName(comp); else { - comp = getString(state,"massFractions"); + comp = getChildValue(state,"massFractions"); if (comp != "") setMassFractionsByName(comp); }