liboptcfg

changeset 2:9c73004c7af3

works
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 20 Nov 2015 17:05:39 +0200
parents 8fd2858c6a29
children f971dfa66076
files .hgignore example/example.c src/optcfg.c src/optcfg.h
diffstat 4 files changed, 28 insertions(+), 3 deletions(-) [+]
line diff
     1.1 --- a/.hgignore	Sat Nov 14 14:12:30 2015 +0200
     1.2 +++ b/.hgignore	Fri Nov 20 17:05:39 2015 +0200
     1.3 @@ -4,3 +4,4 @@
     1.4  \.a$
     1.5  \.so\.
     1.6  \.dylib$
     1.7 +^example/example$
     2.1 --- a/example/example.c	Sat Nov 14 14:12:30 2015 +0200
     2.2 +++ b/example/example.c	Fri Nov 20 17:05:39 2015 +0200
     2.3 @@ -54,10 +54,10 @@
     2.4  
     2.5  	switch(opt) {
     2.6  	case OPT_FOO:
     2.7 -		foo = 1;
     2.8 +		optcfg_enabled_value(oc, &foo);
     2.9  		break;
    2.10  	case OPT_BAR:
    2.11 -		bar = 1;
    2.12 +		optcfg_enabled_value(oc, &bar);
    2.13  		break;
    2.14  	case OPT_XYZZY:
    2.15  		/* this option needs an integer value */
     3.1 --- a/src/optcfg.c	Sat Nov 14 14:12:30 2015 +0200
     3.2 +++ b/src/optcfg.c	Fri Nov 20 17:05:39 2015 +0200
     3.3 @@ -159,7 +159,7 @@
     3.4  
     3.5  	len = strlen(line);
     3.6  	buf = alloca(len + 1);
     3.7 -	memcpy(buf, line, len);
     3.8 +	memcpy(buf, line, len + 1);
     3.9  
    3.10  	start = skip_spaces(buf);
    3.11  	strip_comments(start);
    3.12 @@ -188,6 +188,20 @@
    3.13  	return 0;
    3.14  }
    3.15  
    3.16 +int optcfg_enabled_value(struct optcfg *oc, int *enabledp)
    3.17 +{
    3.18 +	if(oc->argidx) {
    3.19 +		*enabledp = 1; /* TODO take -no- prefix into account */
    3.20 +	} else {
    3.21 +		char *val = optcfg_next_value(oc);
    3.22 +		if(optcfg_bool_value(val, enabledp) == -1) {
    3.23 +			return -1;
    3.24 +		}
    3.25 +	}
    3.26 +	return 0;
    3.27 +}
    3.28 +
    3.29 +
    3.30  char *optcfg_next_value(struct optcfg *oc)
    3.31  {
    3.32  	if(oc->argidx) {	/* we're in the middle of parsing arguments, so get the next one */
    3.33 @@ -314,6 +328,7 @@
    3.34  	*eq = 0;
    3.35  	strip_trailing_spaces(line);
    3.36  	val = skip_spaces(eq + 1);
    3.37 +	strip_trailing_spaces(val);
    3.38  
    3.39  	if(!*line || !*val) {
    3.40  		return 0;
     4.1 --- a/src/optcfg.h	Sat Nov 14 14:12:30 2015 +0200
     4.2 +++ b/src/optcfg.h	Fri Nov 20 17:05:39 2015 +0200
     4.3 @@ -50,6 +50,15 @@
     4.4  int optcfg_parse_config_line(struct optcfg *oc, const char *line);
     4.5  /* TODO custom I/O callback version of config file parsing */
     4.6  
     4.7 +/* special value function which returns if the option is enabled or disabled
     4.8 + * For config files it works similar to calling optcfg_next_value, and
     4.9 + * optcfg_bool_value in sequence.
    4.10 + * For argument parsing however, it doesn't consume further arguments. Merely
    4.11 + * the presence of the option makes it enabled, and its presence with a -no-
    4.12 + * prefix disables it. (TODO the second part of this)
    4.13 + */
    4.14 +int optcfg_enabled_value(struct optcfg *oc, int *enabledp);
    4.15 +
    4.16  /* call optcfg_next_value in the option callback to retrieve the next value
    4.17   * of the current option. returns 0 if there is no next value.
    4.18   */