Repository navigation
Expand file tree
/
Copy pathCore.cpp
More file actions
535 lines (443 loc) · 11.7 KB
/
Copy pathCore.cpp
File metadata and controls
535 lines (443 loc) · 11.7 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#include "Core.h"
#if _WIN32
#include <direct.h> // for mkdir()
#endif
#include <sys/stat.h> // for mkdir(), stat()
#if VSTUDIO_INTEGRATION
#define WIN32_LEAN_AND_MEAN // exclude rarely-used services from windown headers
#define _WIN32_WINDOWS 0x0500 // for IsDebuggerPresent()
#include <windows.h>
#endif // VSTUDIO_INTEGRATION
static FILE *GLogFile = NULL;
void appOpenLogFile(const char *filename)
{
if (GLogFile) fclose(GLogFile);
GLogFile = fopen(filename, "a");
if (!GLogFile)
appPrintf("Unable to open log \"%s\"\n", filename);
}
void appPrintf(const char *fmt, ...)
{
va_list argptr;
va_start(argptr, fmt);
char buf[4096];
int len = vsnprintf(ARRAY_ARG(buf), fmt, argptr);
va_end(argptr);
assert(len >= 0 && len < ARRAY_COUNT(buf) - 1);
fwrite(buf, len, 1, stdout);
if (GLogFile) fwrite(buf, len, 1, GLogFile);
#if VSTUDIO_INTEGRATION
if (IsDebuggerPresent())
OutputDebugString(buf);
#endif
}
/*-----------------------------------------------------------------------------
Simple error/notofication functions
-----------------------------------------------------------------------------*/
bool GIsSwError = false; // software-gererated error
void appError(const char *fmt, ...)
{
va_list argptr;
va_start(argptr, fmt);
char buf[4096];
int len = vsnprintf(ARRAY_ARG(buf), fmt, argptr);
va_end(argptr);
assert(len >= 0 && len < ARRAY_COUNT(buf) - 1);
GIsSwError = true;
#if DO_GUARD
// appNotify("ERROR: %s\n", buf);
strcpy(GErrorHistory, buf);
appStrcatn(ARRAY_ARG(GErrorHistory), "\n");
THROW;
#else
fprintf(stderr, "Fatal Error: %s\n", buf);
if (GLogFile) fprintf(GLogFile, "Fatal Error: %s\n", buf);
exit(1);
#endif
}
static char NotifyBuf[512];
void appSetNotifyHeader(const char *fmt, ...)
{
if (!fmt)
{
NotifyBuf[0] = 0;
return;
}
va_list argptr;
va_start(argptr, fmt);
vsnprintf(ARRAY_ARG(NotifyBuf), fmt, argptr);
va_end(argptr);
}
void appNotify(const char *fmt, ...)
{
va_list argptr;
va_start(argptr, fmt);
char buf[4096];
int len = vsnprintf(ARRAY_ARG(buf), fmt, argptr);
va_end(argptr);
assert(len >= 0 && len < ARRAY_COUNT(buf) - 1);
fflush(stdout);
// a bit ugly code: printing the same thing into 3 streams
// print to notify file
if (FILE *f = fopen("notify.log", "a"))
{
if (NotifyBuf[0])
fprintf(f, "\n******** %s ********\n\n", NotifyBuf);
fprintf(f, "%s\n", buf);
fclose(f);
}
// print to log file
if (GLogFile)
{
if (NotifyBuf[0])
fprintf(GLogFile, "******** %s ********\n", NotifyBuf);
fprintf(GLogFile, "*** %s\n", buf);
fflush(GLogFile);
}
// print to console
if (NotifyBuf[0])
fprintf(stderr, "******** %s ********\n", NotifyBuf);
fprintf(stderr, "*** %s\n", buf);
fflush(stderr);
// clean notify header
NotifyBuf[0] = 0;
}
char GErrorHistory[2048];
static bool WasError = false;
static void LogHistory(const char *part)
{
if (!GErrorHistory[0]) strcpy(GErrorHistory, "General Protection Fault !\n");
appStrcatn(ARRAY_ARG(GErrorHistory), part);
}
void appUnwindPrefix(const char *fmt)
{
char buf[512];
appSprintf(ARRAY_ARG(buf), WasError ? " <- %s:" : "%s:", fmt);
LogHistory(buf);
WasError = false;
}
#if DO_GUARD
void appUnwindThrow(const char *fmt, ...)
{
char buf[512];
va_list argptr;
va_start(argptr, fmt);
if (WasError)
{
strcpy(buf, " <- ");
vsnprintf(buf+4, ARRAY_COUNT(buf)-4, fmt, argptr);
}
else
{
vsnprintf(buf, ARRAY_COUNT(buf), fmt, argptr);
WasError = true;
}
va_end(argptr);
LogHistory(buf);
THROW;
}
#endif // DO_GUARD
/*-----------------------------------------------------------------------------
String functions
-----------------------------------------------------------------------------*/
#define VA_GOODSIZE 512
#define VA_BUFSIZE 2048
// name of this function is a short form of "VarArgs"
const char *va(const char *format, ...)
{
// guardSlow(va);
static char buf[VA_BUFSIZE];
static int bufPos = 0;
// wrap buffer
if (bufPos >= VA_BUFSIZE - VA_GOODSIZE) bufPos = 0;
va_list argptr;
va_start(argptr, format);
// print
char *str = buf + bufPos;
int len = vsnprintf(str, VA_BUFSIZE - bufPos, format, argptr);
if (len < 0 && bufPos > 0)
{
// buffer overflow - try again with printing to buffer start
bufPos = 0;
str = buf;
len = vsnprintf(buf, VA_BUFSIZE, format, argptr);
}
va_end(argptr);
if (len < 0) // not enough buffer space
{
const char suffix[] = " ... (overflow)"; // it is better, than return empty string
memcpy(buf + VA_BUFSIZE - ARRAY_COUNT(suffix), suffix, ARRAY_COUNT(suffix));
return str;
}
bufPos += len + 1;
return str;
// unguardSlow;
}
char* appStrdup(const char* str)
{
int len = strlen(str) + 1;
char* buf = (char*)appMalloc(len);
memcpy(buf, str, len);
return buf;
}
int appSprintf(char *dest, int size, const char *fmt, ...)
{
va_list argptr;
va_start(argptr, fmt);
int len = vsnprintf(dest, size, fmt, argptr);
va_end(argptr);
if (len < 0 || len >= size - 1)
appPrintf("appSprintf: overflow of size %d (fmt=%s)\n", size, fmt);
return len;
}
// Unicode appSprintf
int appSprintf(wchar_t *dest, int size, const wchar_t *fmt, ...)
{
va_list argptr;
va_start(argptr, fmt);
int len = vsnwprintf(dest, size, fmt, argptr);
va_end(argptr);
if (len < 0 || len >= size - 1)
appPrintf("appSprintf: overflow of size %d (fmt=%S)\n", size, fmt);
return len;
}
void appStrncpyz(char *dst, const char *src, int count)
{
if (count <= 0) return; // zero-length string
char c;
do
{
if (!--count)
{
// out of dst space -- add zero to the string end
*dst = 0;
return;
}
c = *src++;
*dst++ = c;
} while (c);
}
void appStrncpylwr(char *dst, const char *src, int count)
{
if (count <= 0) return;
char c;
do
{
if (!--count)
{
// out of dst space -- add zero to the string end
*dst = 0;
return;
}
c = tolower(*src++);
*dst++ = c;
} while (c);
}
void appStrcatn(char *dst, int count, const char *src)
{
char *p = strchr(dst, 0);
int maxLen = count - (p - dst);
if (maxLen > 1)
appStrncpyz(p, src, maxLen);
}
const char *appStristr(const char *s1, const char *s2)
{
char buf1[1024], buf2[1024];
appStrncpylwr(buf1, s1, ARRAY_COUNT(buf1));
appStrncpylwr(buf2, s2, ARRAY_COUNT(buf2));
char *s = strstr(buf1, buf2);
if (!s) return NULL;
return s1 + (s - buf1);
}
void appNormalizeFilename(char *filename)
{
char *src = filename;
char *dst = filename;
char prev = 0;
while (true)
{
char c = *src++;
if (c == '\\') c = '/';
if (c == '/' && prev == '/') continue; // squeeze multiple slashes
*dst++ = prev = c;
if (!c) break;
}
if (--dst > filename)
{
// strip trailing slash, if one
if (*dst == '/') *dst = 0;
}
}
/*-----------------------------------------------------------------------------
Simple wildcard matching
-----------------------------------------------------------------------------*/
// Wildcard matching function from
// http://www.drdobbs.com/architecture-and-design/matching-wildcards-an-empirical-way-to-t/240169123
// This function compares text strings, one of which can have wildcards ('*' or '?').
static bool WildTextCompare(
const char *pTameText, // A string without wildcards
const char *pWildText // A (potentially) corresponding string with wildcards
)
{
// These two values are set when we observe a wildcard character. They
// represent the locations, in the two strings, from which we start once
// we've observed it.
const char *pTameBookmark = NULL;
const char *pWildBookmark = NULL;
// Walk the text strings one character at a time.
while (true)
{
// How do you match a unique text string?
if (*pWildText == '*')
{
// Easy: unique up on it!
while (*(++pWildText) == '*')
{
} // "xy" matches "x**y"
if (!*pWildText)
{
return true; // "x" matches "*"
}
if (*pWildText != '?')
{
// Fast-forward to next possible match.
while (*pTameText != *pWildText)
{
if (!(*(++pTameText)))
return false; // "x" doesn't match "*y*"
}
}
pWildBookmark = pWildText;
pTameBookmark = pTameText;
}
else if (*pTameText != *pWildText && *pWildText != '?')
{
// Got a non-match. If we've set our bookmarks, back up to one
// or both of them and retry.
//
if (pWildBookmark)
{
if (pWildText != pWildBookmark)
{
pWildText = pWildBookmark;
if (*pTameText != *pWildText)
{
// Don't go this far back again.
pTameText = ++pTameBookmark;
continue; // "xy" matches "*y"
}
else
{
pWildText++;
}
}
if (*pTameText)
{
pTameText++;
continue; // "mississippi" matches "*sip*"
}
}
return false; // "xy" doesn't match "x"
}
pTameText++;
pWildText++;
// How do you match a tame text string?
if (!*pTameText)
{
// The tame way: unique up on it!
while (*pWildText == '*')
{
pWildText++; // "x" matches "x*"
}
if (!*pWildText)
{
return true; // "x" matches "x"
}
return false; // "x" doesn't match "xy"
}
}
}
bool appMatchWildcard(const char *name, const char *mask, bool ignoreCase)
{
guard(appMatchWildcard);
if (!name[0] && !mask[0]) return true; // empty strings matched
if (ignoreCase)
{
char NameCopy[1024], MaskCopy[1024];
appStrncpylwr(NameCopy, name, ARRAY_COUNT(NameCopy));
appStrncpylwr(MaskCopy, mask, ARRAY_COUNT(MaskCopy));
return WildTextCompare(NameCopy, MaskCopy);
}
else
{
return WildTextCompare(name, mask);
}
unguard;
}
bool appContainsWildcard(const char *string)
{
if (strchr(string, '*')) return true;
if (strchr(string, ',')) return true;
if (strchr(string, '?')) return true;
return false;
}
/*-----------------------------------------------------------------------------
File helpers
-----------------------------------------------------------------------------*/
void appMakeDirectory(const char *dirname)
{
if (!dirname[0]) return;
// Win32 and Unix: there is no API to create directory chains
// so - we will create "a", then "a/b", then "a/b/c"
char Name[256];
appStrncpyz(Name, dirname, ARRAY_COUNT(Name));
appNormalizeFilename(Name);
for (char *s = Name; /* empty */ ; s++)
{
char c = *s;
if (c != '/' && c != 0)
continue;
*s = 0; // temporarily cut rest of path
// here: path delimiter or end of string
if (Name[0] != '.' || Name[1] != 0) // do not create "."
#if _WIN32
_mkdir(Name);
#else
mkdir(Name, S_IRWXU);
#endif
if (!c) break; // end of string
*s = '/'; // restore string (c == '/')
}
}
void appMakeDirectoryForFile(const char *filename)
{
char Name[256];
appStrncpyz(Name, filename, ARRAY_COUNT(Name));
appNormalizeFilename(Name);
char *s = strrchr(Name, '/');
if (s)
{
*s = 0;
appMakeDirectory(Name);
}
}
#ifndef S_ISDIR
// no such declarations in windows headers, but exists in mingw32 ...
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#define stat _stati64
#endif
unsigned appGetFileType(const char *filename)
{
char Name[256];
appStrncpyz(Name, filename, ARRAY_COUNT(Name));
appNormalizeFilename(Name);
struct stat buf;
if (stat(filename, &buf) == -1)
return 0; // no such file/dir
if (S_ISDIR(buf.st_mode))
return FS_DIR;
else if (S_ISREG(buf.st_mode))
return FS_FILE;
return 0; // just in case ... (may be, win32 have other file types?)
}