mirror of
https://github.com/paboyle/Grid.git
synced 2025-06-16 23:07:05 +01:00
Merge branch 'master' of github.com:paboyle/Grid
Merge done Conflicts: lib/serialisation/XmlIO.h tests/Test_stencil.cc
This commit is contained in:
@ -11,136 +11,94 @@
|
||||
|
||||
#include "pugixml/pugixml.h"
|
||||
|
||||
|
||||
namespace Grid {
|
||||
|
||||
class XMLWriter : public Writer {
|
||||
private:
|
||||
|
||||
pugi::xml_document doc;
|
||||
pugi::xml_node node;
|
||||
std::string file;
|
||||
|
||||
public:
|
||||
|
||||
XMLWriter(const std::string &_file) : file(_file)
|
||||
namespace Grid
|
||||
{
|
||||
|
||||
class XmlWriter: public Writer<XmlWriter>
|
||||
{
|
||||
node=doc.append_child();
|
||||
node.set_name("document");
|
||||
}
|
||||
|
||||
~XMLWriter()
|
||||
{
|
||||
// simple_walker walker;
|
||||
// doc.traverse(walker);
|
||||
|
||||
doc.save_file(file.c_str()," ");
|
||||
}
|
||||
|
||||
void push(const std::string &s)
|
||||
{
|
||||
node = node.append_child(s.c_str());
|
||||
}
|
||||
void pop(void) {
|
||||
node = node.parent();
|
||||
}
|
||||
|
||||
void write( const std::string& s,const std::string &output ) {
|
||||
pugi::xml_node leaf=node.append_child(s.c_str());
|
||||
leaf.append_child(pugi::node_pcdata).set_value(output.c_str());
|
||||
|
||||
public:
|
||||
XmlWriter(const std::string &fileName);
|
||||
virtual ~XmlWriter(void);
|
||||
void push(const std::string &s);
|
||||
void pop(void);
|
||||
template <typename U>
|
||||
void writeDefault(const std::string &s, const U &x);
|
||||
template <typename U>
|
||||
void writeDefault(const std::string &s, const std::vector<U> &x);
|
||||
private:
|
||||
pugi::xml_document doc_;
|
||||
pugi::xml_node node_;
|
||||
std::string fileName_;
|
||||
};
|
||||
|
||||
void write( const std::string& s,const int16_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const uint16_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const int32_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const uint32_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const int64_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const uint64_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const float output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const double output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const bool output ) { writeInternal(s,output); };
|
||||
|
||||
private:
|
||||
|
||||
template<class T> void writeInternal( const std::string& s,const T output ){
|
||||
|
||||
class XmlReader: public Reader<XmlReader>
|
||||
{
|
||||
public:
|
||||
XmlReader(const std::string &fileName);
|
||||
virtual ~XmlReader(void) = default;
|
||||
void push(const std::string &s);
|
||||
void pop(void);
|
||||
template <typename U>
|
||||
void readDefault(const std::string &s, U &output);
|
||||
template <typename U>
|
||||
void readDefault(const std::string &s, std::vector<U> &output);
|
||||
private:
|
||||
pugi::xml_document doc_;
|
||||
pugi::xml_node node_;
|
||||
std::string fileName_;
|
||||
};
|
||||
|
||||
// Writer template implementation ////////////////////////////////////////////
|
||||
template <typename U>
|
||||
void XmlWriter::writeDefault(const std::string &s, const U &x)
|
||||
{
|
||||
std::ostringstream os;
|
||||
os << std::boolalpha << output;
|
||||
write(s,os.str());
|
||||
|
||||
os << std::boolalpha << x;
|
||||
pugi::xml_node leaf = node_.append_child(s.c_str());
|
||||
leaf.append_child(pugi::node_pcdata).set_value(os.str().c_str());
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
class XMLReader : public Reader {
|
||||
private:
|
||||
|
||||
pugi::xml_document doc;
|
||||
pugi::xml_node node;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
XMLReader(const std::string &_file)
|
||||
template <typename U>
|
||||
void XmlWriter::writeDefault(const std::string &s, const std::vector<U> &x)
|
||||
{
|
||||
pugi::xml_parse_result result = doc.load_file(_file.c_str());
|
||||
|
||||
if ( !result ) {
|
||||
std::cout << "XML error description: " << result.description() << "\n";
|
||||
std::cout << "XML error offset: " << result.offset << "\n";
|
||||
push(s);
|
||||
for (auto &x_i: x)
|
||||
{
|
||||
write("elem", x_i);
|
||||
}
|
||||
|
||||
assert(result);
|
||||
|
||||
// simple_walker walker;
|
||||
// doc.traverse(walker);
|
||||
|
||||
node= doc.child("document");
|
||||
pop();
|
||||
}
|
||||
|
||||
~XMLReader() { }
|
||||
|
||||
void read( const std::string& s,std::string &output ) {
|
||||
output=node.child(s.c_str()).first_child().value();
|
||||
};
|
||||
void push(const std::string &s)
|
||||
|
||||
// Reader template implementation ////////////////////////////////////////////
|
||||
template <typename U>
|
||||
void XmlReader::readDefault(const std::string &s, U &output)
|
||||
{
|
||||
node = node.child(s.c_str());
|
||||
std::string buf;
|
||||
|
||||
readDefault(s, buf);
|
||||
fromString(output, buf);
|
||||
}
|
||||
void pop(void) {
|
||||
node = node.parent();
|
||||
}
|
||||
|
||||
void read( const std::string& s, int16_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, uint16_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, int32_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, uint32_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, int64_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, uint64_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, float &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, double &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, bool &output ) { readInternal(s,output); };
|
||||
|
||||
|
||||
private:
|
||||
|
||||
template<class T> void readInternal( const std::string& path, T &output ){
|
||||
std::string asString;
|
||||
read(path,asString);
|
||||
convert(asString,output);
|
||||
}
|
||||
|
||||
template<class T> void convert(const std::string &asString,T &output)
|
||||
|
||||
template <typename U>
|
||||
void XmlReader::readDefault(const std::string &s, std::vector<U> &output)
|
||||
{
|
||||
std::istringstream is(asString); is.exceptions(std::ios::failbit);
|
||||
try {
|
||||
is >> std::boolalpha >> output;
|
||||
} catch(std::istringstream::failure e) {
|
||||
std::cerr << "XML read failure on "<<" "<<asString<<" "<<typeid(T).name()<<std::endl;
|
||||
pugi::xml_node nodeCpy;
|
||||
std::string buf;
|
||||
unsigned int i = 0;
|
||||
|
||||
push(s);
|
||||
while (node_.child("elem"))
|
||||
{
|
||||
output.resize(i + 1);
|
||||
read("elem", output[i]);
|
||||
node_.child("elem").set_name("elem-done");
|
||||
i++;
|
||||
}
|
||||
// assert( is.tellg()==-1);
|
||||
pop();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user