diff --git a/include/cantera/base/xml.h b/include/cantera/base/xml.h index b28918067..e8a8ae61c 100644 --- a/include/cantera/base/xml.h +++ b/include/cantera/base/xml.h @@ -569,6 +569,9 @@ public: */ void setRoot(const XML_Node& root); + //! Populate the XML tree from an input file + void build(const std::string& filename); + //! Main routine to create an tree-like representation of an XML file /*! * Given an input stream, this routine will read matched XML tags diff --git a/src/base/application.cpp b/src/base/application.cpp index bdf75f2f0..bf4acd061 100644 --- a/src/base/application.cpp +++ b/src/base/application.cpp @@ -222,14 +222,7 @@ XML_Node* Application::get_XML_File(const std::string& file, int debug) std::stringstream phase_xml(ct2ctml_string(path)); x->build(phase_xml); } else { - std::ifstream s(path.c_str()); - if (s) { - x->build(s); - } else { - throw CanteraError("get_XML_File", - "cannot open "+file+" for reading.\n" - "Note, this error indicates a possible configuration problem."); - } + x->build(path); } x->lock(); xmlfiles[path] = {x, mtime}; diff --git a/src/base/xml.cpp b/src/base/xml.cpp index cb35051dc..69af51a4f 100644 --- a/src/base/xml.cpp +++ b/src/base/xml.cpp @@ -12,6 +12,7 @@ #include "cantera/base/utilities.h" #include +#include using namespace std; @@ -716,6 +717,16 @@ void XML_Node::writeHeader(std::ostream& s) s << "" << endl; } +void XML_Node::build(const std::string& filename) +{ + ifstream fin(filename); + if (!fin) { + throw CanteraError("XML_Node::build", + "Unable to open file '{}' for reading.", filename); + } + build(fin); +} + void XML_Node::build(std::istream& f) { XML_Reader r(f); diff --git a/src/oneD/Sim1D.cpp b/src/oneD/Sim1D.cpp index 7639087e9..8eaae6fcd 100644 --- a/src/oneD/Sim1D.cpp +++ b/src/oneD/Sim1D.cpp @@ -9,8 +9,6 @@ #include "cantera/numerics/funcs.h" #include "cantera/base/xml.h" -#include - using namespace std; namespace Cantera @@ -105,15 +103,8 @@ void Sim1D::saveResidual(const std::string& fname, const std::string& id, void Sim1D::restore(const std::string& fname, const std::string& id, int loglevel) { - ifstream s(fname); - if (!s) { - throw CanteraError("Sim1D::restore", - "could not open input file "+fname); - } - XML_Node root; - root.build(s); - s.close(); + root.build(fname); XML_Node* f = root.findID(id); if (!f) { diff --git a/src/thermo/PDSS_ConstVol.cpp b/src/thermo/PDSS_ConstVol.cpp index 17af4ff03..b49d518ac 100644 --- a/src/thermo/PDSS_ConstVol.cpp +++ b/src/thermo/PDSS_ConstVol.cpp @@ -12,8 +12,6 @@ #include "cantera/thermo/PDSS_ConstVol.h" #include "cantera/thermo/VPStandardStateTP.h" -#include - using namespace std; namespace Cantera @@ -101,17 +99,11 @@ void PDSS_ConstVol::constructPDSSFile(VPStandardStateTP* tp, size_t spindex, throw CanteraError("PDSS_ConstVol::initThermo", "input file is null"); } - std::string path = findInputFile(inputFile); - ifstream fin(path); - if (!fin) { - throw CanteraError("PDSS_ConstVol::initThermo","could not open " - +path+" for reading."); - } // The phase object automatically constructs an XML object. Use this object // to store information. XML_Node fxml; - fxml.build(fin); + fxml.build(findInputFile(inputFile)); XML_Node* fxml_phase = findXMLPhase(&fxml, id); if (!fxml_phase) { throw CanteraError("PDSS_ConstVol::initThermo", diff --git a/src/thermo/PDSS_HKFT.cpp b/src/thermo/PDSS_HKFT.cpp index 96bf99cd7..fb233eabd 100644 --- a/src/thermo/PDSS_HKFT.cpp +++ b/src/thermo/PDSS_HKFT.cpp @@ -17,8 +17,6 @@ #include "cantera/thermo/VPStandardStateTP.h" #include "cantera/base/stringUtils.h" -#include - using namespace std; namespace Cantera @@ -607,17 +605,11 @@ void PDSS_HKFT::constructPDSSFile(VPStandardStateTP* tp, size_t spindex, throw CanteraError("PDSS_HKFT::initThermo", "input file is null"); } - std::string path = findInputFile(inputFile); - ifstream fin(path); - if (!fin) { - throw CanteraError("PDSS_HKFT::initThermo","could not open " - +path+" for reading."); - } // The phase object automatically constructs an XML object. Use this object // to store information. XML_Node fxml; - fxml.build(fin); + fxml.build(findInputFile(inputFile)); XML_Node* fxml_phase = findXMLPhase(&fxml, id); if (!fxml_phase) { throw CanteraError("PDSS_HKFT::initThermo", diff --git a/src/thermo/PDSS_IdealGas.cpp b/src/thermo/PDSS_IdealGas.cpp index db73d967e..ee45acc0c 100644 --- a/src/thermo/PDSS_IdealGas.cpp +++ b/src/thermo/PDSS_IdealGas.cpp @@ -12,8 +12,6 @@ #include "cantera/thermo/PDSS_IdealGas.h" #include "cantera/thermo/VPStandardStateTP.h" -#include - using namespace std; namespace Cantera @@ -82,17 +80,11 @@ void PDSS_IdealGas::constructPDSSFile(VPStandardStateTP* tp, size_t spindex, throw CanteraError("PDSS_IdealGas::constructPDSSFile", "input file is null"); } - std::string path = findInputFile(inputFile); - ifstream fin(path); - if (!fin) { - throw CanteraError("PDSS_IdealGas::constructPDSSFile","could not open " - +path+" for reading."); - } // The phase object automatically constructs an XML object. Use this object // to store information. XML_Node fxml; - fxml.build(fin); + fxml.build(findInputFile(inputFile)); XML_Node* fxml_phase = findXMLPhase(&fxml, id); if (!fxml_phase) { throw CanteraError("PDSS_IdealGas::constructPDSSFile", diff --git a/src/thermo/PDSS_IonsFromNeutral.cpp b/src/thermo/PDSS_IonsFromNeutral.cpp index f2449b593..5b5b45b51 100644 --- a/src/thermo/PDSS_IonsFromNeutral.cpp +++ b/src/thermo/PDSS_IonsFromNeutral.cpp @@ -14,8 +14,6 @@ #include "cantera/base/stringUtils.h" #include "cantera/base/ctml.h" -#include - using namespace std; namespace Cantera @@ -168,17 +166,11 @@ void PDSS_IonsFromNeutral::constructPDSSFile(VPStandardStateTP* tp, size_t spind throw CanteraError("PDSS_IonsFromNeutral::constructPDSSFile", "input file is null"); } - std::string path = findInputFile(inputFile); - ifstream fin(path); - if (!fin) { - throw CanteraError("PDSS_IonsFromNeutral::constructPDSSFile","could not open " - +path+" for reading."); - } // The phase object automatically constructs an XML object. Use this object // to store information. XML_Node fxml; - fxml.build(fin); + fxml.build(findInputFile(inputFile)); XML_Node* fxml_phase = findXMLPhase(&fxml, id); if (!fxml_phase) { throw CanteraError("PDSS_IonsFromNeutral::constructPDSSFile", diff --git a/src/thermo/PDSS_SSVol.cpp b/src/thermo/PDSS_SSVol.cpp index 72572f6c4..5c00cc864 100644 --- a/src/thermo/PDSS_SSVol.cpp +++ b/src/thermo/PDSS_SSVol.cpp @@ -12,8 +12,6 @@ #include "cantera/thermo/PDSS_SSVol.h" #include "cantera/thermo/VPStandardStateTP.h" -#include - using namespace std; namespace Cantera @@ -129,17 +127,11 @@ void PDSS_SSVol::constructPDSSFile(VPStandardStateTP* tp, size_t spindex, throw CanteraError("PDSS_SSVol::initThermo", "input file is null"); } - std::string path = findInputFile(inputFile); - ifstream fin(path); - if (!fin) { - throw CanteraError("PDSS_SSVol::initThermo","could not open " - +path+" for reading."); - } // The phase object automatically constructs an XML object. Use this object // to store information. XML_Node fxml; - fxml.build(fin); + fxml.build(findInputFile(inputFile)); XML_Node* fxml_phase = findXMLPhase(&fxml, id); if (!fxml_phase) { throw CanteraError("PDSS_SSVol::initThermo", diff --git a/src/thermo/PDSS_Water.cpp b/src/thermo/PDSS_Water.cpp index d1b57b339..fcc200eec 100644 --- a/src/thermo/PDSS_Water.cpp +++ b/src/thermo/PDSS_Water.cpp @@ -11,8 +11,6 @@ #include "cantera/thermo/WaterPropsIAPWS.h" #include "cantera/base/stringUtils.h" -#include - namespace Cantera { PDSS_Water::PDSS_Water() : @@ -144,17 +142,11 @@ void PDSS_Water::constructPDSSFile(VPStandardStateTP* tp, int spindex, throw CanteraError("PDSS_Water::constructPDSSFile", "input file is null"); } - std::string path = findInputFile(inputFile); - std::ifstream fin(path); - if (!fin) { - throw CanteraError("PDSS_Water::initThermo","could not open " - +path+" for reading."); - } // The phase object automatically constructs an XML object. Use this object // to store information. XML_Node fxml; - fxml.build(fin); + fxml.build(findInputFile(inputFile)); XML_Node* fxml_phase = findXMLPhase(&fxml, id); if (!fxml_phase) { throw CanteraError("PDSS_Water::initThermo", diff --git a/test/general/test_xml.cpp b/test/general/test_xml.cpp index 06abf2194..94d1d47f0 100644 --- a/test/general/test_xml.cpp +++ b/test/general/test_xml.cpp @@ -10,8 +10,7 @@ TEST(XML_Node, read_write) // Check that we can re-read our own XML output XML_Node node1, node2; std::stringstream out; - std::ifstream xmlfile("../data/air-no-reactions.xml"); - node1.build(xmlfile); + node1.build("../data/air-no-reactions.xml"); node1.write(out); node2.build(out);