strvec.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "cache.h"
  2. #include "strvec.h"
  3. #include "strbuf.h"
  4. const char *empty_strvec[] = { NULL };
  5. void strvec_init(struct strvec *array)
  6. {
  7. array->v = empty_strvec;
  8. array->nr = 0;
  9. array->alloc = 0;
  10. }
  11. static void strvec_push_nodup(struct strvec *array, const char *value)
  12. {
  13. if (array->v == empty_strvec)
  14. array->v = NULL;
  15. ALLOC_GROW(array->v, array->nr + 2, array->alloc);
  16. array->v[array->nr++] = value;
  17. array->v[array->nr] = NULL;
  18. }
  19. const char *strvec_push(struct strvec *array, const char *value)
  20. {
  21. strvec_push_nodup(array, xstrdup(value));
  22. return array->v[array->nr - 1];
  23. }
  24. const char *strvec_pushf(struct strvec *array, const char *fmt, ...)
  25. {
  26. va_list ap;
  27. struct strbuf v = STRBUF_INIT;
  28. va_start(ap, fmt);
  29. strbuf_vaddf(&v, fmt, ap);
  30. va_end(ap);
  31. strvec_push_nodup(array, strbuf_detach(&v, NULL));
  32. return array->v[array->nr - 1];
  33. }
  34. void strvec_pushl(struct strvec *array, ...)
  35. {
  36. va_list ap;
  37. const char *arg;
  38. va_start(ap, array);
  39. while ((arg = va_arg(ap, const char *)))
  40. strvec_push(array, arg);
  41. va_end(ap);
  42. }
  43. void strvec_pushv(struct strvec *array, const char **items)
  44. {
  45. for (; *items; items++)
  46. strvec_push(array, *items);
  47. }
  48. void strvec_pop(struct strvec *array)
  49. {
  50. if (!array->nr)
  51. return;
  52. free((char *)array->v[array->nr - 1]);
  53. array->v[array->nr - 1] = NULL;
  54. array->nr--;
  55. }
  56. void strvec_split(struct strvec *array, const char *to_split)
  57. {
  58. while (isspace(*to_split))
  59. to_split++;
  60. for (;;) {
  61. const char *p = to_split;
  62. if (!*p)
  63. break;
  64. while (*p && !isspace(*p))
  65. p++;
  66. strvec_push_nodup(array, xstrndup(to_split, p - to_split));
  67. while (isspace(*p))
  68. p++;
  69. to_split = p;
  70. }
  71. }
  72. void strvec_clear(struct strvec *array)
  73. {
  74. if (array->v != empty_strvec) {
  75. int i;
  76. for (i = 0; i < array->nr; i++)
  77. free((char *)array->v[i]);
  78. free(array->v);
  79. }
  80. strvec_init(array);
  81. }
  82. const char **strvec_detach(struct strvec *array)
  83. {
  84. if (array->v == empty_strvec)
  85. return xcalloc(1, sizeof(const char *));
  86. else {
  87. const char **ret = array->v;
  88. strvec_init(array);
  89. return ret;
  90. }
  91. }