Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
ByteBuffer.cpp
Go to the documentation of this file.
1
2
4
6
8
10
11namespace Divide {
12
13ByteBufferException::ByteBufferException(const bool add, const size_t pos, const size_t esize, const size_t size)
14 : _add(add), _pos(pos), _esize(esize), _size(size)
15{
17}
18
20
21 Console::errorfn(LOCALE_STR("BYTE_BUFFER_ERROR"),
22 _add ? "append" : "read",
23 _pos,
24 _esize,
25 _size);
26}
27
28void ByteBuffer::clear() noexcept {
29 _storage.clear();
30 _rpos = _wpos = 0u;
31}
32
33void ByteBuffer::append(const Byte *src, const size_t cnt) {
34 if (src != nullptr && cnt > 0) {
35 _storage.reserve(DEFAULT_SIZE);
36
37 if (_storage.size() < _wpos + cnt) {
38 _storage.resize(_wpos + cnt);
39 }
40
41 memcpy(&_storage[_wpos], src, cnt);
42 _wpos += cnt;
43 }
44}
45
46
47bool ByteBuffer::dumpToFile(const ResourcePath& path, const std::string_view fileName, const U8 version)
48{
49 if (!_storage.empty() && to_U8(_storage.back()) != version)
50 {
51 append(version);
52 }
53
54 return writeFile(path, fileName, _storage.data(), _storage.size(), FileType::BINARY) == FileError::NONE;
55}
56
57bool ByteBuffer::loadFromFile(const ResourcePath& path, const std::string_view fileName, const U8 version)
58{
59 clear();
60
61 std::ifstream data;
62 if (readFile(path, fileName, FileType::BINARY, data) == FileError::NONE)
63 {
64 data.seekg(0, std::ios::end);
65 const size_t fileSize = to_size(data.tellg());
66 data.seekg(0);
67 _storage.resize(fileSize);
68 data.read(reinterpret_cast<char*>(_storage.data()), fileSize);
69 _wpos = fileSize;
70 return version == 0u || to_U8(_storage.back()) == version;
71 }
72
73 return false;
74}
75
76} // namespace Divide
#define LOCALE_STR(X)
Definition: Localization.h:91
ByteBufferException(bool add, size_t pos, size_t esize, size_t size)
Definition: ByteBuffer.cpp:13
void append(const Byte *src, size_t cnt)
Appends 'cnt' bytes from 'src' to the buffer.
Definition: ByteBuffer.cpp:33
bool loadFromFile(const ResourcePath &path, std::string_view fileName, const U8 version=BUFFER_FORMAT_VERSION)
Definition: ByteBuffer.cpp:57
void clear() noexcept
Resets the entire storage and the read and write positions.
Definition: ByteBuffer.cpp:28
vector< Byte > _storage
Definition: ByteBuffer.h:193
static const size_t DEFAULT_SIZE
Definition: ByteBuffer.h:77
bool dumpToFile(const ResourcePath &path, std::string_view fileName, const U8 version=BUFFER_FORMAT_VERSION)
Saves the entire buffer contents to file. Always appends the version at the end of the file.
Definition: ByteBuffer.cpp:47
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
std::byte Byte
FileError writeFile(const ResourcePath &filePath, const std::string_view fileName, const char *content, const size_t length, const FileType fileType)
FileError readFile(const ResourcePath &filePath, std::string_view fileName, FileType fileType, std::ifstream &sreamOut)
uint8_t U8
constexpr resolve_uac< A, B >::return_type add(const A &a, const B &b) noexcept
constexpr U8 to_U8(const T value)
constexpr size_t to_size(const T value)
static NO_INLINE void errorfn(const char *format, T &&... args)