-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslist.c
52 lines (41 loc) · 857 Bytes
/
slist.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
// based on cs3650 starter code
#include <alloca.h>
#include <stdlib.h>
#include <string.h>
#include "slist.h"
slist_t *s_cons(const char *text, slist_t *rest) {
slist_t *xs = malloc(sizeof(slist_t));
xs->data = strdup(text);
xs->refs = 1;
xs->next = rest;
return xs;
}
void s_free(slist_t *xs) {
if (xs == 0) {
return;
}
xs->refs -= 1;
if (xs->refs == 0) {
s_free(xs->next);
free(xs->data);
free(xs);
}
}
slist_t *s_explode(const char *text, char delim) {
if (*text == 0) {
return 0;
}
int plen = 0;
while (text[plen] != 0 && text[plen] != delim) {
plen += 1;
}
int skip = 0;
if (text[plen] == delim) {
skip = 1;
}
slist_t *rest = s_explode(text + plen + skip, delim);
char *part = alloca(plen + 2);
memcpy(part, text, plen);
part[plen] = 0;
return s_cons(part, rest);
}