-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils.c
More file actions
77 lines (67 loc) · 1.31 KB
/
utils.c
File metadata and controls
77 lines (67 loc) · 1.31 KB
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
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"
string_t string(const char *str)
{
string_t p = checked_malloc(strlen(str) + 1);
strcpy(p, str);
return p;
}
void *checked_malloc(int size)
{
void *p = malloc(size);
assert(p);
return p;
}
list_t list(void *data, list_t next)
{
list_t p = checked_malloc(sizeof(*p));
p->data = data;
p->next = next;
return p;
}
list_t vlist(int count, ...)
{
list_t result = NULL, next = NULL;
va_list ap;
va_start(ap, count);
for (; count > 0; count--)
{
void *data = va_arg(ap, void *);
list_t p = list(data, NULL);
if (result)
next = next->next = p;
else
result = next = p;
}
return result;
}
list_t int_list(int i, list_t next)
{
list_t p = checked_malloc(sizeof(*p));
p->i = i;
p->next = next;
return p;
}
list_t bool_list(bool b, list_t next)
{
list_t p = checked_malloc(sizeof(*p));
p->b = b;
p->next = next;
return p;
}
list_t join_list(list_t list1, list_t list2)
{
list_t p = list1;
if (!p)
return list2;
while (p->next)
p = p->next;
p->next = list2;
return list1;
}
list_t list_append(list_t list1, void *data)
{
return join_list(list1, list(data, NULL));
}