[XML] Add version of XML_Node::build that takes file name directly

This commit is contained in:
Ray Speth 2016-05-02 15:05:07 -04:00
parent c7a1a57fbd
commit 6003af01a3
11 changed files with 23 additions and 74 deletions

View file

@ -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

View file

@ -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};

View file

@ -12,6 +12,7 @@
#include "cantera/base/utilities.h"
#include <sstream>
#include <fstream>
using namespace std;
@ -716,6 +717,16 @@ void XML_Node::writeHeader(std::ostream& s)
s << "<?xml version=\"1.0\"?>" << 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);

View file

@ -9,8 +9,6 @@
#include "cantera/numerics/funcs.h"
#include "cantera/base/xml.h"
#include <fstream>
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) {

View file

@ -12,8 +12,6 @@
#include "cantera/thermo/PDSS_ConstVol.h"
#include "cantera/thermo/VPStandardStateTP.h"
#include <fstream>
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",

View file

@ -17,8 +17,6 @@
#include "cantera/thermo/VPStandardStateTP.h"
#include "cantera/base/stringUtils.h"
#include <fstream>
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",

View file

@ -12,8 +12,6 @@
#include "cantera/thermo/PDSS_IdealGas.h"
#include "cantera/thermo/VPStandardStateTP.h"
#include <fstream>
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",

View file

@ -14,8 +14,6 @@
#include "cantera/base/stringUtils.h"
#include "cantera/base/ctml.h"
#include <fstream>
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",

View file

@ -12,8 +12,6 @@
#include "cantera/thermo/PDSS_SSVol.h"
#include "cantera/thermo/VPStandardStateTP.h"
#include <fstream>
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",

View file

@ -11,8 +11,6 @@
#include "cantera/thermo/WaterPropsIAPWS.h"
#include "cantera/base/stringUtils.h"
#include <fstream>
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",

View file

@ -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);