Initial version

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman
2023-09-15 11:18:54 +02:00
commit bcd2fd127a
15 changed files with 3414 additions and 0 deletions

17
include/MacStd/common.hpp Normal file
View File

@@ -0,0 +1,17 @@
/*
SPDX-License-Identifier: MIT
Copyright © 2023 Amebis
*/
#pragma once
#include <stddef.h>
///
/// Size of the stack buffer in bytes used for initial system function call
///
/// Some system functions with variable length output data fail for insufficient buffer sizes. The function helpers use a fixed size stack buffer first. If the stack buffer really prooved sufficient, the helper allocates the exact length output on heap and copies the data without calling the system function again. Otherwise it allocates the memory on heap and retries.
///
/// \note Decrease this value in case of stack overflow.
///
#define MACSTD_STACK_BUFFER_BYTES 1024

35
include/MacStd/dyld.hpp Normal file
View File

@@ -0,0 +1,35 @@
/*
SPDX-License-Identifier: MIT
Copyright © 2023 Amebis
*/
#pragma once
#include "common.hpp"
#include <mach-o/dyld.h>
#include <string>
///
/// Copies the path of the main executable into the string path.
///
/// \sa [_NSGetExecutablePath function](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dyld.3.html)
///
inline int _NSGetExecutablePath(std::string& path)
{
char stack_buffer[MACSTD_STACK_BUFFER_BYTES];
uint32_t capacity = MACSTD_STACK_BUFFER_BYTES;
int result = _NSGetExecutablePath(stack_buffer, &capacity);
if (result == 0) {
path = stack_buffer;
return 0;
}
if (result == -1) {
std::unique_ptr<char[]> heap_buffer(new char[capacity]);
result = _NSGetExecutablePath(heap_buffer.get(), &capacity);
if (result == 0) {
path = heap_buffer.get();
return 0;
}
}
return result;
}