summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Base/ByteArray.h')
-rw-r--r--Swiften/Base/ByteArray.h151
1 files changed, 18 insertions, 133 deletions
diff --git a/Swiften/Base/ByteArray.h b/Swiften/Base/ByteArray.h
index ebc22d8..8ef8dd6 100644
--- a/Swiften/Base/ByteArray.h
+++ b/Swiften/Base/ByteArray.h
@@ -8,147 +8,32 @@
#include <vector>
#include <string>
-#include <cstring> // for memcpy
namespace Swift {
- class ByteArray
- {
- public:
- typedef std::vector<unsigned char>::const_iterator const_iterator;
+ typedef std::vector<unsigned char> ByteArray;
- ByteArray() : data_() {}
+ ByteArray createByteArray(const unsigned char* c, size_t n);
+ ByteArray createByteArray(const std::string& s);
+ ByteArray createByteArray(const char* c);
+ ByteArray createByteArray(const char* c, size_t n);
- ByteArray(const std::string& s) : data_(s.begin(), s.end()) {}
+ inline ByteArray createByteArray(char c) {
+ return std::vector<unsigned char>(1, c);
+ }
- ByteArray(const char* c) {
- while (*c) {
- data_.push_back(static_cast<unsigned char>(*c));
- ++c;
- }
- }
- ByteArray(const char* c, size_t n) {
- if (n > 0) {
- data_.resize(n);
- memcpy(&data_[0], c, n);
- }
- }
+ template<typename T, typename A>
+ static const T* vecptr(const std::vector<T, A>& v) {
+ return v.empty() ? NULL : &v[0];
+ }
- ByteArray(const unsigned char* c, size_t n) {
- if (n > 0) {
- data_.resize(n);
- memcpy(&data_[0], c, n);
- }
- }
+ template<typename T, typename A>
+ static T* vecptr(std::vector<T, A>& v) {
+ return v.empty() ? NULL : &v[0];
+ }
- ByteArray(const std::vector<unsigned char>& data) : data_(data) {
- }
+ std::string byteArrayToString(const ByteArray& b);
- const unsigned char* getData() const {
- return data_.empty() ? NULL : &data_[0];
- }
-
- unsigned char* getData() {
- return data_.empty() ? NULL : &data_[0];
- }
-
- const std::vector<unsigned char>& getVector() const {
- return data_;
- }
-
- std::vector<unsigned char>& getVector() {
- return data_;
- }
-
- size_t getSize() const {
- return data_.size();
- }
-
- bool isEmpty() const {
- return data_.empty();
- }
-
- void resize(size_t size) {
- return data_.resize(size);
- }
-
- void resize(size_t size, char c) {
- return data_.resize(size, static_cast<unsigned char>(c));
- }
-
- friend ByteArray operator+(const ByteArray& a, const ByteArray&b) {
- ByteArray result(a);
- result.data_.insert(result.data_.end(), b.data_.begin(), b.data_.end());
- return result;
- }
-
- friend ByteArray operator+(const ByteArray& a, char b) {
- ByteArray x;
- x.resize(1);
- x[0] = static_cast<unsigned char>(b);
- return a + x;
- }
-
- ByteArray& operator+=(const ByteArray& b) {
- data_.insert(data_.end(), b.data_.begin(), b.data_.end());
- return *this;
- }
-
- ByteArray& operator+=(char c) {
- data_.push_back(static_cast<unsigned char>(c));
- return *this;
- }
-
- friend bool operator==(const ByteArray& a, const ByteArray& b) {
- return a.data_ == b.data_;
- }
-
-
- const unsigned char& operator[](size_t i) const {
- return data_[i];
- }
-
- unsigned char& operator[](size_t i) {
- return data_[i];
- }
-
- const_iterator begin() const {
- return data_.begin();
- }
-
- const_iterator end() const {
- return data_.end();
- }
-
- std::string toString() const;
-
- void readFromFile(const std::string& file);
-
- void clear() {
- 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_;
- };
+ void readByteArrayFromFile(ByteArray&, const std::string& file);
}
-std::ostream& operator<<(std::ostream& os, const Swift::ByteArray& s);
-std::ostream& operator<<(std::ostream& os, const std::vector<unsigned char>& s);