Skip to content

Commit

Permalink
argv-array: add a new "pushl" method
Browse files Browse the repository at this point in the history
It can be convenient to push many strings in a single line
(e.g., if you are initializing an array with defaults). This
patch provides a convenience wrapper to allow this.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
peff authored and gitster committed Apr 18, 2012
1 parent fd93d2e commit d15bbe1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Documentation/technical/api-argv-array.txt
Expand Up @@ -37,6 +37,11 @@ Functions
`argv_array_push`:: `argv_array_push`::
Push a copy of a string onto the end of the array. Push a copy of a string onto the end of the array.


`argv_array_pushl`::
Push a list of strings onto the end of the array. The arguments
should be a list of `const char *` strings, terminated by a NULL
argument.

`argv_array_pushf`:: `argv_array_pushf`::
Format a string and push it onto the end of the array. This is a Format a string and push it onto the end of the array. This is a
convenience wrapper combining `strbuf_addf` and `argv_array_push`. convenience wrapper combining `strbuf_addf` and `argv_array_push`.
Expand Down
11 changes: 11 additions & 0 deletions argv-array.c
Expand Up @@ -38,6 +38,17 @@ void argv_array_pushf(struct argv_array *array, const char *fmt, ...)
argv_array_push_nodup(array, strbuf_detach(&v, NULL)); argv_array_push_nodup(array, strbuf_detach(&v, NULL));
} }


void argv_array_pushl(struct argv_array *array, ...)
{
va_list ap;
const char *arg;

va_start(ap, array);
while((arg = va_arg(ap, const char *)))
argv_array_push(array, arg);
va_end(ap);
}

void argv_array_clear(struct argv_array *array) void argv_array_clear(struct argv_array *array)
{ {
if (array->argv != empty_argv) { if (array->argv != empty_argv) {
Expand Down
1 change: 1 addition & 0 deletions argv-array.h
Expand Up @@ -15,6 +15,7 @@ void argv_array_init(struct argv_array *);
void argv_array_push(struct argv_array *, const char *); void argv_array_push(struct argv_array *, const char *);
__attribute__((format (printf,2,3))) __attribute__((format (printf,2,3)))
void argv_array_pushf(struct argv_array *, const char *fmt, ...); void argv_array_pushf(struct argv_array *, const char *fmt, ...);
void argv_array_pushl(struct argv_array *, ...);
void argv_array_clear(struct argv_array *); void argv_array_clear(struct argv_array *);


#endif /* ARGV_ARRAY_H */ #endif /* ARGV_ARRAY_H */

0 comments on commit d15bbe1

Please sign in to comment.