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