comparison 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
comparison
equal deleted inserted replaced
86:de8a5329b069 87:8517e2c44283
29 29
30 #if defined(_WIN32) 30 #if defined(_WIN32)
31 #include <windows.h> 31 #include <windows.h>
32 #endif 32 #endif
33 33
34 #if defined(__APPLE__) && defined(__MACH__)
35 #include <mach-o/dyld.h> /* _NSGetExecutablePath */
36 #include <limits.h> /* PATH_MAX */
37 #endif
38
34 #if defined(__linux) 39 #if defined(__linux)
40 #include <limits.h> /* PATH_MAX */
35 #include <signal.h> 41 #include <signal.h>
36 #include <unistd.h> 42 #include <unistd.h>
37 #endif 43 #endif
38 44
39 #include "../Resources/md5/md5.h" 45 #include "../Resources/md5/md5.h"
344 std::string Toolbox::EncodeBase64(const std::string& data) 350 std::string Toolbox::EncodeBase64(const std::string& data)
345 { 351 {
346 return base64_encode(data); 352 return base64_encode(data);
347 } 353 }
348 354
355
356 #if defined(_WIN32)
357 std::string Toolbox::GetPathToExecutable()
358 {
359 // Yes, this is ugly, but there is no simple way to get the
360 // required buffer size, so we use a big constant
361 std::vector<char> buffer(32768);
362 /*int bytes =*/ GetModuleFileNameA(NULL, &buffer[0], static_cast<DWORD>(buffer.size() - 1));
363 return std::string(&buffer[0]);
364 }
365
366 #elif defined(__linux)
367 std::string Toolbox::GetPathToExecutable()
368 {
369 std::vector<char> buffer(PATH_MAX + 1);
370 ssize_t bytes = readlink("/proc/self/exe", &buffer[0], buffer.size() - 1);
371 if (bytes == 0)
372 {
373 throw OrthancException("Unable to get the path to the executable");
374 }
375
376 return std::string(&buffer[0]);
377 }
378
379 #elif defined(__APPLE__) && defined(__MACH__)
380 std::string Toolbox::GetPathToExecutable()
381 {
382 char pathbuf[PATH_MAX + 1];
383 unsigned int bufsize = static_cast<int>(sizeof(pathbuf));
384
385 _NSGetExecutablePath( pathbuf, &bufsize);
386
387 return std::string(pathbuf);
388 }
389
390 #else
391 #error Support your platform here
392 #endif
393
394
395 std::string Toolbox::GetDirectoryOfExecutable()
396 {
397 boost::filesystem::path p(GetPathToExecutable());
398 return p.parent_path().string();
399 }
400
349 } 401 }