-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplatform_support.c
More file actions
137 lines (123 loc) · 2.65 KB
/
platform_support.c
File metadata and controls
137 lines (123 loc) · 2.65 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "platform_support.h"
#include <stdio.h>
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#elif defined(__APPLE__)
#include <mach-o/dyld.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#else
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#endif
int platform_support_get_executable_path(char* out_path, size_t out_path_size)
{
if (out_path == NULL || out_path_size == 0U)
{
return 0;
}
#if defined(_WIN32)
{
const DWORD length = GetModuleFileNameA(NULL, out_path, (DWORD)out_path_size);
if (length == 0U || length >= (DWORD)out_path_size)
{
out_path[0] = '\0';
return 0;
}
}
return 1;
#elif defined(__APPLE__)
{
uint32_t size = (uint32_t)out_path_size;
if (_NSGetExecutablePath(out_path, &size) != 0)
{
out_path[0] = '\0';
return 0;
}
}
return 1;
#else
{
const ssize_t length = readlink("/proc/self/exe", out_path, out_path_size - 1U);
if (length <= 0 || (size_t)length >= out_path_size)
{
out_path[0] = '\0';
return 0;
}
out_path[length] = '\0';
}
return 1;
#endif
}
int platform_support_get_current_directory(char* out_path, size_t out_path_size)
{
if (out_path == NULL || out_path_size == 0U)
{
return 0;
}
#if defined(_WIN32)
if (GetCurrentDirectoryA((DWORD)out_path_size, out_path) == 0U)
{
out_path[0] = '\0';
return 0;
}
#else
if (getcwd(out_path, out_path_size) == NULL)
{
out_path[0] = '\0';
return 0;
}
#endif
return 1;
}
int platform_support_file_exists(const char* path)
{
if (path == NULL || path[0] == '\0')
{
return 0;
}
#if defined(_WIN32)
{
const DWORD attributes = GetFileAttributesA(path);
if (attributes == INVALID_FILE_ATTRIBUTES)
{
return 0;
}
return (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0U;
}
#else
{
struct stat status = { 0 };
if (stat(path, &status) != 0)
{
return 0;
}
return S_ISREG(status.st_mode);
}
#endif
}
void platform_support_sleep_ms(unsigned int milliseconds)
{
#if defined(_WIN32)
Sleep(milliseconds);
#else
struct timespec duration = {
(time_t)(milliseconds / 1000U),
(long)((milliseconds % 1000U) * 1000000UL)
};
while (nanosleep(&duration, &duration) != 0);
#endif
}
void platform_support_show_error_dialog(const char* title, const char* message)
{
const char* safe_title = (title != NULL) ? title : "Error";
const char* safe_message = (message != NULL) ? message : "Unknown error";
#if defined(_WIN32)
(void)MessageBoxA(NULL, safe_message, safe_title, MB_ICONERROR | MB_OK);
#else
(void)fprintf(stderr, "%s: %s\n", safe_title, safe_message);
#endif
}