1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-22 17:18:39 +00:00

Add BufReader and vector-based serialization methods

This commit is contained in:
kwolekr 2015-08-06 00:26:18 -04:00
parent e067ceacb8
commit 1a5b4b38f3
3 changed files with 573 additions and 2 deletions

View file

@ -28,6 +28,77 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <iomanip>
#include <vector>
SerializationError eof_ser_err("Attempted read past end of data");
////
//// BufReader
////
bool BufReader::getStringNoEx(std::string *val)
{
u16 num_chars;
if (!getU16NoEx(&num_chars))
return false;
if (pos + num_chars > size) {
pos -= sizeof(num_chars);
return false;
}
val->assign((const char *)data + pos, num_chars);
pos += num_chars;
return true;
}
bool BufReader::getWideStringNoEx(std::wstring *val)
{
u16 num_chars;
if (!getU16NoEx(&num_chars))
return false;
if (pos + num_chars * 2 > size) {
pos -= sizeof(num_chars);
return false;
}
for (size_t i = 0; i != num_chars; i++) {
val->push_back(readU16(data + pos));
pos += 2;
}
return true;
}
bool BufReader::getLongStringNoEx(std::string *val)
{
u32 num_chars;
if (!getU32NoEx(&num_chars))
return false;
if (pos + num_chars > size) {
pos -= sizeof(num_chars);
return false;
}
val->assign((const char *)data + pos, num_chars);
pos += num_chars;
return true;
}
bool BufReader::getRawDataNoEx(void *val, size_t len)
{
if (pos + len > size)
return false;
memcpy(val, data + pos, len);
pos += len;
return true;
}
////
//// String
////