Skip to content

Commit 438e7c9

Browse files
committed
Added function to determine the path of the executable
1 parent b48493a commit 438e7c9

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

include/sysinfo.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#pragma once
1616

1717
#include <string>
18+
#include <filesystem>
1819

1920
#if defined(_WIN32) || defined(_WIN64)
2021
#include <windows.h>
@@ -673,4 +674,27 @@ inline std::string getVersion() {
673674
(torch::xpu::is_available() ? ", XPU" : "") + ")\n";
674675
}
675676

677+
/// @brief Returns the path of the executable
678+
std::filesystem::path getExecutablePath()
679+
{
680+
#if defined(_WIN32)
681+
char buffer[MAX_PATH];
682+
DWORD len = GetModuleFileNameA(NULL, buffer, MAX_PATH);
683+
if (len == 0 || len == MAX_PATH)
684+
throw std::runtime_error("Failed to get executable path");
685+
return std::filesystem::path(buffer).parent_path();
686+
687+
#elif defined(__APPLE__)
688+
char buffer[1024];
689+
uint32_t size = sizeof(buffer);
690+
if (_NSGetExecutablePath(buffer, &size) != 0)
691+
throw std::runtime_error("Buffer too small for executable path");
692+
return std::filesystem::canonical(buffer).parent_path();
693+
694+
#else // Linux / Unix
695+
std::filesystem::path exe = std::filesystem::canonical("/proc/self/exe");
696+
return exe.parent_path();
697+
#endif
698+
}
699+
676700
} // namespace iganet

0 commit comments

Comments
 (0)