gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
carg.c
Go to the documentation of this file.
1
5#include "gwion_util.h"
6
8 const char key = ca->argv[ca->idx][1];
9 const m_str str = (ca->argv[ca->idx][2] == '\0' ? ca->argv[++ca->idx]
10 : ca->argv[ca->idx] + 2);
11 if (!str) gw_err(_("option '-%c' needs arguments\n"), key);
12 return str;
13}
14
20
21ANN static void _split_args(MemPool mp, struct ArgSplitter *as) {
22 const size_t sz = strlen(as->str);
23 char *buf = mp_malloc2(mp, sz + 1);
24 char prev = '\0';
25 m_uint i = 0, j = 0;
26 char c;
27 while ((c = as->str[i]) != '\0') {
28 const bool skip = prev == '\\';
29 const bool comma = c == ',';
30 if (comma) {
31 if (!skip) break;
32 --j;
33 }
34 buf[j++] = (prev = c);
35 ++i;
36 }
37 buf[i] = '\0';
38 vector_add(as->v, (vtype)mstrdup(mp, buf));
39 mp_free2(mp, sz + 1, buf);
40 if (i == sz) return;
41 as->str += i + 1;
42 _split_args(mp, as);
43}
44
46 struct ArgSplitter as = {.str = str, .v = new_vector(p) };
47 _split_args(p, &as);
48 return as.v;
49}
ANN Vector split_args(MemPool p, const m_str str)
returns separated arguments from a string in a vector.
Definition carg.c:45
ANN m_str option_argument(struct CArg *ca)
returns the option to the argument, errors and returns if none
Definition carg.c:7
static ANN void _split_args(MemPool mp, struct ArgSplitter *as)
Definition carg.c:21
uintptr_t vtype
Definition container.h:14
#define _(String)
Definition defs.h:13
#define ANN
Definition defs.h:19
#define gw_err(...)
similar to fprintf(stderr, fmt, ...)
Definition err_msg.h:11
uintptr_t m_uint
Definition gwcommon.h:11
char * m_str
Definition gwcommon.h:14
char ** str
Definition gwion_print.h:2
meta header (use this to include the whole library)
static ANN m_str mstrdup(MemPool mp, const char *name)
mp_alloc version of strdup
Definition mp_string.h:13
#define mp_free2(p, sz, a)
Definition mpool.h:28
#define mp_malloc2(p, sz)
Definition mpool.h:30
used internally to split arguments
Definition carg.c:16
m_str str
Definition carg.c:17
Vector v
Definition carg.c:18
structure used to parse arguments
Definition carg.h:8
char ** argv
array of char* arguments
Definition carg.h:9
int idx
curr index [internal]
Definition carg.h:11
ANN void vector_add(const Vector v, const vtype data)
Definition vector.c:21
Vector new_vector(MemPool p)
Definition vector.c:8