executable.cc 665 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "executable.hpp"
  2. #ifdef __linux__
  3. #define _EXEC_PATH_IMPL_ 1
  4. #include <unistd.h>
  5. #include <string>
  6. #include <climits>
  7. boost::filesystem::path get_executable_path()
  8. {
  9. static char buf[PATH_MAX];
  10. readlink("/proc/self/exe", buf, PATH_MAX);
  11. return buf;
  12. }
  13. #endif
  14. #ifdef __WINNT__
  15. #define _EXEC_PATH_IMPL_ 1
  16. #include <windows.h>
  17. boost::filesystem::path get_executable_path()
  18. {
  19. static char buf[MAX_PATH];
  20. GetModuleFileNameA(NULL, buf, MAX_PATH);
  21. return buf;
  22. }
  23. #endif
  24. #ifndef _EXEC_PATH_IMPL_
  25. #error "don't know how to get executable path"
  26. #endif
  27. boost::filesystem::path get_executable_directory()
  28. {
  29. return get_executable_path().parent_path();
  30. }