2015-08-20 23:04:38 +01:00
|
|
|
#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 {
|
|
|
|
|
2015-08-21 10:06:33 +01:00
|
|
|
class BinaryWriter : public Writer {
|
2015-08-20 23:04:38 +01:00
|
|
|
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) {}
|
|
|
|
|
2015-08-21 10:06:33 +01:00
|
|
|
void write(const std::string& s,const std::string &output) {
|
2015-08-20 23:04:38 +01:00
|
|
|
uint32_t sz = output.size();
|
2015-08-21 10:06:33 +01:00
|
|
|
write(s,sz);
|
2015-08-20 23:04:38 +01:00
|
|
|
const char * cstr = output.c_str();
|
|
|
|
for(int c=0;c<output.size();c++){
|
2015-08-21 10:06:33 +01:00
|
|
|
write(s,cstr[c]);
|
2015-08-20 23:04:38 +01:00
|
|
|
}
|
|
|
|
};
|
2015-08-21 10:06:33 +01:00
|
|
|
void write( const std::string& s, char output ) { writeInternal(s,output); };
|
|
|
|
void write( const std::string& s, int16_t output ) { writeInternal(s,output); };
|
|
|
|
void write( const std::string& s, uint16_t output ) { writeInternal(s,output); };
|
|
|
|
void write( const std::string& s, int32_t output ) { writeInternal(s,output); };
|
|
|
|
void write( const std::string& s, uint32_t output ) { writeInternal(s,output); };
|
|
|
|
void write( const std::string& s, int64_t output ) { writeInternal(s,output); };
|
|
|
|
void write( const std::string& s, uint64_t output ) { writeInternal(s,output); };
|
|
|
|
void write( const std::string& s, float output ) { writeInternal(s,output); };
|
|
|
|
void write( const std::string& s, double output ) { writeInternal(s,output); };
|
|
|
|
void write( const std::string& s, bool output ) { writeInternal(s,output); };
|
2015-08-20 23:04:38 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
template<class T> void writeInternal( const std::string& s, T output ){
|
|
|
|
// FIXME --- htons, htonl, htno64 etc..
|
|
|
|
file.write((char *)&output,sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-08-21 10:06:33 +01:00
|
|
|
class BinaryReader : public Reader{
|
2015-08-20 23:04:38 +01:00
|
|
|
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) { }
|
|
|
|
|
2015-08-21 10:06:33 +01:00
|
|
|
void read( const std::string& s,std::string &output ) {
|
2015-08-20 23:04:38 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-08-21 10:06:33 +01:00
|
|
|
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); };
|
2015-08-20 23:04:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
template<class T> void readInternal( const std::string& path, T &output ){
|
|
|
|
file.read((char *)&output,sizeof(output)); // byte order??
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|