# HG changeset patch # User John Tsiombikas # Date 1448031939 -7200 # Node ID 9c73004c7af3eb1446b8bd4533dbdf02a8f2d870 # Parent 8fd2858c6a29d40db7b485ca936ab7c1b901315b works diff -r 8fd2858c6a29 -r 9c73004c7af3 .hgignore --- a/.hgignore Sat Nov 14 14:12:30 2015 +0200 +++ b/.hgignore Fri Nov 20 17:05:39 2015 +0200 @@ -4,3 +4,4 @@ \.a$ \.so\. \.dylib$ +^example/example$ diff -r 8fd2858c6a29 -r 9c73004c7af3 example/example.c --- a/example/example.c Sat Nov 14 14:12:30 2015 +0200 +++ b/example/example.c Fri Nov 20 17:05:39 2015 +0200 @@ -54,10 +54,10 @@ switch(opt) { case OPT_FOO: - foo = 1; + optcfg_enabled_value(oc, &foo); break; case OPT_BAR: - bar = 1; + optcfg_enabled_value(oc, &bar); break; case OPT_XYZZY: /* this option needs an integer value */ diff -r 8fd2858c6a29 -r 9c73004c7af3 src/optcfg.c --- a/src/optcfg.c Sat Nov 14 14:12:30 2015 +0200 +++ b/src/optcfg.c Fri Nov 20 17:05:39 2015 +0200 @@ -159,7 +159,7 @@ len = strlen(line); buf = alloca(len + 1); - memcpy(buf, line, len); + memcpy(buf, line, len + 1); start = skip_spaces(buf); strip_comments(start); @@ -188,6 +188,20 @@ return 0; } +int optcfg_enabled_value(struct optcfg *oc, int *enabledp) +{ + if(oc->argidx) { + *enabledp = 1; /* TODO take -no- prefix into account */ + } else { + char *val = optcfg_next_value(oc); + if(optcfg_bool_value(val, enabledp) == -1) { + return -1; + } + } + return 0; +} + + char *optcfg_next_value(struct optcfg *oc) { if(oc->argidx) { /* we're in the middle of parsing arguments, so get the next one */ @@ -314,6 +328,7 @@ *eq = 0; strip_trailing_spaces(line); val = skip_spaces(eq + 1); + strip_trailing_spaces(val); if(!*line || !*val) { return 0; diff -r 8fd2858c6a29 -r 9c73004c7af3 src/optcfg.h --- a/src/optcfg.h Sat Nov 14 14:12:30 2015 +0200 +++ b/src/optcfg.h Fri Nov 20 17:05:39 2015 +0200 @@ -50,6 +50,15 @@ int optcfg_parse_config_line(struct optcfg *oc, const char *line); /* TODO custom I/O callback version of config file parsing */ +/* special value function which returns if the option is enabled or disabled + * For config files it works similar to calling optcfg_next_value, and + * optcfg_bool_value in sequence. + * For argument parsing however, it doesn't consume further arguments. Merely + * the presence of the option makes it enabled, and its presence with a -no- + * prefix disables it. (TODO the second part of this) + */ +int optcfg_enabled_value(struct optcfg *oc, int *enabledp); + /* call optcfg_next_value in the option callback to retrieve the next value * of the current option. returns 0 if there is no next value. */