27 std::string
getString(
const std::string& key,
const std::string& defaultValue =
"")
const
29 const auto it =
m_data.find(key);
34 std::string val = it->second;
35 if (val.size() >= 2 && val.front() ==
'"' && val.back() ==
'"')
36 val = val.substr(1, val.size() - 2);
40 int getInt(
const std::string& key,
int defaultValue = 0)
const
42 const auto it =
m_data.find(key);
45 try {
return std::stoi(it->second); }
46 catch (...) {
return defaultValue; }
49 double getDouble(
const std::string& key,
double defaultValue = 0.0)
const
51 const auto it =
m_data.find(key);
54 try {
return std::stod(it->second); }
55 catch (...) {
return defaultValue; }
58 bool getBool(
const std::string& key,
bool defaultValue =
false)
const
60 const auto it =
m_data.find(key);
63 const std::string& v = it->second;
64 if (v ==
"true" || v ==
"1")
return true;
65 if (v ==
"false" || v ==
"0")
return false;
69 std::wstring
getWString(
const std::string& key,
const std::wstring& defaultValue = L
"")
const
75 const int len = MultiByteToWideChar(CP_UTF8, 0, s.c_str(),
static_cast<int>(s.size()),
nullptr, 0);
78 std::wstring result(len, L
'\0');
79 MultiByteToWideChar(CP_UTF8, 0, s.c_str(),
static_cast<int>(s.size()), result.data(), len);
83 void setValue(
const std::string& key,
int value)
85 m_data[key] = std::to_string(value);
88 void setValue(
const std::string& key,
double value)
90 m_data[key] = std::to_string(value);
93 void setValue(
const std::string& key,
bool value)
95 m_data[key] = value ?
"true" :
"false";
98 void setValue(
const std::string& key,
const std::string& value)
103 void setValue(
const std::string& key,
const std::wstring& value)
111 const int len = WideCharToMultiByte(CP_UTF8, 0, value.c_str(),
static_cast<int>(value.size()),
112 nullptr, 0,
nullptr,
nullptr);
113 std::string result(len,
'\0');
114 WideCharToMultiByte(CP_UTF8, 0, value.c_str(),
static_cast<int>(value.size()),
115 result.data(), len,
nullptr,
nullptr);
131 std::map<std::string, std::map<std::string, std::string>> sections;
132 for (
const auto& [fullKey, value] :
m_data)
134 const auto pos = fullKey.find(
'/');
135 if (pos != std::string::npos)
136 sections[fullKey.substr(0, pos)][fullKey.substr(pos + 1)] = value;
138 sections[
"General"][fullKey] = value;
142 for (
const auto& [section, keys] : sections)
147 file <<
'[' << section <<
"]\n";
148 for (
const auto& [key, value] : keys)
149 file << key <<
'=' << value <<
'\n';
165 std::string currentSection;
167 while (std::getline(file, line))
170 if (!line.empty() && line.back() ==
'\r')
174 if (line.empty() || line[0] ==
';' || line[0] ==
'#')
178 if (line.front() ==
'[' && line.back() ==
']')
180 currentSection = line.substr(1, line.size() - 2);
185 const auto eq = line.find(
'=');
186 if (eq == std::string::npos)
189 std::string key = line.substr(0, eq);
190 std::string value = line.substr(eq + 1);
193 if (!value.empty() && value[0] ==
'@')
196 if (!currentSection.empty())
197 key = currentSection +
"/" + key;
205 std::map<std::string, std::string>
m_data;
Simple INI file reader/writer with section/key support.
void setValue(const std::string &key, const std::string &value)
IniFile(const std::string &path)
bool getBool(const std::string &key, bool defaultValue=false) const
int getInt(const std::string &key, int defaultValue=0) const
void setValue(const std::string &key, int value)
void setValue(const std::string &key, const std::wstring &value)
std::map< std::string, std::string > m_data
void setValue(const std::string &key, bool value)
IniFile(const std::wstring &path)
void setValue(const std::string &key, double value)
std::wstring getWString(const std::string &key, const std::wstring &defaultValue=L"") const
std::string getString(const std::string &key, const std::string &defaultValue="") const
double getDouble(const std::string &key, double defaultValue=0.0) const
Common string utility functions (split, case-insensitive search, etc.).