Data::LoadFromStream() and Data::StoreToStream()
This commit is contained in:
parent
60b7f853d9
commit
ea30ac641b
|
@ -15,14 +15,10 @@ boost::optional<Data> Data::LoadFromFile(const bf::path &filepath) {
|
||||||
if (!file.good()) {
|
if (!file.good()) {
|
||||||
return boost::none;
|
return boost::none;
|
||||||
}
|
}
|
||||||
size_t size = _getStreamSize(file);
|
return LoadFromStream(file);
|
||||||
|
|
||||||
Data result(size);
|
|
||||||
result._readFromStream(file);
|
|
||||||
return std::move(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Data::_getStreamSize(istream &stream) {
|
std::streampos Data::_getStreamSize(istream &stream) {
|
||||||
auto current_pos = stream.tellg();
|
auto current_pos = stream.tellg();
|
||||||
|
|
||||||
//Retrieve length
|
//Retrieve length
|
||||||
|
@ -35,9 +31,12 @@ size_t Data::_getStreamSize(istream &stream) {
|
||||||
return endpos - current_pos;
|
return endpos - current_pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Data Data::LoadFromStream(istream &stream) {
|
||||||
|
size_t size = _getStreamSize(stream);
|
||||||
|
|
||||||
void Data::_readFromStream(istream &stream) {
|
Data result(size);
|
||||||
stream.read((char*)_data, _size);
|
stream.read(static_cast<char*>(result.data()), result.size());
|
||||||
|
return std::move(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
13
data/Data.h
13
data/Data.h
|
@ -35,11 +35,15 @@ public:
|
||||||
void StoreToFile(const boost::filesystem::path &filepath) const;
|
void StoreToFile(const boost::filesystem::path &filepath) const;
|
||||||
static boost::optional<Data> LoadFromFile(const boost::filesystem::path &filepath);
|
static boost::optional<Data> LoadFromFile(const boost::filesystem::path &filepath);
|
||||||
|
|
||||||
|
//TODO Test LoadFromStream/StoreToStream
|
||||||
|
static Data LoadFromStream(std::istream &stream);
|
||||||
|
void StoreToStream(std::ostream &stream) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_t _size;
|
size_t _size;
|
||||||
void *_data;
|
void *_data;
|
||||||
|
|
||||||
static size_t _getStreamSize(std::istream &stream);
|
static std::streampos _getStreamSize(std::istream &stream);
|
||||||
void _readFromStream(std::istream &stream);
|
void _readFromStream(std::istream &stream);
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(Data);
|
DISALLOW_COPY_AND_ASSIGN(Data);
|
||||||
|
@ -115,7 +119,12 @@ inline Data &Data::FillWithZeroes() {
|
||||||
|
|
||||||
inline void Data::StoreToFile(const boost::filesystem::path &filepath) const {
|
inline void Data::StoreToFile(const boost::filesystem::path &filepath) const {
|
||||||
std::ofstream file(filepath.c_str(), std::ios::binary | std::ios::trunc);
|
std::ofstream file(filepath.c_str(), std::ios::binary | std::ios::trunc);
|
||||||
file.write((const char*)_data, _size);
|
StoreToStream(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline void Data::StoreToStream(std::ostream &stream) const {
|
||||||
|
stream.write(static_cast<const char*>(_data), _size);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool operator==(const Data &lhs, const Data &rhs) {
|
inline bool operator==(const Data &lhs, const Data &rhs) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user