-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfile_utils.cpp
More file actions
348 lines (286 loc) · 7.29 KB
/
file_utils.cpp
File metadata and controls
348 lines (286 loc) · 7.29 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
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
/******************************************************
Copyright © Vitaliy Buturlin, Evgeny Danilovich, 2018
See the license in LICENSE
******************************************************/
#include "file_utils.h"
XDEPRECATED bool FileExistsFile(const char *szPath)
{
WIN32_FIND_DATA wfd;
HANDLE hFind = ::FindFirstFile(szPath, &wfd);
if (INVALID_HANDLE_VALUE != hFind)
{
::FindClose(hFind);
return true;
}
return false;
}
int FileGetSizeFile(const char *szPath)
{
struct stat fi;
stat(szPath, &fi);
return fi.st_size;
}
XDEPRECATED bool FileExistsDir(const char *szPath)
{
DWORD dwFileAttributes = GetFileAttributes(szPath);
if (dwFileAttributes == 0xFFFFFFFF)
return false;
return((dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
}
XDEPRECATED Array<String> FileGetList(const char *szPath, FILE_LIST_TYPE type)
{
Array<String> aStrings;
WIN32_FIND_DATA fd;
String sPath = FileCanonizePathS(szPath);
if (sPath.length() >= 2 && (sPath.find("*") == -1))
{
if (sPath[sPath.length() - 1] != '/')
{
sPath += "/";
}
sPath += "*";
}
HANDLE hFind = ::FindFirstFile(sPath.c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
if (!(strlen(fd.cFileName) == 1 && fd.cFileName[0] == '.') && !(strlen(fd.cFileName) == 2 && fd.cFileName[0] == '.' && fd.cFileName[1] == '.') &&
(
(type == FILE_LIST_TYPE_FILES && !(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) ||
(type == FILE_LIST_TYPE_DIRS && fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
(type == FILE_LIST_TYPE_ALL)
)
)
aStrings.push_back(fd.cFileName);
}
while (::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
return aStrings;
}
XDEPRECATED Array<String> FileGetListRec(const char *szPath, FILE_LIST_TYPE type, const char *szExt)
{
Array<String> aStrings;
Array<String> aQueue;
WIN32_FIND_DATA fd;
String sRootPath = FileAppendSlash(FileCanonizePathS(szPath).c_str());
aQueue.push_back("");
int iCurrDir = 0;
while((int)aQueue.size() > iCurrDir)
{
String sCurrPath = FileCanonizePathS((sRootPath + aQueue[iCurrDir]).c_str());
if(sCurrPath.length() >= 2 && sCurrPath[sCurrPath.length() - 1] != '*')
sCurrPath += "*";
HANDLE hFind = ::FindFirstFile(sCurrPath.c_str(), &fd);
if(hFind != INVALID_HANDLE_VALUE)
{
do
{
if(!(strlen(fd.cFileName) == 1 && fd.cFileName[0] == '.') && !(strlen(fd.cFileName) == 2 && fd.cFileName[0] == '.' && fd.cFileName[1] == '.'))
{
if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
String sNewDir = aQueue[iCurrDir] + fd.cFileName;
aQueue.push_back(FileAppendSlash(sNewDir.c_str()));
}
if (
(type == FILE_LIST_TYPE_FILES && !(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (!szExt || (szExt && FileStrIsExt(fd.cFileName, szExt)))) ||
(type == FILE_LIST_TYPE_DIRS && fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
(type == FILE_LIST_TYPE_ALL)
)
aStrings.push_back(aQueue[iCurrDir] + fd.cFileName);
}
}
while(::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
++iCurrDir;
}
return aStrings;
}
XDEPRECATED const char *FileBaseName(const char *szPath)
{
const char *szPos = szPath;
while (*szPath)
{
if (*szPath == '/' || *szPath == '\\')
{
szPos = szPath + 1;
}
++szPath;
}
return(szPos);
}
XDEPRECATED const char *FileDirName(char *szPath)
{
char * pos = szPath, *ret = szPath;
while (*szPath)
{
if (*szPath == '/' || *szPath == '\\')
{
pos = szPath + 1;
}
++szPath;
}
*pos = 0;
return(ret);
}
XDEPRECATED const char *FileCanonizePath(char *szPath)
{
char * ret = szPath;
while (*szPath)
{
if (*szPath == '\\')
{
*szPath = '/';
}
++szPath;
}
return(ret);
}
XDEPRECATED String FileCanonizePathS(const char *szPath)
{
String sCanonizePath = szPath;
for (int i = 0, il = sCanonizePath.length(); i < il; ++i)
{
if (sCanonizePath[i] == '\\')
sCanonizePath[i] = '/';
}
return sCanonizePath;
}
XDEPRECATED int FileCountNesting(const char *szPath)
{
int iCount = 0;
if (szPath[strlen(szPath)-1] == '/' || szPath[strlen(szPath)-1] == '\\')
--iCount;
for (int i = 0, il = (int)strlen(szPath); i < il; ++i)
{
if (szPath[i] == '/' || szPath[i] == '\\')
++iCount;
}
return iCount;
}
XDEPRECATED String FileGetPrevDir(const char *szPath)
{
int iPosDel = 0;
for(int i = 1, il = (int)strlen(szPath) - 1; i < il; ++i)
{
if (szPath[il - i] == '/' || szPath[il - i] == '\\')
{
iPosDel = (il - i) + 1;
break;
}
}
String sStr = szPath;
if (iPosDel > 0)
sStr = sStr.substr(0, iPosDel - 1);
return sStr;
}
XDEPRECATED bool FileExistsEndSlash(const char *szPath)
{
return (strlen(szPath) > 0 && (szPath[strlen(szPath) - 1] == '\\' || szPath[strlen(szPath) - 1] == '/'));
}
XDEPRECATED String FileAppendSlash(const char *szPath)
{
String sNewPath = szPath;
if (!FileExistsEndSlash(szPath))
sNewPath += "/";
return sNewPath;
}
XDEPRECATED bool FileExistsInPath(const char *szPath, const char *szSubPath)
{
String sPath = StrToLower(szPath);
for (int i = 0, il = sPath.length(); i < il; ++i)
{
if (sPath[i] == '\\')
sPath[i] = '/';
}
String sSubPath = StrToLower(szSubPath);
for (int i = 0, il = sSubPath.length(); i < il; ++i)
{
if (sSubPath[i] == '\\')
sSubPath[i] = '/';
}
return(strstr(sPath.c_str(), sSubPath.c_str()) != NULL);
}
XDEPRECATED bool FileCreateDir(const char *szPath)
{
if (!strstr(szPath, "\\") && !strstr(szPath, "/"))
{
return(!!CreateDirectory(szPath, 0));
}
String sPath = FileAppendSlash(FileCanonizePathS(szPath).c_str());
size_t sizePosSlash = 0;
size_t sizeOldPos = 0;
while((sizePosSlash = sPath.find("/", sizeOldPos)) != String::EOS)
{
String sDir = sPath.substr(0, sizePosSlash);
sizeOldPos = sizePosSlash + 1;
if (sDir.length() > 0 && !FileExistsDir(sDir.c_str()))
{
if (!CreateDirectory(sDir.c_str(), 0))
return false;
}
}
return true;
}
XDEPRECATED time_t FileGetTimeLastModify(const char *szPath)
{
HANDLE hFile = CreateFile(szPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return 0;
SYSTEMTIME stUTC;
FILETIME ftCreate, ftAccess, ftWrite;
GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite);
FileTimeToSystemTime(&ftWrite, &stUTC);
tm tmObj;
ZeroMemory(&tmObj, sizeof(tm));
tmObj.tm_year = stUTC.wYear - 1900;
tmObj.tm_mon = stUTC.wMonth;
tmObj.tm_mday = stUTC.wDay;
tmObj.tm_hour = stUTC.wHour;
tmObj.tm_min = stUTC.wMinute;
tmObj.tm_sec = stUTC.wSecond;
time_t tLastModify = mktime(&tmObj);
CloseHandle(hFile);
return tLastModify;
}
XDEPRECATED String FileSetStrExt(const char *szPath, const char *szExt)
{
String sPath = szPath;
int iPosPoint = -1;
for (int i = 0, il = sPath.length(); i < il; ++i)
{
if (sPath[(il - 1) - i] == '.')
{
iPosPoint = (il - 1) - i;
break;
}
}
if (iPosPoint >= 0)
sPath = sPath.substr(0, iPosPoint + (szExt[0] != 0 ? 1 : 0))+szExt;
else if (szExt && szExt[0] != 0)
{
if (szExt[0] == '.')
sPath += szExt;
else
sPath += String(".") + szExt;
}
return sPath;
}
XDEPRECATED bool FileStrIsExt(const char *szPath, const char *szExt)
{
if (!szPath || !szExt)
return false;
int iPosPoint = -1;
for (int i = 0, il = (int)strlen(szPath); i < il; ++i)
{
if (szPath[(il - 1) - i] == '.')
{
iPosPoint = ((il - 1) - i) + 1;
break;
}
}
return (iPosPoint >= 0 && strcasecmp(szPath + iPosPoint, szExt) == 0);
}