| 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 | * `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 |
| 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 | |
| 21 | extern 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 `v` |
| 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 | */ |
| 30 | struct strvec { |
| 31 | const char **v; |
| 32 | size_t nr; |
| 33 | size_t alloc; |
| 34 | }; |
| 35 | |
| 36 | #define STRVEC_INIT { \ |
| 37 | .v = empty_strvec, \ |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Initialize an array. This is no different than assigning from |
| 42 | * `STRVEC_INIT`. |
| 43 | */ |
| 44 | void strvec_init(struct strvec *); |
| 45 | |
| 46 | /* Push a copy of a string onto the end of the array. */ |
| 47 | const char *strvec_push(struct strvec *, const char *); |
| 48 | |
| 49 | /* Push an allocated string onto the end of the array, taking ownership. */ |
| 50 | void strvec_push_nodup(struct strvec *array, char *value); |
| 51 | |
| 52 | /** |
| 53 | * Format a string and push it onto the end of the array. This is a |
| 54 | * convenience wrapper combining `strbuf_addf` and `strvec_push`. |
| 55 | */ |
| 56 | __attribute__((format (printf,2,3))) |
| 57 | const char *strvec_pushf(struct strvec *, const char *fmt, ...); |
| 58 | |
| 59 | /** |
| 60 | * Push a list of strings onto the end of the array. The arguments |
| 61 | * should be a list of `const char *` strings, terminated by a NULL |
| 62 | * argument. |
| 63 | */ |
| 64 | LAST_ARG_MUST_BE_NULL |
| 65 | void strvec_pushl(struct strvec *, ...); |
| 66 | |
| 67 | /* Push a null-terminated array of strings onto the end of the array. */ |
| 68 | void strvec_pushv(struct strvec *, const char **); |
| 69 | |
| 70 | /* |
| 71 | * Replace `len` values starting at `idx` with the provided replacement |
| 72 | * strings. If `len` is zero this is effectively an insert at the given `idx`. |
| 73 | * If `replacement_len` is zero this is effectively a delete of `len` items |
| 74 | * starting at `idx`. |
| 75 | */ |
| 76 | void strvec_splice(struct strvec *array, size_t idx, size_t len, |
| 77 | const char **replacement, size_t replacement_len); |
| 78 | |
| 79 | /** |
| 80 | * Replace the value at the given index with a new value. The index must be |
| 81 | * valid. Returns a pointer to the inserted value. |
| 82 | */ |
| 83 | const char *strvec_replace(struct strvec *array, size_t idx, const char *replacement); |
| 84 | |
| 85 | /* |
| 86 | * Remove the value at the given index. The remainder of the array will be |
| 87 | * moved to fill the resulting gap. The provided index must point into the |
| 88 | * array. |
| 89 | */ |
| 90 | void strvec_remove(struct strvec *array, size_t idx); |
| 91 | |
| 92 | /** |
| 93 | * Remove the final element from the array. If there are no |
| 94 | * elements in the array, do nothing. |
| 95 | */ |
| 96 | void strvec_pop(struct strvec *); |
| 97 | |
| 98 | /* Splits by whitespace; does not handle quoted arguments! */ |
| 99 | void strvec_split(struct strvec *, const char *); |
| 100 | |
| 101 | /** |
| 102 | * Free all memory associated with the array and return it to the |
| 103 | * initial, empty state. |
| 104 | */ |
| 105 | void strvec_clear(struct strvec *); |
| 106 | |
| 107 | /** |
| 108 | * Disconnect the `v` member from the `strvec` struct and |
| 109 | * return it. The caller is responsible for freeing the memory used |
| 110 | * by the array, and by the strings it references. After detaching, |
| 111 | * the `strvec` is in a reinitialized state and can be pushed |
| 112 | * into again. |
| 113 | */ |
| 114 | const char **strvec_detach(struct strvec *); |
| 115 | |
| 116 | #endif /* STRVEC_H */ |