mirror of
https://github.com/paboyle/Grid.git
synced 2025-06-17 15:27:06 +01:00
new I/O interface
This commit is contained in:
@ -9,101 +9,76 @@
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
|
||||
namespace Grid {
|
||||
|
||||
class BinaryWriter : public Writer {
|
||||
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 write(const std::string& s,const std::string &output) {
|
||||
uint32_t sz = output.size();
|
||||
write(s,sz);
|
||||
const char * cstr = output.c_str();
|
||||
for(int c=0;c<output.size();c++){
|
||||
write(s,cstr[c]);
|
||||
}
|
||||
class BinaryWriter: public Writer<BinaryWriter>
|
||||
{
|
||||
public:
|
||||
BinaryWriter(const std::string &fileName);
|
||||
virtual ~BinaryWriter(void) = default;
|
||||
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:
|
||||
std::ofstream file_;
|
||||
};
|
||||
void write( const std::string& s,const char output ) { writeInternal(s,output); };
|
||||
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 ){
|
||||
// FIXME --- htons, htonl, htno64 etc..
|
||||
file.write((char *)&output,sizeof(T));
|
||||
|
||||
class BinaryReader: public Reader<BinaryReader>
|
||||
{
|
||||
public:
|
||||
BinaryReader(const std::string &fileName);
|
||||
virtual ~BinaryReader(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:
|
||||
std::ifstream file_;
|
||||
};
|
||||
|
||||
// Writer template implementation ////////////////////////////////////////////
|
||||
template <typename U>
|
||||
void BinaryWriter::writeDefault(const std::string &s, const U &x)
|
||||
{
|
||||
file_.write((char *)&x, sizeof(U));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
class BinaryReader : public Reader{
|
||||
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 read( 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 <typename U>
|
||||
void BinaryWriter::writeDefault(const std::string &s, const std::vector<U> &x)
|
||||
{
|
||||
uint64_t sz = x.size();
|
||||
|
||||
write("", sz);
|
||||
for (uint64_t i = 0; i < sz; ++i)
|
||||
{
|
||||
write("", x[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Reader template implementation ////////////////////////////////////////////
|
||||
template <typename U>
|
||||
void BinaryReader::readDefault(const std::string &s, U &output)
|
||||
{
|
||||
file_.read((char *)&output, sizeof(U));
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
void BinaryReader::readDefault(const std::string &s, std::vector<U> &output)
|
||||
{
|
||||
uint64_t sz;
|
||||
|
||||
read("", sz);
|
||||
output.resize(sz);
|
||||
for (uint64_t i = 0; i < sz; ++i)
|
||||
{
|
||||
read("", output[i]);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
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 ){
|
||||
file.read((char *)&output,sizeof(output)); // byte order??
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user