summaryrefslogtreecommitdiffstats
blob: bcc3756909d5b8f3a4f3b3b0dca031d398212181 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef SWIFTEN_BYTEARRAY_H
#define SWIFTEN_BYTEARRAY_H

#include <cstring>
#include <vector>
#include <iostream>

#include "Swiften/Base/String.h"

namespace Swift {
	class ByteArray
	{
		public:
			typedef std::vector<char>::const_iterator const_iterator;

			ByteArray() : data_() {}

			ByteArray(const String& s) : data_(s.getUTF8String().begin(), s.getUTF8String().end()) {}

			ByteArray(const char* c) {
				while (*c) {
					data_.push_back(*c);
					++c;
				}
			}

			ByteArray(const char* c, size_t n) {
				data_.resize(n);
				memcpy(&data_[0], c, n);
			}

			const char* getData() const {
				return &data_[0];
			}

			char* getData() {
				return &data_[0];
			}

			size_t getSize() const {
				return data_.size();
			}

			bool isEmpty() const {
				return data_.empty();
			}

			void resize(size_t size) {
				return data_.resize(size);
			}

			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;
			}

			ByteArray& operator+=(const ByteArray& b) {
				data_.insert(data_.end(), b.data_.begin(), b.data_.end());
				return *this;
			}

			friend bool operator==(const ByteArray& a, const ByteArray& b) {
				return a.data_ == b.data_;
			}


			const char& operator[](size_t i) const {
				return data_[i];
			}

			char& operator[](size_t i) {
				return data_[i];
			}

			const_iterator begin() const { 
				return data_.begin(); 
			}
		
			const_iterator end() const { 
				return data_.end(); 
			}

			String toString() const {
				return String(getData(), getSize());
			}

			void readFromFile(const String& file);

		private:
			std::vector<char> data_;
	};
}

std::ostream& operator<<(std::ostream& os, const Swift::ByteArray& s);

#endif