5 const char *empty_strvec
[] = { NULL
};
7 void strvec_init(struct strvec
*array
)
9 array
->v
= empty_strvec
;
14 static void strvec_push_nodup(struct strvec
*array
, const char *value
)
16 if (array
->v
== empty_strvec
)
19 ALLOC_GROW(array
->v
, array
->nr
+ 2, array
->alloc
);
20 array
->v
[array
->nr
++] = value
;
21 array
->v
[array
->nr
] = NULL
;
24 const char *strvec_push(struct strvec
*array
, const char *value
)
26 strvec_push_nodup(array
, xstrdup(value
));
27 return array
->v
[array
->nr
- 1];
30 const char *strvec_pushf(struct strvec
*array
, const char *fmt
, ...)
33 struct strbuf v
= STRBUF_INIT
;
36 strbuf_vaddf(&v
, fmt
, ap
);
39 strvec_push_nodup(array
, strbuf_detach(&v
, NULL
));
40 return array
->v
[array
->nr
- 1];
43 void strvec_pushl(struct strvec
*array
, ...)
49 while ((arg
= va_arg(ap
, const char *)))
50 strvec_push(array
, arg
);
54 void strvec_pushv(struct strvec
*array
, const char **items
)
56 for (; *items
; items
++)
57 strvec_push(array
, *items
);
60 void strvec_pop(struct strvec
*array
)
64 free((char *)array
->v
[array
->nr
- 1]);
65 array
->v
[array
->nr
- 1] = NULL
;
69 void strvec_split(struct strvec
*array
, const char *to_split
)
71 while (isspace(*to_split
))
74 const char *p
= to_split
;
79 while (*p
&& !isspace(*p
))
81 strvec_push_nodup(array
, xstrndup(to_split
, p
- to_split
));
89 void strvec_clear(struct strvec
*array
)
91 if (array
->v
!= empty_strvec
) {
93 for (i
= 0; i
< array
->nr
; i
++)
94 free((char *)array
->v
[i
]);
100 const char **strvec_detach(struct strvec
*array
)
102 if (array
->v
== empty_strvec
)
103 return xcalloc(1, sizeof(const char *));
105 const char **ret
= array
->v
;