summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Base')
-rw-r--r--Swiften/Base/API.h42
-rw-r--r--Swiften/Base/Algorithm.h5
-rw-r--r--Swiften/Base/BoostFilesystemVersion.h10
-rw-r--r--Swiften/Base/BoostRandomGenerator.h3
-rw-r--r--Swiften/Base/ByteArray.cpp11
-rw-r--r--Swiften/Base/ByteArray.h15
-rw-r--r--Swiften/Base/DateTime.cpp5
-rw-r--r--Swiften/Base/DateTime.h10
-rw-r--r--Swiften/Base/Error.h10
-rw-r--r--Swiften/Base/FileSize.cpp23
-rw-r--r--Swiften/Base/FileSize.h17
-rw-r--r--Swiften/Base/IDGenerator.h4
-rw-r--r--Swiften/Base/Listenable.h52
-rw-r--r--Swiften/Base/Log.cpp34
-rw-r--r--Swiften/Base/Log.h42
-rw-r--r--Swiften/Base/Override.h34
-rw-r--r--Swiften/Base/Path.cpp28
-rw-r--r--Swiften/Base/Path.h27
-rw-r--r--Swiften/Base/Paths.cpp9
-rw-r--r--Swiften/Base/Paths.h4
-rw-r--r--Swiften/Base/RandomGenerator.h7
-rw-r--r--Swiften/Base/Regex.cpp37
-rw-r--r--Swiften/Base/Regex.h19
-rw-r--r--Swiften/Base/SConscript5
-rw-r--r--Swiften/Base/SafeAllocator.cpp27
-rw-r--r--Swiften/Base/SafeAllocator.h20
-rw-r--r--Swiften/Base/SafeByteArray.h7
-rw-r--r--Swiften/Base/SimpleIDGenerator.h4
-rw-r--r--Swiften/Base/String.cpp57
-rw-r--r--Swiften/Base/String.h24
-rw-r--r--Swiften/Base/URL.cpp189
-rw-r--r--Swiften/Base/URL.h26
-rw-r--r--Swiften/Base/UnitTest/PathTest.cpp36
-rw-r--r--Swiften/Base/UnitTest/StringTest.cpp15
-rw-r--r--Swiften/Base/UnitTest/URLTest.cpp114
-rw-r--r--Swiften/Base/foreach.h1
-rw-r--r--Swiften/Base/sleep.cpp5
-rw-r--r--Swiften/Base/sleep.h4
38 files changed, 918 insertions, 64 deletions
diff --git a/Swiften/Base/API.h b/Swiften/Base/API.h
new file mode 100644
index 0000000..0913181
--- /dev/null
+++ b/Swiften/Base/API.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2012-2014 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <Swiften/Base/Platform.h>
+#include <boost/config.hpp>
+
+#ifdef SWIFTEN_STATIC
+# define SWIFTEN_API
+#else
+# ifdef SWIFTEN_PLATFORM_WINDOWS
+# ifdef SWIFTEN_BUILDING
+# define SWIFTEN_API __declspec(dllexport)
+# else
+# define SWIFTEN_API __declspec(dllimport)
+# endif
+# elif __GNUC__ >= 4
+# define SWIFTEN_API __attribute__((visibility("default")))
+# else
+# define SWIFTEN_API
+# endif
+#endif
+
+#ifdef BOOST_NO_DEFAULTED_FUNCTIONS
+# define SWIFTEN_DEFAULT_COPY_CONSTRUCTOR(cls)
+# define SWIFTEN_DEFAULT_COPY_ASSIGMNENT_OPERATOR(cls)
+#else
+# define SWIFTEN_DEFAULT_COPY_CONSTRUCTOR(cls) \
+ cls(const cls&) = default;
+# define SWIFTEN_DEFAULT_COPY_ASSIGMNENT_OPERATOR(cls) \
+ cls& operator=(const cls&) = default;
+#endif
+
+#ifdef BOOST_NO_NOEXCEPT
+#define SWIFTEN_NOEXCEPT throw()
+#else
+#define SWIFTEN_NOEXCEPT noexcept
+#endif
diff --git a/Swiften/Base/Algorithm.h b/Swiften/Base/Algorithm.h
index 4e68e70..d5edab1 100644
--- a/Swiften/Base/Algorithm.h
+++ b/Swiften/Base/Algorithm.h
@@ -100,4 +100,9 @@ namespace Swift {
}
+ template<typename Source, typename Target>
+ void assign(Target& target, const Source& source) {
+ target.assign(source.begin(), source.end());
+ }
+
template<typename A, typename B, typename C, typename D>
B get(const std::map<A, B, C, D>& map, const A& key, const B& defaultValue) {
diff --git a/Swiften/Base/BoostFilesystemVersion.h b/Swiften/Base/BoostFilesystemVersion.h
new file mode 100644
index 0000000..1b1676c
--- /dev/null
+++ b/Swiften/Base/BoostFilesystemVersion.h
@@ -0,0 +1,10 @@
+/*
+ * Copyright (c) 2010-2012 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+#pragma once
+
+#ifndef BOOST_FILESYSTEM_VERSION
+#define BOOST_FILESYSTEM_VERSION 2
+#endif
diff --git a/Swiften/Base/BoostRandomGenerator.h b/Swiften/Base/BoostRandomGenerator.h
index b5a6cac..6065ff3 100644
--- a/Swiften/Base/BoostRandomGenerator.h
+++ b/Swiften/Base/BoostRandomGenerator.h
@@ -8,4 +8,5 @@
#include <Swiften/Base/RandomGenerator.h>
+#include <Swiften/Base/Override.h>
#include <boost/random/mersenne_twister.hpp>
@@ -16,5 +17,5 @@ namespace Swift {
BoostRandomGenerator();
- int generateRandomInteger(int max);
+ int generateRandomInteger(int max) SWIFTEN_OVERRIDE;
private:
diff --git a/Swiften/Base/ByteArray.cpp b/Swiften/Base/ByteArray.cpp
index 6be96aa..466db5f 100644
--- a/Swiften/Base/ByteArray.cpp
+++ b/Swiften/Base/ByteArray.cpp
@@ -1,4 +1,4 @@
/*
- * Copyright (c) 2010 Remko Tronçon
+ * Copyright (c) 2010-2013 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
@@ -7,5 +7,6 @@
#include <Swiften/Base/ByteArray.h>
-#include <fstream>
+#include <boost/numeric/conversion/cast.hpp>
+#include <boost/filesystem/fstream.hpp>
namespace Swift {
@@ -13,11 +14,11 @@ namespace Swift {
static const int BUFFER_SIZE = 4096;
-void readByteArrayFromFile(ByteArray& data, const std::string& file) {
- std::ifstream input(file.c_str(), std::ios_base::in|std::ios_base::binary);
+void readByteArrayFromFile(ByteArray& data, const boost::filesystem::path& file) {
+ boost::filesystem::ifstream input(file, std::ios_base::in|std::ios_base::binary);
while (input.good()) {
size_t oldSize = data.size();
data.resize(oldSize + BUFFER_SIZE);
input.read(reinterpret_cast<char*>(&data[oldSize]), BUFFER_SIZE);
- data.resize(oldSize + input.gcount());
+ data.resize(oldSize + boost::numeric_cast<size_t>(input.gcount()));
}
input.close();
diff --git a/Swiften/Base/ByteArray.h b/Swiften/Base/ByteArray.h
index 01cd5d0..133b75f 100644
--- a/Swiften/Base/ByteArray.h
+++ b/Swiften/Base/ByteArray.h
@@ -1,4 +1,4 @@
/*
- * Copyright (c) 2010 Remko Tronçon
+ * Copyright (c) 2010-2013 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
@@ -10,9 +10,12 @@
#include <string>
+#include <Swiften/Base/API.h>
+#include <boost/filesystem/path.hpp>
+
namespace Swift {
typedef std::vector<unsigned char> ByteArray;
- ByteArray createByteArray(const std::string& s);
- ByteArray createByteArray(const char* c);
+ SWIFTEN_API ByteArray createByteArray(const std::string& s);
+ SWIFTEN_API ByteArray createByteArray(const char* c);
inline ByteArray createByteArray(const unsigned char* c, size_t n) {
@@ -25,5 +28,5 @@ namespace Swift {
inline ByteArray createByteArray(char c) {
- return std::vector<unsigned char>(1, c);
+ return std::vector<unsigned char>(1, static_cast<unsigned char>(c));
}
@@ -38,7 +41,7 @@ namespace Swift {
}
- std::string byteArrayToString(const ByteArray& b);
+ SWIFTEN_API std::string byteArrayToString(const ByteArray& b);
- void readByteArrayFromFile(ByteArray&, const std::string& file);
+ SWIFTEN_API void readByteArrayFromFile(ByteArray&, const boost::filesystem::path& file);
}
diff --git a/Swiften/Base/DateTime.cpp b/Swiften/Base/DateTime.cpp
index fae26d4..5d192c4 100644
--- a/Swiften/Base/DateTime.cpp
+++ b/Swiften/Base/DateTime.cpp
@@ -11,4 +11,5 @@
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
+#include <boost/date_time/c_local_time_adjustor.hpp>
#include <Swiften/Base/String.h>
@@ -32,3 +33,7 @@ std::string dateTimeToString(const boost::posix_time::ptime& time) {
}
+std::string dateTimeToLocalString(const boost::posix_time::ptime& time) {
+ return boost::posix_time::to_simple_string(boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local(time));
+}
+
}
diff --git a/Swiften/Base/DateTime.h b/Swiften/Base/DateTime.h
index 2407ddc..891b170 100644
--- a/Swiften/Base/DateTime.h
+++ b/Swiften/Base/DateTime.h
@@ -7,4 +7,5 @@
#pragma once
+#include <Swiften/Base/API.h>
#include <boost/date_time/posix_time/ptime.hpp>
@@ -14,9 +15,14 @@ namespace Swift {
* object (in UTC).
*/
- boost::posix_time::ptime stringToDateTime(const std::string& string);
+ SWIFTEN_API boost::posix_time::ptime stringToDateTime(const std::string& string);
/**
* Converts a UTC ptime object to a XEP-0082 formatted string.
*/
- std::string dateTimeToString(const boost::posix_time::ptime& time);
+ SWIFTEN_API std::string dateTimeToString(const boost::posix_time::ptime& time);
+
+ /**
+ * Converts a UTC ptime object to a localized human readable string.
+ */
+ SWIFTEN_API std::string dateTimeToLocalString(const boost::posix_time::ptime& time);
}
diff --git a/Swiften/Base/Error.h b/Swiften/Base/Error.h
index e99f175..00287fc 100644
--- a/Swiften/Base/Error.h
+++ b/Swiften/Base/Error.h
@@ -1,4 +1,4 @@
/*
- * Copyright (c) 2010 Remko Tronçon
+ * Copyright (c) 2010-2014 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
@@ -7,8 +7,12 @@
#pragma once
+#include <Swiften/Base/API.h>
+
namespace Swift {
- class Error {
+ class SWIFTEN_API Error {
public:
+ Error() {}
+ SWIFTEN_DEFAULT_COPY_CONSTRUCTOR(Error)
virtual ~Error();
};
-};
+}
diff --git a/Swiften/Base/FileSize.cpp b/Swiften/Base/FileSize.cpp
new file mode 100644
index 0000000..90fdc9a
--- /dev/null
+++ b/Swiften/Base/FileSize.cpp
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2010-2014 Kevin Smith and Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <Swiften/Base/FileSize.h>
+#include <boost/format.hpp>
+
+namespace Swift {
+
+std::string formatSize(const boost::uintmax_t bytes) {
+ static const char *siPrefix[] = {"k", "M", "G", "T", "P", "E", "Z", "Y", NULL};
+ int power = 0;
+ double engBytes = bytes;
+ while (engBytes >= 1000) {
+ ++power;
+ engBytes = engBytes / 1000.0;
+ }
+ return str( boost::format("%.1lf %sB") % engBytes % (power > 0 ? siPrefix[power-1] : "") );
+}
+
+}
diff --git a/Swiften/Base/FileSize.h b/Swiften/Base/FileSize.h
new file mode 100644
index 0000000..c9ed5fe
--- /dev/null
+++ b/Swiften/Base/FileSize.h
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2010-2014 Kevin Smith and Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <Swiften/Base/API.h>
+#include <boost/cstdint.hpp>
+#include <string>
+
+namespace Swift {
+
+SWIFTEN_API std::string formatSize(const boost::uintmax_t bytes);
+
+}
diff --git a/Swiften/Base/IDGenerator.h b/Swiften/Base/IDGenerator.h
index 44eeb76..14ecfdc 100644
--- a/Swiften/Base/IDGenerator.h
+++ b/Swiften/Base/IDGenerator.h
@@ -9,6 +9,8 @@
#include <string>
+#include <Swiften/Base/API.h>
+
namespace Swift {
- class IDGenerator {
+ class SWIFTEN_API IDGenerator {
public:
IDGenerator();
diff --git a/Swiften/Base/Listenable.h b/Swiften/Base/Listenable.h
new file mode 100644
index 0000000..445dfd7
--- /dev/null
+++ b/Swiften/Base/Listenable.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2013 Remko Tronçon
+ * Licensed under the GNU General Public License.
+ * See the COPYING file for more information.
+ */
+
+#pragma once
+
+#include <Swiften/Base/API.h>
+#include <boost/bind.hpp>
+#include <algorithm>
+
+namespace Swift {
+ template<typename T>
+ class SWIFTEN_API Listenable {
+ public:
+ void addListener(T* listener) {
+ listeners.push_back(listener);
+ }
+
+ void removeListener(T* listener) {
+ listeners.erase(std::remove(listeners.begin(), listeners.end(), listener), listeners.end());
+ }
+
+ protected:
+ template<typename F>
+ void notifyListeners(F event) {
+ for (typename std::vector<T*>::iterator i = listeners.begin(); i != listeners.end(); ++i) {
+ event(*i);
+ }
+ }
+
+ template<typename F, typename A1>
+ void notifyListeners(F f, const A1& a1) {
+ notifyListeners(boost::bind(f, _1, a1));
+ }
+
+ template<typename F, typename A1, typename A2>
+ void notifyListeners(F f, const A1& a1, const A2& a2) {
+ notifyListeners(boost::bind(f, _1, a1, a2));
+ }
+
+ template<typename F, typename A1, typename A2, typename A3>
+ void notifyListeners(F f, const A1& a1, const A2& a2, const A3& a3) {
+ notifyListeners(boost::bind(f, _1, a1, a2, a3));
+ }
+
+ private:
+ std::vector<T*> listeners;
+ };
+}
+
diff --git a/Swiften/Base/Log.cpp b/Swiften/Base/Log.cpp
index 4132353..317798c 100644
--- a/Swiften/Base/Log.cpp
+++ b/Swiften/Base/Log.cpp
@@ -1,4 +1,4 @@
/*
- * Copyright (c) 2010 Remko Tronçon
+ * Copyright (c) 2010-2013 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
@@ -7,7 +7,37 @@
#include <Swiften/Base/Log.h>
+#include <cstdio>
+
+
namespace Swift {
-bool logging = false;
+static Log::Severity logLevel = Log::warning;
+
+Log::Log() {
+}
+
+Log::~Log() {
+ // Using stdio for thread safety (POSIX file i/o calls are guaranteed to be atomic)
+ fprintf(stderr, "%s", stream.str().c_str());
+ fflush(stderr);
+}
+
+std::ostringstream& Log::getStream(
+ Severity /*severity*/,
+ const std::string& severityString,
+ const std::string& file,
+ int line,
+ const std::string& function) {
+ stream << "[" << severityString << "] " << file << ":" << line << " " << function << ": ";
+ return stream;
+}
+
+Log::Severity Log::getLogLevel() {
+ return logLevel;
+}
+
+void Log::setLogLevel(Severity level) {
+ logLevel = level;
+}
}
diff --git a/Swiften/Base/Log.h b/Swiften/Base/Log.h
index 06f5b55..a46860f 100644
--- a/Swiften/Base/Log.h
+++ b/Swiften/Base/Log.h
@@ -1,4 +1,4 @@
/*
- * Copyright (c) 2010-2012 Remko Tronçon
+ * Copyright (c) 2010-2013 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
@@ -7,18 +7,38 @@
#pragma once
-#include <iostream>
+#include <sstream>
+
+#include <Swiften/Base/API.h>
namespace Swift {
- extern bool logging;
- namespace LogDetail {
- // Only here to be able to statically check the correctness of the severity levers
- namespace Severity {
- enum {
- debug, info, warning, error
+ class SWIFTEN_API Log {
+ public:
+ enum Severity {
+ error, warning, info, debug
+ };
+
+ Log();
+ ~Log();
+
+ std::ostringstream& getStream(
+ Severity severity,
+ const std::string& severityString,
+ const std::string& file,
+ int line,
+ const std::string& function);
+
+ static Severity getLogLevel();
+ static void setLogLevel(Severity level);
+
+ private:
+ std::ostringstream stream;
};
- }
- }
}
#define SWIFT_LOG(severity) \
- if (!Swift::logging) {} else (void) LogDetail::Severity::severity, std::cerr << "[" << #severity << "] " << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << ": "
+ if (Log::severity > Log::getLogLevel()) ; \
+ else Log().getStream(Log::severity, #severity, __FILE__, __LINE__, __FUNCTION__)
+
+#define SWIFT_LOG_ASSERT(test, severity) \
+ if (Log::severity > Log::getLogLevel() || (test)) ; \
+ else Log().getStream(Log::severity, #severity, __FILE__, __LINE__, __FUNCTION__) << "Assertion failed: " << #test << ". "
diff --git a/Swiften/Base/Override.h b/Swiften/Base/Override.h
new file mode 100644
index 0000000..6d9baaa
--- /dev/null
+++ b/Swiften/Base/Override.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2012 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#if defined(__clang__)
+# if __has_feature(cxx_override_control) || __has_extension(cxx_override_control)
+# define SWIFTEN_OVERRIDE override
+# else
+# define SWIFTEN_OVERRIDE
+# endif
+
+#elif defined(__GNUC__)
+# if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7))) && defined(__GXX_EXPERIMENTAL_CXX0X__)
+# define SWIFTEN_OVERRIDE override
+# else
+# define SWIFTEN_OVERRIDE
+# endif
+
+#elif defined(_MSC_VER)
+// Actually, 1700 is the first version that supports the C++11 override, but
+// older versions apparently support a similar keyword.
+# if _MSC_VER >= 1400
+# define SWIFTEN_OVERRIDE override
+# else
+# define SWIFTEN_OVERRIDE
+# endif
+
+#else
+# define SWIFTEN_OVERRIDE
+#endif
diff --git a/Swiften/Base/Path.cpp b/Swiften/Base/Path.cpp
new file mode 100644
index 0000000..2a49676
--- /dev/null
+++ b/Swiften/Base/Path.cpp
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2013 Remko Tronçon
+ * Licensed under the GNU General Public License.
+ * See the COPYING file for more information.
+ */
+
+#include <Swiften/Base/Path.h>
+
+#include <Swiften/Base/Platform.h>
+#include <Swiften/Base/String.h>
+
+using namespace Swift;
+
+boost::filesystem::path Swift::stringToPath(const std::string& path) {
+#ifdef SWIFTEN_PLATFORM_WINDOWS
+ return boost::filesystem::path(convertStringToWString(path));
+#else
+ return boost::filesystem::path(path);
+#endif
+}
+
+std::string Swift::pathToString(const boost::filesystem::path& path) {
+#ifdef SWIFTEN_PLATFORM_WINDOWS
+ return convertWStringToString(path.native());
+#else
+ return path.native();
+#endif
+}
diff --git a/Swiften/Base/Path.h b/Swiften/Base/Path.h
new file mode 100644
index 0000000..ea99be9
--- /dev/null
+++ b/Swiften/Base/Path.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2013 Remko Tronçon
+ * Licensed under the GNU General Public License.
+ * See the COPYING file for more information.
+ */
+
+#pragma once
+
+#include <Swiften/Base/API.h>
+#include <boost/filesystem/path.hpp>
+#include <string>
+
+namespace Swift {
+ /**
+ * Creates a path for the given UTF-8 encoded string.
+ * This works independently of global locale settings.
+ */
+ SWIFTEN_API boost::filesystem::path stringToPath(const std::string&);
+
+ /**
+ * Returns the UTF-8 representation of the given path
+ * This works independently of global locale settings.
+ */
+ SWIFTEN_API std::string pathToString(const boost::filesystem::path&);
+}
+
+
diff --git a/Swiften/Base/Paths.cpp b/Swiften/Base/Paths.cpp
index edebc30..8ad1159 100644
--- a/Swiften/Base/Paths.cpp
+++ b/Swiften/Base/Paths.cpp
@@ -31,5 +31,5 @@ boost::filesystem::path Paths::getExecutablePath() {
ByteArray path;
path.resize(4096);
- ssize_t size = readlink("/proc/self/exe", reinterpret_cast<char*>(vecptr(path)), path.size());
+ size_t size = static_cast<size_t>(readlink("/proc/self/exe", reinterpret_cast<char*>(vecptr(path)), path.size()));
if (size > 0) {
path.resize(size);
@@ -37,8 +37,9 @@ boost::filesystem::path Paths::getExecutablePath() {
}
#elif defined(SWIFTEN_PLATFORM_WINDOWS)
- ByteArray data;
+ std::vector<wchar_t> data;
data.resize(2048);
- GetModuleFileName(NULL, reinterpret_cast<char*>(vecptr(data)), data.size());
- return boost::filesystem::path(std::string(reinterpret_cast<const char*>(vecptr(data)), data.size()).c_str()).parent_path();
+ GetModuleFileNameW(NULL, vecptr(data), data.size());
+ return boost::filesystem::path(
+ std::wstring(vecptr(data), data.size())).parent_path();
#endif
return boost::filesystem::path();
diff --git a/Swiften/Base/Paths.h b/Swiften/Base/Paths.h
index 8ac4640..94e62d1 100644
--- a/Swiften/Base/Paths.h
+++ b/Swiften/Base/Paths.h
@@ -9,6 +9,8 @@
#include <boost/filesystem/path.hpp>
+#include <Swiften/Base/API.h>
+
namespace Swift {
- class Paths {
+ class SWIFTEN_API Paths {
public:
static boost::filesystem::path getExecutablePath();
diff --git a/Swiften/Base/RandomGenerator.h b/Swiften/Base/RandomGenerator.h
index 4a3550d..eb5b84d 100644
--- a/Swiften/Base/RandomGenerator.h
+++ b/Swiften/Base/RandomGenerator.h
@@ -7,11 +7,16 @@
#pragma once
+#include <Swiften/Base/API.h>
#include <vector>
namespace Swift {
- class RandomGenerator {
+ class SWIFTEN_API RandomGenerator {
public:
virtual ~RandomGenerator();
+ /**
+ * Generates a random integer between 0 and 'max',
+ * 'max' inclusive.
+ */
virtual int generateRandomInteger(int max) = 0;
};
diff --git a/Swiften/Base/Regex.cpp b/Swiften/Base/Regex.cpp
new file mode 100644
index 0000000..5e3d89a
--- /dev/null
+++ b/Swiften/Base/Regex.cpp
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2013 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+/*
+ * Copyright (c) 2012 Maciej Niedzielski
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+#include <Swiften/Base/Regex.h>
+
+#include <string>
+
+#include <boost/regex.hpp>
+
+namespace Swift {
+
+ namespace Regex {
+ std::string escape(const std::string& source) {
+ // escape regex special characters: ^.$| etc
+ // these need to be escaped: [\^\$\|.........]
+ // and then C++ requires '\' to be escaped, too....
+ static const boost::regex esc("([\\^\\.\\$\\|\\(\\)\\[\\]\\*\\+\\?\\/\\{\\}\\\\])");
+ // matched character should be prepended with '\'
+ // replace matched special character with \\\1
+ // and escape once more for C++ rules...
+ static const std::string rep("\\\\\\1");
+ return boost::regex_replace(source, esc, rep);
+ }
+
+ }
+
+}
+
diff --git a/Swiften/Base/Regex.h b/Swiften/Base/Regex.h
new file mode 100644
index 0000000..6d12a60
--- /dev/null
+++ b/Swiften/Base/Regex.h
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2013 Kevin Smith
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <string>
+
+#include <Swiften/Base/API.h>
+
+namespace Swift {
+
+ namespace Regex {
+ SWIFTEN_API std::string escape(const std::string& source);
+ }
+
+}
diff --git a/Swiften/Base/SConscript b/Swiften/Base/SConscript
index a5f3592..d7fd7cc 100644
--- a/Swiften/Base/SConscript
+++ b/Swiften/Base/SConscript
@@ -5,6 +5,8 @@ objects = swiften_env.SwiftenObject([
"DateTime.cpp",
"SafeByteArray.cpp",
+ "SafeAllocator.cpp",
"Error.cpp",
"Log.cpp",
+ "Path.cpp",
"Paths.cpp",
"String.cpp",
@@ -14,4 +16,7 @@ objects = swiften_env.SwiftenObject([
"BoostRandomGenerator.cpp",
"sleep.cpp",
+ "URL.cpp",
+ "Regex.cpp",
+ "FileSize.cpp"
])
swiften_env.Append(SWIFTEN_OBJECTS = [objects])
diff --git a/Swiften/Base/SafeAllocator.cpp b/Swiften/Base/SafeAllocator.cpp
new file mode 100644
index 0000000..d61d8b9
--- /dev/null
+++ b/Swiften/Base/SafeAllocator.cpp
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2013 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <Swiften/Base/SafeByteArray.h>
+
+#include <Swiften/Base/Platform.h>
+#ifdef SWIFTEN_PLATFORM_WINDOWS
+#include <windows.h>
+#endif
+
+namespace Swift {
+
+void secureZeroMemory(char* memory, size_t numberOfBytes) {
+#ifdef SWIFTEN_PLATFORM_WINDOWS
+ SecureZeroMemory(memory, numberOfBytes);
+#else
+ volatile char* p = memory;
+ for (size_t i = 0; i < numberOfBytes; ++i) {
+ *(p++) = 0;
+ }
+#endif
+}
+
+}
diff --git a/Swiften/Base/SafeAllocator.h b/Swiften/Base/SafeAllocator.h
index 9f9dd42..8a8b25e 100644
--- a/Swiften/Base/SafeAllocator.h
+++ b/Swiften/Base/SafeAllocator.h
@@ -1,4 +1,4 @@
/*
- * Copyright (c) 2011 Remko Tronçon
+ * Copyright (c) 2011-2014 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
@@ -10,5 +10,9 @@
#include <algorithm>
+#include <Swiften/Base/API.h>
+
namespace Swift {
+ void secureZeroMemory(char* memory, size_t numberOfBytes);
+
template<typename T>
class SafeAllocator : public std::allocator<T> {
@@ -18,13 +22,15 @@ namespace Swift {
};
- SafeAllocator() throw() {}
- SafeAllocator(const SafeAllocator&) throw() : std::allocator<T>() {}
- template <class U> SafeAllocator(const SafeAllocator<U>&) throw() {}
- ~SafeAllocator() throw() {}
+ SafeAllocator() SWIFTEN_NOEXCEPT {}
+ SafeAllocator(const SafeAllocator&) SWIFTEN_NOEXCEPT : std::allocator<T>() {}
+ template <class U> SafeAllocator(const SafeAllocator<U>&) SWIFTEN_NOEXCEPT {}
+ ~SafeAllocator() SWIFTEN_NOEXCEPT {}
void deallocate (T* p, size_t num) {
- std::fill(reinterpret_cast<char*>(p), reinterpret_cast<char*>(p + num), 0);
+ secureZeroMemory(reinterpret_cast<char*>(p), num);
std::allocator<T>::deallocate(p, num);
}
+
+ SWIFTEN_DEFAULT_COPY_ASSIGMNENT_OPERATOR(SafeAllocator)
};
-};
+}
diff --git a/Swiften/Base/SafeByteArray.h b/Swiften/Base/SafeByteArray.h
index 1ef1d84..b85373c 100644
--- a/Swiften/Base/SafeByteArray.h
+++ b/Swiften/Base/SafeByteArray.h
@@ -1,4 +1,4 @@
/*
- * Copyright (c) 2011 Remko Tronçon
+ * Copyright (c) 2011-2013 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
@@ -9,4 +9,5 @@
#include <vector>
+#include <Swiften/Base/API.h>
#include <Swiften/Base/SafeAllocator.h>
#include <Swiften/Base/ByteArray.h>
@@ -20,5 +21,5 @@ namespace Swift {
}
- SafeByteArray createSafeByteArray(const char* c);
+ SWIFTEN_API SafeByteArray createSafeByteArray(const char* c);
inline SafeByteArray createSafeByteArray(const std::string& s) {
@@ -31,5 +32,5 @@ namespace Swift {
inline SafeByteArray createSafeByteArray(char c) {
- return SafeByteArray(1, c);
+ return SafeByteArray(1, static_cast<unsigned char>(c));
}
diff --git a/Swiften/Base/SimpleIDGenerator.h b/Swiften/Base/SimpleIDGenerator.h
index 677c8d1..fee857d 100644
--- a/Swiften/Base/SimpleIDGenerator.h
+++ b/Swiften/Base/SimpleIDGenerator.h
@@ -9,6 +9,8 @@
#include <string>
+#include <Swiften/Base/API.h>
+
namespace Swift {
- class SimpleIDGenerator {
+ class SWIFTEN_API SimpleIDGenerator {
public:
SimpleIDGenerator();
diff --git a/Swiften/Base/String.cpp b/Swiften/Base/String.cpp
index 7ddf614..40ea2e1 100644
--- a/Swiften/Base/String.cpp
+++ b/Swiften/Base/String.cpp
@@ -1,12 +1,20 @@
/*
- * Copyright (c) 2010 Remko Tronçon
+ * Copyright (c) 2010-2013 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
+#include <Swiften/Base/Platform.h>
+
#include <cassert>
#include <algorithm>
+#include <sstream>
+#include <iomanip>
+#ifdef SWIFTEN_PLATFORM_WINDOWS
+#include <windows.h>
+#endif
#include <Swiften/Base/String.h>
+#include <Swiften/Base/ByteArray.h>
namespace Swift {
@@ -96,3 +104,50 @@ std::vector<std::string> String::split(const std::string& s, char c) {
}
+std::string String::convertIntToHexString(int h) {
+ std::stringstream ss;
+ ss << std::setbase(16);
+ ss << h;
+ return ss.str();
+}
+
+int String::convertHexStringToInt(const std::string& s) {
+ std::stringstream ss;
+ int h;
+ ss << std::setbase(16);
+ ss << s;
+ ss >> h;
+ return h;
+}
+
+
+#ifdef SWIFTEN_PLATFORM_WINDOWS
+std::string convertWStringToString(const std::wstring& s) {
+ int utf8Size = WideCharToMultiByte(CP_UTF8, 0, s.c_str(), -1, NULL, 0, NULL, NULL);
+ if (utf8Size < 0) {
+ throw std::runtime_error("Conversion error");
+ }
+ std::vector<char> utf8Data(utf8Size);
+ int result = WideCharToMultiByte(
+ CP_UTF8, 0, s.c_str(), -1, vecptr(utf8Data), utf8Data.size(), NULL, NULL);
+ if (result < 0) {
+ throw std::runtime_error("Conversion error");
+ }
+ return std::string(vecptr(utf8Data), utf8Size-1 /* trailing 0 character */);
+}
+
+std::wstring convertStringToWString(const std::string& s) {
+ int utf16Size = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, NULL, 0);
+ if (utf16Size < 0) {
+ throw std::runtime_error("Conversion error");
+ }
+ std::vector<wchar_t> utf16Data(utf16Size);
+ int result = MultiByteToWideChar(
+ CP_UTF8, 0, s.c_str(), -1, vecptr(utf16Data), utf16Data.size());
+ if (result < 0) {
+ throw std::runtime_error("Conversion error");
+ }
+ return std::wstring(vecptr(utf16Data), utf16Size-1 /* trailing 0 character */);
+}
+#endif
+
}
diff --git a/Swiften/Base/String.h b/Swiften/Base/String.h
index db6c28b..5a5642e 100644
--- a/Swiften/Base/String.h
+++ b/Swiften/Base/String.h
@@ -11,4 +11,7 @@
#include <sstream>
+#include <Swiften/Base/API.h>
+#include <Swiften/Base/Platform.h>
+
#define SWIFTEN_STRING_TO_CFSTRING(a) \
CFStringCreateWithBytes(NULL, reinterpret_cast<const UInt8*>(a.c_str()), a.size(), kCFStringEncodingUTF8, false)
@@ -16,8 +19,8 @@
namespace Swift {
namespace String {
- std::vector<unsigned int> getUnicodeCodePoints(const std::string&);
- std::pair<std::string, std::string> getSplittedAtFirst(const std::string&, char c);
- std::vector<std::string> split(const std::string&, char c);
- void replaceAll(std::string&, char c, const std::string& s);
+ SWIFTEN_API std::vector<unsigned int> getUnicodeCodePoints(const std::string&);
+ SWIFTEN_API std::pair<std::string, std::string> getSplittedAtFirst(const std::string&, char c);
+ SWIFTEN_API std::vector<std::string> split(const std::string&, char c);
+ SWIFTEN_API void replaceAll(std::string&, char c, const std::string& s);
inline bool beginsWith(const std::string& s, char c) {
@@ -28,7 +31,16 @@ namespace Swift {
return s.size() > 0 && s[s.size()-1] == c;
}
- };
- class makeString {
+ std::string convertIntToHexString(int h);
+ int convertHexStringToInt(const std::string& s);
+
+ }
+
+#ifdef SWIFTEN_PLATFORM_WINDOWS
+ SWIFTEN_API std::string convertWStringToString(const std::wstring& s);
+ SWIFTEN_API std::wstring convertStringToWString(const std::string& s);
+#endif
+
+ class SWIFTEN_API makeString {
public:
template <typename T> makeString& operator<<(T const& v) {
diff --git a/Swiften/Base/URL.cpp b/Swiften/Base/URL.cpp
new file mode 100644
index 0000000..866cd45
--- /dev/null
+++ b/Swiften/Base/URL.cpp
@@ -0,0 +1,189 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <Swiften/Base/URL.h>
+
+#include <iostream>
+
+namespace Swift {
+
+int URL::getPortOrDefaultPort(const URL& url) {
+ if (url.getPort()) {
+ return *url.getPort();
+ }
+ else if (url.getScheme() == "http") {
+ return 80;
+ }
+ else if (url.getScheme() == "https") {
+ return 443;
+ }
+ else {
+ std::cerr << "Unknown scheme: " + url.getScheme() << std::endl;
+ return 80;
+ }
+}
+
+URL URL::fromString(const std::string& urlString) {
+ size_t colonIndex = urlString.find(':');
+ if (colonIndex == std::string::npos) {
+ return URL();
+ }
+ std::string scheme = urlString.substr(0, colonIndex);
+
+ // Authority
+ if (urlString.size() > colonIndex + 2 && urlString[colonIndex+1] == '/' && urlString[colonIndex+2] == '/') {
+ size_t authorityIndex = colonIndex + 3;
+ size_t slashIndex = urlString.find('/', authorityIndex);
+ std::string authority;
+ std::string path;
+ if (slashIndex == std::string::npos) {
+ authority = urlString.substr(authorityIndex);
+ path = "";
+ }
+ else {
+ authority = urlString.substr(authorityIndex, slashIndex - authorityIndex);
+ path = unescape(urlString.substr(slashIndex));
+ }
+
+ size_t atIndex = authority.find('@');
+ std::string userInfo;
+ std::string hostAndPort;
+ if (atIndex != std::string::npos) {
+ userInfo = authority.substr(0, atIndex);
+ hostAndPort = authority.substr(atIndex + 1);
+ }
+ else {
+ userInfo = "";
+ hostAndPort = authority;
+ }
+
+ std::string host;
+ boost::optional<int> port;
+ colonIndex = hostAndPort.find(':');
+ if (colonIndex != std::string::npos) {
+ host = unescape(hostAndPort.substr(0, colonIndex));
+ try {
+ port = boost::lexical_cast<int>(hostAndPort.substr(colonIndex + 1));
+ }
+ catch (const boost::bad_lexical_cast&) {
+ return URL();
+ }
+ }
+ else {
+ host = unescape(hostAndPort);
+ }
+
+ if (port) {
+ return URL(scheme, host, *port, path);
+ }
+ else {
+ return URL(scheme, host, path);
+ }
+ }
+ else {
+ // We don't support URLs without authorities yet
+ return URL();
+ }
+}
+
+// FIXME: Escape non-ascii characters
+std::string URL::toString() const {
+ if (empty) {
+ return "";
+ }
+ std::string result = scheme + "://";
+ if (!user.empty()) {
+ result += user;
+ if (!password.empty()) {
+ result += ":" + password;
+ }
+ result += "@";
+ }
+ result += host;
+ if (port) {
+ result += ":";
+ result += boost::lexical_cast<std::string>(*port);
+ }
+ result += path;
+ return result;
+}
+
+// Disabling this code for now, since GCC4.5+boost1.42 (on ubuntu) seems to
+// result in a bug. Replacing it with naive code.
+#if 0
+// Should be in anonymous namespace, but older GCCs complain if we do that
+struct PercentEncodedCharacterFinder {
+template<typename Iterator>
+boost::iterator_range<Iterator> operator()(Iterator begin, Iterator end) {
+ boost::iterator_range<Iterator> r = boost::first_finder("%")(begin, end);
+ if (r.end() == end) {
+ return r;
+ }
+ else {
+ if (r.end() + 1 == end || r.end() + 2 == end) {
+ throw std::runtime_error("Incomplete escape character");
+ }
+ else {
+ r.advance_end(2);
+ return r;
+ }
+ }
+}
+};
+
+struct PercentUnencodeFormatter {
+template<typename FindResult>
+std::string operator()(const FindResult& match) const {
+ std::stringstream s;
+ s << std::hex << std::string(match.begin() + 1, match.end());
+ unsigned int value;
+ s >> value;
+ if (s.fail() || s.bad()) {
+ throw std::runtime_error("Invalid escape character");
+ }
+ unsigned char charValue = static_cast<unsigned char>(value);
+ return std::string(reinterpret_cast<const char*>(&charValue), 1);
+}
+};
+
+std::string unescape(const std::string& s) {
+ try {
+ return boost::find_format_all_copy(s, PercentEncodedCharacterFinder(), PercentUnencodeFormatter());
+ }
+ catch (const std::exception&) {
+ return "";
+ }
+}
+#endif
+
+std::string URL::unescape(const std::string& str) {
+ std::string result;
+ for (size_t i = 0; i < str.size(); ++i) {
+ if (str[i] == '%') {
+ if (i + 3 < str.size()) {
+ std::stringstream s;
+ s << std::hex << str.substr(i+1, 2);
+ unsigned int value;
+ s >> value;
+ if (s.fail() || s.bad()) {
+ return "";
+ }
+ unsigned char charValue = static_cast<unsigned char>(value);
+ result += std::string(reinterpret_cast<const char*>(&charValue), 1);
+ i += 2;
+ }
+ else {
+ return "";
+ }
+ }
+ else {
+ result += str[i];
+ }
+ }
+ return result;
+}
+
+}
diff --git a/Swiften/Base/URL.h b/Swiften/Base/URL.h
index 7a5aa59..75cf1a6 100644
--- a/Swiften/Base/URL.h
+++ b/Swiften/Base/URL.h
@@ -8,15 +8,20 @@
#include <string>
+#include <boost/lexical_cast.hpp>
+#include <boost/optional.hpp>
+#include <Swiften/Base/API.h>
namespace Swift {
-class URL {
+class SWIFTEN_API URL {
public:
- URL() : scheme(""), user(""), password(""), host(""), port(-1), path(""), isEmpty(true) {
+ URL() : scheme(""), user(""), password(""), host(""), path(""), empty(true) {
}
- URL(const std::string& scheme, const std::string& host, int port, const std::string& path) : scheme(scheme), user(), password(), host(host), port(port), path(path), isEmpty(false) {
+ URL(const std::string& scheme, const std::string& host, int port, const std::string& path) : scheme(scheme), user(), password(), host(host), port(port), path(path), empty(false) {
+ }
+ URL(const std::string& scheme, const std::string& host, const std::string& path) : scheme(scheme), user(), password(), host(host), path(path), empty(false) {
}
@@ -24,6 +29,6 @@ class URL {
* Whether the URL is empty.
*/
- bool empty() const {
- return isEmpty;
+ bool isEmpty() const {
+ return empty;
}
@@ -45,5 +50,5 @@ class URL {
* Port number
*/
- int getPort() const {
+ boost::optional<int> getPort() const {
return port;
}
@@ -56,4 +61,9 @@ class URL {
}
+ std::string toString() const;
+
+ static int getPortOrDefaultPort(const URL& url);
+ static URL fromString(const std::string&);
+ static std::string unescape(const std::string&);
@@ -63,7 +73,7 @@ class URL {
std::string password;
std::string host;
- int port;
+ boost::optional<int> port;
std::string path;
- bool isEmpty;
+ bool empty;
};
}
diff --git a/Swiften/Base/UnitTest/PathTest.cpp b/Swiften/Base/UnitTest/PathTest.cpp
new file mode 100644
index 0000000..f5f99e7
--- /dev/null
+++ b/Swiften/Base/UnitTest/PathTest.cpp
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2013 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+#include <Swiften/Base/Path.h>
+#include <Swiften/Base/Platform.h>
+
+using namespace Swift;
+
+class PathTest : public CppUnit::TestFixture {
+ CPPUNIT_TEST_SUITE(PathTest);
+ CPPUNIT_TEST(testStringToPath);
+ CPPUNIT_TEST(testPathToString);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void testStringToPath() {
+#ifdef SWIFTEN_PLATFORM_WINDOWS
+ CPPUNIT_ASSERT(std::wstring(L"tron\xe7on") == stringToPath("tron\xc3\xa7on").native());
+#else
+ CPPUNIT_ASSERT_EQUAL(std::string("tron\xc3\xa7on"), stringToPath("tron\xc3\xa7on").native());
+#endif
+ }
+
+ void testPathToString() {
+ CPPUNIT_ASSERT_EQUAL(std::string("tron\xc3\xa7on"), pathToString(stringToPath("tron\xc3\xa7on")));
+ }
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(PathTest);
+
diff --git a/Swiften/Base/UnitTest/StringTest.cpp b/Swiften/Base/UnitTest/StringTest.cpp
index b29f331..ffca98a 100644
--- a/Swiften/Base/UnitTest/StringTest.cpp
+++ b/Swiften/Base/UnitTest/StringTest.cpp
@@ -10,4 +10,5 @@
#include <Swiften/Base/String.h>
+#include <Swiften/Base/Platform.h>
using namespace Swift;
@@ -25,4 +26,8 @@ class StringTest : public CppUnit::TestFixture {
CPPUNIT_TEST(testReplaceAll_MatchingReplace);
CPPUNIT_TEST(testSplit);
+#ifdef SWIFTEN_PLATFORM_WINDOWS
+ CPPUNIT_TEST(testConvertWStringToString);
+ CPPUNIT_TEST(testConvertStringToWString);
+#endif
CPPUNIT_TEST_SUITE_END();
@@ -110,4 +115,14 @@ class StringTest : public CppUnit::TestFixture {
CPPUNIT_ASSERT_EQUAL(std::string("ghi"), result[2]);
}
+
+#ifdef SWIFTEN_PLATFORM_WINDOWS
+ void testConvertWStringToString() {
+ CPPUNIT_ASSERT_EQUAL(std::string("tron\xc3\xa7on"), convertWStringToString(std::wstring(L"tron\xe7on")));
+ }
+
+ void testConvertStringToWString() {
+ CPPUNIT_ASSERT(std::wstring(L"tron\xe7on") == convertStringToWString(std::string("tron\xc3\xa7on")));
+ }
+#endif
};
diff --git a/Swiften/Base/UnitTest/URLTest.cpp b/Swiften/Base/UnitTest/URLTest.cpp
new file mode 100644
index 0000000..e82321f
--- /dev/null
+++ b/Swiften/Base/UnitTest/URLTest.cpp
@@ -0,0 +1,114 @@
+/*
+ * Copyright (c) 2012 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+#include <Swiften/Base/URL.h>
+#include <boost/lexical_cast.hpp>
+
+using namespace Swift;
+
+class URLTest : public CppUnit::TestFixture {
+ CPPUNIT_TEST_SUITE(URLTest);
+ CPPUNIT_TEST(testFromString);
+ CPPUNIT_TEST(testFromString_WithoutPath);
+ CPPUNIT_TEST(testFromString_WithRootPath);
+ CPPUNIT_TEST(testFromString_WithPort);
+ CPPUNIT_TEST(testFromString_WithPortOnePartPath);
+ CPPUNIT_TEST(testFromString_WithPortWithoutPath);
+ CPPUNIT_TEST(testFromString_WithUserInfo);
+ CPPUNIT_TEST(testFromString_NonASCIIHost);
+ CPPUNIT_TEST(testFromString_NonASCIIPath);
+ CPPUNIT_TEST(testToString);
+ CPPUNIT_TEST(testToString_WithPort);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void testFromString() {
+ URL url = URL::fromString("http://foo.bar/baz/bam");
+
+ CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
+ CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
+ CPPUNIT_ASSERT(!url.getPort());
+ CPPUNIT_ASSERT_EQUAL(std::string("/baz/bam"), url.getPath());
+ }
+
+ void testFromString_WithoutPath() {
+ URL url = URL::fromString("http://foo.bar");
+
+ CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
+ CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
+ CPPUNIT_ASSERT(!url.getPort());
+ CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath());
+ }
+
+ void testFromString_WithRootPath() {
+ URL url = URL::fromString("http://foo.bar/");
+
+ CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
+ CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
+ CPPUNIT_ASSERT(!url.getPort());
+ CPPUNIT_ASSERT_EQUAL(std::string("/"), url.getPath());
+ }
+
+ void testFromString_WithPort() {
+ URL url = URL::fromString("http://foo.bar:1234/baz/bam");
+
+ CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
+ CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
+ CPPUNIT_ASSERT_EQUAL(1234, *url.getPort());
+ CPPUNIT_ASSERT_EQUAL(std::string("/baz/bam"), url.getPath());
+ }
+
+ void testFromString_WithPortOnePartPath() {
+ URL url = URL::fromString("http://foo.bar:11440/http-bind/");
+
+ CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
+ CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
+ CPPUNIT_ASSERT_EQUAL(11440, *url.getPort());
+ CPPUNIT_ASSERT_EQUAL(std::string("/http-bind/"), url.getPath());
+ }
+
+ void testFromString_WithPortWithoutPath() {
+ URL url = URL::fromString("http://foo.bar:1234");
+
+ CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
+ CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
+ CPPUNIT_ASSERT_EQUAL(1234, *url.getPort());
+ CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath());
+ }
+
+ void testFromString_WithUserInfo() {
+ URL url = URL::fromString("http://user:pass@foo.bar/baz/bam");
+
+ CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme());
+ CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost());
+ CPPUNIT_ASSERT_EQUAL(std::string("/baz/bam"), url.getPath());
+ }
+
+ void testFromString_NonASCIIHost() {
+ URL url = URL::fromString("http://www.tron%C3%A7on.be/baz/bam");
+
+ CPPUNIT_ASSERT_EQUAL(std::string("www.tron\xc3\xa7on.be"), url.getHost());
+ }
+
+ void testFromString_NonASCIIPath() {
+ URL url = URL::fromString("http://foo.bar/baz/tron%C3%A7on/bam");
+
+ CPPUNIT_ASSERT_EQUAL(std::string("/baz/tron\xc3\xa7on/bam"), url.getPath());
+ }
+
+ void testToString() {
+ CPPUNIT_ASSERT_EQUAL(std::string("http://foo.bar/baz/bam"), URL("http", "foo.bar", "/baz/bam").toString());
+ }
+
+ void testToString_WithPort() {
+ CPPUNIT_ASSERT_EQUAL(std::string("http://foo.bar:1234/baz/bam"), URL("http", "foo.bar", 1234, "/baz/bam").toString());
+ }
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(URLTest);
diff --git a/Swiften/Base/foreach.h b/Swiften/Base/foreach.h
index 87f6147..3ad506d 100644
--- a/Swiften/Base/foreach.h
+++ b/Swiften/Base/foreach.h
@@ -11,2 +11,3 @@
#undef foreach
#define foreach BOOST_FOREACH
+#define reverse_foreach BOOST_REVERSE_FOREACH
diff --git a/Swiften/Base/sleep.cpp b/Swiften/Base/sleep.cpp
index 7161217..0f1937b 100644
--- a/Swiften/Base/sleep.cpp
+++ b/Swiften/Base/sleep.cpp
@@ -8,4 +8,5 @@
#include <boost/thread.hpp>
+#include <boost/version.hpp>
namespace Swift {
@@ -13,5 +14,9 @@ namespace Swift {
void sleep(unsigned int msecs) {
boost::xtime xt;
+#if BOOST_VERSION >= 105000
+ boost::xtime_get(&xt, boost::TIME_UTC_);
+#else
boost::xtime_get(&xt, boost::TIME_UTC);
+#endif
xt.nsec += msecs*1000000;
boost::thread::sleep(xt);
diff --git a/Swiften/Base/sleep.h b/Swiften/Base/sleep.h
index a95e907..afcf6c7 100644
--- a/Swiften/Base/sleep.h
+++ b/Swiften/Base/sleep.h
@@ -7,5 +7,7 @@
#pragma once
+#include <Swiften/Base/API.h>
+
namespace Swift {
- void sleep(unsigned int msecs);
+ SWIFTEN_API void sleep(unsigned int msecs);
}