-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileIO.cpp
442 lines (354 loc) · 7.91 KB
/
fileIO.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
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
/*
* Copyright (C) fastman92 <[email protected]>, website: http://fastman92.com
* Licensed under the MIT License, see LICENSE at top level directory.
*
*/
#define _CRT_NONSTDC_NO_DEPRECATE // for chdir
#include "fileIO.h"
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#ifdef IS_PLATFORM_WIN
#include <direct.h>
#include <Shlobj.h>
#include <Windows.h>
#endif
#ifdef IS_PLATFORM_ANDROID
#include <sys/stat.h>
#include <unistd.h>
#endif
// Loads line from file without commas
bool LoadLineWithoutCommas(char * str, int num, FILE * stream)
{
if (fgets(str, num, stream))
{
int l = 0;
while (str[l] != 0)
{
if (str[l] == ',')
str[l] = ' ';
l++;
}
if (l > 0)
{
int i = l - 1;
do
{
if (str[i] == '\r' || str[i] == '\n')
str[i] = 0;
else
break;
} while (i >= 0);
}
return true;
}
else
return false;
}
// Returns file size
int64_t GetFileSize(const char* path)
{
#ifdef IS_PLATFORM_WIN
HANDLE hFile = CreateFileA(path, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return -1; // error condition, could call GetLastError to find out more
LARGE_INTEGER size;
if (!GetFileSizeEx(hFile, &size))
{
CloseHandle(hFile);
return -1; // error condition, could call GetLastError to find out more
}
CloseHandle(hFile);
return size.QuadPart;
#elif defined(IS_PLATFORM_ANDROID)
struct stat filestatus;
if (stat(path, &filestatus) == 0)
return filestatus.st_size;
else
return -1;
#else
return -1;
#endif
}
// Returns file size
int64_t GetFileSize(const wchar_t* path)
{
#ifdef IS_PLATFORM_WIN
HANDLE hFile = CreateFileW(path, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return -1; // error condition, could call GetLastError to find out more
LARGE_INTEGER size;
if (!GetFileSizeEx(hFile, &size))
{
CloseHandle(hFile);
return -1; // error condition, could call GetLastError to find out more
}
CloseHandle(hFile);
return size.QuadPart;
#else
return -1;
#endif
}
#ifdef IS_PLATFORM_WIN
// Browses a directory and executes a function for each file
// if called function returns true, files aren't scanned anymore
bool BrowseDirectory(const char* dirPath, bool(*func)(const char* filename, void* pUserData), void* pUserData)
{
wchar_t PreviousDirectory[MAX_PATH];
GetCurrentDirectoryW(_countof(PreviousDirectory), PreviousDirectory);
if (!SetCurrentDirectoryA(dirPath))
return false;
_WIN32_FIND_DATAA fd;
HANDLE File = FindFirstFileA("*", &fd);
if (File != INVALID_HANDLE_VALUE)
{
const void* fileContent = NULL;
do {
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
// avoid standard library functions to reduce dll size
if (func(fd.cFileName, pUserData))
break;
}
} while (FindNextFileA(File, &fd));
FindClose(File);
}
SetCurrentDirectoryW(PreviousDirectory);
return true;
}
// Opens directory if exists or creates and opens directory if it doesn't initially exists
bool OpenOrCreateDirectory(const char* dirname)
{
if (SetCurrentDirectoryA(dirname) || (!SHCreateDirectoryExA(NULL, dirname, NULL) && SetCurrentDirectoryA(dirname)))
return true;
else
{
char AbsoluteDirname[MAX_PATH];
GetFullPathNameA(dirname, _countof(AbsoluteDirname), AbsoluteDirname, NULL);
if (!SHCreateDirectoryExA(NULL, AbsoluteDirname, NULL) && SetCurrentDirectoryA(dirname))
return true;
else
return false;
}
}
DWORD GetFilePointer(HANDLE hFile) {
return SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
}
LONGLONG GetFilePointerEx(HANDLE hFile) {
LARGE_INTEGER liOfs = { 0 };
LARGE_INTEGER liNew = { 0 };
SetFilePointerEx(hFile, liOfs, &liNew, FILE_CURRENT);
return liNew.QuadPart;
}
#endif
bool FileExistsA(const char* szPath)
{
#ifdef IS_PLATFORM_WIN
DWORD dwAttrib = GetFileAttributesA(szPath);
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
#else
return false;
#endif
}
bool FileExistsW(const wchar_t* szPath)
{
#ifdef IS_PLATFORM_WIN
DWORD dwAttrib = GetFileAttributesW(szPath);
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
#else
return false;
#endif
}
// Returns pointer to filename from path
char* GetFilenameFromPath(const char* path)
{
int len = strlen(path);
if (len)
{
const char* onlyFilename = path + len;
while (onlyFilename >= path)
{
if (*onlyFilename == '\\' || *onlyFilename == '/')
break;
onlyFilename--;
}
return (char*)(onlyFilename + 1);
}
else
return (char*)path;
}
// Reads 4 byte aligned string
void FileRead4byteAlignedString(FILE* fp, char* str)
{
char data[4];
while (fread(&data, 4, 1, fp))
{
if (data[3])
*(int32_t*)str = *(int32_t*)data;
else
{
for (int i = 0; i < 4; i++)
{
str[i] = data[i];
if (!str[i])
break;
}
break;
}
str += 4;
}
}
// Writes 4 byte aligned string
void FileWrite4byteAlignedString(FILE* fp, const char* str)
{
while (true)
{
char data[4] = { 0 };
for (int i = 0; i < 4; i++)
{
if (str[i])
data[i] = str[i];
else
break;
}
fwrite(&data, sizeof(data), 1, fp);
if (!data[3])
break;
str += 4;
}
}
// Reads int32
uint32_t FileReadInt32(FILE* fp)
{
int32_t value;
fread(&value, sizeof(value), 1, fp);
return value;
}
// Reads float
float FileReadFloat(FILE* fp)
{
float value;
fread(&value, sizeof(value), 1, fp);
return value;
}
// Writes float
void FileWriteFloat(FILE* fp, float value)
{
fwrite(&value, sizeof(value), 1, fp);
}
// Writes int32
void FileWriteInt32(FILE* fp, int32_t value)
{
fwrite(&value, sizeof(value), 1, fp);
}
// Writes Pascal string
void FileWritePascalString(FILE* fp, const char* str)
{
unsigned int len = strlen(str);
uint8_t lenToWrite;
if (len > 255)
lenToWrite = 255;
else
lenToWrite = len;
fwrite(&len, 1, 1, fp);
fwrite(str, lenToWrite, 1, fp);
}
// Reads Pascal string
void FileReadPascalString(FILE* fp, char* outputStr, unsigned int bufferSize)
{
uint8_t len;
fread(&len, sizeof(len), 1, fp);
unsigned int neededSize = len + 1;
unsigned int sizeToRead = len;
unsigned int sizeToSkip = 0;
if (neededSize > bufferSize)
{
sizeToSkip = neededSize - bufferSize;
sizeToRead -= sizeToSkip;
}
if(sizeToRead)
fread(outputStr, sizeToRead, 1, fp);
outputStr[sizeToRead] = 0;
if (sizeToSkip)
fseek(fp, sizeToSkip, SEEK_CUR);
}
// Sets a current directory
bool SetCurrentWorkingDirectory_OS_independent(const char* directoryPath)
{
return chdir(directoryPath) == 0;
}
#if IS_PLATFORM_WIN
// Create directory recursively
int mkdir_p(const char *path)
{
/* Adapted from http://stackoverflow.com/a/2336245/119527 */
const size_t len = strlen(path);
char _path[4096];
char *p;
errno = 0;
/* Copy string so its mutable */
if (len > sizeof(_path) - 1) {
errno = ENAMETOOLONG;
return -1;
}
strcpy(_path, path);
/* Iterate the string */
for (p = _path + 1; *p; p++) {
if (*p == '\\') {
/* Temporarily truncate */
*p = '\0';
if (_mkdir(_path) != 0) {
if (errno != EEXIST)
return -1;
}
*p = '/';
}
}
if (_mkdir(_path) != 0) {
if (errno != EEXIST)
return -1;
}
return 0;
}
#endif
#if defined(IS_PLATFORM_ANDROID)
// Create directory recursively
int mkdir_p(const char *path, mode_t mode)
{
/* Adapted from http://stackoverflow.com/a/2336245/119527 */
const size_t len = strlen(path);
char _path[PATH_MAX];
char *p;
errno = 0;
/* Copy string so its mutable */
if (len > sizeof(_path) - 1) {
errno = ENAMETOOLONG;
return -1;
}
strcpy(_path, path);
/* Iterate the string */
for (p = _path + 1; *p; p++) {
if (*p == '/') {
/* Temporarily truncate */
*p = '\0';
if (mkdir(_path, mode) != 0) {
if (errno != EEXIST)
return -1;
}
*p = '/';
}
}
if (mkdir(_path, mode) != 0) {
if (errno != EEXIST)
return -1;
}
return 0;
}
#endif