Changeset affb51b for libcfa/src
- Timestamp:
- Oct 10, 2022, 2:47:06 PM (2 years ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- 68456fa
- Parents:
- 95bda0a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/parseargs.cfa
r95bda0a raffb51b 1 1 #include "parseargs.hfa" 2 2 3 #include <ctype.h> 3 4 #include <stdint.h> 4 5 #include <string.h> 5 6 #include <errno.h> 6 7 #include <unistd.h> 7 #include <assert.h>8 8 9 9 extern "C" { … … 74 74 75 75 int maxv = 'h'; 76 assert( opt_count > 0 ); 77 char optstring[opt_count * 3] = { '\0' }; 76 char optstring[(opt_count * 3) + 2] = { '\0' }; 78 77 { 79 78 int idx = 0; … … 272 271 bool parse(const char * arg, int & value) { 273 272 char * end; 274 int r = strtoll(arg, &end, 10); 273 274 errno = 0; 275 long long int r = strtoll(arg, &end, 0); 276 if(errno) return false; 275 277 if(*end != '\0') return false; 276 277 value = r; 278 return true; 278 if(r > (int)MAX) return false; 279 if(r < (int)MIN) return false; 280 281 value = r; 282 return true; 283 } 284 285 static unsigned long long int strict_strtoull( const char * arg, int base) { 286 errno = 0; 287 { 288 const char * in = arg; 289 for() { 290 if('\0' == *in) { 291 errno = EINVAL; 292 return 0; 293 } 294 if(!isspace(*in)) break; 295 in++; 296 } 297 if(!isdigit(*in)) { 298 errno = EINVAL; 299 return 0; 300 } 301 } 302 303 *char end; 304 unsigned long long int r = strtoull(arg, &end, base); 305 if(*end != '\0') errno = EINVAL; 306 if(errno) return 0; 307 return r; 279 308 } 280 309 281 310 bool parse(const char * arg, unsigned & value) { 282 char * end; 283 unsigned long long int r = strtoull(arg, &end, 10); 284 if(*end != '\0') return false; 311 unsigned long long int r = strict_strtoull(arg, 0); 312 if(errno) return false; 285 313 if(r > (unsigned)MAX) return false; 286 314 … … 290 318 291 319 bool parse(const char * arg, unsigned long & value) { 292 char * end; 293 unsigned long long int r = strtoull(arg, &end, 10); 294 if(*end != '\0') return false; 320 unsigned long long int r = strict_strtoull(arg, 0); 321 if(errno) return false; 295 322 if(r > (unsigned long)MAX) return false; 296 323 … … 300 327 301 328 bool parse(const char * arg, unsigned long long & value) { 302 char * end; 303 unsigned long long int r = strtoull(arg, &end, 10); 304 if(*end != '\0') return false; 305 if(r > (unsigned long long)MAX) return false; 306 307 value = r; 308 return true; 329 unsigned long long int r = strict_strtoull(arg, 0); 330 if(errno) return false; 331 if(r > (unsigned long long)MAX) return false; 332 333 value = r; 334 return true; 309 335 } 310 336
Note: See TracChangeset
for help on using the changeset viewer.