Chameleon

Chameleon Commit Details

Date:2018-01-14 16:05:43 (6 years 3 months ago)
Author:ErmaC
Commit:2907
Parents: 2906
Message:Updated 'getIntForKey()' to support different bases (hex and dec.), and not only decimal. Credits to MinusZwei
Changes:
M/branches/ErmaC/Enoch/i386/libsaio/stringTable.c
M/trunk/i386/libsaio/stringTable.c

File differences

trunk/i386/libsaio/stringTable.c
364364
365365
366366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
367387
368388
369389
......
381401
382402
383403
384
385
386
404
405
387406
388
407
389408
390409
391410
392
393
394
395
396
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
397431
398
399
400
401
402
403
404
405
406
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
407449
408
409
410
411
450
451
452
412453
413
454
414455
415456
416457
458
417459
418460
419461
const char *key_val;
int size;
// looking for real boolean (<true/> or <false/>)
// if is a boolean tag, return immediately
TagPtr entry = XMLGetProperty(config->dictionary,key);
if(XMLIsBoolean(entry))
{
int value = XMLCastBoolean(entry);
if (value) {
*result_val = true;
return true;
}
else
{
*result_val = false;
return false;
}
}
// check if is a boolean as "string" (Yes/No)
// (IMHO this should be deprecated soon)
if (getValueForKey(key, &key_val, &size, config))
{
if ((size >= 1) && (key_val[0] == 'Y' || key_val[0] == 'y'))
bool getIntForKey(const char *key, int *value, config_file_t *config)
{
const char *val;
int size, sum;
bool negative = false;
const char *string = NULL;
int size = 0;
if (getValueForKey(key, &val, &size, config))
if (getValueForKey(key, &string, &size, config))
{
if (size)
{
if (*val == '-')
{
negative = true;
val++;
size--;
char *end = NULL;
unsigned long long nres = 0;
long long sres = 0;
/*
* strtouq is used here to properly detect any overflow and still
* accepting extreme values like INT_MIN and UINT_MAX.
*/
nres = strtouq(string, &end, 0);
/*
* basic error and over/overflow detection.
* needed since strtouq() basically always return something.
*/
if (end == string) {
// no digits found
return false;
} else if (nres == UQUAD_MAX) {
// overflow occured: value is too large.
return false;
}
for (sum = 0; size > 0; size--)
{
if (*val < '0' || *val > '9')
{
return false;
}
sum = (sum * 10) + (*val++ - '0');
/*
* Now we make sure that the signed value or unsigned positive
* value can be stored in a 32-bit integer.
*/
sres = (long long)nres;
if (sres < INT_MIN) {
// signed int underflow:
// if the value is negative, it must be signed, and therefore
// cannot be less than INT_MAX.
return false;
} else if (sres > UINT_MAX) {
// unsigned int overflow:
// when reading 32 hex values, the output value is way above
// INT_MAX and should be treated as an unsigned int value.
return false;
}
if (negative)
{
sum = -sum;
if (value != NULL) {
*value = (int)sres;
}
*value = sum;
return true;
}
}
return false;
}
/*
branches/ErmaC/Enoch/i386/libsaio/stringTable.c
401401
402402
403403
404
405
406
404
405
407406
408
407
409408
410409
411410
412
413
414
415
416
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
417431
418
419
420
421
422
423
424
425
426
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
427449
428
429
430
431
450
451
452
432453
433
454
434455
435456
436457
458
437459
438460
439461
bool getIntForKey(const char *key, int *value, config_file_t *config)
{
const char *val;
int size, sum;
bool negative = false;
const char *string = NULL;
int size = 0;
if (getValueForKey(key, &val, &size, config))
if (getValueForKey(key, &string, &size, config))
{
if (size)
{
if (*val == '-')
{
negative = true;
val++;
size--;
char *end = NULL;
unsigned long long nres = 0;
long long sres = 0;
/*
* strtouq is used here to properly detect any overflow and still
* accepting extreme values like INT_MIN and UINT_MAX.
*/
nres = strtouq(string, &end, 0);
/*
* basic error and over/overflow detection.
* needed since strtouq() basically always return something.
*/
if (end == string) {
// no digits found
return false;
} else if (nres == UQUAD_MAX) {
// overflow occured: value is too large.
return false;
}
for (sum = 0; size > 0; size--)
{
if (*val < '0' || *val > '9')
{
return false;
}
sum = (sum * 10) + (*val++ - '0');
/*
* Now we make sure that the signed value or unsigned positive
* value can be stored in a 32-bit integer.
*/
sres = (long long)nres;
if (sres < INT_MIN) {
// signed int underflow:
// if the value is negative, it must be signed, and therefore
// cannot be less than INT_MAX.
return false;
} else if (sres > UINT_MAX) {
// unsigned int overflow:
// when reading 32 hex values, the output value is way above
// INT_MAX and should be treated as an unsigned int value.
return false;
}
if (negative)
{
sum = -sum;
if (value != NULL) {
*value = (int)sres;
}
*value = sum;
return true;
}
}
return false;
}
/*

Archive Download the corresponding diff file

Revision: 2907