Add and use generic name->id mapping code for color slot parsing
[git/raj.git] / argv-array.c
blobcb5bcd2c064961919d965cb2cae79de653162fde
1 #include "cache.h"
2 #include "argv-array.h"
3 #include "strbuf.h"
5 const char *empty_argv[] = { NULL };
7 void argv_array_init(struct argv_array *array)
9 array->argv = empty_argv;
10 array->argc = 0;
11 array->alloc = 0;
14 static void argv_array_push_nodup(struct argv_array *array, const char *value)
16 if (array->argv == empty_argv)
17 array->argv = NULL;
19 ALLOC_GROW(array->argv, array->argc + 2, array->alloc);
20 array->argv[array->argc++] = value;
21 array->argv[array->argc] = NULL;
24 void argv_array_push(struct argv_array *array, const char *value)
26 argv_array_push_nodup(array, xstrdup(value));
29 void argv_array_pushf(struct argv_array *array, const char *fmt, ...)
31 va_list ap;
32 struct strbuf v = STRBUF_INIT;
34 va_start(ap, fmt);
35 strbuf_vaddf(&v, fmt, ap);
36 va_end(ap);
38 argv_array_push_nodup(array, strbuf_detach(&v, NULL));
41 void argv_array_pushl(struct argv_array *array, ...)
43 va_list ap;
44 const char *arg;
46 va_start(ap, array);
47 while((arg = va_arg(ap, const char *)))
48 argv_array_push(array, arg);
49 va_end(ap);
52 void argv_array_pushv(struct argv_array *array, const char **argv)
54 for (; *argv; argv++)
55 argv_array_push(array, *argv);
58 void argv_array_pop(struct argv_array *array)
60 if (!array->argc)
61 return;
62 free((char *)array->argv[array->argc - 1]);
63 array->argv[array->argc - 1] = NULL;
64 array->argc--;
67 void argv_array_split(struct argv_array *array, const char *to_split)
69 while (isspace(*to_split))
70 to_split++;
71 for (;;) {
72 const char *p = to_split;
74 if (!*p)
75 break;
77 while (*p && !isspace(*p))
78 p++;
79 argv_array_push_nodup(array, xstrndup(to_split, p - to_split));
81 while (isspace(*p))
82 p++;
83 to_split = p;
87 void argv_array_clear(struct argv_array *array)
89 if (array->argv != empty_argv) {
90 int i;
91 for (i = 0; i < array->argc; i++)
92 free((char *)array->argv[i]);
93 free(array->argv);
95 argv_array_init(array);
98 const char **argv_array_detach(struct argv_array *array)
100 if (array->argv == empty_argv)
101 return xcalloc(1, sizeof(const char *));
102 else {
103 const char **ret = array->argv;
104 argv_array_init(array);
105 return ret;