The MinGW.OSDN Installation Manager Tool
Revisión | 5107971395660cb226623e42fb2ecf1bf21c3c8b (tree) |
---|---|
Tiempo | 2020-06-21 04:44:58 |
Autor | Keith Marshall <keith@user...> |
Commiter | Keith Marshall |
Simplify numeric argument interpreter function.
@@ -1,3 +1,11 @@ | ||
1 | +2020-06-20 Keith Marshall <keith@users.osdn.me> | |
2 | + | |
3 | + Simplify numeric argument interpreter function. | |
4 | + | |
5 | + * src/clistub.c (xatoi) [input != NULL]: Delegate to... | |
6 | + (strtol): ...this standard library function; hence delete redundant | |
7 | + local implementation, within the NULL pointer rejection guard. | |
8 | + | |
1 | 9 | 2020-06-19 Keith Marshall <keith@users.osdn.me> |
2 | 10 | |
3 | 11 | Accommodate C++11 string constant conflation limitations. |
@@ -185,109 +185,15 @@ char **cli_setargv( HMODULE my_dll, struct pkgopts *opts, char **argv ) | ||
185 | 185 | return argv; |
186 | 186 | } |
187 | 187 | |
188 | -/* FIXME: We may ultimately choose to factor the following atoi() replacement | |
189 | - * into a separate, free-standing module. For now we keep it here, as a static | |
190 | - * function, but we keep the required #include adjacent. | |
191 | - */ | |
192 | -#include <ctype.h> | |
193 | - | |
194 | 188 | static int xatoi( const char *input ) |
195 | 189 | { |
196 | 190 | /* A replacement for the standard atoi() function; this implementation |
197 | - * supports conversion of octal or hexadecimal representations, in addition | |
198 | - * to the decimal representation required by standard atoi(). | |
199 | - * | |
200 | - * We begin by initialising the result accumulator to zero... | |
201 | - */ | |
202 | - int result = 0; | |
203 | - | |
204 | - /* ...then, provided we have an input string to interpret... | |
205 | - */ | |
206 | - if( input != NULL ) | |
207 | - { | |
208 | - /* ...we proceed with interpretation and accumulation of the result, | |
209 | - * noting that we may have to handle an initial minus sign, (but we | |
210 | - * don't know yet, so assume that not for now). | |
211 | - */ | |
212 | - int negate = 0; | |
213 | - while( isspace( *input ) ) | |
214 | - /* | |
215 | - * Ignore leading white space. | |
216 | - */ | |
217 | - ++input; | |
218 | - | |
219 | - if( *input == '-' ) | |
220 | - /* | |
221 | - * An initial minus sign requires negation | |
222 | - * of the accumulated result... | |
223 | - */ | |
224 | - negate = *input++; | |
225 | - | |
226 | - else if( *input == '+' ) | |
227 | - /* | |
228 | - * ...while an initial plus sign is redundant, | |
229 | - * and may simply be ignored. | |
230 | - */ | |
231 | - ++input; | |
232 | - | |
233 | - if( *input == '0' ) | |
234 | - { | |
235 | - /* An initial zero signifies either hexadecimal | |
236 | - * or octal representation... | |
237 | - */ | |
238 | - if( (*++input | '\x20') == 'x' ) | |
239 | - /* | |
240 | - * ...with following 'x' or 'X' indicating | |
241 | - * the hexadecimal case. | |
242 | - */ | |
243 | - while( isxdigit( *++input ) ) | |
244 | - { | |
245 | - /* Interpret sequence of hexadecimal digits... | |
246 | - */ | |
247 | - result = (result << 4) + *input; | |
248 | - if( *input > 'F' ) | |
249 | - /* | |
250 | - * ...with ASCII to binary conversion for | |
251 | - * lower case digits 'a'..'f',... | |
252 | - */ | |
253 | - result -= 'a' - 10; | |
254 | - | |
255 | - else if( *input > '9' ) | |
256 | - /* | |
257 | - * ...ASCII to binary conversion for | |
258 | - * upper case digits 'A'..'F',... | |
259 | - */ | |
260 | - result -= 'A' - 10; | |
261 | - | |
262 | - else | |
263 | - /* ...or ASCII to decimal conversion, | |
264 | - * as appropriate. | |
265 | - */ | |
266 | - result -= '0'; | |
267 | - } | |
268 | - else while( (*input >= '0') && (*input < '8') ) | |
269 | - /* | |
270 | - * Interpret sequence of octal digits... | |
271 | - */ | |
272 | - result = (result << 3) + *input++ - '0'; | |
273 | - } | |
274 | - | |
275 | - else while( isdigit( *input ) ) | |
276 | - /* | |
277 | - * Interpret sequence of decimal digits... | |
278 | - */ | |
279 | - result = (result << 1) + (result << 3) + *input++ - '0'; | |
280 | - | |
281 | - if( negate ) | |
282 | - /* | |
283 | - * We had an initial minus sign, so we must | |
284 | - * return the negated result... | |
285 | - */ | |
286 | - return (0 - result); | |
287 | - } | |
288 | - /* ...otherwise, we simply return the accumulated result | |
191 | + * directs the call to strtol(), and so supports conversion of octal or | |
192 | + * hexadecimal representations, in addition to the decimal representation | |
193 | + * required by standard atoi(), while also protecting against the adverse | |
194 | + * effect of passing a NULL input pointer. | |
289 | 195 | */ |
290 | - return result; | |
196 | + return (input != NULL) ? strtol( input, NULL, 0 ) : 0; | |
291 | 197 | } |
292 | 198 | |
293 | 199 | #define atmost( lim, val ) ((lim) < (val)) ? (lim) : (val) |