summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Base')
-rw-r--r--Swiften/Base/ByteArray.cpp45
-rw-r--r--Swiften/Base/ByteArray.h34
-rw-r--r--Swiften/Base/IDGenerator.h5
-rw-r--r--Swiften/Base/Paths.cpp6
-rw-r--r--Swiften/Base/Paths.h2
-rw-r--r--Swiften/Base/Platform.h9
-rw-r--r--Swiften/Base/String.h1
-rw-r--r--Swiften/Base/UnitTest/ByteArrayTest.cpp29
-rw-r--r--Swiften/Base/foreach.h5
-rw-r--r--Swiften/Base/format.h1
-rw-r--r--Swiften/Base/sleep.h5
11 files changed, 111 insertions, 31 deletions
diff --git a/Swiften/Base/ByteArray.cpp b/Swiften/Base/ByteArray.cpp
index 928e145..323c866 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,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) : "";
+}
+
}
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);
diff --git a/Swiften/Base/IDGenerator.h b/Swiften/Base/IDGenerator.h
index 4b6289b..d536dcc 100644
--- a/Swiften/Base/IDGenerator.h
+++ b/Swiften/Base/IDGenerator.h
@@ -4,8 +4,7 @@
* See Documentation/Licenses/GPLv3.txt for more information.
*/
-#ifndef SWIFTEN_IDGenerator_H
-#define SWIFTEN_IDGenerator_H
+#pragma once
#include <string>
@@ -20,5 +19,3 @@ namespace Swift {
std::string currentID_;
};
}
-
-#endif
diff --git a/Swiften/Base/Paths.cpp b/Swiften/Base/Paths.cpp
index 43eee57..d901ff9 100644
--- a/Swiften/Base/Paths.cpp
+++ b/Swiften/Base/Paths.cpp
@@ -25,7 +25,7 @@ boost::filesystem::path Paths::getExecutablePath() {
uint32_t size = 4096;
path.resize(size);
if (_NSGetExecutablePath(reinterpret_cast<char*>(path.getData()), &size) == 0) {
- return boost::filesystem::path(path.toString().c_str()).parent_path();
+ return boost::filesystem::path(std::string(reinterpret_cast<const char*>(path.getData()), path.getSize()).c_str()).parent_path();
}
#elif defined(SWIFTEN_PLATFORM_LINUX)
ByteArray path;
@@ -33,13 +33,13 @@ boost::filesystem::path Paths::getExecutablePath() {
size_t size = readlink("/proc/self/exe", reinterpret_cast<char*>(path.getData()), path.getSize());
if (size > 0) {
path.resize(size);
- return boost::filesystem::path(path.toString().c_str()).parent_path();
+ return boost::filesystem::path(std::string(reinterpret_cast<const char*>(path.getData()), path.getSize()).c_str()).parent_path();
}
#elif defined(SWIFTEN_PLATFORM_WINDOWS)
ByteArray data;
data.resize(2048);
GetModuleFileName(NULL, reinterpret_cast<char*>(data.getData()), data.getSize());
- return boost::filesystem::path(data.toString().c_str()).parent_path();
+ return boost::filesystem::path(std::string(reinterpret_cast<const char*>(data.getData()), data.getSize()).c_str()).parent_path();
#endif
return boost::filesystem::path();
}
diff --git a/Swiften/Base/Paths.h b/Swiften/Base/Paths.h
index 06c6aeb..8ac4640 100644
--- a/Swiften/Base/Paths.h
+++ b/Swiften/Base/Paths.h
@@ -6,7 +6,7 @@
#pragma once
-#include <boost/filesystem.hpp>
+#include <boost/filesystem/path.hpp>
namespace Swift {
class Paths {
diff --git a/Swiften/Base/Platform.h b/Swiften/Base/Platform.h
index 32e2671..395747c 100644
--- a/Swiften/Base/Platform.h
+++ b/Swiften/Base/Platform.h
@@ -4,8 +4,7 @@
* See Documentation/Licenses/GPLv3.txt for more information.
*/
-#ifndef SWIFTEN_Platform_H
-#define SWIFTEN_Platform_H
+#pragma once
// Base platforms
#if defined(linux) || defined(__linux) || defined(__linux__)
@@ -26,6 +25,10 @@
#define SWIFTEN_PLATFORM_BEOS
#elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
#define SWIFTEN_PLATFORM_MACOSX
+#include <TargetConditionals.h>
+# if defined(TARGET_OS_IPHONE)
+# define SWIFTEN_PLATFORM_IPHONE
+# endif
#elif defined(__IBMCPP__) || defined(_AIX)
#define SWIFTEN_PLATFORM_AIX
#elif defined(__amigaos__)
@@ -46,5 +49,3 @@
#elif defined(BOOST_BIG_ENDIAN)
#define SWIFTEN_BIG_ENDIAN
#endif
-
-#endif
diff --git a/Swiften/Base/String.h b/Swiften/Base/String.h
index 192d53b..0de6cac 100644
--- a/Swiften/Base/String.h
+++ b/Swiften/Base/String.h
@@ -6,7 +6,6 @@
#pragma once
-#include <map>
#include <string>
#include <vector>
diff --git a/Swiften/Base/UnitTest/ByteArrayTest.cpp b/Swiften/Base/UnitTest/ByteArrayTest.cpp
index cb10dd4..069e68e 100644
--- a/Swiften/Base/UnitTest/ByteArrayTest.cpp
+++ b/Swiften/Base/UnitTest/ByteArrayTest.cpp
@@ -8,12 +8,17 @@
#include <cppunit/extensions/TestFactoryRegistry.h>
#include "Swiften/Base/ByteArray.h"
+#include <boost/lexical_cast.hpp>
using namespace Swift;
class ByteArrayTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(ByteArrayTest);
CPPUNIT_TEST(testGetData_NoData);
+ CPPUNIT_TEST(testToString);
+ CPPUNIT_TEST(testToString_NullTerminated);
+ CPPUNIT_TEST(testToString_TwoNullTerminated);
+ CPPUNIT_TEST(testToString_AllNull);
CPPUNIT_TEST_SUITE_END();
public:
@@ -22,6 +27,30 @@ class ByteArrayTest : public CppUnit::TestFixture {
CPPUNIT_ASSERT_EQUAL(reinterpret_cast<const char*>(NULL), reinterpret_cast<const char*>(testling.getData()));
}
+
+ void testToString() {
+ ByteArray testling(ByteArray::create("abcde"));
+
+ CPPUNIT_ASSERT_EQUAL(std::string("abcde"), testling.toString());
+ }
+
+ void testToString_NullTerminated() {
+ ByteArray testling(ByteArray::create("abcde\0", 6));
+
+ CPPUNIT_ASSERT_EQUAL(std::string("abcde"), testling.toString());
+ }
+
+ void testToString_TwoNullTerminated() {
+ ByteArray testling(ByteArray::create("abcde\0\0", 7));
+
+ CPPUNIT_ASSERT_EQUAL(std::string("abcde"), testling.toString());
+ }
+
+ void testToString_AllNull() {
+ ByteArray testling(ByteArray::create("\0\0", 2));
+
+ CPPUNIT_ASSERT_EQUAL(std::string(""), testling.toString());
+ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(ByteArrayTest);
diff --git a/Swiften/Base/foreach.h b/Swiften/Base/foreach.h
index 05366d6..87f6147 100644
--- a/Swiften/Base/foreach.h
+++ b/Swiften/Base/foreach.h
@@ -4,12 +4,9 @@
* See Documentation/Licenses/GPLv3.txt for more information.
*/
-#ifndef SWIFTEN_FOREACH_H
-#define SWIFTEN_FOREACH_H
+#pragma once
#include <boost/foreach.hpp>
#undef foreach
#define foreach BOOST_FOREACH
-
-#endif
diff --git a/Swiften/Base/format.h b/Swiften/Base/format.h
index 4591827..0e49eaa 100644
--- a/Swiften/Base/format.h
+++ b/Swiften/Base/format.h
@@ -7,6 +7,7 @@
#pragma once
#include <boost/format.hpp>
+#include <iostream>
namespace Swift {
inline boost::format format(const std::string& s) {
diff --git a/Swiften/Base/sleep.h b/Swiften/Base/sleep.h
index c2bc601..a95e907 100644
--- a/Swiften/Base/sleep.h
+++ b/Swiften/Base/sleep.h
@@ -4,11 +4,8 @@
* See Documentation/Licenses/GPLv3.txt for more information.
*/
-#ifndef SWIFTEN_sleep_H
-#define SWIFTEN_sleep_H
+#pragma once
namespace Swift {
void sleep(unsigned int msecs);
}
-
-#endif