| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/IniFile.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cstddef> |
| 8 | #include <fstream> |
| 9 | #include <map> |
| 10 | #include <string> |
| 11 | #include <string_view> |
| 12 | #include <utility> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include "Common/FileUtil.h" |
| 16 | #include "Common/StringUtil.h" |
| 17 | |
| 18 | namespace Common |
| 19 | { |
| 20 | void IniFile::ParseLine(std::string_view line, std::string* keyOut, std::string* valueOut) |
| 21 | { |
| 22 | if (line.empty() || line.front() == '#') |
| 23 | return; |
| 24 | |
| 25 | size_t firstEquals = line.find('='); |
| 26 | |
| 27 | if (firstEquals != std::string::npos) |
| 28 | { |
| 29 | // Yes, a valid line! |
| 30 | *keyOut = StripWhitespace(line.substr(0, firstEquals)); |
| 31 | |
| 32 | if (valueOut) |
| 33 | { |
| 34 | *valueOut = StripQuotes(StripWhitespace(line.substr(firstEquals + 1, std::string::npos))); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | const std::string& IniFile::NULL_STRING = ""; |
| 40 | |
| 41 | IniFile::Section::Section() = default; |
| 42 | |
| 43 | IniFile::Section::Section(std::string name_) : name{std::move(name_)} |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | void IniFile::Section::Set(const std::string& key, std::string new_value) |
| 48 | { |
| 49 | const auto result = values.insert_or_assign(key, std::move(new_value)); |
| 50 | const bool insertion_occurred = result.second; |
| 51 | |
| 52 | if (insertion_occurred) |
| 53 | keys_order.push_back(key); |
| 54 | } |
| 55 | |
| 56 | bool IniFile::Section::Get(std::string_view key, std::string* value, |
| 57 | const std::string& default_value) const |
| 58 | { |
| 59 | const auto it = values.find(key); |
| 60 | |
| 61 | if (it != values.end()) |
| 62 | { |
| 63 | *value = it->second; |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | if (&default_value != &NULL_STRING) |
| 68 | { |
| 69 | *value = default_value; |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | bool IniFile::Section::Exists(std::string_view key) const |
| 77 | { |
| 78 | return values.contains(key); |
| 79 | } |
| 80 | |
| 81 | bool IniFile::Section::Delete(std::string_view key) |
| 82 | { |
| 83 | const auto it = values.find(key); |
| 84 | if (it == values.end()) |
| 85 | return false; |
| 86 | |
| 87 | values.erase(it); |
| 88 | keys_order.erase(std::ranges::find_if( |
| 89 | keys_order, [&](std::string_view v) { return CaseInsensitiveEquals(key, v); })); |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | void IniFile::Section::SetLines(std::vector<std::string> lines) |
| 94 | { |
| 95 | m_lines = std::move(lines); |
| 96 | } |
| 97 | |
| 98 | bool IniFile::Section::GetLines(std::vector<std::string>* lines, const bool remove_comments) const |
| 99 | { |
| 100 | for (const std::string& line : m_lines) |
| 101 | { |
| 102 | std::string_view stripped_line = StripWhitespace(line); |
| 103 | |
| 104 | if (remove_comments) |
| 105 | { |
| 106 | size_t commentPos = stripped_line.find('#'); |
| 107 | if (commentPos == 0) |
| 108 | { |
| 109 | continue; |
| 110 | } |
| 111 | |
| 112 | if (commentPos != std::string::npos) |
| 113 | { |
| 114 | stripped_line = StripWhitespace(stripped_line.substr(0, commentPos)); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | lines->emplace_back(stripped_line); |
| 119 | } |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | // IniFile |
| 125 | |
| 126 | IniFile::IniFile() = default; |
| 127 | |
| 128 | IniFile::~IniFile() = default; |
| 129 | |
| 130 | const IniFile::Section* IniFile::GetSection(std::string_view section_name) const |
| 131 | { |
| 132 | for (const Section& sect : sections) |
| 133 | { |
| 134 | if (CaseInsensitiveEquals(sect.name, section_name)) |
| 135 | return § |
| 136 | } |
| 137 | |
| 138 | return nullptr; |
| 139 | } |
| 140 | |
| 141 | IniFile::Section* IniFile::GetSection(std::string_view section_name) |
| 142 | { |
| 143 | for (Section& sect : sections) |
| 144 | { |
| 145 | if (CaseInsensitiveEquals(sect.name, section_name)) |
| 146 | return § |
| 147 | } |
| 148 | |
| 149 | return nullptr; |
| 150 | } |
| 151 | |
| 152 | IniFile::Section* IniFile::GetOrCreateSection(std::string_view section_name) |
| 153 | { |
| 154 | Section* section = GetSection(section_name); |
| 155 | if (!section) |
| 156 | { |
| 157 | sections.emplace_back(std::string(section_name)); |
| 158 | section = §ions.back(); |
| 159 | } |
| 160 | return section; |
| 161 | } |
| 162 | |
| 163 | bool IniFile::DeleteSection(std::string_view section_name) |
| 164 | { |
| 165 | Section* s = GetSection(section_name); |
| 166 | if (!s) |
| 167 | return false; |
| 168 | |
| 169 | for (auto iter = sections.begin(); iter != sections.end(); ++iter) |
| 170 | { |
| 171 | if (&(*iter) == s) |
| 172 | { |
| 173 | sections.erase(iter); |
| 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | bool IniFile::Exists(std::string_view section_name) const |
| 182 | { |
| 183 | return GetSection(section_name) != nullptr; |
| 184 | } |
| 185 | |
| 186 | bool IniFile::Exists(std::string_view section_name, std::string_view key) const |
| 187 | { |
| 188 | const Section* section = GetSection(section_name); |
| 189 | if (!section) |
| 190 | return false; |
| 191 | |
| 192 | return section->Exists(key); |
| 193 | } |
| 194 | |
| 195 | void IniFile::SetLines(std::string_view section_name, std::vector<std::string> lines) |
| 196 | { |
| 197 | Section* section = GetOrCreateSection(section_name); |
| 198 | section->SetLines(std::move(lines)); |
| 199 | } |
| 200 | |
| 201 | bool IniFile::DeleteKey(std::string_view section_name, std::string_view key) |
| 202 | { |
| 203 | Section* section = GetSection(section_name); |
| 204 | if (!section) |
| 205 | return false; |
| 206 | return section->Delete(key); |
| 207 | } |
| 208 | |
| 209 | // Return a list of all keys in a section |
| 210 | bool IniFile::GetKeys(std::string_view section_name, std::vector<std::string>* keys) const |
| 211 | { |
| 212 | const Section* section = GetSection(section_name); |
| 213 | if (!section) |
| 214 | { |
| 215 | return false; |
| 216 | } |
| 217 | *keys = section->keys_order; |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | // Return a list of all lines in a section |
| 222 | bool IniFile::GetLines(std::string_view section_name, std::vector<std::string>* lines, |
| 223 | const bool remove_comments) const |
| 224 | { |
| 225 | lines->clear(); |
| 226 | |
| 227 | const Section* section = GetSection(section_name); |
| 228 | if (!section) |
| 229 | return false; |
| 230 | |
| 231 | return section->GetLines(lines, remove_comments); |
| 232 | } |
| 233 | |
| 234 | void IniFile::SortSections() |
| 235 | { |
| 236 | sections.sort(); |
| 237 | } |
| 238 | |
| 239 | bool IniFile::Load(const std::string& filename, bool keep_current_data) |
| 240 | { |
| 241 | if (!keep_current_data) |
| 242 | sections.clear(); |
| 243 | // first section consists of the comments before the first real section |
| 244 | |
| 245 | // Open file |
| 246 | std::ifstream in; |
| 247 | File::OpenFStream(in, filename, std::ios::in); |
| 248 | |
| 249 | if (in.fail()) |
| 250 | return false; |
| 251 | |
| 252 | Section* current_section = nullptr; |
| 253 | bool first_line = true; |
| 254 | while (!in.eof()) |
| 255 | { |
| 256 | std::string line_str; |
| 257 | if (!std::getline(in, line_str)) |
| 258 | { |
| 259 | if (in.eof()) |
| 260 | return true; |
| 261 | else |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | std::string_view line = line_str; |
| 266 | |
| 267 | // Skips the UTF-8 BOM at the start of files. Notepad likes to add this. |
| 268 | if (first_line && line.substr(0, 3) == "\xEF\xBB\xBF") |
| 269 | line.remove_prefix(3); |
| 270 | first_line = false; |
| 271 | |
| 272 | #ifndef _WIN32 |
| 273 | // Check for CRLF eol and convert it to LF |
| 274 | if (!line.empty() && line.back() == '\r') |
| 275 | line.remove_suffix(1); |
| 276 | #endif |
| 277 | |
| 278 | if (!line.empty()) |
| 279 | { |
| 280 | if (line[0] == '[') |
| 281 | { |
| 282 | size_t endpos = line.find(']'); |
| 283 | |
| 284 | if (endpos != std::string::npos) |
| 285 | { |
| 286 | // New section! |
| 287 | std::string_view sub = line.substr(1, endpos - 1); |
| 288 | current_section = GetOrCreateSection(sub); |
| 289 | } |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | if (current_section) |
| 294 | { |
| 295 | std::string key, value; |
| 296 | ParseLine(line, &key, &value); |
| 297 | |
| 298 | // Lines starting with '$', '*' or '+' are kept verbatim. |
| 299 | // Kind of a hack, but the support for raw lines inside an |
| 300 | // INI is a hack anyway. |
| 301 | if ((key.empty() && value.empty()) || |
| 302 | (!line.empty() && (line[0] == '$' || line[0] == '+' || line[0] == '*'))) |
| 303 | { |
| 304 | current_section->m_lines.emplace_back(line); |
| 305 | } |
| 306 | else |
| 307 | { |
| 308 | current_section->Set(key, value); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | in.close(); |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | bool IniFile::Save(const std::string& filename) |
| 320 | { |
| 321 | std::ofstream out; |
| 322 | std::string temp = File::GetTempFilenameForAtomicWrite(filename); |
| 323 | File::OpenFStream(out, temp, std::ios::out); |
| 324 | |
| 325 | if (out.fail()) |
| 326 | { |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | for (const Section& section : sections) |
| 331 | { |
| 332 | if (!section.keys_order.empty() || !section.m_lines.empty()) |
| 333 | out << '[' << section.name << ']' << std::endl; |
| 334 | |
| 335 | if (section.keys_order.empty()) |
| 336 | { |
| 337 | for (const std::string& s : section.m_lines) |
| 338 | out << s << std::endl; |
| 339 | } |
| 340 | else |
| 341 | { |
| 342 | for (const std::string& kvit : section.keys_order) |
| 343 | { |
| 344 | auto pair = section.values.find(kvit); |
| 345 | out << pair->first << " = " << pair->second << std::endl; |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | out.close(); |
| 351 | |
| 352 | return File::RenameSync(temp, filename); |
| 353 | } |
| 354 | |
| 355 | // Unit test. TODO: Move to the real unit test framework. |
| 356 | /* |
| 357 | int main() |
| 358 | { |
| 359 | IniFile ini; |
| 360 | ini.Load("my.ini"); |
| 361 | ini.Set("Hello", "A", "amaskdfl"); |
| 362 | ini.Set("Moss", "A", "amaskdfl"); |
| 363 | ini.Set("Aissa", "A", "amaskdfl"); |
| 364 | //ini.Read("my.ini"); |
| 365 | std::string x; |
| 366 | ini.Get("Hello", "B", &x, "boo"); |
| 367 | ini.DeleteKey("Moss", "A"); |
| 368 | ini.DeleteSection("Moss"); |
| 369 | ini.SortSections(); |
| 370 | ini.Save("my.ini"); |
| 371 | //UpdateVars(ini); |
| 372 | return 0; |
| 373 | } |
| 374 | */ |
| 375 | } // namespace Common |