-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathats_file.c
240 lines (203 loc) · 5.81 KB
/
ats_file.c
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
#include "ats.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define STB_IMAGE_IMPLEMENTATION
#include "ext/stb_image.h"
static usize _file_get_size(FILE* fp) {
fseek(fp, 0L, SEEK_END);
usize size = ftell(fp);
fseek(fp, 0, SEEK_SET);
return size;
}
static FILE* _file_open(const char* path, const char* mode) {
FILE* file = 0;
errno_t error = fopen_s(&file, path, mode);
return error? 0 : file;
}
ATS_API usize file_get_size(const char* path) {
FILE* fp = _file_open(path, "rb");
if (!fp) return 0;
usize size = _file_get_size(fp);
fclose(fp);
return size;
}
ATS_API s8 file_read_s8(const char* file_name) {
u8* buffer = 0;
usize size = 0;
FILE* fp = _file_open(file_name, "rb");
if (fp) {
size = _file_get_size(fp);
buffer = (u8*)mem_alloc(size + 1);
if (buffer) {
buffer[size] = 0;
if (fread(buffer, 1, size, fp) == 0) {
buffer = 0;
}
}
fclose(fp);
}
return (s8) { buffer, (isize)size };
}
ATS_API b32 file_write_s8(const char* file_name, s8 buffer) {
FILE* fp = _file_open(file_name, "w");
if (fp) {
usize n = fwrite(buffer.buf, 1, buffer.len, fp);
fclose(fp);
return n == buffer.len;
}
return 0;
}
ATS_API b32 file_append_str(const char* file_name, s8 buffer) {
FILE* fp = _file_open(file_name, "a");
if (fp) {
usize n = fwrite(buffer.buf, 1, buffer.len, fp);
fclose(fp);
return n == buffer.len;
}
return 0;
}
ATS_API usize file_read_bin(const char* file_name, void* buffer, usize size) {
FILE* fp = _file_open(file_name, "rb");
if (fp) {
usize file_size = fread(buffer, 1, size, fp);
fclose(fp);
return file_size;
}
return 0;
}
ATS_API b32 file_write_bin(const char* file_name, const void* buffer, usize size) {
FILE *fp = _file_open(file_name, "wb");
if (fp) {
fwrite(buffer, 1, size, fp);
fclose(fp);
return 1;
}
return 0;
}
ATS_API u32* file_load_image(const char* path, u16* width, u16* height) {
u32* pixels = 0;
int channels = 0;
int w = 0;
int h = 0;
pixels = (u32*)stbi_load(path, &w, &h, &channels, 4);
assert(pixels);
*width = w;
*height = h;
return pixels;
}
ATS_API void file_free_image(const u32* pixels) {
stbi_image_free((void*)pixels);
}
static void _file_cstr_concat(char* out, const char* a, const char* b) {
while (*a) *out++ = *a++;
while (*b) *out++ = *b++;
*(out) = '\0';
}
struct _file_iter {
char current[MAX_PATH];
char path[MAX_PATH];
b32 done;
HANDLE handle;
WIN32_FIND_DATAA data;
};
ATS_API int file_iter_is_valid(struct _file_iter* it) {
return !it->done;
}
ATS_API void file_iter_advance(struct _file_iter* it) {
it->done = !FindNextFileA(it->handle, &it->data);
if (!it->done) {
_file_cstr_concat(it->current, it->path, it->data.cFileName);
}
}
ATS_API struct _file_iter file_iter_create(const char* path, const char* ext) {
if (!path) path = "./";
if (!ext) ext = "*";
struct _file_iter it = {0};
u32 i = 0;
for (i = 0; path[i]; ++i) {
it.path[i] = path[i];
}
if (it.path[i - 1] != '/') {
it.path[i] = '/';
}
char find_file_str[MAX_PATH] = {0};
_file_cstr_concat(find_file_str, it.path, ext);
it.handle = FindFirstFileA(find_file_str, &it.data);
it.done = it.handle == INVALID_HANDLE_VALUE;
if (!it.done) {
_file_cstr_concat(it.current, it.path, it.data.cFileName);
}
return it;
}
ATS_API b32 file_iter_at_directory(struct _file_iter* it) {
const char* n = it->data.cFileName;
return (n[0] != '.') && (it->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
}
static struct {
char path[MAX_PATH];
char name[MAX_PATH];
char extension[MAX_PATH];
const char* wildcard;
i32 idx;
struct _file_iter stack[256];
} _dir_iter;
static void _dir_update_file_info(void) {
char* s = _dir_iter.stack[_dir_iter.idx].current;
i32 i = 0;
i32 j = 0;
i32 e = MAX_PATH;
i32 n = MAX_PATH;
_dir_iter.path[0] = '\0';
_dir_iter.name[0] = '\0';
_dir_iter.extension[0] = '\0';
for (i = 0; s && s[i]; ++i) {
_dir_iter.path[i] = s[i];
switch (s[i]) {
case '\\': case '/': n = i; break;
case '.': e = i; break;
}
}
_dir_iter.path[i] = '\0';
if (e < MAX_PATH) {
for (j = 0; (e + j) < (i - 1); ++j) _dir_iter.extension[j] = s[(e + 1) + j];
_dir_iter.extension[j] = '\0';
} else {
e = i;
}
if (n < e) {
for (j = 0; (n + j) < (e - 1); ++j) _dir_iter.name[j] = s[(n + 1) + j];
_dir_iter.name[j] = '\0';
}
}
ATS_API void dir_open(const char* path, const char* wildcard) {
memset(&_dir_iter, 0, sizeof _dir_iter);
_dir_iter.wildcard = wildcard;
_dir_iter.stack[_dir_iter.idx] = file_iter_create(path, _dir_iter.wildcard);
_dir_update_file_info();
}
ATS_API void dir_advance(void) {
if (file_iter_at_directory(&_dir_iter.stack[_dir_iter.idx])) {
_dir_iter.stack[_dir_iter.idx + 1] = file_iter_create(_dir_iter.stack[_dir_iter.idx].current, _dir_iter.wildcard);
file_iter_advance(&_dir_iter.stack[_dir_iter.idx]);
_dir_iter.idx++;
_dir_update_file_info();
} else {
file_iter_advance(&_dir_iter.stack[_dir_iter.idx]);
_dir_update_file_info();
}
while ((_dir_iter.idx >= 0) && !file_iter_is_valid(&_dir_iter.stack[_dir_iter.idx])) {
_dir_iter.idx--;
}
}
ATS_API b32 dir_is_valid(void) {
return _dir_iter.idx >= 0;
}
ATS_API char* dir_path(void) {
return _dir_iter.path;
}
ATS_API char* dir_name(void) {
return _dir_iter.name;
}
ATS_API char* dir_extension(void) {
return _dir_iter.extension;
}