aboutsummaryrefslogtreecommitdiffhomepage
path: root/common.c
blob: fcc0790c0e231464dc35d07b5d4865a241185310 (plain)
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
void fatal_error(const char* msg) {
    fprintf(stderr, "%s\n", msg);
    exit(1);
}

void unreachable() {
    fatal_error("unreachable");
}

char* read_all(FILE* in) {
    char* buf = calloc(1024 * 1024, sizeof(char));
    char* cur = buf;
    char* tmp = calloc(1024, sizeof(char));
    while (fgets(tmp, 1024, in)) {
        size_t len = strlen(tmp);
        memcpy(cur, tmp, len);
        cur += len;
    }
    return buf;
}

struct String {
    char* data;
    size_t len;
};
typedef struct String String;

char* string_to_cstr(const String* s) {
    char* buf = calloc(s->len + 1, sizeof(char));
    memcpy(buf, s->data, s->len);
    return buf;
}

int string_equals(const String* s1, const String* s2) {
    return s1->len == s2->len && strncmp(s1->data, s2->data, s1->len) == 0;
}

int string_equals_cstr(const String* s1, const char* s2) {
    size_t s2_len = strlen(s2);
    return s1->len == s2_len && strncmp(s1->data, s2, s1->len) == 0;
}