2015-04-25 02:21:39 +02:00
|
|
|
#include "Data.h"
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using std::istream;
|
|
|
|
using std::ofstream;
|
|
|
|
using std::ifstream;
|
|
|
|
using std::ios;
|
|
|
|
|
|
|
|
namespace bf = boost::filesystem;
|
|
|
|
|
|
|
|
namespace cpputils {
|
|
|
|
|
|
|
|
boost::optional<Data> Data::LoadFromFile(const bf::path &filepath) {
|
|
|
|
ifstream file(filepath.c_str(), ios::binary);
|
|
|
|
if (!file.good()) {
|
|
|
|
return boost::none;
|
|
|
|
}
|
2015-10-24 18:39:11 +02:00
|
|
|
return LoadFromStream(file);
|
2015-04-25 02:21:39 +02:00
|
|
|
}
|
|
|
|
|
2015-10-24 18:39:11 +02:00
|
|
|
std::streampos Data::_getStreamSize(istream &stream) {
|
2015-04-25 02:21:39 +02:00
|
|
|
auto current_pos = stream.tellg();
|
|
|
|
|
|
|
|
//Retrieve length
|
|
|
|
stream.seekg(0, stream.end);
|
|
|
|
auto endpos = stream.tellg();
|
|
|
|
|
|
|
|
//Restore old position
|
|
|
|
stream.seekg(current_pos, stream.beg);
|
|
|
|
|
|
|
|
return endpos - current_pos;
|
|
|
|
}
|
|
|
|
|
2015-10-25 11:32:40 +01:00
|
|
|
Data Data::LoadFromStream(istream &stream, size_t size) {
|
2015-10-24 18:39:11 +02:00
|
|
|
Data result(size);
|
|
|
|
stream.read(static_cast<char*>(result.data()), result.size());
|
|
|
|
return std::move(result);
|
2015-04-25 02:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|