simplify value list parsing

This commit is contained in:
John
2025-01-06 19:48:52 +01:00
parent fab1dd1b1f
commit 88b039e58e
+7 -12
View File
@@ -309,27 +309,22 @@ result_t DataField::create(bool isWriteMessage, bool isTemplate, bool isBroadcas
while (getline(stream, token, VALUE_SEPARATOR)) { while (getline(stream, token, VALUE_SEPARATOR)) {
FileReader::trim(&token); FileReader::trim(&token);
const char* str = token.c_str(); const char* str = token.c_str();
char* strEnd = nullptr; size_t len = 0;
unsigned long id; unsigned int id = parseInt(str, 0, 0, MAX_VALUE, &result, &len, true);
if (strncasecmp(str, "0x", 2) == 0) { if (result != RESULT_OK) {
str += 2;
id = strtoul(str, &strEnd, 16); // hexadecimal
} else {
id = strtoul(str, &strEnd, 10); // decimal
}
if (strEnd == nullptr || strEnd == str || id > MAX_VALUE) {
*errorDescription = "value "+token+" in field "+formatInt(fieldIndex); *errorDescription = "value "+token+" in field "+formatInt(fieldIndex);
result = RESULT_ERR_INVALID_LIST; result = RESULT_ERR_INVALID_LIST;
break; break;
} }
str += len;
// remove blanks around '=' sign // remove blanks around '=' sign
while (*strEnd == ' ') strEnd++; while (*str == ' ') str++;
if (*strEnd != '=') { if (*str != '=') {
*errorDescription = "value "+token+" in field "+formatInt(fieldIndex); *errorDescription = "value "+token+" in field "+formatInt(fieldIndex);
result = RESULT_ERR_INVALID_LIST; result = RESULT_ERR_INVALID_LIST;
break; break;
} }
token = string(strEnd + 1); token = string(str + 1);
FileReader::trim(&token); FileReader::trim(&token);
values[(unsigned int)id] = token; values[(unsigned int)id] = token;
} }