-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutil.cpp
308 lines (254 loc) · 6.56 KB
/
util.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
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
/*
RESGen. A tool to create .res files for Half-Life.
Copyright (C) 2000-2005 Jeroen Bogers
This file is part of RESGen.
RESGen 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; either version 2 of the License, or
(at your option) any later version.
RESGen 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 RESGen; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <glob.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include "util.h"
File::File()
: fileHandle(NULL)
{
}
File::File(const char* const fileName, const std::string& mode)
: fileHandle(NULL)
{
open(fileName, mode);
}
File::File(const std::string& fileName, const std::string& mode)
: fileHandle(NULL)
{
open(fileName, mode);
}
File::~File()
{
close();
}
void File::close()
{
if(fileHandle != NULL)
{
fclose(fileHandle);
fileHandle = NULL;
}
}
void File::open(const std::string& fileName, const std::string& mode)
{
close();
#ifdef _WIN32
if(fopen_s(&fileHandle, fileName.c_str(), mode.c_str()))
{
fileHandle = NULL;
}
#else
fileHandle = fopen(fileName.c_str(), mode.c_str());
#endif
}
File::operator FILE*()
{
return fileHandle;
}
FILE* File::operator->()
{
return fileHandle;
}
void splitPath(const std::string &fullPath, std::string &baseFolder, std::string &baseFileName)
{
size_t lastSlashIndex = fullPath.rfind('/'); // Linux style path
if (lastSlashIndex == std::string::npos)
{
lastSlashIndex = fullPath.rfind('\\'); // windows style path
}
// folder, including trailing /
if (lastSlashIndex == std::string::npos)
{
baseFolder += ".";
baseFolder += PATH_SEPARATOR;
}
else
{
baseFolder = fullPath.substr(0, lastSlashIndex + 1);
}
const size_t basenameStart =
(lastSlashIndex != std::string::npos) ? lastSlashIndex + 1 : 0;
// Subtract 4 for the '.bsp' extension
baseFileName = fullPath.substr(basenameStart, fullPath.length() - basenameStart - 4);
}
bool fileExists(const std::string &fileName)
{
#ifdef _WIN32
WIN32_FIND_DATA filedata;
HANDLE filehandle = FindFirstFile(fileName.c_str(), &filedata);
if (filehandle != INVALID_HANDLE_VALUE)
{
FindClose(filehandle);
return true;
}
#else
// We use glob to find the file in Linux.
// Please note that params are NOT expanded (tilde will be).
// So it might not work properly in cases where params are used
// The glob man page tell us to use wordexp for expansion.. However, that function does not exist.
glob_t globbuf;
if (!glob(fileName.c_str(), GLOB_TILDE, NULL, &globbuf))
{
globfree(&globbuf);
return true;
}
#endif
return false;
}
std::string replaceCharAllCopy(const std::string &str, const char find, const char replace)
{
std::string result = str;
std::replace(result.begin(), result.end(), find, replace);
return result;
}
void replaceCharAll(std::string &str, const char find, const char replace)
{
std::replace(str.begin(), str.end(), find, replace);
}
std::string strToLowerCopy(const std::string &str)
{
std::string result(str);
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
return result;
}
void strToLower(std::string &str)
{
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
}
int CompareStrEndNoCase(const std::string &str, const std::string &ending)
{
if(str.length() < ending.length())
{
return -1;
}
std::string strLC = strToLowerCopy(str);
std::string endingLC = strToLowerCopy(ending);
return CompareStrEnd(strLC, endingLC);
}
int CompareStrEnd(const std::string &str, const std::string &ending)
{
const size_t strLength = str.length();
const size_t endingLength = ending.length();
if(strLength < endingLength)
{
return -1;
}
return str.compare(strLength - endingLength, endingLength, ending);
}
void leftTrim(std::string &str)
{
leftTrim(str, " \n\r\t");
}
void leftTrim(std::string &str, const std::string &trimmedChars)
{
str.erase(0, str.find_first_not_of(trimmedChars));
}
void rightTrim(std::string &str)
{
rightTrim(str, " \n\r\t");
}
void rightTrim(std::string &str, const std::string &trimmedChars)
{
str.erase(str.find_last_not_of(trimmedChars) + 1);
}
bool stripParentheses(std::string& str)
{
size_t parenStart = str.find('(');
while(parenStart != std::string::npos)
{
size_t parenEnd = str.find(')');
if(parenEnd == std::string::npos)
{
return false;
}
str.erase(parenStart, parenEnd - parenStart + 1);
parenStart = str.find('(');
}
return true;
}
void removeSubstring(std::string& str, const char* const substring)
{
size_t substringLength = strlen(substring);
// while(true) incorrectly triggers MSVC C4127
for(;;)
{
const size_t matchStart = str.find(substring);
if(matchStart == std::string::npos)
{
break;
}
str.erase(matchStart, substringLength);
}
}
bool readFile(const std::string &filename, std::string &outStr)
{
std::ifstream f(filename.c_str());
if(!f.is_open())
{
return false;
}
std::stringstream buffer;
buffer << f.rdbuf();
outStr = buffer.str();
return true;
}
int ICompareStrings(const std::string &a, const std::string &b)
{
return strToLowerCopy(a).compare(strToLowerCopy(b));
}
std::string BuildValvePath(const std::string &respath)
{
// Check the respath and check ../valve if the respath doesn't point to valve
#ifdef _WIN32
const char* valveStr = "\\valve\\";
#else
const char* valveStr = "/valve/";
#endif
if(CompareStrEndNoCase(respath, valveStr))
{
// NOT valve dir, so check it too
size_t slashpos = respath.rfind(PATH_SEPARATOR, respath.length() - 2);
if (slashpos != std::string::npos)
{
return respath.substr(0, slashpos) + valveStr;
}
else
{
return respath + ".." + valveStr;
}
}
return "";
}
void EndWithPathSep(std::string &str)
{
if (str[str.length() - 1] != PATH_SEPARATOR)
{
// No path separator, add
str += PATH_SEPARATOR;
}
}