Added array readers to the ini reader (by rftr).

git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@9171 3b4a5df2-a311-0410-b5c6-a8a6f20db521
This commit is contained in:
Sevenfm
2021-10-04 07:08:32 +00:00
parent 9ebd4a5137
commit 93f8484d56
2 changed files with 77 additions and 0 deletions
+74
View File
@@ -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<FLOAT>& 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<INT32>& 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
+3
View File
@@ -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<FLOAT>& vec);
void ReadINT32Array(const STR8 szSection, const STR8 szKey, std::vector<INT32>& 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);