This repository was archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess.hpp
78 lines (65 loc) · 1.99 KB
/
process.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2023 The Jule Programming Language.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.
#ifndef __JULE_PROCESS_HPP
#define __JULE_PROCESS_HPP
#include <cstring>
#include "platform.hpp"
#include "str.hpp"
#include "slice.hpp"
#include "utf16.hpp"
#if defined(OS_DARWIN)
#include <mach-o/dyld.h>
#include <climits>
#elif defined(OS_WINDOWS)
#include <windows.h>
#elif defined(OS_LINUX)
#include <unistd.h>
#include <linux/limits.h>
#endif
namespace jule {
jule::Slice<jule::Str> command_line_args;
void setup_command_line_args(int argc, char *argv[]) noexcept;
jule::Str executable(void) noexcept;
void setup_command_line_args(int argc, char *argv[]) noexcept {
#ifdef OS_WINDOWS
const LPWSTR cmdl{ GetCommandLineW() };
LPWSTR *argvw{ CommandLineToArgvW(cmdl, &argc) };
#endif
jule::command_line_args = jule::Slice<jule::Str>::alloc(argc);
for (jule::Int i{ 0 }; i < argc; ++i) {
#ifdef OS_WINDOWS
const LPWSTR warg{ argvw[i] };
jule::command_line_args[i] = jule::utf16_to_utf8_str(warg, std::wcslen(warg));
#else
jule::command_line_args[i] = argv[i];
#endif
}
#ifdef OS_WINDOWS
LocalFree(argvw);
argvw = nullptr;
#endif
}
jule::Str executable(void) noexcept {
#if defined(OS_DARWIN)
char buff[PATH_MAX];
uint32_t buff_size{ PATH_MAX };
if(!_NSGetExecutablePath(buff, &buff_size))
return jule::Str(buff);
return jule::Str();
#elif defined(OS_WINDOWS)
wchar_t buffer[MAX_PATH];
const DWORD n{ GetModuleFileNameW(NULL, buffer, MAX_PATH) };
if (n)
return jule::utf16_to_utf8_str(&buffer[0], n);
return jule::Str();
#elif defined(OS_LINUX)
char result[PATH_MAX];
const ssize_t count{ readlink("/proc/self/exe", result, PATH_MAX) };
if (count != -1)
return jule::Str(result);
return jule::Str();
#endif
}
} // namespace jule
#endif // ifndef __JULE_PROCESS_HPP