summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Wilgus <wilgus.william@gmail.com>2021-08-30 21:54:38 -0400
committerWilliam Wilgus <me.theuser@yahoo.com>2021-08-31 21:05:36 +0000
commitc04a944c98c52b6364c8a50aee0ab4e6149e99de (patch)
tree3bc4dab9d891381015b9fa5120ad1f0ab299fe22
parentd929444a413007244155e88bf87e7c75f0d2da30 (diff)
downloadrockbox-c04a944c98c52b6364c8a50aee0ab4e6149e99de.tar.gz
rockbox-c04a944c98c52b6364c8a50aee0ab4e6149e99de.zip
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.c39
-rw-r--r--apps/plugins/lib/arg_helper.h6
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)
88 88
89} 89}
90 90
91
92int bool_parse(const char **parameter, bool *choice) 91int bool_parse(const char **parameter, bool *choice)
93{ 92{
94/* determine true false using the first character the rest are skipped/ignored */ 93/* determine true false using the first character the rest are skipped/ignored */
@@ -113,15 +112,25 @@ int bool_parse(const char **parameter, bool *choice)
113 return found; 112 return found;
114} 113}
115 114
116 115int longnum_parse(const char **parameter, long *number, unsigned long *decimal)
117int longnum_parse(const char **parameter, long *number, long *decimal)
118{ 116{
119/* passes number and or decimal portion of number base 10 only.. */ 117/* passes number and or decimal portion of number base 10 only..
118 fractional portion is scaled by ARGPARSE_FRAC_DEC_MULTIPLIER
119 Example (if ARGPARSE_FRAC_DEC_MULTIPLIER = 10 000)
120 meaning .0009 returns 9 , 9 / 10000 = .0009
121 .009 returns 90
122 .099 returns 990
123 .09 returns 900
124 .9 returns 9000
125 .9999 returns 9999
126*/
127
120 long num = 0; 128 long num = 0;
121 long dec = 0; 129 long dec = 0;
122 int found = 0; 130 int found = 0;
123 int neg = 0; 131 int neg = 0;
124 logf ("n: %s\n", *parameter); 132 int digits = 0;
133 //logf ("n: %s\n", *parameter);
125 const char *start = *parameter; 134 const char *start = *parameter;
126 135
127 if (*start == '-') 136 if (*start == '-')
@@ -139,11 +148,28 @@ int longnum_parse(const char **parameter, long *number, long *decimal)
139 if (*start == DECSEPCHAR) 148 if (*start == DECSEPCHAR)
140 { 149 {
141 start++; 150 start++;
151 while(*start == '0')
152 {
153 digits++;
154 start++;
155 }
142 while (isdigit(*start)) 156 while (isdigit(*start))
143 { 157 {
144 dec = dec *10 + *start - '0'; 158 dec = dec *10 + *start - '0';
159 digits++;
145 start++; 160 start++;
146 } 161 }
162 if (decimal && digits <= AP_MAX_FRAC_DIGITS)
163 {
164 if(digits < AP_MAX_FRAC_DIGITS)
165 {
166 digits = AP_MAX_FRAC_DIGITS - digits;
167 while (digits--)
168 dec *= 10;
169 }
170 }
171 else
172 dec = -1; /* error */
147 } 173 }
148 174
149 if (found > 0) 175 if (found > 0)
@@ -165,13 +191,10 @@ int num_parse(const char **parameter, int *number, int *decimal)
165{ 191{
166 long num, dec; 192 long num, dec;
167 int ret = longnum_parse(parameter, &num, &dec); 193 int ret = longnum_parse(parameter, &num, &dec);
168
169 if(number) 194 if(number)
170 *number = num; 195 *number = num;
171
172 if (decimal) 196 if (decimal)
173 *decimal = dec; 197 *decimal = dec;
174
175 return ret; 198 return ret;
176} 199}
177 200
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 @@
23 23
24#include "plugin.h" 24#include "plugin.h"
25 25
26#define ARGP_MAX_FRAC_DIGITS 9 /* Uses 30 bits max (0.999999999) */
27
28#define ARGP_EXP(a, b) (a ##E## b)
29#define ARGP_FRAC_DEC_MULTIPLIER(n) AP_EXP(1,n) /*1x10^n*/
30#define ARGPARSE_FRAC_DEC_MULTIPLIER (long) ARGP_FRAC_DEC_MULTIPLIER(ARGP_MAX_FRAC_DIGITS)
31
26/* fills buf with a string upto buf_sz, null terminates the buffer 32/* fills buf with a string upto buf_sz, null terminates the buffer
27 * strings break on WS by default but can be enclosed in single or double quotes 33 * strings break on WS by default but can be enclosed in single or double quotes
28 * opening and closing quotes will not be included in the buffer but will be counted 34 * opening and closing quotes will not be included in the buffer but will be counted