blob: fdcad75b45b33fdc6974fd35c2102ecbcaba31e4 [file] [log] [blame]
Jeff Kingdbbcd442020-07-28 16:23:39 -04001#ifndef STRVEC_H
2#define STRVEC_H
Jeff Kingc1189ca2011-09-13 17:57:57 -04003
Heba Waly971b1f22019-11-17 21:04:52 +00004/**
Jeff Kingd70a9eb2020-07-28 20:37:20 -04005 * 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
Heba Waly971b1f22019-11-17 21:04:52 +00009 * 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 *
Jeff King873cd282020-07-28 16:23:25 -040017 * Each `strvec` manages its own memory. Any strings pushed into the
18 * array are duplicated, and all memory is freed by strvec_clear().
Heba Waly971b1f22019-11-17 21:04:52 +000019 */
20
Jeff King873cd282020-07-28 16:23:25 -040021extern const char *empty_strvec[];
Jeff Kingc1189ca2011-09-13 17:57:57 -040022
Heba Waly971b1f22019-11-17 21:04:52 +000023/**
24 * A single array. This should be initialized by assignment from
Jeff Kingd70a9eb2020-07-28 20:37:20 -040025 * `STRVEC_INIT`, or by calling `strvec_init`. The `items`
26 * member contains the actual array; the `nr` member contains the
Heba Waly971b1f22019-11-17 21:04:52 +000027 * number of elements in the array, not including the terminating
28 * NULL.
29 */
Jeff King873cd282020-07-28 16:23:25 -040030struct strvec {
Jeff Kingd70a9eb2020-07-28 20:37:20 -040031 const char **v;
32 int nr;
33 int alloc;
Jeff Kingc1189ca2011-09-13 17:57:57 -040034};
35
Jeff King873cd282020-07-28 16:23:25 -040036#define STRVEC_INIT { empty_strvec, 0, 0 }
Jeff Kingc1189ca2011-09-13 17:57:57 -040037
Heba Waly971b1f22019-11-17 21:04:52 +000038/**
39 * Initialize an array. This is no different than assigning from
Jeff King873cd282020-07-28 16:23:25 -040040 * `STRVEC_INIT`.
Heba Waly971b1f22019-11-17 21:04:52 +000041 */
Jeff King873cd282020-07-28 16:23:25 -040042void strvec_init(struct strvec *);
Heba Waly971b1f22019-11-17 21:04:52 +000043
44/* Push a copy of a string onto the end of the array. */
Jeff King873cd282020-07-28 16:23:25 -040045const char *strvec_push(struct strvec *, const char *);
Heba Waly971b1f22019-11-17 21:04:52 +000046
47/**
48 * Format a string and push it onto the end of the array. This is a
Jeff King873cd282020-07-28 16:23:25 -040049 * convenience wrapper combining `strbuf_addf` and `strvec_push`.
Heba Waly971b1f22019-11-17 21:04:52 +000050 */
Jeff Kingc1189ca2011-09-13 17:57:57 -040051__attribute__((format (printf,2,3)))
Jeff King873cd282020-07-28 16:23:25 -040052const char *strvec_pushf(struct strvec *, const char *fmt, ...);
Heba Waly971b1f22019-11-17 21:04:52 +000053
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 */
Ramsay Jones9fe3edc2013-07-18 21:02:12 +010059LAST_ARG_MUST_BE_NULL
Jeff King873cd282020-07-28 16:23:25 -040060void strvec_pushl(struct strvec *, ...);
Heba Waly971b1f22019-11-17 21:04:52 +000061
62/* Push a null-terminated array of strings onto the end of the array. */
Jeff King873cd282020-07-28 16:23:25 -040063void strvec_pushv(struct strvec *, const char **);
Heba Waly971b1f22019-11-17 21:04:52 +000064
65/**
66 * Remove the final element from the array. If there are no
67 * elements in the array, do nothing.
68 */
Jeff King873cd282020-07-28 16:23:25 -040069void strvec_pop(struct strvec *);
Heba Waly971b1f22019-11-17 21:04:52 +000070
Johannes Schindelinc5aa6db2018-04-25 11:53:57 +020071/* Splits by whitespace; does not handle quoted arguments! */
Jeff King873cd282020-07-28 16:23:25 -040072void strvec_split(struct strvec *, const char *);
Heba Waly971b1f22019-11-17 21:04:52 +000073
74/**
75 * Free all memory associated with the array and return it to the
76 * initial, empty state.
77 */
Jeff King873cd282020-07-28 16:23:25 -040078void strvec_clear(struct strvec *);
Heba Waly971b1f22019-11-17 21:04:52 +000079
80/**
Jeff Kingd70a9eb2020-07-28 20:37:20 -040081 * Disconnect the `items` member from the `strvec` struct and
Heba Waly971b1f22019-11-17 21:04:52 +000082 * return it. The caller is responsible for freeing the memory used
83 * by the array, and by the strings it references. After detaching,
Jeff King873cd282020-07-28 16:23:25 -040084 * the `strvec` is in a reinitialized state and can be pushed
Heba Waly971b1f22019-11-17 21:04:52 +000085 * into again.
86 */
Jeff King873cd282020-07-28 16:23:25 -040087const char **strvec_detach(struct strvec *);
88
Jeff Kingdbbcd442020-07-28 16:23:39 -040089#endif /* STRVEC_H */