]> git.ipfire.org Git - thirdparty/git.git/blame_incremental - strvec.h
Documentation: typofix --column description
[thirdparty/git.git] / strvec.h
... / ...
CommitLineData
1#ifndef STRVEC_H
2#define STRVEC_H
3
4/**
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
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 *
17 * Each `strvec` manages its own memory. Any strings pushed into the
18 * array are duplicated, and all memory is freed by strvec_clear().
19 */
20
21extern const char *empty_strvec[];
22
23/**
24 * A single array. This should be initialized by assignment from
25 * `STRVEC_INIT`, or by calling `strvec_init`. The `items`
26 * member contains the actual array; the `nr` member contains the
27 * number of elements in the array, not including the terminating
28 * NULL.
29 */
30struct strvec {
31 const char **v;
32 int nr;
33 int alloc;
34};
35
36#define STRVEC_INIT { empty_strvec, 0, 0 }
37
38/**
39 * Initialize an array. This is no different than assigning from
40 * `STRVEC_INIT`.
41 */
42void strvec_init(struct strvec *);
43
44/* Push a copy of a string onto the end of the array. */
45const char *strvec_push(struct strvec *, const char *);
46
47/**
48 * Format a string and push it onto the end of the array. This is a
49 * convenience wrapper combining `strbuf_addf` and `strvec_push`.
50 */
51__attribute__((format (printf,2,3)))
52const char *strvec_pushf(struct strvec *, const char *fmt, ...);
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 */
59LAST_ARG_MUST_BE_NULL
60void strvec_pushl(struct strvec *, ...);
61
62/* Push a null-terminated array of strings onto the end of the array. */
63void strvec_pushv(struct strvec *, const char **);
64
65/**
66 * Remove the final element from the array. If there are no
67 * elements in the array, do nothing.
68 */
69void strvec_pop(struct strvec *);
70
71/* Splits by whitespace; does not handle quoted arguments! */
72void strvec_split(struct strvec *, const char *);
73
74/**
75 * Free all memory associated with the array and return it to the
76 * initial, empty state.
77 */
78void strvec_clear(struct strvec *);
79
80/**
81 * Disconnect the `items` member from the `strvec` struct and
82 * return it. The caller is responsible for freeing the memory used
83 * by the array, and by the strings it references. After detaching,
84 * the `strvec` is in a reinitialized state and can be pushed
85 * into again.
86 */
87const char **strvec_detach(struct strvec *);
88
89#endif /* STRVEC_H */