2015-04-25 16:43:37 +02:00
|
|
|
#include "InMemoryFile.h"
|
|
|
|
|
|
|
|
using cpputils::Data;
|
|
|
|
|
|
|
|
InMemoryFile::InMemoryFile(Data data): _data(std::move(data)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
InMemoryFile::~InMemoryFile() {
|
|
|
|
}
|
|
|
|
|
2018-09-15 23:32:58 +02:00
|
|
|
fspp::num_bytes_t InMemoryFile::read(void *buf, fspp::num_bytes_t count, fspp::num_bytes_t offset) const {
|
|
|
|
fspp::num_bytes_t realCount = std::min(count, fspp::num_bytes_t(_data.size()) - offset);
|
|
|
|
std::memcpy(buf, _data.dataOffset(offset.value()), realCount.value());
|
2015-04-25 16:43:37 +02:00
|
|
|
return realCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
const void *InMemoryFile::data() const {
|
|
|
|
return _data.data();
|
|
|
|
}
|
|
|
|
|
2018-09-15 23:32:58 +02:00
|
|
|
bool InMemoryFile::fileContentEquals(const Data &expected, fspp::num_bytes_t offset) const {
|
|
|
|
return 0 == std::memcmp(expected.data(), _data.dataOffset(offset.value()), expected.size());
|
2015-04-25 16:43:37 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 23:32:58 +02:00
|
|
|
fspp::num_bytes_t InMemoryFile::size() const {
|
|
|
|
return fspp::num_bytes_t(_data.size());
|
2015-04-25 16:43:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WriteableInMemoryFile::WriteableInMemoryFile(Data data): InMemoryFile(std::move(data)), _originalData(_data.copy()) {
|
|
|
|
}
|
|
|
|
|
2018-09-15 23:32:58 +02:00
|
|
|
void WriteableInMemoryFile::write(const void *buf, fspp::num_bytes_t count, fspp::num_bytes_t offset) {
|
2015-04-25 16:43:37 +02:00
|
|
|
_extendFileSizeIfNecessary(count + offset);
|
|
|
|
|
2018-09-15 23:32:58 +02:00
|
|
|
std::memcpy(_data.dataOffset(offset.value()), buf, count.value());
|
2015-04-25 16:43:37 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 23:32:58 +02:00
|
|
|
void WriteableInMemoryFile::_extendFileSizeIfNecessary(fspp::num_bytes_t size) {
|
|
|
|
if (size > fspp::num_bytes_t(_data.size())) {
|
2015-04-25 16:43:37 +02:00
|
|
|
_extendFileSize(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 23:32:58 +02:00
|
|
|
void WriteableInMemoryFile::_extendFileSize(fspp::num_bytes_t size) {
|
|
|
|
Data newfile(size.value());
|
2015-04-25 16:43:37 +02:00
|
|
|
std::memcpy(newfile.data(), _data.data(), _data.size());
|
|
|
|
_data = std::move(newfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteableInMemoryFile::sizeUnchanged() const {
|
|
|
|
return _data.size() == _originalData.size();
|
|
|
|
}
|
|
|
|
|
2018-09-15 23:32:58 +02:00
|
|
|
bool WriteableInMemoryFile::regionUnchanged(fspp::num_bytes_t offset, fspp::num_bytes_t count) const {
|
|
|
|
return 0 == std::memcmp(_data.dataOffset(offset.value()), _originalData.dataOffset(offset.value()), count.value());
|
2015-04-25 16:43:37 +02:00
|
|
|
}
|