1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-14 01:35:36 +00:00
Grid/lib/serialisation/TextIO.h

149 lines
4.1 KiB
C
Raw Normal View History

2015-08-20 23:04:38 +01:00
#ifndef GRID_SERIALISATION_TEXT_READER_H
#define GRID_SERIALISATION_TEXT_READER_H
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <math.h>
#include <vector>
#include <cassert>
namespace Grid {
class TextWriter : public Writer {
private:
2015-08-20 23:04:38 +01:00
std::ofstream file;
int level;
void indent(void) {
for(int i=0;i<level;i++){
file <<"\t";
}
}
public:
2015-08-20 23:04:38 +01:00
TextWriter(const std::string &_file) : file(_file,std::ios::out) {
level=0;
}
2015-08-20 23:04:38 +01:00
~TextWriter() { }
void push(const std::string &s)
{
2015-08-20 23:04:38 +01:00
// std::string tmp = s;
// write(s,tmp);
2015-08-20 23:04:38 +01:00
level++;
}
void pop(void) {
2015-08-20 23:04:38 +01:00
level--;
}
void write( const std::string& s,const std::string &output ) {
2015-08-20 23:04:38 +01:00
indent();
file<<output<<std::endl;
};
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:
2015-08-20 23:04:38 +01:00
template<class T> void writeInternal( const std::string& s,const T output ){
2015-08-20 23:04:38 +01:00
indent();
file << std::boolalpha << output<<std::endl;
}
};
class TextReader : public Reader {
private:
2015-08-20 23:04:38 +01:00
std::ifstream file;
int level;
public:
2015-08-20 23:04:38 +01:00
TextReader(const std::string &_file) : file(_file,std::ios::in) { level = 0;};
2015-08-20 23:04:38 +01:00
~TextReader() { }
void read( const std::string& s,std::string &output ) {
2015-08-20 23:04:38 +01:00
char c='a';
for(int i=0;i<level;i++){
file.get(c);
if ( c != '\t' )
std::cout << "mismatch on tab "<<c<<" level "<< level<< " i "<< i<<std::endl;
}
output.clear();
std::getline(file,output);
};
2015-08-20 23:04:38 +01:00
void push(const std::string &s) {
// std::string tmp; read(s,tmp);
2015-08-20 23:04:38 +01:00
level++;
}
2015-08-20 23:04:38 +01:00
void pop(void) { level--; }
template<class T>
void read( const std::string& s, std::vector<T> &output ) {
2015-08-20 23:04:38 +01:00
push(s);
uint64_t n; read("N",n);
2015-08-20 23:04:38 +01:00
// skip the vector length
T tmp;
2015-08-20 23:04:38 +01:00
output.resize(0);
for(int i=0;i<n;i++){
std::ostringstream oss; oss << "elem" << i;
read(*this,oss.str(),tmp);
output.push_back(tmp);
}
2015-08-20 23:04:38 +01:00
pop();
};
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)
{
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;
}
assert( is.tellg()==-1);
}
};
}
#endif