-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathutils.cpp
219 lines (182 loc) · 4.44 KB
/
utils.cpp
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
#include "precompiled.h"
const char* g_platform_postfixes[4] =
{
"_i386.so",
"_i486.so",
"_i586.so",
"_i686.so",
};
bool is_yes(const char* str)
{
return !Q_strcmp(str, "true") || !Q_strcmp(str, "yes") || !Q_strcmp(str, "1");
}
bool is_no(const char* str)
{
return !Q_strcmp(str, "false") || !Q_strcmp(str, "no") || !Q_strcmp(str, "0");
}
char* ENTITY_KEYVALUE(edict_t* entity, char* key)
{
return INFOKEY_VALUE(GET_INFOKEYBUFFER(entity), key);
}
const char* LOCALINFO(char* key)
{
return ENTITY_KEYVALUE(nullptr, key);
}
char* trimbuf(char* str)
{
char* ibuf;
if (str == nullptr) return nullptr;
for (ibuf = str; *ibuf && (byte)(*ibuf) < (byte)0x80 && isspace(*ibuf); ++ibuf)
;
int i = Q_strlen(ibuf);
if (str != ibuf)
Q_memmove(str, ibuf, i);
while (--i >= 0) {
if (!isspace(str[i]))
break;
}
str[i + 1] = '\0';
return str;
}
void normalize_path(char* path)
{
#ifdef _WIN32
for (char* cp = path; *cp; cp++) {
if (isupper(*cp))
*cp = tolower(*cp);
if (*cp == '\\')
*cp = '/';
}
#endif
}
bool is_abs_path(const char* path)
{
if (path[0] == '/') return true;
#ifdef _WIN32
if (path[1] == ':') return true;
if (path[0] == '\\') return true;
#endif // _WIN32
return false;
}
bool is_valid_path(const char* path)
{
struct stat64 st;
return !stat64(path, &st) && S_ISREG(st.st_mode);
}
bool is_platform_postfix(const char* pf)
{
if (pf) {
for (size_t i = 0; i < arraysize(g_platform_postfixes); i++) {
if (!Q_strcmp(pf, g_platform_postfixes[i]))
return true;
}
}
return false;
}
#ifdef _WIN32
char* realpath(const char* file_name, char* resolved_name)
{
int ret = GetFullPathName(file_name, MAX_PATH, resolved_name, nullptr);
if (ret > MAX_PATH) {
errno = ENAMETOOLONG;
return nullptr;
}
if (ret > 0) {
WIN32_FIND_DATA find_data;
HANDLE handle = FindFirstFile(resolved_name, &find_data);
if (INVALID_HANDLE_VALUE == handle) {
errno = ENOENT;
return nullptr;
}
FindClose(handle);
normalize_path(resolved_name);
return resolved_name;
}
return nullptr;
}
#endif // _WIN32
bool is_file_exists(const char* file)
{
struct stat64 st;
int ret = stat64(file, &st);
if (ret != 0) {
META_DEBUG(5, "Unable to stat '%s': %s", file, strerror(errno));
return false;
}
int reg = S_ISREG(st.st_mode);
if (!reg) {
META_DEBUG(5, "Not a regular file: %s", file);
return false;
}
if (!st.st_size) {
META_DEBUG(5, "Empty file: %s", file);
return false;
}
return true;
}
// Checks for a non-empty file, relative to the gamedir if necessary.
// Formerly used LOAD_FILE_FOR_ME, which provided a simple way to check for
// a file under the gamedir, but which would _also_ look in the sibling
// "valve" directory, thus sometimes finding files that weren't desired.
// Also, formerly named just "valid_file".
//
// Special-case-recognize "/dev/null" as a valid file.
bool is_file_exists_in_gamedir(const char* path)
{
char buf[MAX_PATH];
if (!path)
return false;
if (!Q_strcmp(path, "/dev/null"))
return true;
if (is_abs_path(path)) {
Q_strlcpy(buf, path);
}
else {
Q_snprintf(buf, sizeof buf, "%s/%s", g_GameDLL.gamedir, path);
}
return is_file_exists(buf);
}
// Turns path into a full path:
// - if not absolute, prepends gamedir
// - calls realpath() to collapse ".." and such
// - calls NormalizePath() to fix backslashes, etc
//
// Much like realpath, buffer pointed to by fullpath is assumed to be
// able to store a string of MAX_PATH length.
char* full_gamedir_path(const char* path, char (&fullpath)[MAX_PATH])
{
char buf[MAX_PATH];
// Build pathname from filename, plus gamedir if relative path.
if (is_abs_path(path)) {
Q_strlcpy(buf, path);
}
else {
Q_snprintf(buf, sizeof buf, "%s/%s", g_GameDLL.gamedir, path);
}
// Remove relative path components, if possible.
if (!realpath(buf, fullpath)) {
META_DEBUG(4, "Unable to get realpath for '%s': %s", buf, strerror(errno));
Q_strlcpy(fullpath, path);
}
// Replace backslashes, etc.
normalize_path(fullpath);
return fullpath;
}
void NORETURN Sys_Error(const char *error, ...)
{
va_list argptr;
static char text[1024];
va_start(argptr, error);
Q_vsnprintf(text, sizeof(text), error, argptr);
va_end(argptr);
META_CONS("FATAL ERROR (shutting down): %s\n", text);
META_ERROR(text);
#ifdef _WIN32
MessageBox(GetForegroundWindow(), text, "Fatal error - Metamod", MB_ICONERROR | MB_OK);
#endif // _WIN32
// Allow chance to read the message, before any window closes.
sleep(3);
int *null = nullptr;
*null = 0;
exit(-1);
}