-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_utils.cpp
460 lines (350 loc) · 10.1 KB
/
file_utils.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include "file_utils.h"
#include <glob.h>
// Visits everything in one folder (non-recursively). In our program, this folder is
// the same as the folder of the executable.
// If it finds a directory that doesn't start with '.', call the visit proc on that directory.
bool visit_files(String dir_name, void *data, FileVisitProc visit_proc, bool only_directory)
{
glob_t globlist;
auto wildcard_name = tprint(String("%s/*"), temp_c_string(dir_name));
auto flags = GLOB_PERIOD;
if (only_directory)
{
flags |= GLOB_ONLYDIR;
}
auto glob_error = glob((char*)temp_c_string(wildcard_name), flags, NULL, &globlist);
if (glob_error != 0)
{
return false;
}
for (i32 i = 0; globlist.gl_pathv[i]; ++i)
{
// Both files and dirs
String full_name(globlist.gl_pathv[i]);
auto last_slash = find_index_from_right(full_name, '/');
if (full_name[last_slash + 1] != '.')
{
String short_name = talloc_string(full_name.count - last_slash - 1);
i64 fi = last_slash + 1;
for (i64 si = 0; si < short_name.count; ++si, ++fi)
short_name.data[si] = full_name[fi];
visit_proc(short_name, full_name, data);
}
}
globfree(&globlist);;
return true;
}
// @Important: Be sure to free the memory of the string once used
my_pair<String, bool> read_entire_file(String full_path)
{
auto c_path = temp_c_string(full_path);
FILE *file_stream = fopen((char*)c_path, "rb");
if (!file_stream) return {String(""), false};
i32 success = fseek(file_stream, 0, SEEK_END);
if (success == -1) return {String(""), false};
i64 buffer_size = ftell(file_stream);
success = fseek(file_stream, 0, SEEK_SET);
if (success == -1) return {String(""), false};
// Using the default allocator
String result = alloc_string(buffer_size, global_context.allocator);
fread(result.data, buffer_size, 1, file_stream);
fclose(file_stream);
return {result, true};
}
void advance(String *s, i64 amount)
{
assert(amount >= 0);
assert(s->count >= amount);
s->data += amount;
s->count -= amount;
}
String find_character_from_right(String string, u8 c)
{
auto cursor = string.count - 1;
while (cursor >= 0)
{
if (string[cursor] == c)
{
String substring = string;
substring.data += cursor;
substring.count = string.count - cursor + 1;
return substring;
}
cursor -= 1;
}
return String("");
}
String find_character_from_left(String string, u8 c)
{
auto cursor = 0;
while (cursor < string.count)
{
if (string[cursor] == c)
{
String substring = string;
substring.count = cursor + 1;
return substring;
}
cursor += 1;
}
return String("");
}
void eat_trailing_spaces(String *string)
{
auto cursor = string->count - 1;
while (cursor >= 0)
{
auto c = string->data[cursor];
if (c == ' ' || c == '\t' || c == '\n') cursor -= 1;
else break;
}
string->count = cursor + 1;
}
my_pair<String, bool> consume_next_line(Text_File_Handler *handler)
{
while (true)
{
auto [line, found] = consume_next_line(&handler->file_data);
if (!found) return {String(""), false};
handler->line_number += 1;
eat_spaces(&line);
if (!line) continue;
if (handler->strip_comments_from_ends_of_lines)
{
auto lhs = find_character_from_left(line, handler->comment_character);
if (lhs)
{
line = lhs;
if (line.count == 0) continue;
}
}
if (line[0] == handler->comment_character) continue;
eat_trailing_spaces(&line);
assert(line.count > 0);
return {line, found};
}
}
my_pair<String, bool> consume_next_line(String *sp)
{
auto s = *sp;
auto t = find_index_from_left(s, '\n');
if (t == -1)
{
if (s.count)
{
t = sp->count;
}
else
{
return {s, false};
}
}
auto result = talloc_string(t);
for (i32 i = 0; i < t; ++i)
{
result.data[i] = s.data[i];
}
auto removal_count = t + 1;
if (removal_count > sp->count) removal_count = sp->count;
advance(sp, removal_count);
return {result, true};
}
void eat_spaces(String *sp)
{
while (true)
{
if (!sp->count) return;
auto c = sp->data[0];
if (!isspace(c)) return;
sp->data += 1;
sp->count -= 1;
}
}
void deinit(Text_File_Handler *handler)
{
my_free(handler->orig_file_data.data);
}
// @Todo: cleanup this and string_to_float
my_pair<i64, String> string_to_int(String s, bool *success)
{
eat_spaces(&s);
if (!s)
{
*success = false;
return {0, s};
}
i64 i = 0;
// if (!s || !isdigit(s[i]))
// {
// }
String int_up_to_space = s;
for (auto c : int_up_to_space)
{
if (isdigit(c)) {/* We are good! */}
else if (c == '-') {/* We are good! */}
else if (c == 'e') {/* We are good! */}
else break;
i += 1;
}
int_up_to_space.count = i;
auto c_int = temp_c_string(int_up_to_space);
i64 result = 0;
auto total_read = sscanf((char*)c_int, "%ld", &result);
if (total_read != 1)
{
*success = false;
return {0, s};
}
advance(&s, i);
*success = true;
return {result, s};
}
// @Todo: revisit this later, currently not good
my_pair<f32, String> string_to_float(String s, bool *success)
{
eat_spaces(&s);
if (!s)
{
*success = false;
return {0.0, s};
}
i64 i = 0;
// if (!s || (!isdigit(s[i]) && (s[i] != '.')))
// {
// *success = false;
// return {0.0, s};
// }
String float_up_to_space = s;
bool has_encounter_f = false;
// @Incomplete:
for (auto c : float_up_to_space)
{
if (isdigit(c)) {/* We are good! */}
else if (c == '-' || c == '.') {/* We are good! */}
else if (c == 'e') {/* We are good! */}
else if (isalpha(c))
{
if (c == 'f')
{
i += 1;
}
break;
}
else break;
i += 1;
}
float_up_to_space.count = i;
auto c_float = (char*)temp_c_string(float_up_to_space);
f32 result = 0;
auto scan_read = sscanf((char*)c_float, "%f", &result);
if (scan_read != 1)
{
*success = false;
return {0.0, s};
}
advance(&s, i);
*success = true;
return {result, s};
}
my_pair<Vector4, String> string_to_vec4(String s, bool *success)
{
Vector4 result;
String orig_string;
f32 value;
String remainder;
bool f_success;
my_pair<f32, String> p;
p = string_to_float(s, &f_success);
value = p.first;
remainder = p.second;
*success = f_success;
if (!f_success) return {Vector4(0, 0, 0, 0), orig_string};
result.x = value;
p = string_to_float(remainder, &f_success);
value = p.first;
remainder = p.second;
*success = f_success;
if (!f_success) return {Vector4(0, 0, 0, 0), orig_string};
result.y = value;
p = string_to_float(remainder, &f_success);
value = p.first;
remainder = p.second;
*success = f_success;
if (!f_success) return {Vector4(0, 0, 0, 0), orig_string};
result.z = value;
p = string_to_float(remainder, &f_success);
value = p.first;
remainder = p.second;
*success = f_success;
if (!f_success) return {Vector4(0, 0, 0, 0), orig_string};
result.w = value;
return {result, remainder};
}
my_pair<String, String> break_by_spaces(String line)
{
eat_spaces(&line);
i64 i = 0;
String first_half = line;
for (auto c : first_half)
{
if (isspace(c)) break;
i += 1;
}
first_half.count = i;
advance(&line, i);
eat_spaces(&line);
return {first_half, line};
}
void start_file(Text_File_Handler *handler, String full_path, String log_agent)
{
// @Important: We do NOT copy any of these string; we presume they remain valid
// throughout the lifetime of the handler.
handler->full_path = full_path;
handler->log_agent = log_agent;
auto [file_data, success] = read_entire_file(full_path);
handler->file_data = file_data;
handler->orig_file_data = file_data;
char *c_log_agent = (char*)temp_c_string(log_agent);
char *c_full_path = (char*)temp_c_string(full_path);
if (!success)
{
logprint(c_log_agent, "Unable to load file '%s'.\n", c_full_path);
handler->failed = true;
return;
}
if (handler->do_version_number)
{
// Parse the verison number.
auto [line, found] = consume_next_line(&handler->file_data);
handler->line_number += 1;
if (!found)
{
logprint(c_log_agent, "Unable to find a version number at the top of file '%s'!", c_full_path);
handler->failed = true;
return;
}
assert(line.count > 0);
if (line[0] != '[')
{
logprint(c_log_agent, "Expected '[' at the top of file '%s', but did not get it!\n", c_full_path);
handler->failed = true;
return;
}
advance(&line, 1);
bool version_success = false;
auto [version, remainder] = string_to_int(line, &version_success);
if (!version_success)
{
logprint(c_log_agent, "Unable to parse the version number at the top of file '%s'!\n", c_full_path);
handler->failed = true;
return;
}
if ((!remainder.count) || (remainder[0] != ']'))
{
logprint(c_log_agent, "Expected ']' after version number in file '%s', but did not get it! (Remainder is: '%s')\n", c_full_path, temp_c_string(remainder));
handler->failed = true;
return;
}
handler->version = version;
}
}