1
0

xsystem.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /***************************************************************************
  2. * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
  3. * Copyright (c) QuantStack *
  4. * *
  5. * Distributed under the terms of the BSD 3-Clause License. *
  6. * *
  7. * The full license is in the file LICENSE, distributed with this software. *
  8. ****************************************************************************/
  9. #ifndef XTL_XSYSTEM_HPP
  10. #define XTL_XSYSTEM_HPP
  11. #if defined(__linux__)
  12. # include <unistd.h>
  13. #endif
  14. #if defined(_WIN32)
  15. # if defined(NOMINMAX)
  16. # include <windows.h>
  17. # else
  18. # define NOMINMAX
  19. # include <windows.h>
  20. # undef NOMINMAX
  21. # endif
  22. #endif
  23. #ifdef __APPLE__
  24. # include <cstdint>
  25. # include <mach-o/dyld.h>
  26. #endif
  27. #if defined(__sun)
  28. # include <stdlib.h>
  29. #endif
  30. #ifdef __FreeBSD__
  31. # include <sys/types.h>
  32. # include <sys/sysctl.h>
  33. #endif
  34. #include <cstring>
  35. #include <string>
  36. namespace xtl
  37. {
  38. std::string executable_path();
  39. std::string prefix_path();
  40. /******************
  41. * implementation *
  42. ******************/
  43. inline std::string executable_path()
  44. {
  45. std::string path;
  46. #if defined(UNICODE)
  47. wchar_t buffer[1024];
  48. #else
  49. char buffer[1024];
  50. #endif
  51. std::memset(buffer, '\0', sizeof(buffer));
  52. #if defined(__linux__)
  53. if (readlink("/proc/self/exe", buffer, sizeof(buffer)) != -1)
  54. {
  55. path = buffer;
  56. }
  57. else
  58. {
  59. // failed to determine run path
  60. }
  61. #elif defined (_WIN32)
  62. #if defined(UNICODE)
  63. if (GetModuleFileNameW(nullptr, buffer, sizeof(buffer)) != 0)
  64. {
  65. // Convert wchar_t to std::string
  66. std::wstring wideString(buffer);
  67. std::string narrowString(wideString.begin(), wideString.end());
  68. path = narrowString;
  69. }
  70. #else
  71. if (GetModuleFileNameA(nullptr, buffer, sizeof(buffer)) != 0)
  72. {
  73. path = buffer;
  74. }
  75. #endif
  76. // failed to determine run path
  77. #elif defined (__APPLE__)
  78. std::uint32_t size = sizeof(buffer);
  79. if(_NSGetExecutablePath(buffer, &size) == 0)
  80. {
  81. path = buffer;
  82. }
  83. else
  84. {
  85. // failed to determine run path
  86. }
  87. #elif defined (__FreeBSD__)
  88. int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
  89. size_t buffer_size = sizeof(buffer);
  90. if (sysctl(mib, 4, buffer, &buffer_size, NULL, 0) != -1)
  91. {
  92. path = buffer;
  93. }
  94. else
  95. {
  96. // failed to determine run path
  97. }
  98. #elif defined(__sun)
  99. path = getexecname();
  100. #endif
  101. return path;
  102. }
  103. inline std::string prefix_path()
  104. {
  105. std::string path = executable_path();
  106. #if defined (_WIN32)
  107. char separator = '\\';
  108. #else
  109. char separator = '/';
  110. #endif
  111. std::string bin_folder = path.substr(0, path.find_last_of(separator));
  112. std::string prefix = bin_folder.substr(0, bin_folder.find_last_of(separator)) + separator;
  113. return prefix;
  114. }
  115. }
  116. #endif