changeset 87:8517e2c44283

path to configuration
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 26 Sep 2012 17:39:03 +0200
parents de8a5329b069
children 8936a3bbb129
files Core/Toolbox.cpp Core/Toolbox.h OrthancServer/OrthancInitialization.cpp UnitTests/main.cpp
diffstat 4 files changed, 94 insertions(+), 4 deletions(-) [+]
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();
+  }
+
 }
--- a/Core/Toolbox.h	Wed Sep 26 11:21:05 2012 +0200
+++ b/Core/Toolbox.h	Wed Sep 26 17:39:03 2012 +0200
@@ -62,5 +62,9 @@
                     const std::string& data);
 
     std::string EncodeBase64(const std::string& data);
+
+    std::string GetPathToExecutable();
+
+    std::string GetDirectoryOfExecutable();
   }
 }
--- a/OrthancServer/OrthancInitialization.cpp	Wed Sep 26 11:21:05 2012 +0200
+++ b/OrthancServer/OrthancInitialization.cpp	Wed Sep 26 17:39:03 2012 +0200
@@ -45,25 +45,53 @@
     if (configurationFile)
     {
       Toolbox::ReadFile(content, configurationFile);
+      printf("Using the configuration from: [%s]\n", configurationFile);
     }
     else
     {
+#if ORTHANC_STANDALONE == 1 && defined(__linux)
+      // Under Linux, try and open "../etc/orthanc/Configuration.json"
       try
       {
-#if ORTHANC_STANDALONE == 0
+        boost::filesystem::path p = ORTHANC_PATH;
+        p = p.parent_path();
+        p /= "etc";
+        p /= "orthanc";
+        p /= CONFIGURATION_FILE;
+          
+        Toolbox::ReadFile(content, p.string());
+        printf("Using the configuration from: [%s]\n", p.string().c_str());
+      }
+      catch (OrthancException&)
+      {
+        // No configuration file found, give up with empty configuration
+        printf("Using the default Orthanc configuration\n");
+        return;
+      }
+
+#elif ORTHANC_STANDALONE == 1
+      // No default path for the configuration file in Windows
+      printf("Using the default Orthanc configuration\n");
+      return;
+
+#else
+      // In a non-standalone build, we use the
+      // "Resources/Configuration.json" from the Orthanc distribution
+      try
+      {
         boost::filesystem::path p = ORTHANC_PATH;
         p /= "Resources";
         p /= CONFIGURATION_FILE;
         Toolbox::ReadFile(content, p.string());
-#else
-        Toolbox::ReadFile(content, CONFIGURATION_FILE);
-#endif
+        printf("Using the configuration from: [%s]\n", p.string().c_str());
       }
       catch (OrthancException&)
       {
         // No configuration file found, give up with empty configuration
+        printf("Using the default Orthanc configuration\n");
         return;
       }
+#endif
     }
 
     Json::Reader reader;
--- a/UnitTests/main.cpp	Wed Sep 26 11:21:05 2012 +0200
+++ b/UnitTests/main.cpp	Wed Sep 26 17:39:03 2012 +0200
@@ -273,6 +273,12 @@
   ASSERT_EQ("SGVsbG8gd29ybGQ=", Toolbox::EncodeBase64("Hello world"));
 }
 
+TEST(Toolbox, PathToExecutable)
+{
+  printf("[%s]\n", Toolbox::GetPathToExecutable().c_str());
+  printf("[%s]\n", Toolbox::GetDirectoryOfExecutable().c_str());
+}
+
 int main(int argc, char **argv)
 {
   OrthancInitialize();