]>
Commit | Line | Data |
---|---|---|
36bf1958 | 1 | #include "git-compat-util.h" |
dbbcd44f | 2 | #include "strvec.h" |
c1189cae JK |
3 | #include "strbuf.h" |
4 | ||
873cd28a | 5 | const char *empty_strvec[] = { NULL }; |
c1189cae | 6 | |
873cd28a | 7 | void strvec_init(struct strvec *array) |
c1189cae | 8 | { |
5726a6b4 ÆAB |
9 | struct strvec blank = STRVEC_INIT; |
10 | memcpy(array, &blank, sizeof(*array)); | |
c1189cae JK |
11 | } |
12 | ||
873cd28a | 13 | static void strvec_push_nodup(struct strvec *array, const char *value) |
c1189cae | 14 | { |
d70a9eb6 JK |
15 | if (array->v == empty_strvec) |
16 | array->v = NULL; | |
c1189cae | 17 | |
d70a9eb6 JK |
18 | ALLOC_GROW(array->v, array->nr + 2, array->alloc); |
19 | array->v[array->nr++] = value; | |
20 | array->v[array->nr] = NULL; | |
c1189cae JK |
21 | } |
22 | ||
873cd28a | 23 | const char *strvec_push(struct strvec *array, const char *value) |
c1189cae | 24 | { |
873cd28a | 25 | strvec_push_nodup(array, xstrdup(value)); |
d70a9eb6 | 26 | return array->v[array->nr - 1]; |
c1189cae JK |
27 | } |
28 | ||
873cd28a | 29 | const char *strvec_pushf(struct strvec *array, const char *fmt, ...) |
c1189cae JK |
30 | { |
31 | va_list ap; | |
32 | struct strbuf v = STRBUF_INIT; | |
33 | ||
34 | va_start(ap, fmt); | |
35 | strbuf_vaddf(&v, fmt, ap); | |
36 | va_end(ap); | |
37 | ||
873cd28a | 38 | strvec_push_nodup(array, strbuf_detach(&v, NULL)); |
d70a9eb6 | 39 | return array->v[array->nr - 1]; |
c1189cae JK |
40 | } |
41 | ||
873cd28a | 42 | void strvec_pushl(struct strvec *array, ...) |
d15bbe13 JK |
43 | { |
44 | va_list ap; | |
45 | const char *arg; | |
46 | ||
47 | va_start(ap, array); | |
828e829b | 48 | while ((arg = va_arg(ap, const char *))) |
873cd28a | 49 | strvec_push(array, arg); |
d15bbe13 JK |
50 | va_end(ap); |
51 | } | |
52 | ||
d70a9eb6 | 53 | void strvec_pushv(struct strvec *array, const char **items) |
85b34324 | 54 | { |
d70a9eb6 JK |
55 | for (; *items; items++) |
56 | strvec_push(array, *items); | |
85b34324 PT |
57 | } |
58 | ||
11ce77b5 PS |
59 | const char *strvec_replace(struct strvec *array, size_t idx, const char *replacement) |
60 | { | |
61 | char *to_free; | |
62 | if (idx >= array->nr) | |
63 | BUG("index outside of array boundary"); | |
64 | to_free = (char *) array->v[idx]; | |
65 | array->v[idx] = xstrdup(replacement); | |
66 | free(to_free); | |
67 | return array->v[idx]; | |
68 | } | |
69 | ||
70 | void strvec_remove(struct strvec *array, size_t idx) | |
71 | { | |
72 | if (idx >= array->nr) | |
73 | BUG("index outside of array boundary"); | |
74 | free((char *)array->v[idx]); | |
75 | memmove(array->v + idx, array->v + idx + 1, (array->nr - idx) * sizeof(char *)); | |
76 | array->nr--; | |
77 | } | |
78 | ||
873cd28a | 79 | void strvec_pop(struct strvec *array) |
fe4a0a28 | 80 | { |
d70a9eb6 | 81 | if (!array->nr) |
fe4a0a28 | 82 | return; |
d70a9eb6 JK |
83 | free((char *)array->v[array->nr - 1]); |
84 | array->v[array->nr - 1] = NULL; | |
85 | array->nr--; | |
fe4a0a28 JK |
86 | } |
87 | ||
873cd28a | 88 | void strvec_split(struct strvec *array, const char *to_split) |
c5aa6db6 JS |
89 | { |
90 | while (isspace(*to_split)) | |
91 | to_split++; | |
92 | for (;;) { | |
93 | const char *p = to_split; | |
94 | ||
95 | if (!*p) | |
96 | break; | |
97 | ||
98 | while (*p && !isspace(*p)) | |
99 | p++; | |
873cd28a | 100 | strvec_push_nodup(array, xstrndup(to_split, p - to_split)); |
c5aa6db6 JS |
101 | |
102 | while (isspace(*p)) | |
103 | p++; | |
104 | to_split = p; | |
105 | } | |
106 | } | |
107 | ||
873cd28a | 108 | void strvec_clear(struct strvec *array) |
c1189cae | 109 | { |
d70a9eb6 | 110 | if (array->v != empty_strvec) { |
c1189cae | 111 | int i; |
d70a9eb6 JK |
112 | for (i = 0; i < array->nr; i++) |
113 | free((char *)array->v[i]); | |
114 | free(array->v); | |
c1189cae | 115 | } |
873cd28a | 116 | strvec_init(array); |
c1189cae | 117 | } |
b992657e | 118 | |
873cd28a | 119 | const char **strvec_detach(struct strvec *array) |
b992657e | 120 | { |
d70a9eb6 | 121 | if (array->v == empty_strvec) |
b992657e JK |
122 | return xcalloc(1, sizeof(const char *)); |
123 | else { | |
d70a9eb6 | 124 | const char **ret = array->v; |
873cd28a | 125 | strvec_init(array); |
b992657e JK |
126 | return ret; |
127 | } | |
128 | } |