]> git.ipfire.org Git - thirdparty/git.git/blame - strvec.h
config: improve error message for boolean config
[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
7 * `items` member always points to a non-NULL array, and that the array is
8 * always NULL-terminated at the element pointed to by `items[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
d70a9eb6
JK
25 * `STRVEC_INIT`, or by calling `strvec_init`. The `items`
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
JK
31 const char **v;
32 int nr;
33 int alloc;
c1189cae
JK
34};
35
873cd28a 36#define STRVEC_INIT { empty_strvec, 0, 0 }
c1189cae 37
971b1f24
HW
38/**
39 * Initialize an array. This is no different than assigning from
873cd28a 40 * `STRVEC_INIT`.
971b1f24 41 */
873cd28a 42void strvec_init(struct strvec *);
971b1f24
HW
43
44/* Push a copy of a string onto the end of the array. */
873cd28a 45const char *strvec_push(struct strvec *, const char *);
971b1f24
HW
46
47/**
48 * Format a string and push it onto the end of the array. This is a
873cd28a 49 * convenience wrapper combining `strbuf_addf` and `strvec_push`.
971b1f24 50 */
c1189cae 51__attribute__((format (printf,2,3)))
873cd28a 52const char *strvec_pushf(struct strvec *, const char *fmt, ...);
971b1f24
HW
53
54/**
55 * Push a list of strings onto the end of the array. The arguments
56 * should be a list of `const char *` strings, terminated by a NULL
57 * argument.
58 */
9fe3edc4 59LAST_ARG_MUST_BE_NULL
873cd28a 60void strvec_pushl(struct strvec *, ...);
971b1f24
HW
61
62/* Push a null-terminated array of strings onto the end of the array. */
873cd28a 63void strvec_pushv(struct strvec *, const char **);
971b1f24
HW
64
65/**
66 * Remove the final element from the array. If there are no
67 * elements in the array, do nothing.
68 */
873cd28a 69void strvec_pop(struct strvec *);
971b1f24 70
c5aa6db6 71/* Splits by whitespace; does not handle quoted arguments! */
873cd28a 72void strvec_split(struct strvec *, const char *);
971b1f24
HW
73
74/**
75 * Free all memory associated with the array and return it to the
76 * initial, empty state.
77 */
873cd28a 78void strvec_clear(struct strvec *);
971b1f24
HW
79
80/**
d70a9eb6 81 * Disconnect the `items` member from the `strvec` struct and
971b1f24
HW
82 * return it. The caller is responsible for freeing the memory used
83 * by the array, and by the strings it references. After detaching,
873cd28a 84 * the `strvec` is in a reinitialized state and can be pushed
971b1f24
HW
85 * into again.
86 */
873cd28a
JK
87const char **strvec_detach(struct strvec *);
88
dbbcd44f 89#endif /* STRVEC_H */