Skip to content

Commit f0bad56

Browse files
committed
Treat keys in FileParser case-insensitively
Presets and user sprites could use any casing for the keys, so we should also behave properly.
1 parent 86164be commit f0bad56

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/libprojectM/PresetFileParser.cpp

+25-10
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,16 @@ auto PresetFileParser::Read(std::istream& presetStream) -> bool
7575

7676
auto PresetFileParser::GetCode(const std::string& keyPrefix) const -> std::string
7777
{
78+
auto lowerKey = ToLower(keyPrefix);
79+
7880
std::stringstream code; //!< The parsed code
79-
std::string key(keyPrefix.length() + 5, '\0'); //!< Allocate a string that can hold up to 5 digits.
81+
std::string key(lowerKey.length() + 5, '\0'); //!< Allocate a string that can hold up to 5 digits.
8082

81-
key.replace(0, keyPrefix.length(), keyPrefix);
83+
key.replace(0, lowerKey.length(), lowerKey);
8284

8385
for (int index{1}; index <= 99999; ++index)
8486
{
85-
key.replace(keyPrefix.length(), 5, std::to_string(index));
87+
key.replace(lowerKey.length(), 5, std::to_string(index));
8688
if (m_presetValues.find(key) == m_presetValues.end())
8789
{
8890
break;
@@ -105,11 +107,12 @@ auto PresetFileParser::GetCode(const std::string& keyPrefix) const -> std::strin
105107

106108
auto PresetFileParser::GetInt(const std::string& key, int defaultValue) -> int
107109
{
108-
if (m_presetValues.find(key) != m_presetValues.end())
110+
auto lowerKey = ToLower(key);
111+
if (m_presetValues.find(lowerKey) != m_presetValues.end())
109112
{
110113
try
111114
{
112-
return std::stoi(m_presetValues.at(key));
115+
return std::stoi(m_presetValues.at(lowerKey));
113116
}
114117
catch (std::logic_error&)
115118
{
@@ -121,11 +124,12 @@ auto PresetFileParser::GetInt(const std::string& key, int defaultValue) -> int
121124

122125
auto PresetFileParser::GetFloat(const std::string& key, float defaultValue) -> float
123126
{
124-
if (m_presetValues.find(key) != m_presetValues.end())
127+
auto lowerKey = ToLower(key);
128+
if (m_presetValues.find(lowerKey) != m_presetValues.end())
125129
{
126130
try
127131
{
128-
return std::stof(m_presetValues.at(key));
132+
return std::stof(m_presetValues.at(lowerKey));
129133
}
130134
catch (std::logic_error&)
131135
{
@@ -142,9 +146,10 @@ auto PresetFileParser::GetBool(const std::string& key, bool defaultValue) -> boo
142146

143147
auto PresetFileParser::GetString(const std::string& key, const std::string& defaultValue) -> std::string
144148
{
145-
if (m_presetValues.find(key) != m_presetValues.end())
149+
auto lowerKey = ToLower(key);
150+
if (m_presetValues.find(lowerKey) != m_presetValues.end())
146151
{
147-
return m_presetValues.at(key);
152+
return m_presetValues.at(lowerKey);
148153
}
149154

150155
return defaultValue;
@@ -166,7 +171,8 @@ void PresetFileParser::ParseLine(const std::string& line)
166171
return;
167172
}
168173

169-
std::string varName(line.begin(), line.begin() + varNameDelimiterPos);
174+
// Convert key to lower case, as INI functions are not case-sensitive.
175+
std::string varName(ToLower(std::string(line.begin(), line.begin() + varNameDelimiterPos)));
170176
std::string value(line.begin() + varNameDelimiterPos + 1, line.end());
171177

172178
// Only add first occurrence to mimic Milkdrop behaviour
@@ -176,4 +182,13 @@ void PresetFileParser::ParseLine(const std::string& line)
176182
}
177183
}
178184

185+
auto PresetFileParser::ToLower(std::string str) -> std::string
186+
{
187+
std::transform(str.begin(), str.end(), str.begin(),
188+
[](unsigned char c){ return std::tolower(c); }
189+
);
190+
191+
return str;
192+
}
193+
179194
} // namespace libprojectM

src/libprojectM/PresetFileParser.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ class PresetFileParser
117117
void ParseLine(const std::string& line);
118118

119119
private:
120+
/**
121+
* @brief Converts the string to lower-case.
122+
* Only letters A-Z are converted to a-z by default.
123+
* @param str The original string.
124+
* @return The lower-case string.
125+
*/
126+
static auto ToLower(std::string str) -> std::string;
127+
120128
ValueMap m_presetValues; //!< Map with preset keys and their value.
121129
};
122130

0 commit comments

Comments
 (0)