diff options
Diffstat (limited to 'Swiften/Base')
-rw-r--r-- | Swiften/Base/ByteArray.cpp | 35 | ||||
-rw-r--r-- | Swiften/Base/ByteArray.h | 10 |
2 files changed, 45 insertions, 0 deletions
diff --git a/Swiften/Base/ByteArray.cpp b/Swiften/Base/ByteArray.cpp index 928e145..c3869fc 100644 --- a/Swiften/Base/ByteArray.cpp +++ b/Swiften/Base/ByteArray.cpp @@ -9,6 +9,10 @@ #include <fstream> std::ostream& operator<<(std::ostream& os, const Swift::ByteArray& s) { + return operator<<(os, s.getDataVector()); +} + +std::ostream& operator<<(std::ostream& os, const std::vector<unsigned char>& s) { std::ios::fmtflags oldFlags = os.flags(); os << std::hex; for (Swift::ByteArray::const_iterator i = s.begin(); i != s.end(); ++i) { @@ -37,4 +41,35 @@ void ByteArray::readFromFile(const std::string& file) { input.close(); } +std::vector<unsigned char> ByteArray::create(const std::string& s) { + return std::vector<unsigned char>(s.begin(), s.end()); +} + +std::vector<unsigned char> ByteArray::create(const char* c) { + std::vector<unsigned char> data; + while (*c) { + data.push_back(static_cast<unsigned char>(*c)); + ++c; + } + return data; +} + +std::vector<unsigned char> ByteArray::create(const char* c, size_t n) { + std::vector<unsigned char> data; + if (n > 0) { + data.resize(n); + memcpy(&data[0], c, n); + } + return data; +} + +std::vector<unsigned char> ByteArray::create(const unsigned char* c, size_t n) { + std::vector<unsigned char> data; + if (n > 0) { + data.resize(n); + memcpy(&data[0], c, n); + } + return data; +} + } diff --git a/Swiften/Base/ByteArray.h b/Swiften/Base/ByteArray.h index 2059052..9e7a928 100644 --- a/Swiften/Base/ByteArray.h +++ b/Swiften/Base/ByteArray.h @@ -130,9 +130,19 @@ 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); + 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); |