Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Mathias Pedersen 2019 */
- #pragma once
- #include <iostream>
- #include <vector>
- #include <algorithm>
- class VectorStream
- {
- std::vector<char> data;
- size_t offset = 0;
- public:
- void write(char* buffer, size_t len)
- {
- for (size_t i = 0; i < len; i++)
- data.emplace_back(buffer[i]);
- offset += len;
- }
- void writeByte(char value)
- {
- write(&value, sizeof(value));
- }
- void writeInt(unsigned int value)
- {
- write(reinterpret_cast<char*>(&value), sizeof(value));
- }
- void writeDouble(double value)
- {
- write(reinterpret_cast<char*>(&value), sizeof(value));
- }
- void writeString(const std::string& value)
- {
- writeCompressedInt(value.length());
- for (size_t i = 0; i < value.length(); i++)
- writeByte(value[i]);
- }
- void writeCompressedInt(unsigned int value)
- {
- do
- {
- int v = value & 0x7F;
- value >>= 7;
- if (value)
- v |= 0x80;
- writeByte(v);
- } while (value);
- }
- std::string str()
- {
- std::string data(data.begin(), data.end());
- return data;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement