diff --git a/Utils/INIReader.cpp b/Utils/INIReader.cpp index e16978f7..f9649c86 100644 --- a/Utils/INIReader.cpp +++ b/Utils/INIReader.cpp @@ -253,6 +253,80 @@ FLOAT CIniReader::ReadFloat(const STR8 szSection, const STR8 szKey, FLOAT defaul return iniValueReadFromFile; } +void CIniReader::ReadFloatArray(const STR8 szSection, const STR8 szKey, std::vector& vec) +{ + // read the array as a string + STRING512 textBuffer; + ReadString(szSection, szKey, "", textBuffer, _countof(textBuffer)); + + std::string str, token; + std::string delim = ","; + size_t offset = 0, prevOffset = 0; + + // sanitise input + vec.clear(); + str = textBuffer; + str.erase(std::remove(str.begin(), str.end(), ' '), str.end()); + + // split into array + try + { + do + { + offset = str.find(delim, prevOffset); + token = str.substr(prevOffset, offset - prevOffset); + prevOffset = offset + delim.length(); + + vec.push_back(std::stof(token.c_str())); + } while (offset != std::string::npos); + } + catch (...) + { + std::stringstream errMessage; + errMessage << "There was an error reading array [" << szSection << "][" << szKey << "] in file [" << m_szFileName << "]. Defaulting to [0]."; + iniErrorMessages.push(errMessage.str()); + + vec.push_back(0); + } +} + +void CIniReader::ReadINT32Array(const STR8 szSection, const STR8 szKey, std::vector& vec) +{ + // read the array as a string + STRING512 textBuffer; + ReadString(szSection, szKey, "", textBuffer, _countof(textBuffer)); + + std::string str, token; + std::string delim = ","; + size_t offset = 0, prevOffset = 0; + + // sanitise input + vec.clear(); + str = textBuffer; + str.erase(std::remove(str.begin(), str.end(), ' '), str.end()); + + // split into array + try + { + do + { + offset = str.find(delim, prevOffset); + token = str.substr(prevOffset, offset - prevOffset); + prevOffset = offset + delim.length(); + + vec.push_back(std::stoi(token.c_str())); + } while (offset != std::string::npos); + } + catch (...) + { + std::stringstream errMessage; + errMessage << "There was an error reading array [" << szSection << "][" << szKey << "] in file [" << m_szFileName << "]. Defaulting to [0]."; + iniErrorMessages.push(errMessage.str()); + + vec.push_back(0); + } +} + BOOLEAN CIniReader::ReadBoolean(const STR8 szSection, const STR8 szKey, bool defaultValue, bool bolDisplayError) { #ifndef USE_VFS diff --git a/Utils/INIReader.h b/Utils/INIReader.h index 340028f3..947cdeff 100644 --- a/Utils/INIReader.h +++ b/Utils/INIReader.h @@ -43,6 +43,9 @@ public: DOUBLE ReadDouble(const STR8 szSection, const STR8 szKey, DOUBLE defaultValue, DOUBLE minValue, DOUBLE maxValue); FLOAT ReadFloat (const STR8 szSection, const STR8 szKey, FLOAT defaultValue, FLOAT minValue, FLOAT maxValue); + void ReadFloatArray(const STR8 szSection, const STR8 szKey, std::vector& vec); + void ReadINT32Array(const STR8 szSection, const STR8 szKey, std::vector& vec); + BOOLEAN ReadBoolean(const STR8 szSection, const STR8 szKey, bool bolDefaultValue, bool bolDisplayError = true); void ReadString(const STR8 szSection, const STR8 szKey, const STR8 szDefaultValue, STR8 input_buffer, size_t buffer_size);