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
|
#include "buffers.h"
#include "buffer.h"
#include <stdlib.h>
#include <string.h>
void buffers_init(struct buffers *buffers, uint32_t initial_capacity) {
VEC_INIT(&buffers->buffers, initial_capacity);
VEC_INIT(&buffers->add_hooks, 32);
}
struct buffer *buffers_add(struct buffers *buffers, struct buffer buffer) {
VEC_PUSH(&buffers->buffers, buffer);
struct buffer *slot = VEC_BACK(&buffers->buffers);
VEC_FOR_EACH(&buffers->add_hooks, struct buffers_hook * hook) {
hook->callback(slot, hook->userdata);
}
return slot;
}
uint32_t buffers_add_add_hook(struct buffers *buffers, buffers_hook_cb callback,
void *userdata) {
VEC_PUSH(&buffers->add_hooks, ((struct buffers_hook){
.callback = callback,
.userdata = userdata,
}));
return VEC_SIZE(&buffers->add_hooks) - 1;
}
struct buffer *buffers_find(struct buffers *buffers, const char *name) {
VEC_FOR_EACH(&buffers->buffers, struct buffer * b) {
if (strcmp(name, b->name) == 0) {
return b;
}
}
return NULL;
}
struct buffer *buffers_find_by_filename(struct buffers *buffers,
const char *path) {
VEC_FOR_EACH(&buffers->buffers, struct buffer * b) {
if (b->filename != NULL && strcmp(path, b->filename) == 0) {
return b;
}
}
return NULL;
}
void buffers_destroy(struct buffers *buffers) {
VEC_FOR_EACH(&buffers->buffers, struct buffer * b) { buffer_destroy(b); }
VEC_DESTROY(&buffers->buffers);
}
|