2016-01-02 14:51:32 +00:00
|
|
|
/*************************************************************************************
|
|
|
|
|
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
|
|
|
Source file: ./lib/serialisation/XmlIO.cc
|
|
|
|
|
|
|
|
Copyright (C) 2015
|
|
|
|
|
|
|
|
Author: Antonin Portelli <antonin.portelli@me.com>
|
|
|
|
Author: paboyle <paboyle@ph.ed.ac.uk>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
See the full license in the file "LICENSE" in the top level distribution directory
|
|
|
|
*************************************************************************************/
|
|
|
|
/* END LEGAL */
|
2017-02-22 18:09:33 +00:00
|
|
|
#include <Grid/GridCore.h>
|
2015-11-16 18:14:37 +00:00
|
|
|
|
2018-01-24 13:14:43 +00:00
|
|
|
//using namespace Grid;
|
|
|
|
//using namespace std;
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(Grid);
|
2015-12-08 13:53:33 +00:00
|
|
|
|
2015-11-16 18:14:37 +00:00
|
|
|
// Writer implementation ///////////////////////////////////////////////////////
|
2018-01-24 13:14:43 +00:00
|
|
|
XmlWriter::XmlWriter(const std::string &fileName, std::string toplev) : fileName_(fileName)
|
2015-11-16 18:14:37 +00:00
|
|
|
{
|
2017-06-11 23:14:10 +01:00
|
|
|
if ( toplev == std::string("") ) {
|
|
|
|
node_=doc_;
|
|
|
|
} else {
|
|
|
|
node_=doc_.append_child();
|
|
|
|
node_.set_name(toplev.c_str());
|
|
|
|
}
|
2015-11-16 18:14:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
XmlWriter::~XmlWriter(void)
|
|
|
|
{
|
2017-06-11 23:14:10 +01:00
|
|
|
if ( fileName_ != std::string("") ) {
|
|
|
|
doc_.save_file(fileName_.c_str(), " ");
|
|
|
|
}
|
2015-11-16 18:14:37 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 13:14:43 +00:00
|
|
|
void XmlWriter::push(const std::string &s)
|
2015-11-16 18:14:37 +00:00
|
|
|
{
|
|
|
|
node_ = node_.append_child(s.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlWriter::pop(void)
|
|
|
|
{
|
|
|
|
node_ = node_.parent();
|
|
|
|
}
|
2017-06-11 23:14:10 +01:00
|
|
|
std::string XmlWriter::XmlString(void)
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
doc_.save(oss);
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2018-01-24 13:14:43 +00:00
|
|
|
XmlReader::XmlReader(const char *xmlstring,std::string toplev) : fileName_("")
|
2017-06-11 23:14:10 +01:00
|
|
|
{
|
|
|
|
pugi::xml_parse_result result;
|
2017-10-26 23:30:37 +01:00
|
|
|
result = doc_.load_string(xmlstring);
|
2017-06-11 23:14:10 +01:00
|
|
|
if ( !result ) {
|
2018-01-24 13:14:43 +00:00
|
|
|
std::cerr << "XML error description (from char *): " << result.description() << "\nXML\n"<< xmlstring << "\n";
|
|
|
|
std::cerr << "XML error offset (from char *) " << result.offset << "\nXML\n"<< xmlstring <<"\n";
|
|
|
|
std::abort();
|
2017-06-11 23:14:10 +01:00
|
|
|
}
|
|
|
|
if ( toplev == std::string("") ) {
|
|
|
|
node_ = doc_;
|
|
|
|
} else {
|
|
|
|
node_ = doc_.child(toplev.c_str());
|
|
|
|
}
|
|
|
|
}
|
2015-11-16 18:14:37 +00:00
|
|
|
|
|
|
|
// Reader implementation ///////////////////////////////////////////////////////
|
2018-01-24 13:14:43 +00:00
|
|
|
XmlReader::XmlReader(const std::string &fileName,std::string toplev) : fileName_(fileName)
|
2015-11-16 18:14:37 +00:00
|
|
|
{
|
2017-06-11 23:14:10 +01:00
|
|
|
pugi::xml_parse_result result;
|
|
|
|
result = doc_.load_file(fileName_.c_str());
|
|
|
|
if ( !result ) {
|
2018-01-24 13:14:43 +00:00
|
|
|
std::cerr << "XML error description: " << result.description() <<" "<< fileName_ <<"\n";
|
|
|
|
std::cerr << "XML error offset : " << result.offset <<" "<< fileName_ <<"\n";
|
|
|
|
std::abort();
|
2015-11-16 18:14:37 +00:00
|
|
|
}
|
2017-06-11 23:14:10 +01:00
|
|
|
if ( toplev == std::string("") ) {
|
|
|
|
node_ = doc_;
|
|
|
|
} else {
|
|
|
|
node_ = doc_.child(toplev.c_str());
|
|
|
|
}
|
2015-11-16 18:14:37 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 13:14:43 +00:00
|
|
|
bool XmlReader::push(const std::string &s)
|
2015-11-16 18:14:37 +00:00
|
|
|
{
|
2017-12-01 19:44:07 +00:00
|
|
|
if (node_.child(s.c_str()))
|
|
|
|
{
|
|
|
|
node_ = node_.child(s.c_str());
|
2017-01-23 14:57:38 +00:00
|
|
|
|
2017-12-01 19:44:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-20 12:56:20 +00:00
|
|
|
return false;
|
2017-12-01 19:44:07 +00:00
|
|
|
}
|
2015-11-16 18:14:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReader::pop(void)
|
|
|
|
{
|
|
|
|
node_ = node_.parent();
|
|
|
|
}
|
|
|
|
|
2015-12-23 14:20:05 +00:00
|
|
|
bool XmlReader::nextElement(const std::string &s)
|
|
|
|
{
|
|
|
|
if (node_.next_sibling(s.c_str()))
|
2017-12-01 19:44:07 +00:00
|
|
|
{
|
|
|
|
node_ = node_.next_sibling(s.c_str());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-23 14:20:05 +00:00
|
|
|
else
|
2017-12-01 19:44:07 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-23 14:57:38 +00:00
|
|
|
|
2015-12-23 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 18:14:37 +00:00
|
|
|
template <>
|
2018-01-24 13:14:43 +00:00
|
|
|
void XmlReader::readDefault(const std::string &s, std::string &output)
|
2015-11-16 18:14:37 +00:00
|
|
|
{
|
2017-12-01 19:44:07 +00:00
|
|
|
if (node_.child(s.c_str()))
|
|
|
|
{
|
|
|
|
output = node_.child(s.c_str()).first_child().value();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout << GridLogWarning << "XML: cannot open node '" << s << "'";
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
output = "";
|
|
|
|
}
|
2015-11-16 18:14:37 +00:00
|
|
|
}
|
2018-01-24 13:14:43 +00:00
|
|
|
NAMESPACE_END(Grid);
|