Skip to content

Commit 0f65933

Browse files
committed
getCurrentExecutablePath() on macOS
1 parent b022661 commit 0f65933

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Resources/[Pp]resets
2121
*.pkg
2222
**.pkg
2323
installer/macOS/[Pp]ayload/*
24+
.vscode
2425
.vscode/*
2526
!.vscode/settings.json
2627
!.vscode/tasks.json

source/utils/marvin_Utils.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,23 @@
1010
#include <marvin/utils/marvin_Utils.h>
1111

1212
#if defined(MARVIN_MACOS)
13-
13+
#include <mach-o/dyld.h>
1414
#elif defined(MARVIN_WINDOWS)
1515
#include <windows.h>
1616
#elif defined(MARVIN_LINUX)
1717

1818
#endif
1919
namespace marvin::utils {
2020
std::optional<std::string> getCurrentExecutablePath() {
21+
constexpr static auto maxLength{ 512 };
2122
#if defined(MARVIN_MACOS)
22-
return {};
23+
char data[maxLength];
24+
std::uint32_t length{ sizeof(data) };
25+
const auto resCode = _NSGetExecutablePath(data, &length);
26+
if(resCode != 0) return {};
27+
std::string res = data;
28+
return res;
2329
#elif defined(MARVIN_WINDOWS)
24-
constexpr static auto maxLength{ 512 };
2530
char data[maxLength];
2631
auto dest = (LPSTR)data;
2732
GetModuleFileName(nullptr, dest, maxLength);

tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set(MARVIN_TEST_SOURCE
1515
${CMAKE_CURRENT_SOURCE_DIR}/math/marvin_LeakyIntegratorTests.cpp
1616
${CMAKE_CURRENT_SOURCE_DIR}/math/marvin_VecOpsTests.cpp
1717
${CMAKE_CURRENT_SOURCE_DIR}/math/marvin_WindowedSincInterpolatorTests.cpp
18+
${CMAKE_CURRENT_SOURCE_DIR}/utils/marvin_UtilsTests.cpp
1819
${CMAKE_CURRENT_SOURCE_DIR}/utils/marvin_SmoothedValueTests.cpp
1920
${CMAKE_CURRENT_SOURCE_DIR}/utils/marvin_RandomTests.cpp
2021
${CMAKE_CURRENT_SOURCE_DIR}/utils/marvin_FIFOTests.cpp

tests/utils/marvin_UtilsTests.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ========================================================================================================
2+
// _______ _______ ______ ___ ___ _______ _______
3+
// | | | _ | __ \ | |_ _| | |
4+
// | | | < | |_| |_| |
5+
// |__|_|__|___|___|___|__|\_____/|_______|__|____|
6+
//
7+
// This file is part of the Marvin open source library and is licensed under the terms of the MIT License.
8+
//
9+
// ========================================================================================================
10+
11+
#include <marvin/utils/marvin_Utils.h>
12+
#include <catch2/catch_test_macros.hpp>
13+
#include <catch2/matchers/catch_matchers_floating_point.hpp>
14+
#include <iostream>
15+
namespace marvin::testing {
16+
TEST_CASE("Test getCurrentExecutablePath()") {
17+
const auto pathRes = utils::getCurrentExecutablePath();
18+
REQUIRE(pathRes);
19+
std::cout << *pathRes << "\n";
20+
}
21+
22+
}

0 commit comments

Comments
 (0)