// // Created by shell on 2025/12/4. // #include "util/application_path.hpp" #include #include #ifdef _WIN32 #include #elif __linux__ #include #else #error "Getting application path operation is only supported on Windows or Linux" #endif namespace shell::util { #ifdef _WIN32 std::filesystem::path ApplicationPath::get() { std::wstring path(20, L'\0'); while (true) { const DWORD len = GetModuleFileNameW(nullptr, &path[0], path.size()); if (len == 0) { throw std::runtime_error("GetModuleFileName() failed"); } if (len < path.size()) { path.resize(len); return std::filesystem::path{path}; } path.resize(path.size() * 2); } } #elif __linux__ std::filesystem::path ApplicationPath::get() { std::vector buffer(256); while (true) { const size_t len = readlink("/proc/self/exe", buffer.data(), buffer.size()); if (len == -1) { throw std::runtime_error("failed to read /proc/self/exe"); } if (len < buffer.size()) { return std::filesystem::path{std::string(buffer.data(), len)}; } buffer.reserve(buffer.size() * 2); } } #endif }