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 |
| 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 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 |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 25 | * `STRVEC_INIT`, or by calling `strvec_init`. The `items` |
| 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; |
| 32 | int nr; |
| 33 | int alloc; |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 34 | }; |
| 35 | |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 36 | #define STRVEC_INIT { empty_strvec, 0, 0 } |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 37 | |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 38 | /** |
| 39 | * Initialize an array. This is no different than assigning from |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 40 | * `STRVEC_INIT`. |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 41 | */ |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 42 | void strvec_init(struct strvec *); |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 43 | |
| 44 | /* Push a copy of a string onto the end of the array. */ |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 45 | const char *strvec_push(struct strvec *, const char *); |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * 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] | 49 | * convenience wrapper combining `strbuf_addf` and `strvec_push`. |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 50 | */ |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 51 | __attribute__((format (printf,2,3))) |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 52 | const char *strvec_pushf(struct strvec *, const char *fmt, ...); |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 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 | */ |
Ramsay Jones | 9fe3edc | 2013-07-18 21:02:12 +0100 | [diff] [blame] | 59 | LAST_ARG_MUST_BE_NULL |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 60 | void strvec_pushl(struct strvec *, ...); |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 61 | |
| 62 | /* 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] | 63 | void strvec_pushv(struct strvec *, const char **); |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Remove the final element from the array. If there are no |
| 67 | * elements in the array, do nothing. |
| 68 | */ |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 69 | void strvec_pop(struct strvec *); |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 70 | |
Johannes Schindelin | c5aa6db | 2018-04-25 11:53:57 +0200 | [diff] [blame] | 71 | /* Splits by whitespace; does not handle quoted arguments! */ |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 72 | void strvec_split(struct strvec *, const char *); |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Free all memory associated with the array and return it to the |
| 76 | * initial, empty state. |
| 77 | */ |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 78 | void strvec_clear(struct strvec *); |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 79 | |
| 80 | /** |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 81 | * Disconnect the `items` member from the `strvec` struct and |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 82 | * return it. The caller is responsible for freeing the memory used |
| 83 | * by the array, and by the strings it references. After detaching, |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 84 | * the `strvec` is in a reinitialized state and can be pushed |
Heba Waly | 971b1f2 | 2019-11-17 21:04:52 +0000 | [diff] [blame] | 85 | * into again. |
| 86 | */ |
Jeff King | 873cd28 | 2020-07-28 16:23:25 -0400 | [diff] [blame] | 87 | const char **strvec_detach(struct strvec *); |
| 88 | |
Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 89 | #endif /* STRVEC_H */ |