mirror of
https://github.com/paboyle/Grid.git
synced 2025-06-10 19:36:56 +01:00
Text and Binary readers
This commit is contained in:
124
lib/serialisation/BinaryIO.h
Normal file
124
lib/serialisation/BinaryIO.h
Normal file
@ -0,0 +1,124 @@
|
||||
#ifndef GRID_SERIALISATION_BINARY_READER_H
|
||||
#define GRID_SERIALISATION_BINARY_READER_H
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <math.h>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
|
||||
namespace Grid {
|
||||
|
||||
class BinaryWriter {
|
||||
private:
|
||||
|
||||
std::ofstream file;
|
||||
|
||||
public:
|
||||
|
||||
BinaryWriter(const std::string &_file) : file(_file,std::ios::binary|std::ios::out) {}
|
||||
|
||||
~BinaryWriter() {}
|
||||
|
||||
// Binary is scopeless
|
||||
void push(const std::string &s) {}
|
||||
void pop(void) {}
|
||||
|
||||
void iwrite(const std::string& s,const std::string &output) {
|
||||
uint32_t sz = output.size();
|
||||
iwrite(s,sz);
|
||||
const char * cstr = output.c_str();
|
||||
for(int c=0;c<output.size();c++){
|
||||
iwrite(s,cstr[c]);
|
||||
}
|
||||
};
|
||||
void iwrite( const std::string& s, char output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, int16_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, uint16_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, int32_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, uint32_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, int64_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, uint64_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, float output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, double output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, bool output ) { writeInternal(s,output); };
|
||||
|
||||
private:
|
||||
template<class T> void writeInternal( const std::string& s, T output ){
|
||||
// FIXME --- htons, htonl, htno64 etc..
|
||||
file.write((char *)&output,sizeof(T));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
class BinaryReader {
|
||||
private:
|
||||
|
||||
std::ifstream file;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
BinaryReader(const std::string &_file) : file(_file,std::ios::binary|std::ios::in) {}
|
||||
|
||||
~BinaryReader() {}
|
||||
|
||||
// Binary is scopeless
|
||||
void push(const std::string &s) { }
|
||||
void pop(void) { }
|
||||
|
||||
void iread( const std::string& s,std::string &output ) {
|
||||
|
||||
output.clear();
|
||||
|
||||
uint32_t sz;
|
||||
file.read((char *)&sz,sizeof(sz));
|
||||
|
||||
for(int c=0;c<sz;c++){
|
||||
char ch;
|
||||
file.read(&ch,sizeof(ch));
|
||||
output.push_back(ch);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<class T> void iread( const std::string& s, std::vector<T> &output ) {
|
||||
|
||||
T tmp;
|
||||
uint64_t n;
|
||||
|
||||
iread("N",n);
|
||||
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);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
void iread( const std::string& s, int16_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, uint16_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, int32_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, uint32_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, int64_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, uint64_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, float &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, double &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, bool &output ) { readInternal(s,output); };
|
||||
|
||||
|
||||
private:
|
||||
|
||||
template<class T> void readInternal( const std::string& path, T &output ){
|
||||
file.read((char *)&output,sizeof(output)); // byte order??
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
@ -35,9 +35,10 @@ namespace Grid {
|
||||
template< class Reader> void read(Reader& rd, const std::string& s, bool &output ) { rd.iread(s,output); };
|
||||
|
||||
|
||||
template<class Writer, class T>
|
||||
void write(Writer& wr, const std::string& s,const std::vector<T> output ) {
|
||||
template<class Writer, class T> void write(Writer& wr, const std::string& s,const std::vector<T> output ) {
|
||||
push(wr,s);
|
||||
uint64_t sz =output.size();
|
||||
write(wr,"N",sz);
|
||||
for(int i=0;i<output.size();i++){
|
||||
std::ostringstream oss; oss << "elem" << i;
|
||||
write(wr,oss.str(),output[i]);
|
||||
@ -66,21 +67,17 @@ namespace Grid {
|
||||
//////////////////////////////////////////
|
||||
// Todo:
|
||||
//////////////////////////////////////////
|
||||
//#include <serialisation/CoutReader.h>
|
||||
//#include <serialisation/TextReader.h>
|
||||
//#include <serialisation/JSONReader.h>
|
||||
//#include <serialisation/YAMLReader.h>
|
||||
|
||||
#include <serialisation/XMLReader.h>
|
||||
#include <serialisation/BinaryIO.h>
|
||||
#include <serialisation/TextIO.h>
|
||||
//#include <serialisation/JsonIO.h>
|
||||
//#include <serialisation/YamlIO.h>
|
||||
#include <serialisation/XmlIO.h>
|
||||
|
||||
//////////////////////////////////////////
|
||||
// Select the default serialiser
|
||||
// Select the default serialiser use ifdef's
|
||||
//////////////////////////////////////////
|
||||
namespace Grid {
|
||||
|
||||
using XMLPolicy::Reader;
|
||||
using XMLPolicy::Writer;
|
||||
|
||||
typedef XMLReader Reader;
|
||||
typedef XMLWriter Writer;
|
||||
}
|
||||
|
||||
#endif
|
147
lib/serialisation/TextIO.h
Normal file
147
lib/serialisation/TextIO.h
Normal file
@ -0,0 +1,147 @@
|
||||
#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 {
|
||||
private:
|
||||
|
||||
std::ofstream file;
|
||||
int level;
|
||||
void indent(void) {
|
||||
for(int i=0;i<level;i++){
|
||||
file <<"\t";
|
||||
}
|
||||
}
|
||||
public:
|
||||
|
||||
TextWriter(const std::string &_file) : file(_file,std::ios::out) {
|
||||
level=0;
|
||||
}
|
||||
|
||||
~TextWriter() { }
|
||||
|
||||
void push(const std::string &s)
|
||||
{
|
||||
// std::string tmp = s;
|
||||
// iwrite(s,tmp);
|
||||
level++;
|
||||
}
|
||||
void pop(void) {
|
||||
level--;
|
||||
}
|
||||
|
||||
void iwrite( const std::string& s,const std::string &output ) {
|
||||
indent();
|
||||
file<<output<<std::endl;
|
||||
};
|
||||
void iwrite( const std::string& s, int16_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, uint16_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, int32_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, uint32_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, int64_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, uint64_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, float output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, double output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, bool output ) { writeInternal(s,output); };
|
||||
|
||||
private:
|
||||
|
||||
template<class T> void writeInternal( const std::string& s, T output ){
|
||||
indent();
|
||||
file << std::boolalpha << output<<std::endl;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class TextReader {
|
||||
private:
|
||||
|
||||
std::ifstream file;
|
||||
int level;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
TextReader(const std::string &_file) : file(_file,std::ios::in) { level = 0;};
|
||||
|
||||
~TextReader() { }
|
||||
|
||||
void iread( const std::string& s,std::string &output ) {
|
||||
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);
|
||||
};
|
||||
void push(const std::string &s) {
|
||||
// std::string tmp; iread(s,tmp);
|
||||
level++;
|
||||
}
|
||||
void pop(void) { level--; }
|
||||
|
||||
template<class T>
|
||||
void iread( const std::string& s, std::vector<T> &output ) {
|
||||
|
||||
push(s);
|
||||
|
||||
uint64_t n; iread("N",n);
|
||||
|
||||
// skip the vector length
|
||||
T tmp;
|
||||
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);
|
||||
}
|
||||
|
||||
pop();
|
||||
|
||||
};
|
||||
|
||||
void iread( const std::string& s, int16_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, uint16_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, int32_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, uint32_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, int64_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, uint64_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, float &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, double &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, bool &output ) { readInternal(s,output); };
|
||||
|
||||
|
||||
private:
|
||||
|
||||
template<class T> void readInternal( const std::string& path, T &output ){
|
||||
std::string asString;
|
||||
iread(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
|
@ -60,6 +60,7 @@ public:
|
||||
void iwrite( const std::string& s, bool output ) { writeInternal(s,output); };
|
||||
|
||||
private:
|
||||
|
||||
template<class T> void writeInternal( const std::string& s, T output ){
|
||||
std::ostringstream os;
|
||||
os << std::boolalpha << output;
|
||||
@ -95,9 +96,7 @@ public:
|
||||
node= doc.child("document");
|
||||
}
|
||||
|
||||
~XMLReader()
|
||||
{
|
||||
}
|
||||
~XMLReader() { }
|
||||
|
||||
void iread( const std::string& s,std::string &output ) {
|
||||
output=node.child(s.c_str()).first_child().value();
|
||||
@ -112,17 +111,25 @@ public:
|
||||
|
||||
template<class T>
|
||||
void iread( const std::string& s, std::vector<T> &output ) {
|
||||
output.resize(0);
|
||||
|
||||
T tmp;
|
||||
push(s);
|
||||
|
||||
uint64_t n;
|
||||
|
||||
pugi::xml_node it=node.first_child();
|
||||
|
||||
// skip the vector length
|
||||
T tmp;
|
||||
int i=0;
|
||||
for(pugi::xml_node it=node.first_child(); it; it = it.next_sibling() ){
|
||||
output.resize(0);
|
||||
for(it = it.next_sibling(); it; it = it.next_sibling() ){
|
||||
std::ostringstream oss; oss << "elem" << i;
|
||||
read(*this,oss.str(),tmp);
|
||||
output.push_back(tmp);
|
||||
i++;
|
||||
}
|
||||
|
||||
assert(i == n );
|
||||
pop();
|
||||
|
||||
};
|
||||
@ -159,10 +166,5 @@ private:
|
||||
|
||||
};
|
||||
|
||||
namespace XMLPolicy
|
||||
{
|
||||
typedef XMLReader Reader;
|
||||
typedef XMLWriter Writer;
|
||||
};
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user