Commit 342c513a authored by Junio C Hamano's avatar Junio C Hamano

argv-array: return the pushed string from argv_push*()

Such an API change allows us to use an argv_array this way:

    struct argv_array to_free = ARGV_ARRAY_INIT;
    const char *msg;

    if (some condition) {
            msg = "constant string message";
            ... other logic ...
    } else {
            msg = argv_array_pushf(&to_free, "format %s", var);
    }
    ... use "msg" ...
    ... do other things ...
    argv_array_clear(&to_free);

Note that argv_array_pushl() and argv_array_pushv() are used to push
one or more strings with a single call, so we do not return any one
of these strings from these two functions in order to reduce the
chance to misuse the API.
Signed-off-by: 's avatarJunio C Hamano <gitster@pobox.com>
Signed-off-by: 's avatarMartin Ågren <martin.agren@gmail.com>
Signed-off-by: 's avatarJunio C Hamano <gitster@pobox.com>
parent 3f1c1c36
......@@ -21,12 +21,13 @@ static void argv_array_push_nodup(struct argv_array *array, const char *value)
array->argv[array->argc] = NULL;
}
void argv_array_push(struct argv_array *array, const char *value)
const char *argv_array_push(struct argv_array *array, const char *value)
{
argv_array_push_nodup(array, xstrdup(value));
return array->argv[array->argc - 1];
}
void argv_array_pushf(struct argv_array *array, const char *fmt, ...)
const char *argv_array_pushf(struct argv_array *array, const char *fmt, ...)
{
va_list ap;
struct strbuf v = STRBUF_INIT;
......@@ -36,6 +37,7 @@ void argv_array_pushf(struct argv_array *array, const char *fmt, ...)
va_end(ap);
argv_array_push_nodup(array, strbuf_detach(&v, NULL));
return array->argv[array->argc - 1];
}
void argv_array_pushl(struct argv_array *array, ...)
......
......@@ -12,9 +12,9 @@ struct argv_array {
#define ARGV_ARRAY_INIT { empty_argv, 0, 0 }
void argv_array_init(struct argv_array *);
void argv_array_push(struct argv_array *, const char *);
const char *argv_array_push(struct argv_array *, const char *);
__attribute__((format (printf,2,3)))
void argv_array_pushf(struct argv_array *, const char *fmt, ...);
const char *argv_array_pushf(struct argv_array *, const char *fmt, ...);
LAST_ARG_MUST_BE_NULL
void argv_array_pushl(struct argv_array *, ...);
void argv_array_pushv(struct argv_array *, const char **);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment