# HG changeset patch # User Sebastien Jodogne # Date 1348673943 -7200 # Node ID 8517e2c44283eb4ab229ebc61505e2b70ca9ac28 # Parent de8a5329b069213339a4e5057a6eac0f3337de48 path to configuration diff -r de8a5329b069 -r 8517e2c44283 Core/Toolbox.cpp --- 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 #endif +#if defined(__APPLE__) && defined(__MACH__) +#include /* _NSGetExecutablePath */ +#include /* PATH_MAX */ +#endif + #if defined(__linux) +#include /* PATH_MAX */ #include #include #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 buffer(32768); + /*int bytes =*/ GetModuleFileNameA(NULL, &buffer[0], static_cast(buffer.size() - 1)); + return std::string(&buffer[0]); + } + +#elif defined(__linux) + std::string Toolbox::GetPathToExecutable() + { + std::vector 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(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(); + } + } diff -r de8a5329b069 -r 8517e2c44283 Core/Toolbox.h --- 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(); } } diff -r de8a5329b069 -r 8517e2c44283 OrthancServer/OrthancInitialization.cpp --- 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; diff -r de8a5329b069 -r 8517e2c44283 UnitTests/main.cpp --- 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();