diff options
Diffstat (limited to 'Swiften/Base/ByteArray.h')
-rw-r--r-- | Swiften/Base/ByteArray.h | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/Swiften/Base/ByteArray.h b/Swiften/Base/ByteArray.h index ad2c1e5..ebc22d8 100644 --- a/Swiften/Base/ByteArray.h +++ b/Swiften/Base/ByteArray.h @@ -6,11 +6,9 @@ #pragma once -#include <cstring> #include <vector> -#include <iostream> - #include <string> +#include <cstring> // for memcpy namespace Swift { class ByteArray @@ -24,7 +22,7 @@ namespace Swift { ByteArray(const char* c) { while (*c) { - data_.push_back(*c); + data_.push_back(static_cast<unsigned char>(*c)); ++c; } } @@ -75,7 +73,7 @@ namespace Swift { } void resize(size_t size, char c) { - return data_.resize(size, c); + return data_.resize(size, static_cast<unsigned char>(c)); } friend ByteArray operator+(const ByteArray& a, const ByteArray&b) { @@ -87,7 +85,7 @@ namespace Swift { friend ByteArray operator+(const ByteArray& a, char b) { ByteArray x; x.resize(1); - x[0] = b; + x[0] = static_cast<unsigned char>(b); return a + x; } @@ -97,7 +95,7 @@ namespace Swift { } ByteArray& operator+=(char c) { - data_.push_back(c); + data_.push_back(static_cast<unsigned char>(c)); return *this; } @@ -122,9 +120,7 @@ namespace Swift { return data_.end(); } - std::string toString() const { - return std::string(reinterpret_cast<const char*>(getData()), getSize()); - } + std::string toString() const; void readFromFile(const std::string& file); @@ -132,9 +128,27 @@ namespace Swift { data_.clear(); } + const std::vector<unsigned char>& getDataVector() const { + return data_; + } + + static std::vector<unsigned char> create(const std::string& s); + static std::vector<unsigned char> create(const char* c); + static std::vector<unsigned char> create(const unsigned char* c, size_t n); + static std::vector<unsigned char> create(const char* c, size_t n); + + static const unsigned char* data(const std::vector<unsigned char>& v) { + return v.empty() ? NULL : &v[0]; + } + + static const char* charData(const std::vector<unsigned char>& v) { + return v.empty() ? NULL : reinterpret_cast<const char*>(&v[0]); + } + private: std::vector<unsigned char> data_; }; } std::ostream& operator<<(std::ostream& os, const Swift::ByteArray& s); +std::ostream& operator<<(std::ostream& os, const std::vector<unsigned char>& s); |