| 407 | } |
| 408 | |
| 409 | static fs::path get_executable_path() { |
| 410 | #if defined(__APPLE__) |
| 411 | // get executable path |
| 412 | std::vector<char> path; |
| 413 | uint32_t size; |
| 414 | while (true) { |
| 415 | size = path.size(); |
| 416 | if (_NSGetExecutablePath(path.data(), &size) == 0) { |
| 417 | break; |
| 418 | } |
| 419 | path.resize(size); |
| 420 | } |
| 421 | std::string base_path(path.data(), size); |
| 422 | // remove executable name |
| 423 | auto last_slash = base_path.find_last_of('/'); |
| 424 | if (last_slash != std::string::npos) { |
| 425 | base_path = base_path.substr(0, last_slash); |
| 426 | } |
| 427 | return base_path + "/"; |
| 428 | #elif defined(__linux__) || defined(__FreeBSD__) |
| 429 | std::string base_path = "."; |
| 430 | std::vector<char> path(1024); |
| 431 | while (true) { |
| 432 | // get executable path |
| 433 | # if defined(__linux__) |
| 434 | ssize_t len = readlink("/proc/self/exe", path.data(), path.size()); |
| 435 | # elif defined(__FreeBSD__) |
| 436 | ssize_t len = readlink("/proc/curproc/file", path.data(), path.size()); |
| 437 | # endif |
| 438 | if (len == -1) { |
| 439 | break; |
| 440 | } |
| 441 | if (len < (ssize_t) path.size()) { |
| 442 | base_path = std::string(path.data(), len); |
| 443 | // remove executable name |
| 444 | auto last_slash = base_path.find_last_of('/'); |
| 445 | if (last_slash != std::string::npos) { |
| 446 | base_path = base_path.substr(0, last_slash); |
| 447 | } |
| 448 | break; |
| 449 | } |
| 450 | path.resize(path.size() * 2); |
| 451 | } |
| 452 | |
| 453 | return base_path + "/"; |
| 454 | #elif defined(_WIN32) |
| 455 | std::vector<wchar_t> path(MAX_PATH); |
| 456 | DWORD len = GetModuleFileNameW(NULL, path.data(), path.size()); |
| 457 | if (len == 0) { |
| 458 | return {}; |
| 459 | } |
| 460 | std::wstring base_path(path.data(), len); |
| 461 | // remove executable name |
| 462 | auto last_slash = base_path.find_last_of('\\'); |
| 463 | if (last_slash != std::string::npos) { |
| 464 | base_path = base_path.substr(0, last_slash); |
| 465 | } |
| 466 | return base_path + L"\\"; |