-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathfs.cpp
More file actions
311 lines (255 loc) · 5.62 KB
/
Copy pathfs.cpp
File metadata and controls
311 lines (255 loc) · 5.62 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
#pragma once
#include "fs.h"
#include "../sys/AppInfo.h"
#include "../common/io.h"
#include <sys/stat.h>
#include <Windows.h>
const char* FS_Cwd(void)
{
static char cwd[MAX_PATH] = "\0";
if (!cwd[0])
{
GetCurrentDirectoryA(MAX_PATH, cwd);
}
return cwd;
}
bool FS_FileExists(const char* qpath)
{
if (qpath == NULL)
return false;
FILE* h = NULL;
if (fopen_s(&h, qpath, "r") == NULL)
{
fclose(h);
return true;
}
return false;
}
/*
// This is now defined in gsc_lib
long int FS_FileSize(const char* qpath)
{
struct stat st_buf;
int r = stat(qpath, &st_buf);
return r == 0 ? st_buf.st_size : -1;
}
*/
const char* FS_GetExtensionSubString(const char* filename)
{
_ASSERT(filename != NULL);
const char* substr = 0;
while (*filename)
{
if (*filename == '.')
substr = filename;
else if (*filename == '/' || *filename == '\\')
substr = 0;
++filename;
}
if (!substr)
substr = filename;
return substr;
}
void FS_StripExtension(const char* in, char* out)
{
const char* extension = FS_GetExtensionSubString(in);
while (in != extension)
*out++ = *in++;
*out = 0;
}
const char* FS_GetFilenameSubString(const char* pathname)
{
const char* last = pathname;
while (*pathname)
{
if (*pathname == '/' || *pathname == '\\')
last = pathname + 1;
++pathname;
}
return last;
}
const wchar_t* FS_GetFilenameSubStringW(wchar_t* pathname)
{
wchar_t* last = pathname;
while (*pathname)
{
if (*pathname == '/' || *pathname == '\\')
last = pathname + 1;
++pathname;
}
return last;
}
void FS_StripFilename(const char* in, char* out)
{
const char* extension = FS_GetFilenameSubString(in);
while (in != extension)
*out++ = *in++;
*out = 0;
}
int FS_FileCount(const char* path, const char* pattern)
{
HANDLE dir;
WIN32_FIND_DATAA file_data;
char spath[MAX_PATH];
sprintf_s(spath, "%s/%s", path, pattern);
if ((dir = FindFirstFileA(spath, &file_data)) == INVALID_HANDLE_VALUE)
{
return 0;
}
int count = 0;
do
{
if ((file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
{
continue;
}
count++;
} while (FindNextFileA(dir, &file_data));
FindClose(dir);
return count;
}
int FS_FileIterator(const char* path, const char* pattern, FS_FileHandlerCallback_t FS_FileHandlerCallback)
{
HANDLE dir;
WIN32_FIND_DATAA file_data;
char spath[MAX_PATH];
sprintf_s(spath, "%s/%s", path, pattern);
if ((dir = FindFirstFileA(spath, &file_data)) == INVALID_HANDLE_VALUE)
{
return 0;
}
int count = 0;
do
{
if ((file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
{
continue;
}
char fpath[MAX_PATH];
sprintf_s(fpath, "%s/%s", path, file_data.cFileName);
FS_FileHandlerCallback(fpath, file_data.cFileName);
count++;
} while (FindNextFileA(dir, &file_data));
FindClose(dir);
return count;
}
int FS_DirectoryIterator(const char* path, FS_DirectoryHandlerCallback_t FS_DirectoryHandlerCallback)
{
HANDLE dir;
WIN32_FIND_DATAA file_data;
char qpath[MAX_PATH];
sprintf(qpath, "%s/*", path);
if ((dir = FindFirstFileA(qpath, &file_data)) == INVALID_HANDLE_VALUE)
{
return 0;
}
int count = 0;
do
{
if (file_data.cFileName[0] == '.')
{
continue;
}
if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
char tpath[MAX_PATH];
sprintf(tpath, "%s/%s", path, file_data.cFileName);
FS_DirectoryHandlerCallback(tpath);
count++;
}
} while (FindNextFileA(dir, &file_data));
FindClose(dir);
return count;
}
int FS_CreatePath(const char* _targetPath)
{
char targetPath[1024];
if (strchr(_targetPath, ':') != NULL)
strcpy_s(targetPath, _targetPath);
else
sprintf_s(targetPath, "%s/%s", AppInfo_OutDir(), _targetPath);
int len = strlen(targetPath);
for (int i = 0; i < len; i++)
{
bool skip = false;
if (i > 0 && targetPath[i-1] == ':')
skip = true;
if (!skip && (targetPath[i] == '/' || targetPath[i] == '\\'))
{
char buf[1024] = "";
strncpy(buf + strlen(buf), targetPath, i);
char* qpath = buf;
if (qpath[0] == '\0')
continue;
FS_SanitizePath(qpath);
#if _DEBUG
if (!CreateDirectoryA(buf, 0) && GetLastError() != ERROR_ALREADY_EXISTS)
#else
if (!CreateDirectoryA(qpath, 0) && GetLastError() != ERROR_ALREADY_EXISTS)
#endif
{
return GetLastError();
}
}
}
return 0;
}
int FS_CopyDirectory(char* srcPath, char* destPath, bool overwriteFiles)
{
WIN32_FIND_DATAA file_data;
HANDLE h;
char tmp[1025] = "\0";
strcpy(tmp, srcPath);
char tmp_srcPath[1025] = { 0 };
char tmp_destPath[1025] = { 0 };
strcat(tmp, "*");
if (!(h = FindFirstFileA(tmp, &file_data)))
{
return 1;
}
do
{
if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (strcmp(file_data.cFileName, ".") && strcmp(file_data.cFileName, ".."))
{
sprintf(tmp_destPath, "%s%s\\", destPath, file_data.cFileName);
sprintf(tmp_srcPath, "%s%s\\", srcPath, file_data.cFileName);
CreateDirectoryA(tmp_destPath, NULL);
FS_CopyDirectory(tmp_srcPath, tmp_destPath, overwriteFiles);
}
}
else
{
char srcFile[1025] = "\0";
char destFile[1025] = "\0";
sprintf(destFile, "%s%s", destPath, file_data.cFileName);
sprintf(srcFile, "%s%s", srcPath, file_data.cFileName);
CopyFileA(srcFile, destFile, !overwriteFiles);
}
} while (FindNextFileA(h, &file_data));
FindClose(h);
return 0;
}
void FS_SanitizePath(char* path)
{
if (path[0] == '/')
path[0] = '\\';
//
// Cleanup double / or \
//
int len = strlen(path);
for (int i = 1; i < len; i++)
{
if (path[i] == '/')
path[i] = '\\';
if (path[i] == '\\' && path[i - 1] == '\\')
strcpy(&path[i - 1], &path[i]);
}
//
// Trim any leading / or \
//
const char* c = path;
for (; *c == '/' || *c == '\\'; c++);
strcpy(path, c);
}