diff options
author | William Wilgus <wilgus.william@gmail.com> | 2021-08-30 21:54:38 -0400 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2021-08-31 21:05:36 +0000 |
commit | c04a944c98c52b6364c8a50aee0ab4e6149e99de (patch) | |
tree | 3bc4dab9d891381015b9fa5120ad1f0ab299fe22 | |
parent | d929444a413007244155e88bf87e7c75f0d2da30 (diff) |
lib/argparse scale decimals to int for return to user
scales the fractional portion of the parsed number
by ARGPARSE_FRAC_DEC_MULTIPLIER
Example ARGPARSE_FRAC_DEC_MULTIPLIER = 10 000
meaning .0009 returns 9 , 9 / 10000 = .0009
.009 returns 90
.099 returns 990
.09 returns 900
.9 returns 9000
.9999 returns 9999
Change-Id: I38573dbc1877760b0d52df8686a6647894d76196
-rw-r--r-- | apps/plugins/lib/arg_helper.c | 39 | ||||
-rw-r--r-- | apps/plugins/lib/arg_helper.h | 6 |
2 files changed, 37 insertions, 8 deletions
diff --git a/apps/plugins/lib/arg_helper.c b/apps/plugins/lib/arg_helper.c index dcf3e31834..756549c2ad 100644 --- a/apps/plugins/lib/arg_helper.c +++ b/apps/plugins/lib/arg_helper.c @@ -88,7 +88,6 @@ int char_parse(const char **parameter, char* character) } - int bool_parse(const char **parameter, bool *choice) { /* determine true false using the first character the rest are skipped/ignored */ @@ -113,15 +112,25 @@ int bool_parse(const char **parameter, bool *choice) return found; } - -int longnum_parse(const char **parameter, long *number, long *decimal) +int longnum_parse(const char **parameter, long *number, unsigned long *decimal) { -/* passes number and or decimal portion of number base 10 only.. */ +/* passes number and or decimal portion of number base 10 only.. + fractional portion is scaled by ARGPARSE_FRAC_DEC_MULTIPLIER + Example (if ARGPARSE_FRAC_DEC_MULTIPLIER = 10 000) + meaning .0009 returns 9 , 9 / 10000 = .0009 + .009 returns 90 + .099 returns 990 + .09 returns 900 + .9 returns 9000 + .9999 returns 9999 +*/ + long num = 0; long dec = 0; int found = 0; int neg = 0; - logf ("n: %s\n", *parameter); + int digits = 0; + //logf ("n: %s\n", *parameter); const char *start = *parameter; if (*start == '-') @@ -139,11 +148,28 @@ int longnum_parse(const char **parameter, long *number, long *decimal) if (*start == DECSEPCHAR) { start++; + while(*start == '0') + { + digits++; + start++; + } while (isdigit(*start)) { dec = dec *10 + *start - '0'; + digits++; start++; } + if (decimal && digits <= AP_MAX_FRAC_DIGITS) + { + if(digits < AP_MAX_FRAC_DIGITS) + { + digits = AP_MAX_FRAC_DIGITS - digits; + while (digits--) + dec *= 10; + } + } + else + dec = -1; /* error */ } if (found > 0) @@ -165,13 +191,10 @@ int num_parse(const char **parameter, int *number, int *decimal) { long num, dec; int ret = longnum_parse(parameter, &num, &dec); - if(number) *number = num; - if (decimal) *decimal = dec; - return ret; } diff --git a/apps/plugins/lib/arg_helper.h b/apps/plugins/lib/arg_helper.h index 7c770e0162..248e8e4eaf 100644 --- a/apps/plugins/lib/arg_helper.h +++ b/apps/plugins/lib/arg_helper.h @@ -23,6 +23,12 @@ #include "plugin.h" +#define ARGP_MAX_FRAC_DIGITS 9 /* Uses 30 bits max (0.999999999) */ + +#define ARGP_EXP(a, b) (a ##E## b) +#define ARGP_FRAC_DEC_MULTIPLIER(n) AP_EXP(1,n) /*1x10^n*/ +#define ARGPARSE_FRAC_DEC_MULTIPLIER (long) ARGP_FRAC_DEC_MULTIPLIER(ARGP_MAX_FRAC_DIGITS) + /* fills buf with a string upto buf_sz, null terminates the buffer * strings break on WS by default but can be enclosed in single or double quotes * opening and closing quotes will not be included in the buffer but will be counted |