38 std::vector<wchar_t> buffer(MAX_PATH);
39 HMODULE hModule = GetModuleHandle(NULL);
42 DWORD size = GetModuleFileNameW(hModule, buffer.data(), buffer.size());
45 while (size == buffer.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
46 buffer.resize(buffer.size() * 2);
47 size = GetModuleFileNameW(hModule, buffer.data(), buffer.size());
50 if (size == 0)
throw std::runtime_error(
"Failed to get executable path.");
51 std::wstring exe_path(buffer.begin(), buffer.begin() + size);
52 std::wstring::size_type pos = exe_path.find_last_of(L
"\\/");
53 if (pos != std::wstring::npos) {
54 exe_path = exe_path.substr(0, pos);
57# if __cplusplus >= 201703L
58 auto utf8_path = std::filesystem::path(exe_path).u8string();
59# if defined(__cpp_char8_t)
60 return std::string(
reinterpret_cast<const char*
>(utf8_path.data()), utf8_path.size());
65 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
66 return converter.to_bytes(exe_path);
69# elif defined(__APPLE__)
70 uint32_t path_size = 0;
71 if (_NSGetExecutablePath(NULL, &path_size) != -1 || path_size == 0) {
72 throw std::runtime_error(
"Failed to determine executable path size.");
75 std::vector<char> buffer(path_size + 1,
'\0');
76 if (_NSGetExecutablePath(buffer.data(), &path_size) != 0) {
77 throw std::runtime_error(
"Failed to get executable path.");
80 std::string exe_path(buffer.data());
81 std::vector<char> resolved_path(PATH_MAX,
'\0');
82 if (realpath(exe_path.c_str(), resolved_path.data()) != NULL) {
83 exe_path.assign(resolved_path.data());
86 const size_t pos = exe_path.find_last_of(
"/");
87 if (pos != std::string::npos) {
93 char result[PATH_MAX];
94 ssize_t count = readlink(
"/proc/self/exe", result, PATH_MAX);
96 if (count == -1)
throw std::runtime_error(
"Failed to get executable path.");
98 std::string exe_path(result, count);
101 size_t pos = exe_path.find_last_of(
"\\/");
102 if (pos != std::string::npos) {
103 exe_path = exe_path.substr(0, pos);
Primary namespace for the Kurlyk library, encompassing initialization, request management,...