]> git.ipfire.org Git - thirdparty/git.git/blame - strvec.h
Start the 2.46 cycle
[thirdparty/git.git] / strvec.h
CommitLineData
dbbcd44f
JK
1#ifndef STRVEC_H
2#define STRVEC_H
c1189cae 3
971b1f24 4/**
d70a9eb6
JK
5 * The strvec API allows one to dynamically build and store
6 * NULL-terminated arrays of strings. A strvec maintains the invariant that the
f10b0989
LA
7 * `v` member always points to a non-NULL array, and that the array is
8 * always NULL-terminated at the element pointed to by `v[nr]`. This
971b1f24
HW
9 * makes the result suitable for passing to functions expecting to receive
10 * argv from main().
11 *
12 * The string-list API (documented in string-list.h) is similar, but cannot be
13 * used for these purposes; instead of storing a straight string pointer,
14 * it contains an item structure with a `util` field that is not compatible
15 * with the traditional argv interface.
16 *
873cd28a
JK
17 * Each `strvec` manages its own memory. Any strings pushed into the
18 * array are duplicated, and all memory is freed by strvec_clear().
971b1f24
HW
19 */
20
873cd28a 21extern const char *empty_strvec[];
c1189cae 22
971b1f24
HW
23/**
24 * A single array. This should be initialized by assignment from
f10b0989 25 * `STRVEC_INIT`, or by calling `strvec_init`. The `v`
d70a9eb6 26 * member contains the actual array; the `nr` member contains the
971b1f24
HW
27 * number of elements in the array, not including the terminating
28 * NULL.
29 */
873cd28a 30struct strvec {
d70a9eb6 31 const char **v;
8d133a46
JK
32 size_t nr;
33 size_t alloc;
c1189cae
JK
34};
35
f69a6e4f
ÆAB
36#define STRVEC_INIT { \
37 .v = empty_strvec, \
38}
c1189cae 39
971b1f24
HW
40/**
41 * Initialize an array. This is no different than assigning from
873cd28a 42 * `STRVEC_INIT`.
971b1f24 43 */
873cd28a 44void strvec_init(struct strvec *);
971b1f24
HW
45
46/* Push a copy of a string onto the end of the array. */
873cd28a 47const char *strvec_push(struct strvec *, const char *);
971b1f24
HW
48
49/**
50 * Format a string and push it onto the end of the array. This is a
873cd28a 51 * convenience wrapper combining `strbuf_addf` and `strvec_push`.
971b1f24 52 */
c1189cae 53__attribute__((format (printf,2,3)))
873cd28a 54const char *strvec_pushf(struct strvec *, const char *fmt, ...);
971b1f24
HW
55
56/**
57 * Push a list of strings onto the end of the array. The arguments
58 * should be a list of `const char *` strings, terminated by a NULL
59 * argument.
60 */
9fe3edc4 61LAST_ARG_MUST_BE_NULL
873cd28a 62void strvec_pushl(struct strvec *, ...);
971b1f24
HW
63
64/* Push a null-terminated array of strings onto the end of the array. */
873cd28a 65void strvec_pushv(struct strvec *, const char **);
971b1f24
HW
66
67/**
68 * Remove the final element from the array. If there are no
69 * elements in the array, do nothing.
70 */
873cd28a 71void strvec_pop(struct strvec *);
971b1f24 72
c5aa6db6 73/* Splits by whitespace; does not handle quoted arguments! */
873cd28a 74void strvec_split(struct strvec *, const char *);
971b1f24
HW
75
76/**
77 * Free all memory associated with the array and return it to the
78 * initial, empty state.
79 */
873cd28a 80void strvec_clear(struct strvec *);
971b1f24
HW
81
82/**
f10b0989 83 * Disconnect the `v` member from the `strvec` struct and
971b1f24
HW
84 * return it. The caller is responsible for freeing the memory used
85 * by the array, and by the strings it references. After detaching,
873cd28a 86 * the `strvec` is in a reinitialized state and can be pushed
971b1f24
HW
87 * into again.
88 */
873cd28a
JK
89const char **strvec_detach(struct strvec *);
90
dbbcd44f 91#endif /* STRVEC_H */