From fffffda255d80a56def292a9f7adf101382bee1a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Thu, 19 Aug 2010 21:50:14 +0200
Subject: Added getExecutablePath().


diff --git a/Swiften/Application/ApplicationPathProvider.h b/Swiften/Application/ApplicationPathProvider.h
index ba671ee..cf97299 100644
--- a/Swiften/Application/ApplicationPathProvider.h
+++ b/Swiften/Application/ApplicationPathProvider.h
@@ -20,6 +20,7 @@ namespace Swift {
 			boost::filesystem::path getAvatarDir() const;
 			virtual boost::filesystem::path getHomeDir() const = 0;
 			virtual boost::filesystem::path getSettingsDir() const = 0;
+			virtual boost::filesystem::path getExecutableDir() const = 0;
 			boost::filesystem::path getProfileDir(const String& profile) const;
 
 		protected:
diff --git a/Swiften/Application/MacOSXApplicationPathProvider.cpp b/Swiften/Application/MacOSXApplicationPathProvider.cpp
index 3b9a797..7e4931a 100644
--- a/Swiften/Application/MacOSXApplicationPathProvider.cpp
+++ b/Swiften/Application/MacOSXApplicationPathProvider.cpp
@@ -7,6 +7,7 @@
 #include "Swiften/Application/MacOSXApplicationPathProvider.h"
 
 #include <iostream>
+#include <mach-o/dyld.h>
 
 namespace Swift {
 
@@ -28,4 +29,17 @@ boost::filesystem::path MacOSXApplicationPathProvider::getHomeDir() const {
 	return boost::filesystem::path(getenv("HOME"));
 }
 
+
+boost::filesystem::path MacOSXApplicationPathProvider::getExecutableDir() const {
+	ByteArray path;
+	uint32_t size = 4096;
+	path.resize(size);
+	if (_NSGetExecutablePath(path, &size) == 0) {
+		return boost::filesystem::path(path.toString().getUTF8Data()).parent_path();
+	}
+	else {
+		return boost::filesystem::path();
+	}
+}
+
 }
diff --git a/Swiften/Application/MacOSXApplicationPathProvider.h b/Swiften/Application/MacOSXApplicationPathProvider.h
index c1d86d0..e03c753 100644
--- a/Swiften/Application/MacOSXApplicationPathProvider.h
+++ b/Swiften/Application/MacOSXApplicationPathProvider.h
@@ -15,5 +15,6 @@ namespace Swift {
 
 			virtual boost::filesystem::path getHomeDir() const;
 			boost::filesystem::path getSettingsDir() const;
+			virtual boost::filesystem::path getExecutableDir() const;
 	};
 }
diff --git a/Swiften/Application/UnitTest/ApplicationPathProviderTest.cpp b/Swiften/Application/UnitTest/ApplicationPathProviderTest.cpp
index 7cb277a..75b8639 100644
--- a/Swiften/Application/UnitTest/ApplicationPathProviderTest.cpp
+++ b/Swiften/Application/UnitTest/ApplicationPathProviderTest.cpp
@@ -8,12 +8,14 @@
 #include <cppunit/extensions/TestFactoryRegistry.h>
 
 #include "Swiften/Application/PlatformApplicationPathProvider.h"
+#include "Swiften/Base/String.h"
 
 using namespace Swift;
 
 class ApplicationPathProviderTest : public CppUnit::TestFixture {
 		CPPUNIT_TEST_SUITE(ApplicationPathProviderTest);
 		CPPUNIT_TEST(testGetSettingsDir);
+		CPPUNIT_TEST(testGetExecutableDir);
 		CPPUNIT_TEST_SUITE_END();
 
 	public:
@@ -33,6 +35,12 @@ class ApplicationPathProviderTest : public CppUnit::TestFixture {
 
 			boost::filesystem::remove(dir);
 		}
+
+		void testGetExecutableDir() {
+			boost::filesystem::path dir = testling_->getExecutableDir();
+			CPPUNIT_ASSERT(boost::filesystem::is_directory(dir));
+			CPPUNIT_ASSERT(String(dir.string()).endsWith("UnitTest"));
+		}
 	
 	private:
 		ApplicationPathProvider* testling_;
diff --git a/Swiften/Application/UnixApplicationPathProvider.h b/Swiften/Application/UnixApplicationPathProvider.h
index 04387f7..170d53a 100644
--- a/Swiften/Application/UnixApplicationPathProvider.h
+++ b/Swiften/Application/UnixApplicationPathProvider.h
@@ -9,6 +9,7 @@
 #include "Swiften/Application/ApplicationPathProvider.h"
 
 #include <iostream>
+#include <unistd.h>
 
 namespace Swift {
 	class UnixApplicationPathProvider : public ApplicationPathProvider {
@@ -30,6 +31,19 @@ namespace Swift {
 				}
 				return result;
 			}
+
+			virtual boost::filesystem::path getExecutableDir() const {
+				ByteArray path;
+				path.resize(SSIZE_MAX);
+				size_t size = readlink("/proc/self/exe", path.getData(), path.getSize());
+				if (size > 0) {
+					path.resize(size);
+					return boost::filesystem::path(path.toString().getUTF8Data()).parent_path();
+				}
+				else {
+					return boost::filesystem::path();
+				}
+			}
 	};
 }
 
