Skip to content

Commit

Permalink
options: parse floats expressed as fractions
Browse files Browse the repository at this point in the history
Allows float options to accept values as fractions
  • Loading branch information
llyyr committed Sep 3, 2023
1 parent 648efac commit d6f1db1
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,24 @@ static void print_float(opt_ctx p, pl_str *out, const void *ptr)
pl_str_append_asprintf_c(p->alloc, out, "%f", *val);
}

static bool parse_fraction(pl_str str, float *val)
{
pl_str denom, num = pl_str_split_char(str, '/', &denom);
float n, d;
bool ok = denom.buf && denom.len && pl_str_parse_float(num, &n) &&
pl_str_parse_float(denom, &d);
if (ok)
*val = n / d;
return ok;
}

static bool parse_float(opt_ctx p, pl_str str, void *out)
{
pl_opt opt = p->opt;
float val;
if (!pl_str_parse_float(str, &val)) {
PL_ERR(p, "Invalid value '%.*s' for option '%s', expected floating point",
PL_STR_FMT(str), opt->key);
if (!parse_fraction(str, &val) && !pl_str_parse_float(str, &val)) {
PL_ERR(p, "Invalid value '%.*s' for option '%s', expected floating point "
"or fraction", PL_STR_FMT(str), opt->key);
return false;
}

Expand Down

0 comments on commit d6f1db1

Please sign in to comment.