Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 1 | #ifndef STRVEC_H |
| 2 | #define STRVEC_H |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 3 | |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 4 | /** |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 5 | * The strvec API allows one to dynamically build and store |
| 6 | * NULL-terminated arrays of strings. A strvec maintains the invariant that the |
Linus Arver | f10b098 | 2024-01-12 07:06:35 +0000 | [diff] [blame] | 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 |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 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 | * |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 17 | * Each `strvec` manages its own memory. Any strings pushed into the |
| 18 | * array are duplicated, and all memory is freed by strvec_clear(). |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 21 | extern const char *empty_strvec[]; |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 22 | |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 23 | /** |
| 24 | * A single array. This should be initialized by assignment from |
Linus Arver | f10b098 | 2024-01-12 07:06:35 +0000 | [diff] [blame] | 25 | * `STRVEC_INIT`, or by calling `strvec_init`. The `v` |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 26 | * member contains the actual array; the `nr` member contains the |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 27 | * number of elements in the array, not including the terminating |
| 28 | * NULL. |
| 29 | */ |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 30 | struct strvec { |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 31 | const char **v; |
Jeff King | 8d133a4 | 2021-09-11 11:01:16 -0400 | [diff] [blame] | 32 | size_t nr; |
| 33 | size_t alloc; |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 34 | }; |
| 35 | |
Ævar Arnfjörð Bjarmason | f69a6e4 | 2021-09-27 14:54:27 +0200 | [diff] [blame] | 36 | #define STRVEC_INIT { \ |
| 37 | .v = empty_strvec, \ |
| 38 | } |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 39 | |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Initialize an array. This is no different than assigning from |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 42 | * `STRVEC_INIT`. |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 43 | */ |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 44 | void strvec_init(struct strvec *); |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 45 | |
| 46 | /* Push a copy of a string onto the end of the array. */ |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 47 | const char *strvec_push(struct strvec *, const char *); |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Format a string and push it onto the end of the array. This is a |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 51 | * convenience wrapper combining `strbuf_addf` and `strvec_push`. |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 52 | */ |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 53 | __attribute__((format (printf,2,3))) |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 54 | const char *strvec_pushf(struct strvec *, const char *fmt, ...); |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 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 | */ |
Ramsay Jones | 9fe3edc | 2013-07-18 21:02:12 +0100 | [diff] [blame] | 61 | LAST_ARG_MUST_BE_NULL |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 62 | void strvec_pushl(struct strvec *, ...); |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 63 | |
| 64 | /* Push a null-terminated array of strings onto the end of the array. */ |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 65 | void strvec_pushv(struct strvec *, const char **); |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Remove the final element from the array. If there are no |
| 69 | * elements in the array, do nothing. |
| 70 | */ |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 71 | void strvec_pop(struct strvec *); |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 72 | |
Johannes Schindelin | c5aa6db | 2018-04-25 11:53:57 +0200 | [diff] [blame] | 73 | /* Splits by whitespace; does not handle quoted arguments! */ |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 74 | void strvec_split(struct strvec *, const char *); |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Free all memory associated with the array and return it to the |
| 78 | * initial, empty state. |
| 79 | */ |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 80 | void strvec_clear(struct strvec *); |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 81 | |
| 82 | /** |
Linus Arver | f10b098 | 2024-01-12 07:06:35 +0000 | [diff] [blame] | 83 | * Disconnect the `v` member from the `strvec` struct and |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 84 | * return it. The caller is responsible for freeing the memory used |
| 85 | * by the array, and by the strings it references. After detaching, |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 86 | * the `strvec` is in a reinitialized state and can be pushed |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 87 | * into again. |
| 88 | */ |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 89 | const char **strvec_detach(struct strvec *); |
| 90 | |
Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 91 | #endif /* STRVEC_H */ |