#pragma once // The MIT License (MIT) // Simplistic Binary Streams 0.9 // Copyright (C) 2014, by Wong Shao Voon (shaovoon@yahoo.com) // // http://opensource.org/licenses/MIT // #include #include #include #include #include #include //http://www.codeproject.com/Tips/808776/Cplusplus-Simplistic-Binary-Streams namespace byte_utils { class file_istream { public: file_istream() {} file_istream(const char * file, std::ios_base::openmode mode) { open(file, mode); } void open(const char * file, std::ios_base::openmode mode) { m_istm.open(file, mode); } void close() { m_istm.close(); } bool is_open() { return m_istm.is_open(); } bool eof() const { return m_istm.eof(); } std::ifstream::pos_type tellg() { return m_istm.tellg(); } void seekg(std::streampos pos) { m_istm.seekg(pos); } void seekg(std::streamoff offset, std::ios_base::seekdir way) { m_istm.seekg(offset, way); } template void read(T& t) { if (m_istm.read(reinterpret_cast(&t), sizeof(T)).bad()) { throw std::runtime_error("Read Error!"); } } void read(char* p, size_t size) { if (m_istm.read(p, size).bad()) { throw std::runtime_error("Read Error!"); } } private: std::ifstream m_istm; }; template<> void file_istream::read(std::vector& vec) { if (m_istm.read(reinterpret_cast(&vec[0]), vec.size()).bad()) { throw std::runtime_error("Read Error!"); } } template file_istream& operator >> (file_istream& istm, T& val) { istm.read(val); return istm; } template<> file_istream& operator >> (file_istream& istm, std::string& val) { int size = 0; istm.read(size); if (size <= 0) return istm; std::vector vec((size_t)size); istm.read(vec); val.assign(&vec[0], (size_t)size); return istm; } class mem_istream { public: mem_istream() : m_index(0) {} mem_istream(const char * mem, size_t size) { open(mem, size); } mem_istream(const std::vector& vec) { m_index = 0; m_vec.clear(); m_vec.reserve(vec.size()); m_vec.assign(vec.begin(), vec.end()); } void open(const char * mem, size_t size) { m_index = 0; m_vec.clear(); m_vec.reserve(size); m_vec.assign(mem, mem + size); } void close() { m_vec.clear(); } bool eof() const { return m_index >= m_vec.size(); } std::ifstream::pos_type tellg() { return m_index; } bool seekg(size_t pos) { if (pos& get_internal_vec() { return m_vec; } const char* get_ptr() { return &m_vec[0]; } template void read(T& t) { if (eof()) throw std::runtime_error("Premature end of array!"); if ((m_index + sizeof(T)) > m_vec.size()) throw std::runtime_error("Premature end of array!"); std::memcpy(reinterpret_cast(&t), &m_vec[m_index], sizeof(T)); m_index += sizeof(T); } void read(char* p, size_t size) { if (eof()) throw std::runtime_error("Premature end of array!"); if ((m_index + size) > m_vec.size()) throw std::runtime_error("Premature end of array!"); std::memcpy(reinterpret_cast(p), &m_vec[m_index], size); m_index += size; } private: std::vector m_vec; size_t m_index; }; template<> void mem_istream::read(std::vector& vec) { if (eof()) throw std::runtime_error("Premature end of array!"); if ((m_index + vec.size()) > m_vec.size()) throw std::runtime_error("Premature end of array!"); std::memcpy(reinterpret_cast(&vec[0]), &m_vec[m_index], vec.size()); m_index += vec.size(); } template mem_istream& operator >> (mem_istream& istm, T& val) { istm.read(val); return istm; } template<> mem_istream& operator >> (mem_istream& istm, std::string& val) { int size = 0; istm.read(size); if (size <= 0) return istm; std::vector vec((size_t)size); istm.read(vec); val.assign(&vec[0], (size_t)size); return istm; } class file_ostream { public: file_ostream() {} file_ostream(const char * file, std::ios_base::openmode mode) { open(file, mode); } void open(const char * file, std::ios_base::openmode mode) { m_ostm.open(file, mode); } void flush() { m_ostm.flush(); } void close() { m_ostm.close(); } bool is_open() { return m_ostm.is_open(); } template void write(const T& t) { m_ostm.write(reinterpret_cast(&t), sizeof(T)); } void write(const char* p, size_t size) { m_ostm.write(p, size); } private: std::ofstream m_ostm; }; template<> void file_ostream::write(const std::vector& vec) { m_ostm.write(reinterpret_cast(&vec[0]), vec.size()); } template file_ostream& operator << (file_ostream& ostm, const T& val) { ostm.write(val); return ostm; } template<> file_ostream& operator << (file_ostream& ostm, const std::string& val) { int size = val.size(); ostm.write(size); if (val.size() <= 0) return ostm; ostm.write(val.c_str(), val.size()); return ostm; } file_ostream& operator << (file_ostream& ostm, const char* val) { int size = std::strlen(val); ostm.write(size); if (size <= 0) return ostm; ostm.write(val, size); return ostm; } class mem_ostream { public: mem_ostream() {} void close() { m_vec.clear(); } const std::vector& get_internal_vec() { return m_vec; } const char* get_ptr() { return &m_vec[0]; } template void write(const T& t) { std::vector vec(sizeof(T)); std::memcpy(reinterpret_cast(&vec[0]), reinterpret_cast(&t), sizeof(T)); write(vec); } void write(const char* p, size_t size) { for (size_t i = 0; i m_vec; }; template<> void mem_ostream::write(const std::vector& vec) { for (size_t i = 0; i mem_ostream& operator << (mem_ostream& ostm, const T& val) { ostm.write(val); return ostm; } template<> mem_ostream& operator << (mem_ostream& ostm, const std::string& val) { int size = val.size(); ostm.write(size); if (val.size() <= 0) return ostm; ostm.write(val.c_str(), val.size()); return ostm; } mem_ostream& operator << (mem_ostream& ostm, const char* val) { int size = std::strlen(val); ostm.write(size); if (size <= 0) return ostm; ostm.write(val, size); return ostm; } char* intToByte(int value) { char bytes[sizeof value]; bytes[3] = (value >> 24) & 0xFF; bytes[2] = (value >> 16) & 0xFF; bytes[1] = (value >> 8) & 0xFF; bytes[0] = (value)& 0xFF; return bytes; } int bytesToInt(char* bytes) { return (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8) | (bytes[0]); } } // ns simple