-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
87 lines (70 loc) · 2.89 KB
/
util.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
#ifndef CHIP8_UTIL_H
#define CHIP8_UTIL_H
#include <stdlib.h>
#define UTIL_INSTRUCTION_START 0x200 // where CHIP-8 programs start in memory
#define UTIL_INIT_CAP 256
#define util_da_append(da, item) \
do { \
if ((da)->count >= (da)->capacity) { \
(da)->capacity = (da)->capacity == 0 ? UTIL_INIT_CAP : (da)->capacity*2; \
(da)->items = realloc((da)->items, sizeof(*(da)->items) * (da)->capacity); \
} \
(da)->items[(da)->count++] = (item); \
} while (0)
#define util_da_free(da) free((da)->items)
#define util_da_append_many(da, items, new_count) \
do { \
if ((da)->count + (new_count) >= (da)->capacity) { \
if ((da)->capacity == 0) (da)->capacity = UTIL_INIT_CAP; \
while ((da)->count + (new_count) >= (da)->capacity) { \
(da)->capacity *= 2; \
} \
(da)->items = realloc((da)->items, sizeof(*(da)->items) * (da)->capacity); \
} \
memcopy((da)->items + (da)->count, (items), sizeof(*(da)->items) * (new_count)); \
(da)->count += (new_count); \
} while (0)
typedef struct {
char* items;
size_t count;
size_t capacity;
} String;
typedef struct {
char** items;
size_t count;
size_t capacity;
} CString_List;
bool util_read_file(const char *path, String *out) {
FILE* file = fopen(path, "rb");
if (file == NULL) return false;
fseek(file, 0, SEEK_END);
size_t length = ftell(file);
fseek(file, 0, SEEK_SET);
size_t new_count = out->count + length;
while (new_count >= out->capacity) {
if (out->capacity == 0) out->capacity = UTIL_INIT_CAP;
else out->capacity *= 2;
out->items = realloc(out->items, out->capacity);
}
fread(out->items + out->count, length, 1, file);
out->count = new_count;
if (file) fclose(file);
return true;
}
bool util_write_file(const char *path, void *data, size_t length) {
FILE* file = fopen(path, "wb");
if (file == NULL) return false;
const char* buf = data;
while (length > 0) {
size_t written = fwrite(buf, 1, length, file);
if (written == 0) {
fclose(file);
return false;
}
length -= written;
buf += written;
}
if (file) fclose(file);
return true;
}
#endif //CHIP8_UTIL_H