Line | Count | Source (jump to first uncovered line) |
1 | | #include "cache.h" |
2 | | #include "strvec.h" |
3 | | #include "strbuf.h" |
4 | | |
5 | | const char *empty_strvec[] = { NULL }; |
6 | | |
7 | | void strvec_init(struct strvec *array) |
8 | 0 | { |
9 | 0 | struct strvec blank = STRVEC_INIT; |
10 | 0 | memcpy(array, &blank, sizeof(*array)); |
11 | 0 | } |
12 | | |
13 | | static void strvec_push_nodup(struct strvec *array, const char *value) |
14 | 0 | { |
15 | 0 | if (array->v == empty_strvec) |
16 | 0 | array->v = NULL; |
17 | |
|
18 | 0 | ALLOC_GROW(array->v, array->nr + 2, array->alloc); |
19 | 0 | array->v[array->nr++] = value; |
20 | 0 | array->v[array->nr] = NULL; |
21 | 0 | } |
22 | | |
23 | | const char *strvec_push(struct strvec *array, const char *value) |
24 | 0 | { |
25 | 0 | strvec_push_nodup(array, xstrdup(value)); |
26 | 0 | return array->v[array->nr - 1]; |
27 | 0 | } |
28 | | |
29 | | const char *strvec_pushf(struct strvec *array, const char *fmt, ...) |
30 | 0 | { |
31 | 0 | va_list ap; |
32 | 0 | struct strbuf v = STRBUF_INIT; |
33 | |
|
34 | 0 | va_start(ap, fmt); |
35 | 0 | strbuf_vaddf(&v, fmt, ap); |
36 | 0 | va_end(ap); |
37 | |
|
38 | 0 | strvec_push_nodup(array, strbuf_detach(&v, NULL)); |
39 | 0 | return array->v[array->nr - 1]; |
40 | 0 | } |
41 | | |
42 | | void strvec_pushl(struct strvec *array, ...) |
43 | 0 | { |
44 | 0 | va_list ap; |
45 | 0 | const char *arg; |
46 | |
|
47 | 0 | va_start(ap, array); |
48 | 0 | while ((arg = va_arg(ap, const char *))) |
49 | 0 | strvec_push(array, arg); |
50 | 0 | va_end(ap); |
51 | 0 | } |
52 | | |
53 | | void strvec_pushv(struct strvec *array, const char **items) |
54 | 0 | { |
55 | 0 | for (; *items; items++) |
56 | 0 | strvec_push(array, *items); |
57 | 0 | } |
58 | | |
59 | | void strvec_pop(struct strvec *array) |
60 | 0 | { |
61 | 0 | if (!array->nr) |
62 | 0 | return; |
63 | 0 | free((char *)array->v[array->nr - 1]); |
64 | 0 | array->v[array->nr - 1] = NULL; |
65 | 0 | array->nr--; |
66 | 0 | } |
67 | | |
68 | | void strvec_split(struct strvec *array, const char *to_split) |
69 | 0 | { |
70 | 0 | while (isspace(*to_split)) |
71 | 0 | to_split++; |
72 | 0 | for (;;) { |
73 | 0 | const char *p = to_split; |
74 | |
|
75 | 0 | if (!*p) |
76 | 0 | break; |
77 | | |
78 | 0 | while (*p && !isspace(*p)) |
79 | 0 | p++; |
80 | 0 | strvec_push_nodup(array, xstrndup(to_split, p - to_split)); |
81 | |
|
82 | 0 | while (isspace(*p)) |
83 | 0 | p++; |
84 | 0 | to_split = p; |
85 | 0 | } |
86 | 0 | } |
87 | | |
88 | | void strvec_clear(struct strvec *array) |
89 | 0 | { |
90 | 0 | if (array->v != empty_strvec) { |
91 | 0 | int i; |
92 | 0 | for (i = 0; i < array->nr; i++) |
93 | 0 | free((char *)array->v[i]); |
94 | 0 | free(array->v); |
95 | 0 | } |
96 | 0 | strvec_init(array); |
97 | 0 | } |
98 | | |
99 | | const char **strvec_detach(struct strvec *array) |
100 | 0 | { |
101 | 0 | if (array->v == empty_strvec) |
102 | 0 | return xcalloc(1, sizeof(const char *)); |
103 | 0 | else { |
104 | 0 | const char **ret = array->v; |
105 | 0 | strvec_init(array); |
106 | 0 | return ret; |
107 | 0 | } |
108 | 0 | } |