1 #include "git-compat-util.h"
7 const char *empty_strvec
[] = { NULL
};
9 void strvec_init(struct strvec
*array
)
11 struct strvec blank
= STRVEC_INIT
;
12 memcpy(array
, &blank
, sizeof(*array
));
15 static void strvec_push_nodup(struct strvec
*array
, const char *value
)
17 if (array
->v
== empty_strvec
)
20 ALLOC_GROW(array
->v
, array
->nr
+ 2, array
->alloc
);
21 array
->v
[array
->nr
++] = value
;
22 array
->v
[array
->nr
] = NULL
;
25 const char *strvec_push(struct strvec
*array
, const char *value
)
27 strvec_push_nodup(array
, xstrdup(value
));
28 return array
->v
[array
->nr
- 1];
31 const char *strvec_pushf(struct strvec
*array
, const char *fmt
, ...)
34 struct strbuf v
= STRBUF_INIT
;
37 strbuf_vaddf(&v
, fmt
, ap
);
40 strvec_push_nodup(array
, strbuf_detach(&v
, NULL
));
41 return array
->v
[array
->nr
- 1];
44 void strvec_pushl(struct strvec
*array
, ...)
50 while ((arg
= va_arg(ap
, const char *)))
51 strvec_push(array
, arg
);
55 void strvec_pushv(struct strvec
*array
, const char **items
)
57 for (; *items
; items
++)
58 strvec_push(array
, *items
);
61 void strvec_pop(struct strvec
*array
)
65 free((char *)array
->v
[array
->nr
- 1]);
66 array
->v
[array
->nr
- 1] = NULL
;
70 void strvec_split(struct strvec
*array
, const char *to_split
)
72 while (isspace(*to_split
))
75 const char *p
= to_split
;
80 while (*p
&& !isspace(*p
))
82 strvec_push_nodup(array
, xstrndup(to_split
, p
- to_split
));
90 void strvec_clear(struct strvec
*array
)
92 if (array
->v
!= empty_strvec
) {
94 for (i
= 0; i
< array
->nr
; i
++)
95 free((char *)array
->v
[i
]);
101 const char **strvec_detach(struct strvec
*array
)
103 if (array
->v
== empty_strvec
)
104 return xcalloc(1, sizeof(const char *));
106 const char **ret
= array
->v
;