-
Notifications
You must be signed in to change notification settings - Fork 0
/
mf_file.h
303 lines (253 loc) · 7.33 KB
/
mf_file.h
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
#ifndef MF_FILE_H
#define MF_FILE_H
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include <stdint.h>
#define FUNC_CROSS
#define FUNC_WIN32
#define FUNC_LINUX
#if defined(WIN32) || defined(_WIN32)
#include <windows.h>
#else
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#endif
#define API static inline
// {{{ Macros
#define MF_PATH_SEPARATOR_WINDOWS '\\'
#define MF_PATH_SEPARATOR_UNIX '/'
#if defined(WIN32) || defined(_WIN32)
#define MF_PATH_SEPARATOR MF_PATH_SEPARATOR_WINDOWS
#else
#define MF_PATH_SEPARATOR MF_PATH_SEPARATOR_UNIX
#endif
// }}}
// {{{ Declarations
// -- {{{ General
// TODO: rename to mf_filesystem
FUNC_CROSS API bool MFF_Exists(const char *target);
FUNC_CROSS API bool MFF_IsFile(const char *filename);
// -- }}}
// -- {{{ Path
API char* MFF_PathJoinCreate(const char *a, const char *b, char separator);
// -- }}}
// -- {{{ File
API char* MFF_FileRead(const char *path, const char *mode, size_t *size);
FUNC_CROSS API void MFF_FileCopy(const char *src, const char *dest);
FUNC_CROSS API uint64_t MFF_FileGetWriteTime(const char *filename);
// -- }}}
// -- {{{ Directory
typedef struct MFF_PathItem MFF_PathItem;
typedef struct MFF_Directory MFF_Directory;
FUNC_CROSS API bool MFF_DirectoryOpen(MFF_Directory *dir, const char* name, bool recursive);
FUNC_CROSS API bool MFF_DirectoryNext(MFF_Directory *dir, MFF_PathItem *item);
FUNC_CROSS API void MFF_DirectoryClose(MFF_Directory *dir);
API bool MFF_PathItemIsDirectory(MFF_PathItem *item);
API bool MFF_PathItemIsFile(MFF_PathItem *item);
// -- }}}
// }}}
#if defined(MF_FILE_IMPLEMENTATION) | defined(MF_IMPLEMENTATION)
// {{{ Definitions
// -- {{{ Path
API char* MFF_PathJoinCreate(const char *a, const char *b, char separator) {
char *res = (char *) malloc(strlen(a) + strlen(b) + 2);
sprintf(res, "%s%c%s", a, separator, b);
return res;
}
// -- }}}
// -- {{{ File
API char* MFF_FileRead(const char *path, const char *mode, size_t *size) {
FILE *file = fopen(path, mode);
size_t bytesToRead = 0;
fseek(file, 0, SEEK_END);
bytesToRead = ftell(file);
if (size)
*size = bytesToRead;
fseek(file, 0, SEEK_SET);
char *res = (char *) malloc(bytesToRead);
while (bytesToRead > 0) {
size_t readBytes = fread(res, 1, bytesToRead, file);
bytesToRead -= readBytes;
}
return res;
}
// -- }}}
// -- {{{ Directory
enum MF_PathItemType {
PATH_ITEM_UNKNOWN,
PATH_ITEM_FILE,
PATH_ITEM_DIRECTORY,
};
struct MFF_PathItem {
char *name;
enum MF_PathItemType type;
};
struct MFF_Directory {
#ifdef WIN32
HANDLE handle;
WIN32_FIND_DATAA data;
bool firstOne;
#else
DIR *d;
#endif
};
API bool MFF_PathItemIsDirectory(MFF_PathItem *item) {
return item->type == PATH_ITEM_DIRECTORY;
}
API bool MFF_PathItemIsFile(MFF_PathItem *item) {
return item->type == PATH_ITEM_FILE;
}
// }}}
#ifdef WIN32
FUNC_WIN32 API bool MFF_Exists(const char *target) {
WIN32_FIND_DATA data;
HANDLE handle = FindFirstFile(target, &data);
bool res = handle != INVALID_HANDLE_VALUE;
return res;
}
FUNC_WIN32 API bool MFF_IsFile(const char *filename) {
WIN32_FIND_DATA data;
HANDLE handle = FindFirstFile(filename, &data);
bool res = handle != INVALID_HANDLE_VALUE;
if (res && (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
res = false;
}
return res;
}
FUNC_WIN32 API void MFF_FileCopy(const char *src, const char *dest) {
int res = CopyFile(src, dest, 0);
assert(res != 0);
}
FUNC_WIN32 API uint64_t MFF_FileGetWriteTime(const char *filename) {
uint64_t res = 0;
WIN32_FILE_ATTRIBUTE_DATA data;
if (GetFileAttributesEx(filename, GetFileExInfoStandard, &data)) {
res = data.ftLastWriteTime.dwHighDateTime;
res = (res << 32);
res = data.ftLastWriteTime.dwLowDateTime + res;
}
return res;
}
FUNC_WIN32 API bool MFF_DirectoryOpen(MFF_Directory *dir, const char *name, bool recursive) {
bool res = false;
char buffer[256];
sprintf(buffer, "%s\\*.*", name);
dir->handle = FindFirstFile(buffer, &dir->data);
res = dir->handle != INVALID_HANDLE_VALUE;
dir->firstOne = true;
return res;
}
FUNC_WIN32 API bool MFF_DirectoryNext(MFF_Directory *dir, MFF_PathItem *item) {
bool res = false;
if (dir->firstOne) {
dir->firstOne = false;
res = dir->handle != INVALID_HANDLE_VALUE;
} else {
res = FindNextFileA(dir->handle, &dir->data);
}
if (res) {
item->name = dir->data.cFileName;
item->type = PATH_ITEM_FILE;
if (dir->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
item->type = PATH_ITEM_DIRECTORY;
}
if (strcmp(item->name, ".") == 0 || strcmp(item->name, "..") == 0) {
MFF_DirectoryNext(dir, item);
}
}
return res;
}
FUNC_WIN32 API void MFF_DirectoryClose(MFF_Directory *dir) {
FindClose(dir->handle);
}
#elif __linux__
FUNC_LINUX API bool MFF_Exists(const char *target) {
bool res = access(target, F_OK) == 0;
return res;
}
FUNC_LINUX API bool MFF_IsFile(const char *filename) {
bool res = access(filename, F_OK) == 0;
if (res) {
// TODO: dirty hack to check if its a directory
DIR *d;
d = opendir(filename);
struct dirent *dire;
if (d) {
dire = readdir(d);
if (dire && dire->d_type != DT_REG) {
res = false;
}
}
}
return res;
}
FUNC_LINUX API void MFF_FileCopy(const char *src, const char *dest) {
struct stat st;
assert(stat(src, &st) == 0);
int fdSrc = open(src, O_RDONLY);
assert(fdSrc >= 0);
int fdDest = open(dest, O_CREAT | O_WRONLY | O_TRUNC, st.st_mode);
assert(fdDest >= 0);
char buf[8 * 1024];
while (1) {
size_t bytesRead = read(fdSrc, &buf[0], sizeof(buf));
if (!bytesRead)
break;
size_t bytesWrote = write(fdDest, &buf[0], bytesRead);
assert(bytesRead == bytesWrote);
}
close(fdSrc);
close(fdDest);
}
FUNC_LINUX API uint64_t MFF_FileGetWriteTime(const char *filename) {
uint64_t res = 0;
struct stat st = {};
assert(stat(filename, &st) == 0);
res = st.st_mtime;
return res;
}
FUNC_LINUX API bool MFF_DirectoryOpen(MFF_Directory *dir, const char *name, bool recursive) {
bool res = false;
dir->d = opendir(name);
res = dir->d != NULL;
return res;
}
FUNC_LINUX API bool MFF_DirectoryNext(MFF_Directory *dir, MFF_PathItem *item) {
bool res = false;
struct dirent *dire;
dire = readdir(dir->d);
while (dire != NULL && strcmp(dire->d_name, ".") == 0
|| dire != NULL && strcmp(dire->d_name, "..") == 0) {
dire = readdir(dir->d);
}
if (dire) {
if (strcmp(dire->d_name, "..") == 0) {
dire = readdir(dir->d);
}
item->name = dire->d_name;
switch (dire->d_type) {
case DT_DIR: {
item->type = PATH_ITEM_DIRECTORY;
} break;
case DT_REG: {
item->type = PATH_ITEM_FILE;
} break;
default:
item->type =PATH_ITEM_UNKNOWN;
}
res = true;
}
return res;
}
FUNC_LINUX API void MFF_DirectoryClose(MFF_Directory *dir) {
closedir(dir->d);
}
#endif
// }}}
#endif // MF_FILE_IMPLEMENTATION
#endif // MF_FILE_H