add platform::get_executable_path (#638)

This commit is contained in:
MihailRis
2025-10-03 21:22:16 +03:00
committed by GitHub
parent ecd95967c4
commit b14bad5d24
5 changed files with 88 additions and 8 deletions
+55 -5
View File
@@ -10,13 +10,22 @@
#include "stringutil.hpp"
#include "typedefs.hpp"
#include "debug/Logger.hpp"
static debug::Logger logger("platform");
#include "frontend/locale.hpp"
#ifdef _WIN32
#include <Windows.h>
#pragma comment(lib, "winmm.lib")
#else
#include <unistd.h>
#endif
namespace platform::internal {
std::filesystem::path get_executable_path();
}
static debug::Logger logger("platform");
#ifdef _WIN32
void platform::configure_encoding() {
// set utf-8 encoding to console output
SetConsoleOutputCP(CP_UTF8);
@@ -83,9 +92,6 @@ bool platform::open_url(const std::string& url) {
#else // _WIN32
#include <unistd.h>
#include "frontend/locale.hpp"
void platform::configure_encoding() {
}
@@ -158,3 +164,47 @@ void platform::open_folder(const std::filesystem::path& folder) {
#endif
}
std::filesystem::path platform::get_executable_path() {
#ifdef _WIN32
wchar_t buffer[MAX_PATH];
DWORD result = GetModuleFileNameW(NULL, buffer, MAX_PATH);
if (result == 0) {
DWORD error = GetLastError();
throw std::runtime_error("GetModuleFileName failed with code: " + std::to_string(error));
}
int size = WideCharToMultiByte(
CP_UTF8, 0, buffer, -1, nullptr, 0, nullptr, nullptr
);
if (size == 0) {
throw std::runtime_error("could not get executable path");
}
std::string str(size, 0);
size = WideCharToMultiByte(
CP_UTF8, 0, buffer, -1, str.data(), size, nullptr, nullptr
);
if (size == 0) {
DWORD error = GetLastError();
throw std::runtime_error("WideCharToMultiByte failed with code: " + std::to_string(error));
}
str.resize(size - 1);
return std::filesystem::path(str);
#elif defined(__APPLE__)
auto path = platform::internal::get_executable_path();
if (path.empty()) {
throw std::runtime_error("could not get executable path");
}
return path;
#else
char buffer[1024];
ssize_t count = readlink("/proc/self/exe", buffer, sizeof(buffer));
if (count != -1) {
return std::filesystem::canonical(std::filesystem::path(
std::string(buffer, static_cast<size_t>(count))
));
}
throw std::runtime_error("could not get executable path");
#endif
}
+6 -2
View File
@@ -5,13 +5,17 @@
namespace platform {
void configure_encoding();
/// @return environment locale in ISO format ll_CC
/// @brief Get Environment locale in ISO format ll_CC
std::string detect_locale();
/// @brief Open folder using system file manager asynchronously
/// @param folder target folder
void open_folder(const std::filesystem::path& folder);
/// Makes the current thread sleep for the specified amount of milliseconds.
/// @brief Makes the current thread sleep for the specified amount of milliseconds.
void sleep(size_t millis);
/// @brief Get current process id
int get_process_id();
/// @brief Get current process running executable path
std::filesystem::path get_executable_path();
/// @brief Open URL in web browser
bool open_url(const std::string& url);
}
+24
View File
@@ -0,0 +1,24 @@
#include <filesystem>
#ifndef _WIN32
#include <unistd.h>
#endif
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
namespace platform::internal {
std::filesystem::path get_executable_path() {
#ifdef __APPLE__
char buffer[1024];
uint32_t size = sizeof(buffer);
if (_NSGetExecutablePath(buffer, &size) == 0) {
return std::filesystem::canonical(std::filesystem::path(buffer));
} else {
return std::filesystem::path();
}
#endif
return std::filesystem::path();
}
}