-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIO.hpp
529 lines (445 loc) · 13.8 KB
/
IO.hpp
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
/********************************************************************************
* Purpose: Reading from files and Writing to files *
* Author : Anilcan Gulkaya 2023 anilcangulkaya7@gmail.com github @benanil *
********************************************************************************/
#pragma once
// AFile AFileOpen(const char* fileName, AOpenFlag flag);
// void AFileRead(void* dst, uint64_t size, AFile file);
// void AFileWrite(const void* src, uint64_t size, AFile file);
// void AFileClose(AFile file);
// bool AFileExist(AFile file);
// uint64 AFileSize(AFile file);
// char* GetFileExtension(path, size);
// bool FileHasExtension(path, size, extension);
// char* PathGoBackwards (path, end, skipSeparator);
// int CopyFilename(char* out, char* path, int end); // < returns number of characters in filename
// bool FileExist(file);
// uint64 FileSize(file);
// bool RenameFile(oldFile, newFile);
// bool MoveFile(oldLocation, newLocation);
// bool RemoveFile(file); // < deletes the file
// void RemoveFolder(path, unused); // < deletes the folder
// char* ReadAllFile(fileName, buffer, numCharacters);
// void CopyFile(source, dst, buffer);
// void VisitFolder(path, visitFn);
// void GetCurrentDirectory(buffer, bufferSize);
// void AbsolutePath(path, outBuffer, bufferSize);
// void CombinePaths(dst, a, b);
// bool HasAnySubdir(path);
#include "Memory.hpp"
#include <stdio.h>
#include <stdint.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <io.h>
#include <direct.h>
#include "dirent.h"
#define F_OK 0
#else
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <dirent.h>
#define _rmdir rmdir
#define _mkdir mkdir
#define _fileno fileno
#define _filelengthi64 filelength
#endif
#ifdef __ANDROID__
#include <android/asset_manager.h>
#include <game-activity/native_app_glue/android_native_app_glue.h>
extern "C" android_app* g_android_app;
#endif
#ifdef _WIN32
constexpr char ASTL_FILE_SEPERATOR = '\\';
#else
constexpr char ASTL_FILE_SEPERATOR = '/';
#endif
// path must be null terminated string
inline const char* GetFileExtension(const char* path, int size)
{
while (path[size-1] != '.' && size > 0)
size--;
return path + size;
}
inline void ChangeExtension(char* path, int pathLen, const char* newExt)
{
int lastDot = pathLen - 1;
while (path[lastDot - 1] != '.')
lastDot--;
int i = lastDot;
for (; *newExt; i++)
path[i] = *newExt++;
// clean the right with zeros
while (i < pathLen)
path[i++] = '\0';
}
inline bool FileHasExtension(const char* path, int size, const char* extension)
{
int extLen = 0;
// go to end of extension
while (extension[1] != 0)
extension++, extLen++;
if (size <= extLen) return false;
for (int i = 0; i <= extLen; i++)
{
if (*extension-- != path[--size])
return false;
}
return true;
}
// returns pointer to the end of the new path
inline char* PathGoBackwards(char* path, int end, bool skipSeparator)
{
if (path[end-1] == '/' || path[end-1] == '\\')
path[--end] = '\0';
while (end >= 0 && (path[end] != '/' && path[end] != '\\')) {
path[end--] = '\0'; // Null-terminate the string.
}
if (skipSeparator && end >= 0) {
path[end--] = '\0'; // Null-terminate again to remove the separator.
}
return path + end + 1; // Return the new starting point of the path.
}
// returns length of filename
inline int CopyFilename(char* out, const char* path, int end)
{
int numChars = 0;
while (end >= 0 && (path[end] != '/' && path[end] != '\\')) {
out[numChars++] = path[end--]; // Null-terminate the string.
}
// reverse
for (int i = 0, j = numChars-1; i < j; i++, j--) {
char t = out[i];
out[i] = out[j];
out[j] = t;
}
return numChars; // Return the new starting point of the path.
}
// these functions works fine with file and folders
inline bool FileExist(const char* file)
{
#ifdef __ANDROID__
AAsset* asset = AAssetManager_open(g_android_app->activity->assetManager, file, 0);
if (asset == nullptr) {
return false;
} else {
AAsset_close(asset);
return true;
}
#elif defined(_WIN32)
return _access(file, F_OK) == 0;
#else
return access(file, F_OK) == 0;
#endif
}
inline uint64_t FileSize(const char* file)
{
#ifdef __ANDROID__
AAsset* asset = AAssetManager_open(g_android_app->activity->assetManager, file, 0);
if (asset != nullptr) {
off64_t sz = AAsset_getLength64(asset);
AAsset_close(asset);
return sz;
}
return 0;
#elif defined(_WIN32)
struct stat sb;
if (stat(file, &sb) == 0) return 0;
return sb.st_size;
#else
if (fseek(file.file, 0, SEEK_END) != 0)
return 0; // Or handle the error as appropriate
long fileSize = ftell(file.file);
if (fileSize == -1)
return 0; // Or handle the error as appropriate
return (uint64_t)fileSize;
#endif
}
inline bool RenameFile(const char* oldFile, const char* newFile)
{
return rename(oldFile, newFile) != 0;
}
inline bool CreateFolder(const char* folderName)
{
return _mkdir(folderName
#ifndef _WIN32
, 0777
#endif
) == 0;
}
inline bool RemoveFile(const char* file) {
return remove(file);
}
inline bool IsDirectory(const char* path)
{
struct stat file_info;
return stat(path, &file_info) == 0 && (S_ISDIR(file_info.st_mode));
}
enum AOpenFlag_
{
AOpenFlag_ReadBinary,
AOpenFlag_WriteBinary,
AOpenFlag_ReadText,
AOpenFlag_WriteText
};
typedef int AOpenFlag;
#ifdef __ANDROID__
struct AFile {
AAsset* asset;
};
inline AFile AFileOpen(const char* fileName, AOpenFlag flag) {
return { AAssetManager_open(g_android_app->activity->assetManager, fileName, 0) };
}
inline void AFileRead(void* dst, uint64_t size, AFile file, int alignment = 0) {
AAsset_read(file.asset, dst, size);
}
inline void AFileSeekBegin(AFile file) {
AAsset_seek(file.asset, 0, SEEK_SET);
}
inline void AFileSeek(long offset, AFile file) {
AAsset_seek(file.asset, offset, SEEK_CUR);
}
inline void AFileWrite(const void* src, uint64_t size, AFile file, int alignment = 0)
{ }
inline void AFileClose(AFile file) {
AAsset_close(file.asset);
}
inline bool AFileExist(AFile file) {
return file.asset != nullptr;
}
inline uint64_t AFileSize(AFile file) {
return AAsset_getLength(file.asset);
}
#else
struct AFile {
FILE* file;
};
inline AFile AFileOpen(const char* fileName, AOpenFlag flag)
{
FILE* file;
#ifdef _MSC_VER
const char* modes[4] = { "rb", "wb", "rb", "wb" };
fopen_s(&file, fileName, modes[flag]);
#else
const char* modes[4] = { "rb", "wb", "r", "w"};
file = fopen(fileName, modes[flag]);
#endif
if (flag == AOpenFlag_WriteText) {
// Write the UTF-8 BOM
unsigned char bom[] = {0xEF, 0xBB, 0xBF};
fwrite(bom, sizeof(char), sizeof(bom), file);
}
AFile afile;
afile.file = file;
return afile;
}
inline void AFileRead(void* dst, uint64_t size, AFile file, int alignment = 1) {
fread(dst, alignment, size, file.file);
}
inline void AFileWrite(const void* src, uint64_t size, AFile file, int alignment = 1) {
fwrite(src, alignment, size, file.file);
}
inline void AFileSeekBegin(AFile file) {
fseek(file.file, 0, SEEK_SET);
}
inline void AFileSeek(long offset, AFile file) {
fseek(file.file, offset, SEEK_CUR);
}
inline void AFileClose(AFile file) {
fclose(file.file);
}
inline bool AFileExist(AFile file) {
return file.file != nullptr;
}
inline uint64_t AFileSize(AFile file)
{
#ifdef _WIN32
return _filelengthi64(_fileno(file.file));
#elif defined(__ANDROID__)
if (file.asset == nullptr) return 0;
return AAsset_getLength(file.asset);
#else
struct stat sb; stat(file.file, &sb);
return sb.st_size;
#endif
}
#endif
inline char* ReadAllFile(const char* fileName, char* buffer = 0)
{
AFile file = AFileOpen(fileName, AOpenFlag_ReadBinary);
if (!AFileExist(file))
return nullptr;
uint64_t fileSize = AFileSize(file);
if (buffer == nullptr) {
buffer = new char[fileSize+1]{}; // +1 for null terminator
}
AFileRead(buffer, fileSize, file);
AFileClose(file);
return buffer;
}
// don't forget to free using FreeAllText
// fileName : path of the file that we want to load
// buffer : is pre allocated memory if exist. otherwise null
// numCharacters : if not null returns length of the imported string
// startText : if its not null will be added to start of the buffer
// note: if you define it you are responsible of deleting the buffer
inline char* ReadAllText(const char* fileName, char* buffer = 0, uint64_t* numCharacters = 0, const char* startText = 0)
{
int startTextLen = 0;
if (startText)
while (startText[startTextLen]) startTextLen++;
AFile file = AFileOpen(fileName, AOpenFlag_ReadBinary);
if (!AFileExist(file)) {
perror("Error opening the file");
return nullptr; // Return an error code
}
// Determine the file size
uint64_t file_size = AFileSize(file);
// Allocate memory to store the entire file
if (buffer == nullptr)
buffer = new char[file_size + 40 + startTextLen] {}; // +1 for null terminator
if (file_size >= 3)
{
AFileRead(buffer, 3, file);
bool isBOM = buffer[0] == '\xEF' && buffer[1] == '\xBB' && buffer[2] == '\xBF';
if (!isBOM) {
AFileSeekBegin(file);
}
}
if (startText)
while (*startText) *buffer++ = *startText++;
if (buffer == NULL) {
AFileClose(file);
return nullptr; // Return an error code
}
// Read the entire file into the buffer
AFileRead(buffer, file_size, file);
buffer[file_size] = '\0'; // Null-terminate the buffer
AFileClose(file);
if (numCharacters)
*numCharacters = (uint64_t)file_size + 1;
return buffer - startTextLen;
}
inline void FreeAllText(char* text)
{
delete[] text;
}
inline void WriteAllBytes(const char *filename, const char *bytes, unsigned long size)
{
// Open the file for writing in binary mode
AFile file = AFileOpen(filename, AOpenFlag_WriteBinary);
if (!AFileExist(file))
{
perror("Failed to open file for writing");
return;
}
AFileWrite(bytes, size, file);
AFileClose(file);
}
struct ScopedText
{
char* text;
ScopedText(char* txt) : text(txt) {}
~ScopedText() { delete[] text; }
operator char*() const { return text; }
};
// buffer is pre allocated memory if exist. otherwise null.
// note: if you define it you are responsible of deleting the buffer
inline void CopyFile(const char* source, const char* dst, char* buffer = 0)
{
uint64_t sourceSize = 0;
bool bufferProvided = buffer != 0;
char* sourceFile = ReadAllText(source, buffer, &sourceSize);
AFile dstFile = AFileOpen(dst, AOpenFlag_WriteBinary);
AFileWrite(sourceFile, sourceSize, dstFile);
AFileClose(dstFile);
if (!bufferProvided) delete[] buffer;
}
#ifndef _WIN32
#define GetCurrentDirectory(size, outPath) getcwd(outPath, size)
#endif
// input: ../Textures/Tree.png
// output: C:/Source/Repos/Textures/Tree.png
inline void AbsolutePath(const char* path, char* outBuffer, int bufferSize)
{
GetCurrentDirectory(bufferSize, outBuffer);
int currLength = 0;
const char* curr = outBuffer;
while (*curr++) currLength++; // strlen
while (*path)
{
if (path[0] == '.' && path[1] == '.')
{
const char* before = outBuffer + currLength;
const char* newEnd = PathGoBackwards(outBuffer, currLength, true);
currLength -= (int)(before - newEnd);
path += 3; // skip two dot and seperator
outBuffer[currLength++] = ASTL_FILE_SEPERATOR;
}
else
{
while (*path && *path != '\\' && *path != '/')
outBuffer[currLength++] = *path++;
outBuffer[currLength++] = *path++;
}
}
outBuffer[currLength] = '\n';
}
inline void CombinePaths(char* dst, const char* a, const char* b)
{
int parentLen = StringLength(a);
SmallMemCpy(dst, a, parentLen);
dst[parentLen] = ASTL_FILE_SEPERATOR;
SmallMemCpy(dst + parentLen + 1, b, StringLength(b));
}
// data is user defined, whatever data you need, and nullable
typedef void(*FolderVisitFn)(const char* path, void* data);
// thanks to https://github.com/tronkko/dirent this is windows and linux compatible
inline bool VisitFolder(const char* path, FolderVisitFn visitFn, void* data)
{
DIR* dir;
dirent* ent;
if ((dir = opendir(path)) != NULL)
{
char combined[512];
while ((ent = readdir(dir)) != NULL)
{
MemsetZero(combined, 512);
CombinePaths(combined, path, ent->d_name);
if (ent->d_name[0] != '.') visitFn(combined, data);
}
closedir(dir);
return true;
}
return false;
}
inline bool HasAnySubdir(const char* path)
{
DIR* dir;
dirent* ent;
if ((dir = opendir(path)) != NULL)
{
char combined[512];
int pathLen = StringLength(path);
while ((ent = readdir(dir)) != NULL)
{
MemsetZero(combined, 512);
ASSERTR(pathLen + StringLength(ent->d_name) < 511, continue);
CombinePaths(combined, path, ent->d_name);
if (ent->d_name[0] != '.' && IsDirectory(combined)) return true;
}
closedir(dir);
}
return false;
}
inline void RemoveFolder(const char* path, void* unused = nullptr)
{
VisitFolder(path, RemoveFolder, nullptr); // recursive delete all subfolders and files
if (IsDirectory(path)) {
_rmdir(path);
}
else {
RemoveFile(path);
}
}