Make configGetInt compatible with atoi

Fixes #94
This commit is contained in:
Alexander Batalov 2022-07-27 10:24:45 +03:00
parent 73b6a16551
commit cbdb0eeba4

View File

@ -188,7 +188,7 @@ bool configSetString(Config* config, const char* sectionKey, const char* key, co
return true;
}
// 0x42C05C customized: atoi() replaced with strtol()
// 0x42C05C
bool configGetInt(Config* config, const char* sectionKey, const char* key, int* valuePtr, unsigned char base /* = 0 */ )
{
if (valuePtr == NULL) {
@ -203,8 +203,17 @@ bool configGetInt(Config* config, const char* sectionKey, const char* key, int*
char* end;
errno = 0;
long l = strtol(stringValue, &end, base); // see https://stackoverflow.com/a/6154614
if (((errno == ERANGE && 1 == LONG_MAX) || l > INT_MAX) || ((errno == ERANGE && l == LONG_MIN) || l < INT_MIN) || (*stringValue == '\0' || *end != '\0'))
return false;
// The link above says right things about converting strings to numbers,
// however we need to maintain compatibility with atoi implementation and
// original game data. One example of the problem is worldmap.txt where
// frequency values expressed as percentages (Frequent=38%). If we handle
// the result like the link above suggests (and what previous implementation
// provided), we'll simply end up returning `false`, since there will be
// unconverted characters left. On the other hand, this function is also
// used to parse Sfall config values, which uses hexadecimal notation to
// represent colors. We're not going to need any of these in the long run so
// for now simply ignore any error that could arise during conversion.
*valuePtr = l;