Skip to content

Commit

Permalink
FilePath: Added ability to retreive exectuable file path.
Browse files Browse the repository at this point in the history
  • Loading branch information
bkaradzic committed Jun 30, 2023
1 parent 781ad4a commit db15e54
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 19 deletions.
7 changes: 4 additions & 3 deletions include/bx/filepath.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ namespace bx
/// Special OS directories:
enum Enum
{
Current, //!< Current directory.
Temp, //!< Temporary directory.
Home, //!< User's home directory.
Current, //!< Current directory.
Executable, //!< Executable file path.
Home, //!< User's home directory.
Temp, //!< Temporary directory.

Count
};
Expand Down
50 changes: 34 additions & 16 deletions src/filepath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
#endif // !BX_CRT_NONE

#if BX_PLATFORM_WINDOWS
extern "C" __declspec(dllimport) unsigned long __stdcall GetTempPathA(unsigned long _max, char* _ptr);
extern "C" __declspec(dllimport) unsigned long __stdcall GetModuleFileNameA(void* _module, char* _outFilePath, unsigned long _size);
extern "C" __declspec(dllimport) unsigned long __stdcall GetTempPathA(unsigned long _max, char* _outFilePath);
#endif // BX_PLATFORM_WINDOWS

namespace bx
Expand Down Expand Up @@ -180,7 +181,7 @@ namespace bx
return ::_getcwd(_buffer, (int32_t)_size);
#else
return ::getcwd(_buffer, _size);
#endif // BX_COMPILER_
#endif // BX_PLATFORM_*
}

static bool getCurrentPath(char* _out, uint32_t* _inOutSize)
Expand All @@ -195,6 +196,29 @@ namespace bx
return false;
}

static bool getExecutablePath(char* _out, uint32_t* _inOutSize)
{
#if BX_PLATFORM_WINDOWS
uint32_t len = ::GetModuleFileNameA(NULL, _out, *_inOutSize);
bool result = len != 0 && len < *_inOutSize;
*_inOutSize = len;
return result;
#elif BX_PLATFORM_LINUX
char tmp[64];
snprintf(tmp, sizeof(tmp), "/proc/%d/exe", getpid() );
ssize_t result = readlink(tmp, _out, *_inOutSize);

if (-1 < result)
{
*_inOutSize = uint32_t(result);
return true;
}
#elif BX_PLATFORM_OSX
#endif // BX_PLATFORM_*

return false;
}

static bool getHomePath(char* _out, uint32_t* _inOutSize)
{
return false
Expand Down Expand Up @@ -287,28 +311,22 @@ namespace bx

void FilePath::set(Dir::Enum _dir)
{
bool ok = false;
char tmp[kMaxFilePath];
uint32_t len = BX_COUNTOF(tmp);

switch (_dir)
{
case Dir::Current:
getCurrentPath(tmp, &len);
break;

case Dir::Temp:
getTempPath(tmp, &len);
break;
case Dir::Current: ok = getCurrentPath(tmp, &len); break;
case Dir::Executable: ok = getExecutablePath(tmp, &len); break;
case Dir::Home: ok = getHomePath(tmp, &len); break;
case Dir::Temp: ok = getTempPath(tmp, &len); break;

case Dir::Home:
getHomePath(tmp, &len);
break;

default:
len = 0;
break;
default: break;
}

len = ok ? len : 0;

set(StringView(tmp, len) );
}

Expand Down
27 changes: 27 additions & 0 deletions tests/filepath_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,30 @@ TEST_CASE("FilePath temp", "[filepath]")
REQUIRE(bx::removeAll(tmp, &err) );
REQUIRE(err.isOk() );
}

TEST_CASE("FilePath special", "[filepath]")
{
{
bx::FilePath tmp(bx::Dir::Current);
bx::StringView sv(tmp);
DBG("%S", &sv);
}

{
bx::FilePath tmp(bx::Dir::Executable);
bx::StringView sv(tmp);
DBG("%S", &sv);
}

{
bx::FilePath tmp(bx::Dir::Home);
bx::StringView sv(tmp);
DBG("%S", &sv);
}

{
bx::FilePath tmp(bx::Dir::Temp);
bx::StringView sv(tmp);
DBG("%S", &sv);
}
}

0 comments on commit db15e54

Please sign in to comment.