-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
paths
410 lines (333 loc) · 15.8 KB
/
paths
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_IO_PATHS_HPP
#define GTL_IO_PATHS_HPP
// Summary: Collection of cross platform functions to provide useful paths.
#if defined(linux) || defined(__linux) || defined(__linux__)
# include <cstring>
# include <dirent.h>
# include <linux/limits.h>
# include <pwd.h>
# include <sys/stat.h>
# include <unistd.h>
#endif
#if defined(_WIN32)
# if defined(_MSC_VER)
# pragma warning(push, 0)
# endif
# define WIN32_LEAN_AND_MEAN
# define VC_EXTRALEAN
# define STRICT
# include <sdkddkver.h>
# if defined(_AFXDLL)
# include <afxwin.h>
# else
# include <Windows.h>
# endif
# include <direct.h>
# include <Shlobj.h>
# if defined(_MSC_VER)
# pragma warning(pop)
# endif
#endif
#if defined(__APPLE__)
# include <cstring>
# include <dirent.h>
# include <mach-o/dyld.h>
# include <pwd.h>
# include <sys/stat.h>
# include <sys/syslimits.h>
# include <unistd.h>
#endif
#if defined(_MSC_VER)
# pragma warning(push, 0)
#endif
#include <algorithm>
#include <string>
#include <vector>
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
namespace gtl {
class paths final {
private:
/// @brief Deleted destructor.
~paths() = delete;
/// @brief Deleted constructor.
paths() = delete;
/// @brief Deleted copy constructor.
paths(paths&) = delete;
/// @brief Deleted move constructor.
paths(paths&&) = delete;
/// @brief Deleted copy assignment operator.
paths& operator=(paths& other) = delete;
/// @brief Deleted move assignment operator.
paths& operator=(paths&& other) = delete;
public:
/// @brief Get the full path to the current executable.
/// @param[out] executable_path the full path to the current executable.
/// @return true if the executable path was successfully returned, false otherwise.
static bool get_executable_path(std::string& executable_path) {
executable_path.clear();
#if defined(linux) || defined(__linux) || defined(__linux__)
executable_path.resize(PATH_MAX + 1, '\0');
ssize_t executable_path_size = ::readlink("/proc/self/exe", &executable_path[0], PATH_MAX);
if (executable_path_size <= 0) {
return false;
}
executable_path.resize(static_cast<std::size_t>(executable_path_size));
return true;
#elif defined(_WIN32)
executable_path.resize(MAX_PATH + 1, '\0');
GetModuleFileNameA(nullptr, &executable_path[0], MAX_PATH);
std::size_t executable_path_size = executable_path.find_last_not_of('\0');
if (executable_path_size == std::string::npos) {
return false;
}
executable_path.resize(executable_path_size + 1);
return true;
#elif defined(__APPLE__)
uint32_t executable_path_size = 0;
if (_NSGetExecutablePath(nullptr, &executable_path_size) == 0) {
return false;
}
if (executable_path_size == 0) {
return false;
}
executable_path.resize(executable_path_size, '\0');
if (_NSGetExecutablePath(&executable_path[0], &executable_path_size) != 0) {
return false;
}
executable_path.resize(executable_path_size - 1);
return true;
#else
return false;
#endif
}
/// @brief Get the name of the current executable.
/// @param[out] executable_name the name of the current executable.
/// @return true if the executable name was successfully returned, false otherwise.
static bool get_executable_name(std::string& executable_name) {
executable_name.clear();
std::string executable_path;
if (!get_executable_path(executable_path)) {
return false;
}
std::string::size_type executable_name_start_index = executable_path.find_last_of("\\/");
if (executable_name_start_index == std::string::npos) {
return false;
}
executable_name = executable_path.substr(executable_name_start_index + 1);
executable_name = executable_name.substr(0, executable_name.find_last_of("."));
return true;
}
/// @brief Get the directory of the current executable.
/// @param[out] executable_directory the directory of the current executable.
/// @return true if the executable directory was successfully returned, false otherwise.
static bool get_executable_directory(std::string& executable_directory) {
executable_directory.clear();
std::string executable_path;
if (!get_executable_path(executable_path)) {
return false;
}
std::string::size_type executable_directory_end_index = executable_path.find_last_of("\\/");
if (executable_directory_end_index == std::string::npos) {
return false;
}
executable_directory = executable_path.substr(0, executable_directory_end_index);
return true;
}
/// @brief Get the directory of the current executable.
/// @param directory_path The path to the directory to process.
/// @return An array of paths, one for each entry inside the specified directory.
static std::vector<std::string> get_directory_contents(const std::string& directory_path) {
#if (defined(linux) || defined(__linux) || defined(__linux__)) || defined(__APPLE__)
DIR* directory_handle;
if ((directory_handle = opendir(directory_path.c_str())) == nullptr) {
return {};
}
std::vector<std::string> directory_contents;
struct dirent* directoryEntry;
while ((directoryEntry = readdir(directory_handle)) != nullptr) {
if ((std::strcmp(directoryEntry->d_name, ".") == 0) || (std::strcmp(directoryEntry->d_name, "..") == 0)) {
continue;
}
directory_contents.emplace_back(directory_path + "/" + std::string(&directoryEntry->d_name[0]));
}
closedir(directory_handle);
std::sort(directory_contents.begin(), directory_contents.end());
return directory_contents;
#elif defined(_WIN32)
HANDLE directory_handle;
WIN32_FIND_DATA directory_entry;
if ((directory_handle = FindFirstFileA((directory_path + "/*").c_str(), &directory_entry)) == INVALID_HANDLE_VALUE) {
return {};
}
std::vector<std::string> directory_contents;
do {
if ((std::strcmp(directory_entry.cFileName, ".") == 0) || (std::strcmp(directory_entry.cFileName, "..") == 0)) {
continue;
}
directory_contents.push_back(directory_path + "/" + directory_entry.cFileName);
} while(FindNextFileA(directory_handle, &directory_entry));
FindClose(directory_handle);
std::sort(directory_contents.begin(), directory_contents.end());
return directory_contents;
#else
return {};
#endif
}
/// @brief Get the current working directory.
/// @param[out] current_working_directory The current working directory.
/// @return true if the current working directory was successfully returned, false otherwise.
static bool get_current_working_directory(std::string& current_working_directory) {
current_working_directory.clear();
#if (defined(linux) || defined(__linux) || defined(__linux__)) || defined(__APPLE__)
// Get the current working directory status.
struct stat status_current_directory = {};
if (stat(".", &status_current_directory) != 0) {
return false;
}
// Get the root directory status.
struct stat status_root_directory = {};
if (stat("/", &status_root_directory) != 0) {
return false;
}
// Attempt to keep stepping up the directory tree until we find the root.
std::string parent_directory = "./..";
std::vector<std::string> path_components;
while ((status_current_directory.st_dev != status_root_directory.st_dev) || (status_current_directory.st_ino != status_root_directory.st_ino)) {
// Get the status of the parent directory.
struct stat status_parent_directory = {};
if (stat(parent_directory.c_str(), &status_parent_directory) != 0) {
return false;
}
bool pushed = false;
// Check each directory in the parent until we find the current directory.
std::vector<std::string> directory_contents = get_directory_contents(parent_directory);
for (const std::string& directory : directory_contents) {
// Get the potentially current directory status.
struct stat status_potential_directory = {};
if (!lstat(directory.c_str(), &status_potential_directory)) {
// Compare the current directory and the potentially current directory.
if ((status_current_directory.st_dev == status_potential_directory.st_dev) && (status_current_directory.st_ino == status_potential_directory.st_ino)) {
// If we have successfully found the current directory update the current to the parent and continue the search.
status_current_directory = status_parent_directory;
parent_directory += "/..";
path_components.emplace_back(directory.substr(directory.find_last_of("\\/") + 1));
pushed = true;
break;
}
}
}
// If we failed to find the current directory inside the parent directory exit the loop.
if (!pushed) {
break;
}
}
// Double check we found managed to navigate all the way to the root directory.
if ((status_current_directory.st_dev != status_root_directory.st_dev) || (status_current_directory.st_ino != status_root_directory.st_ino)) {
return false;
}
// Convert the path components back into a path.
for (std::vector<std::string>::reverse_iterator path_component = path_components.rbegin(); path_component != path_components.rend(); ++path_component) {
current_working_directory += "/" + *path_component;
}
return true;
#elif defined(_WIN32)
current_working_directory.resize(MAX_PATH + 1, '\0');
if (!_getcwd(¤t_working_directory[0], MAX_PATH)) {
return false;
}
std::size_t current_working_directory_size = current_working_directory.find_last_not_of('\0');
if (current_working_directory_size == std::string::npos) {
return false;
}
current_working_directory.resize(current_working_directory_size + 1);
return true;
#else
return false;
#endif
}
/// @brief Get the users home directory.
/// @param[out] home_directory The users home directory.
/// @return true if the users home directory was successfully returned, false otherwise.
static bool get_home_directory(std::string& home_directory) {
home_directory.clear();
#if defined(linux) || defined(__linux) || defined(__linux__)
passwd* user_information = getpwuid(getuid());
if (!user_information) {
return false;
}
home_directory = std::string(user_information->pw_dir);
return true;
#elif defined(_WIN32)
home_directory.resize(MAX_PATH + 1, '\0');
if (SHGetFolderPathA(nullptr, CSIDL_MYDOCUMENTS, nullptr, 0, &home_directory[0]) < 0) {
return false;
}
std::size_t home_path_size = home_directory.find_last_not_of('\0');
if (home_path_size == std::string::npos) {
return false;
}
home_directory.resize(home_path_size + 1);
return true;
#elif defined(__APPLE__)
passwd* user_information = getpwuid(getuid());
if (!user_information) {
return false;
}
home_directory = std::string(user_information->pw_dir);
return true;
#else
return false;
#endif
}
/// @brief Get the users application data directory.
/// @param[out] application_data_directory The users application data directory.
/// @return true if the users application data directory was successfully returned, false otherwise.
static bool get_application_data_directory(std::string& application_data_directory) {
application_data_directory.clear();
#if defined(linux) || defined(__linux) || defined(__linux__)
std::string home_directory;
if (!get_home_directory(home_directory)) {
return false;
}
application_data_directory = home_directory + "/.config";
return true;
#elif defined(_WIN32)
application_data_directory.resize(MAX_PATH + 1, '\0');
if (SHGetFolderPathA(nullptr, CSIDL_COMMON_APPDATA, nullptr, 0, &application_data_directory[0]) < 0) {
return false;
}
std::size_t pathLength = application_data_directory.find_last_not_of('\0');
if (pathLength == std::string::npos) {
return false;
}
application_data_directory.resize(pathLength + 1);
return true;
#elif defined(__APPLE__)
std::string home_directory;
if (!get_home_directory(home_directory)) {
return false;
}
application_data_directory = home_directory + "/Library/Application Support";
return true;
#else
return false;
#endif
}
};
}
#endif // GTL_IO_PATHS_HPP