Skip to content

Commit

Permalink
Aura - Add getExecutableDirectory()
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Jan 27, 2024
1 parent 38f5ef1 commit 416916e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ if(USING_VCPKG)
target_link_libraries(${PROJECT_NAME} PUBLIC unofficial::maddy::maddy)
endif()
if(WIN32)
target_link_libraries(${PROJECT_NAME} PUBLIC Advapi32 Dwmapi Gdiplus Shell32)
target_link_libraries(${PROJECT_NAME} PUBLIC Advapi32 Dwmapi Gdiplus Kernel32 Shell32)
elseif(LINUX)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
Expand Down
4 changes: 4 additions & 0 deletions docs/aura.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ Path: `Nickvision::Aura::Aura`
```
- Returns: The reference to the singleton `Aura` object.
- Throws: `std::logic_error` if `Aura::init()` was not yet called.
- ```cpp
std::filesystem::path getExecutableDirectory()
```
- Returns: The path of the executable's directory.
- ```cpp
std::string getEnvVar(const std::string& key)
```
Expand Down
5 changes: 5 additions & 0 deletions include/aura/aura.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ namespace Nickvision::Aura
* @return The active aura instance
*/
static Aura& getActive();
/**
* @brief Gets the path of the executable's directory.
* @return The executable's directory path
*/
static std::filesystem::path getExecutableDirectory() noexcept;
/**
* @brief Gets a system environment variable.
* @param key The environment variable to get
Expand Down
21 changes: 19 additions & 2 deletions src/aura/aura.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#include "filesystem/systemdirectories.h"
#include "localization/gettext.h"
#include "helpers/stringhelpers.h"
#ifdef __linux__
#ifdef _WIN32
#include <windows.h>
#elif defined(__linux__)
#include <stdlib.h>
#endif

Expand Down Expand Up @@ -63,6 +65,21 @@ namespace Nickvision::Aura
return *m_instance;
}

std::filesystem::path Aura::getExecutableDirectory() noexcept
{
#ifdef _WIN32
char pth[MAX_PATH];
DWORD len{ GetModuleFileNameA(nullptr, pth, sizeof(pth)) };
if(len > 0)
{
return std::filesystem::path(std::string(pth, len)).parent_path();
}
#elif defined(__linux__)
return std::filesystem::canonical("/proc/self/exe").parent_path();
#endif
return {};
}

std::string Aura::getEnvVar(const std::string& key) noexcept
{
char* var{ std::getenv(key.c_str()) };
Expand Down Expand Up @@ -102,7 +119,7 @@ namespace Nickvision::Aura
}
}
locations[dependency] = std::filesystem::path();
std::filesystem::path path{ std::filesystem::current_path() / dependency };
std::filesystem::path path{ getExecutableDirectory() / dependency };
if (std::filesystem::exists(path))
{
locations[dependency] = path;
Expand Down
5 changes: 3 additions & 2 deletions src/localization/gettext.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "localization/gettext.h"
#include <filesystem>
#include <cstdlib>
#include "aura/aura.h"

namespace Nickvision::Localization
{
Expand All @@ -15,9 +16,9 @@ namespace Nickvision::Localization
setlocale(LC_ALL, "");
m_domainName = domainName;
#ifdef _WIN32
res = res && (wbindtextdomain(m_domainName.c_str(), std::filesystem::current_path().c_str()) != nullptr);
res = res && (wbindtextdomain(m_domainName.c_str(), Aura::Aura::getExecutableDirectory().c_str()) != nullptr);
#elif defined(__linux__)
res = res && (bindtextdomain(m_domainName.c_str(), std::filesystem::current_path().c_str()) != nullptr);
res = res && (bindtextdomain(m_domainName.c_str(), Aura::Aura::getExecutableDirectory().c_str()) != nullptr);
res = res && (bind_textdomain_codeset(m_domainName.c_str(), "UTF-8") != nullptr);
#endif
res = res && (textdomain(m_domainName.c_str()) != nullptr);
Expand Down
1 change: 1 addition & 0 deletions tests/auratests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class AuraTest : public testing::Test
TEST_F(AuraTest, EnsureAura)
{
ASSERT_NO_THROW(Aura::getActive());
std::cout << Aura::getExecutableDirectory() << std::endl;
}

TEST_F(AuraTest, SetAppInfo)
Expand Down
1 change: 1 addition & 0 deletions tests/stringtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ TEST(StringTests, Guid1)
ASSERT_NO_THROW(s = StringHelpers::newGuid());
ASSERT_FALSE(s.empty());
ASSERT_TRUE(s.size() == 36);
std::cout << s << std::endl;
}

TEST(StringTests, UrlValidity1)
Expand Down

0 comments on commit 416916e

Please sign in to comment.