strvec.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #ifndef STRVEC_H
  2. #define STRVEC_H
  3. /**
  4. * The strvec API allows one to dynamically build and store
  5. * NULL-terminated arrays of strings. A strvec maintains the invariant that the
  6. * `items` member always points to a non-NULL array, and that the array is
  7. * always NULL-terminated at the element pointed to by `items[nr]`. This
  8. * makes the result suitable for passing to functions expecting to receive
  9. * argv from main().
  10. *
  11. * The string-list API (documented in string-list.h) is similar, but cannot be
  12. * used for these purposes; instead of storing a straight string pointer,
  13. * it contains an item structure with a `util` field that is not compatible
  14. * with the traditional argv interface.
  15. *
  16. * Each `strvec` manages its own memory. Any strings pushed into the
  17. * array are duplicated, and all memory is freed by strvec_clear().
  18. */
  19. extern const char *empty_strvec[];
  20. /**
  21. * A single array. This should be initialized by assignment from
  22. * `STRVEC_INIT`, or by calling `strvec_init`. The `items`
  23. * member contains the actual array; the `nr` member contains the
  24. * number of elements in the array, not including the terminating
  25. * NULL.
  26. */
  27. struct strvec {
  28. const char **v;
  29. int nr;
  30. int alloc;
  31. };
  32. #define STRVEC_INIT { empty_strvec, 0, 0 }
  33. /**
  34. * Initialize an array. This is no different than assigning from
  35. * `STRVEC_INIT`.
  36. */
  37. void strvec_init(struct strvec *);
  38. /* Push a copy of a string onto the end of the array. */
  39. const char *strvec_push(struct strvec *, const char *);
  40. /**
  41. * Format a string and push it onto the end of the array. This is a
  42. * convenience wrapper combining `strbuf_addf` and `strvec_push`.
  43. */
  44. __attribute__((format (printf,2,3)))
  45. const char *strvec_pushf(struct strvec *, const char *fmt, ...);
  46. /**
  47. * Push a list of strings onto the end of the array. The arguments
  48. * should be a list of `const char *` strings, terminated by a NULL
  49. * argument.
  50. */
  51. LAST_ARG_MUST_BE_NULL
  52. void strvec_pushl(struct strvec *, ...);
  53. /* Push a null-terminated array of strings onto the end of the array. */
  54. void strvec_pushv(struct strvec *, const char **);
  55. /**
  56. * Remove the final element from the array. If there are no
  57. * elements in the array, do nothing.
  58. */
  59. void strvec_pop(struct strvec *);
  60. /* Splits by whitespace; does not handle quoted arguments! */
  61. void strvec_split(struct strvec *, const char *);
  62. /**
  63. * Free all memory associated with the array and return it to the
  64. * initial, empty state.
  65. */
  66. void strvec_clear(struct strvec *);
  67. /**
  68. * Disconnect the `items` member from the `strvec` struct and
  69. * return it. The caller is responsible for freeing the memory used
  70. * by the array, and by the strings it references. After detaching,
  71. * the `strvec` is in a reinitialized state and can be pushed
  72. * into again.
  73. */
  74. const char **strvec_detach(struct strvec *);
  75. #endif /* STRVEC_H */