feat(os): implement OsGetExePath, OsGetExeName

This commit is contained in:
phaneron 2023-08-23 18:09:03 -04:00
parent 90075755b1
commit b4e14cf4d5
2 changed files with 65 additions and 0 deletions

57
bc/os/Path.cpp Normal file
View file

@ -0,0 +1,57 @@
#include "bc/os/Path.hpp"
#include "bc/String.hpp"
#include "bc/file/Path.hpp"
#if defined(WHOA_SYSTEM_WIN)
#include <windows.h>
#endif
#if defined(WHOA_SYSTEM_MAC)
#include <mach-o/dyld.h>
#endif
// Get the full path of the currently running .exe/ELF/Mach-O executable
void OsGetExeName(char* buffer, size_t chars) {
#if defined(WHOA_SYSTEM_WIN)
// Win32
GetModuleFileName(nullptr, buffer, chars);
#endif
#if defined(WHOA_SYSTEM_MAC)
// Darwin
char path[1024] = {0};
_NSGetExecutablePath(path, 1024);
char actualPath[1024] = {0};
realpath(path, actualPath);
Blizzard::File::Path::MakeBackslashPath(actualPath, buffer, chars);
#endif
#if defined(WHOA_SYSTEM_LINUX)
// procfs
char actualPath[4096] = {0};
readlink("/proc/self/exe", actualPath, chars);
Blizzard::File::Path::MakeBackslashPath(actualPath, buffer, chars);
#endif
}
void OsPathStripFilename(char* path) {
auto length = String::Length(path);
char* head = &path[length-1];
while ((head != (path-1)) && (*head != '\0')) {
if (*head == '\\') {
*(head + 1) = '\0';
break;
}
head--;
}
}
// Get the directory containing the currently running executable
void OsGetExePath(char* dest, size_t chars) {
OsGetExeName(dest, chars);
OsPathStripFilename(dest);
}

8
bc/os/Path.hpp Normal file
View file

@ -0,0 +1,8 @@
#ifndef BC_OS_PATH_HPP
#define BC_OS_PATH_HPP
void OsGetExePath(char* buffer, size_t chars);
void OsGetExeName(char* buffer, size_t chars);
#endif