argv-array.con commit Merge branch 'pe/pull-rebase-v-q' into maint (bcd6608)
   1#include "cache.h"
   2#include "argv-array.h"
   3#include "strbuf.h"
   4
   5const char *empty_argv[] = { NULL };
   6
   7void argv_array_init(struct argv_array *array)
   8{
   9        array->argv = empty_argv;
  10        array->argc = 0;
  11        array->alloc = 0;
  12}
  13
  14static void argv_array_push_nodup(struct argv_array *array, const char *value)
  15{
  16        if (array->argv == empty_argv)
  17                array->argv = NULL;
  18
  19        ALLOC_GROW(array->argv, array->argc + 2, array->alloc);
  20        array->argv[array->argc++] = value;
  21        array->argv[array->argc] = NULL;
  22}
  23
  24void argv_array_push(struct argv_array *array, const char *value)
  25{
  26        argv_array_push_nodup(array, xstrdup(value));
  27}
  28
  29void argv_array_pushf(struct argv_array *array, const char *fmt, ...)
  30{
  31        va_list ap;
  32        struct strbuf v = STRBUF_INIT;
  33
  34        va_start(ap, fmt);
  35        strbuf_vaddf(&v, fmt, ap);
  36        va_end(ap);
  37
  38        argv_array_push_nodup(array, strbuf_detach(&v, NULL));
  39}
  40
  41void argv_array_pushl(struct argv_array *array, ...)
  42{
  43        va_list ap;
  44        const char *arg;
  45
  46        va_start(ap, array);
  47        while((arg = va_arg(ap, const char *)))
  48                argv_array_push(array, arg);
  49        va_end(ap);
  50}
  51
  52void argv_array_pop(struct argv_array *array)
  53{
  54        if (!array->argc)
  55                return;
  56        free((char *)array->argv[array->argc - 1]);
  57        array->argv[array->argc - 1] = NULL;
  58        array->argc--;
  59}
  60
  61void argv_array_clear(struct argv_array *array)
  62{
  63        if (array->argv != empty_argv) {
  64                int i;
  65                for (i = 0; i < array->argc; i++)
  66                        free((char *)array->argv[i]);
  67                free(array->argv);
  68        }
  69        argv_array_init(array);
  70}
  71
  72const char **argv_array_detach(struct argv_array *array, int *argc)
  73{
  74        const char **argv =
  75                array->argv == empty_argv || array->argc == 0 ? NULL : array->argv;
  76        if (argc)
  77                *argc = array->argc;
  78        argv_array_init(array);
  79        return argv;
  80}
  81
  82void argv_array_free_detached(const char **argv)
  83{
  84        if (argv) {
  85                int i;
  86                for (i = 0; argv[i]; i++)
  87                        free((char **)argv[i]);
  88                free(argv);
  89        }
  90}