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