41 std::vector<wchar_t> buffer(MAX_PATH);
42 HMODULE hModule = GetModuleHandle(NULL);
45 DWORD size = GetModuleFileNameW(hModule, buffer.data(), buffer.size());
48 while (size == buffer.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
49 buffer.resize(buffer.size() * 2);
50 size = GetModuleFileNameW(hModule, buffer.data(), buffer.size());
53 if (size == 0)
throw std::runtime_error(
"Failed to get executable path.");
54 std::wstring exe_path(buffer.begin(), buffer.begin() + size);
55 std::wstring::size_type pos = exe_path.find_last_of(L
"\\/");
56 if (pos != std::wstring::npos) {
57 exe_path = exe_path.substr(0, pos);
60# if __cplusplus >= 201703L
61 auto utf8_path = std::filesystem::path(exe_path).u8string();
62# if defined(__cpp_char8_t)
63 return std::string(
reinterpret_cast<const char*
>(utf8_path.data()), utf8_path.size());
68 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
69 return converter.to_bytes(exe_path);
72# elif defined(__APPLE__)
73 uint32_t path_size = 0;
74 if (_NSGetExecutablePath(NULL, &path_size) != -1 || path_size == 0) {
75 throw std::runtime_error(
"Failed to determine executable path size.");
78 std::vector<char> buffer(path_size + 1,
'\0');
79 if (_NSGetExecutablePath(buffer.data(), &path_size) != 0) {
80 throw std::runtime_error(
"Failed to get executable path.");
83 std::string exe_path(buffer.data());
84 std::vector<char> resolved_path(PATH_MAX,
'\0');
85 if (realpath(exe_path.c_str(), resolved_path.data()) != NULL) {
86 exe_path.assign(resolved_path.data());
89 const size_t pos = exe_path.find_last_of(
"/");
90 if (pos != std::string::npos) {
96 char result[PATH_MAX];
97 ssize_t count = readlink(
"/proc/self/exe", result, PATH_MAX);
99 if (count == -1)
throw std::runtime_error(
"Failed to get executable path.");
101 std::string exe_path(result, count);
104 size_t pos = exe_path.find_last_of(
"\\/");
105 if (pos != std::string::npos) {
106 exe_path = exe_path.substr(0, pos);
Primary namespace for the Kurlyk library, encompassing initialization, request management,...