diff Core/Toolbox.cpp @ 87:8517e2c44283

path to configuration
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 26 Sep 2012 17:39:03 +0200
parents de8a5329b069
children 3b45473c0a73
line wrap: on
line diff
--- a/Core/Toolbox.cpp	Wed Sep 26 11:21:05 2012 +0200
+++ b/Core/Toolbox.cpp	Wed Sep 26 17:39:03 2012 +0200
@@ -31,7 +31,13 @@
 #include <windows.h>
 #endif
 
+#if defined(__APPLE__) && defined(__MACH__)
+#include <mach-o/dyld.h> /* _NSGetExecutablePath */
+#include <limits.h>      /* PATH_MAX */
+#endif
+
 #if defined(__linux)
+#include <limits.h>      /* PATH_MAX */
 #include <signal.h>
 #include <unistd.h>
 #endif
@@ -346,4 +352,50 @@
     return base64_encode(data);
   }
 
+
+#if defined(_WIN32)
+  std::string Toolbox::GetPathToExecutable()
+  {
+    // Yes, this is ugly, but there is no simple way to get the 
+    // required buffer size, so we use a big constant
+    std::vector<char> buffer(32768);
+    /*int bytes =*/ GetModuleFileNameA(NULL, &buffer[0], static_cast<DWORD>(buffer.size() - 1));
+    return std::string(&buffer[0]);
+  }
+
+#elif defined(__linux)
+  std::string Toolbox::GetPathToExecutable()
+  {
+    std::vector<char> buffer(PATH_MAX + 1);
+    ssize_t bytes = readlink("/proc/self/exe", &buffer[0], buffer.size() - 1);
+    if (bytes == 0)
+    {
+      throw OrthancException("Unable to get the path to the executable");
+    }
+
+    return std::string(&buffer[0]);
+  }
+
+#elif defined(__APPLE__) && defined(__MACH__)
+  std::string Toolbox::GetPathToExecutable()
+  {
+    char pathbuf[PATH_MAX + 1];
+    unsigned int  bufsize = static_cast<int>(sizeof(pathbuf));
+
+    _NSGetExecutablePath( pathbuf, &bufsize);
+
+    return std::string(pathbuf);
+  }
+
+#else
+#error Support your platform here
+#endif
+
+
+  std::string Toolbox::GetDirectoryOfExecutable()
+  {
+    boost::filesystem::path p(GetPathToExecutable());
+    return p.parent_path().string();
+  }
+
 }