diff options
author | kwolekr <kwolekr@minetest.net> | 2015-08-06 00:26:18 -0400 |
---|---|---|
committer | kwolekr <kwolekr@minetest.net> | 2015-10-15 01:31:31 -0400 |
commit | 1a5b4b38f3457a8f1423305cef4e85d05da47d62 (patch) | |
tree | feec7de4cb650d076311fc3848c0ef2ea1a5b7ae /src/util/serialize.cpp | |
parent | e067ceacb8787f1dc5b1ab69b50dba4497c55433 (diff) | |
download | minetest-1a5b4b38f3457a8f1423305cef4e85d05da47d62.tar.gz minetest-1a5b4b38f3457a8f1423305cef4e85d05da47d62.tar.bz2 minetest-1a5b4b38f3457a8f1423305cef4e85d05da47d62.zip |
Add BufReader and vector-based serialization methods
Diffstat (limited to 'src/util/serialize.cpp')
-rw-r--r-- | src/util/serialize.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/util/serialize.cpp b/src/util/serialize.cpp index c0168776e..ced5fc7cf 100644 --- a/src/util/serialize.cpp +++ b/src/util/serialize.cpp @@ -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 //// |