| 380 | } |
| 381 | |
| 382 | static fs::path get_executable_path() { |
| 383 | #if defined(__APPLE__) |
| 384 | // get executable path |
| 385 | std::vector<char> path; |
| 386 | uint32_t size; |
| 387 | while (true) { |
| 388 | size = path.size(); |
| 389 | if (_NSGetExecutablePath(path.data(), &size) == 0) { |
| 390 | break; |
| 391 | } |
| 392 | path.resize(size); |
| 393 | } |
| 394 | std::string base_path(path.data(), size); |
| 395 | // remove executable name |
| 396 | auto last_slash = base_path.find_last_of('/'); |
| 397 | if (last_slash != std::string::npos) { |
| 398 | base_path = base_path.substr(0, last_slash); |
| 399 | } |
| 400 | return base_path + "/"; |
| 401 | #elif defined(__linux__) || defined(__FreeBSD__) |
| 402 | std::string base_path = "."; |
| 403 | std::vector<char> path(1024); |
| 404 | while (true) { |
| 405 | // get executable path |
| 406 | # if defined(__linux__) |
| 407 | ssize_t len = readlink("/proc/self/exe", path.data(), path.size()); |
| 408 | # elif defined(__FreeBSD__) |
| 409 | ssize_t len = readlink("/proc/curproc/file", path.data(), path.size()); |
| 410 | # endif |
| 411 | if (len == -1) { |
| 412 | break; |
| 413 | } |
| 414 | if (len < (ssize_t) path.size()) { |
| 415 | base_path = std::string(path.data(), len); |
| 416 | // remove executable name |
| 417 | auto last_slash = base_path.find_last_of('/'); |
| 418 | if (last_slash != std::string::npos) { |
| 419 | base_path = base_path.substr(0, last_slash); |
| 420 | } |
| 421 | break; |
| 422 | } |
| 423 | path.resize(path.size() * 2); |
| 424 | } |
| 425 | |
| 426 | return base_path + "/"; |
| 427 | #elif defined(_WIN32) |
| 428 | std::vector<wchar_t> path(MAX_PATH); |
| 429 | DWORD len = GetModuleFileNameW(NULL, path.data(), path.size()); |
| 430 | if (len == 0) { |
| 431 | return {}; |
| 432 | } |
| 433 | std::wstring base_path(path.data(), len); |
| 434 | // remove executable name |
| 435 | auto last_slash = base_path.find_last_of('\\'); |
| 436 | if (last_slash != std::string::npos) { |
| 437 | base_path = base_path.substr(0, last_slash); |
| 438 | } |
| 439 | return base_path + L"\\"; |
no test coverage detected