gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
Data Structures | Functions
carg.c File Reference

simple argument utilities More...

#include "gwion_util.h"

Go to the source code of this file.

Data Structures

struct  ArgSplitter
 used internally to split arguments More...
 

Functions

ANN m_str option_argument (struct CArg *ca)
 returns the option to the argument, errors and returns if none
 
static ANN void _split_args (MemPool mp, struct ArgSplitter *as)
 
ANN Vector split_args (MemPool p, const m_str str)
 returns separated arguments from a string in a vector.
 

Detailed Description

simple argument utilities

Definition in file carg.c.

Function Documentation

◆ _split_args()

static ANN void _split_args ( MemPool mp,
struct ArgSplitter * as )
static

Definition at line 21 of file carg.c.

21 {
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}
static ANN void _split_args(MemPool mp, struct ArgSplitter *as)
Definition carg.c:21
uintptr_t vtype
Definition container.h:14
uintptr_t m_uint
Definition gwcommon.h:11
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
m_str str
Definition carg.c:17
Vector v
Definition carg.c:18
ANN void vector_add(const Vector v, const vtype data)
Definition vector.c:21

◆ option_argument()

ANN m_str option_argument ( struct CArg * ca)

returns the option to the argument, errors and returns if none

Definition at line 7 of file carg.c.

7 {
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}
#define _(String)
Definition defs.h:13
#define gw_err(...)
similar to fprintf(stderr, fmt, ...)
Definition err_msg.h:11
char * m_str
Definition gwcommon.h:14
char ** str
Definition gwion_print.h:2
char ** argv
array of char* arguments
Definition carg.h:9
int idx
curr index [internal]
Definition carg.h:11

◆ split_args()

ANN Vector split_args ( MemPool p,
const m_str str )

returns separated arguments from a string in a vector.

each value is a mp_allocated string. freeing it is caller responsability.

Definition at line 45 of file carg.c.

45 {
46 struct ArgSplitter as = {.str = str, .v = new_vector(p) };
47 _split_args(p, &as);
48 return as.v;
49}
used internally to split arguments
Definition carg.c:16
Vector new_vector(MemPool p)
Definition vector.c:8