summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Base/ByteArray.cpp')
-rw-r--r--Swiften/Base/ByteArray.cpp47
1 files changed, 46 insertions, 1 deletions
diff --git a/Swiften/Base/ByteArray.cpp b/Swiften/Base/ByteArray.cpp
index 928e145..8c8b215 100644
--- a/Swiften/Base/ByteArray.cpp
+++ b/Swiften/Base/ByteArray.cpp
@@ -4,11 +4,15 @@
* See Documentation/Licenses/GPLv3.txt for more information.
*/
-#include "Swiften/Base/ByteArray.h"
+#include <Swiften/Base/ByteArray.h>
#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,45 @@ 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;
+}
+
+std::string ByteArray::toString() const {
+ size_t i;
+ for (i = data_.size(); i > 0; --i) {
+ if (data_[i - 1] != 0) {
+ break;
+ }
+ }
+ return i > 0 ? std::string(reinterpret_cast<const char*>(getData()), i) : "";
+}
+
}