diff --git a/Swiften/Application/WindowsApplicationPathProvider.cpp b/Swiften/Application/WindowsApplicationPathProvider.cpp
index 3fdcb88..2e84e90 100644
--- a/Swiften/Application/WindowsApplicationPathProvider.cpp
+++ b/Swiften/Application/WindowsApplicationPathProvider.cpp
@@ -6,9 +6,20 @@
 
 #include "Swiften/Application/WindowsApplicationPathProvider.h"
 
+#include <windows.h>
+
+#include "Swiften/Base/ByteArray.h"
+
 namespace Swift {
 
 WindowsApplicationPathProvider::WindowsApplicationPathProvider(const String& name) : ApplicationPathProvider(name) {
 }
 
+boost::filesystem::path WindowsApplicationPathProvider::getExecutableDir() const {
+	ByteArray data;
+	data.resize(2048);
+	GetModuleFileName(NULL, data.getData(), data.getSize());
+	return boost::filesystem::path(data.toString().getUTF8Data()).parent_path();
+}
+
 }
diff --git a/Swiften/Application/WindowsApplicationPathProvider.h b/Swiften/Application/WindowsApplicationPathProvider.h
index 0d6862e..1ba4298 100644
--- a/Swiften/Application/WindowsApplicationPathProvider.h
+++ b/Swiften/Application/WindowsApplicationPathProvider.h
@@ -26,5 +26,7 @@ namespace Swift {
 				char* homeDirRaw = getenv("USERPROFILE");
 				return boost::filesystem::path(homeDirRaw);
 			}
+
+			virtual boost::filesystem::path getExecutableDir() const;
 	};
 }
diff --git a/Swiften/Base/String.h b/Swiften/Base/String.h
index 03d9029..b05edba 100644
--- a/Swiften/Base/String.h
+++ b/Swiften/Base/String.h
@@ -66,6 +66,10 @@ namespace Swift {
 				return data_.size() > 0 && data_[data_.size()-1] == c; 
 			}
 
+			bool endsWith(const String& s) const {
+				return data_.substr(data_.size() - s.data_.size(), data_.npos) == s;
+			}
+
 			String getSubstring(size_t begin, size_t end) const {
 				return String(data_.substr(begin, end));
 			}
diff --git a/Swiften/Base/UnitTest/StringTest.cpp b/Swiften/Base/UnitTest/StringTest.cpp
index 32d04a6..eff7b80 100644
--- a/Swiften/Base/UnitTest/StringTest.cpp
+++ b/Swiften/Base/UnitTest/StringTest.cpp
@@ -34,11 +34,11 @@ class StringTest : public CppUnit::TestFixture
  		CPPUNIT_TEST(testContains);
 		CPPUNIT_TEST(testContainsFalse);
 		CPPUNIT_TEST(testContainsExact);
+		CPPUNIT_TEST(testEndsWith);
+		CPPUNIT_TEST(testEndsWith_DoesNotEndWith);
 		CPPUNIT_TEST_SUITE_END();
 
 	public:
-		StringTest() {}
-
 		void testGetLength() {
 			String testling("xyz$xyz");
 
@@ -180,6 +180,14 @@ class StringTest : public CppUnit::TestFixture
 			CPPUNIT_ASSERT(String("abcde").contains(String("abcde")));
 		}
 
+		void testEndsWith() {
+			CPPUNIT_ASSERT(String("abcdef").endsWith("cdef"));
+		}
+
+		void testEndsWith_DoesNotEndWith() {
+			CPPUNIT_ASSERT(!String("abcdef").endsWith("ddef"));
+		}
+
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(StringTest);
-- 
cgit v0.10.2-6-g49f6