-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
85 lines (72 loc) · 1.7 KB
/
util.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
#include "util.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h>
Vector *new_vector() {
Vector *vec = malloc(sizeof(Vector));
vec->capacity = 16;
vec->data = malloc(sizeof(void *) * vec->capacity);
vec->len = 0;
return vec;
}
void vec_push(Vector *vec, void *elem) {
if (vec->capacity == vec->len) {
vec->capacity *= 2;
vec->data = realloc(vec->data, sizeof(void *) * vec->capacity);
}
vec->data[vec->len++] = elem;
}
void *vec_pop(Vector *vec) {
return vec->data[--vec->len];
}
void *vec_last(Vector *vec) {
return vec->data[vec->len - 1];
}
void *vec_at(Vector *vec, int index) {
return vec->data[index];
}
Map *new_map() {
Map *map = malloc(sizeof(Map));
map->keys = new_vector();
map->vals = new_vector();
return map;
}
void map_put(Map *map, char *key, void *val) {
vec_push(map->keys, key);
vec_push(map->vals, val);
}
void *map_get(Map *map, char *key) {
for (int i = map->keys->len - 1; i >= 0; i--) {
if (strcmp((char *)vec_at(map->keys, i), key) == 0) {
return vec_at(map->vals, i);
}
}
return NULL;
}
Vector *map_keys(Map *map) {
return map->keys;
}
int *intdup(int n) {
int *num = malloc(sizeof(int));
*num = n;
return num;
}
noreturn void error(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
void expect(int line, int expected, int actual) {
if (expected == actual)
return;
error("%d: %d expected, but got %d\n", line, expected, actual);
}
void expect_ptr(int line, void *expected, void *actual) {
if (expected == actual)
return;
error("%d: %d expected, but got %d\n", line, expected, actual);
}