diff --git a/MissionEditor/IniFile.cpp b/MissionEditor/IniFile.cpp index 36a9651..2d70bb3 100644 --- a/MissionEditor/IniFile.cpp +++ b/MissionEditor/IniFile.cpp @@ -167,7 +167,7 @@ WORD CIniFile::InsertFile(const std::string& filename, const char* Section, BOOL value.Trim(); } - sections[cSec].Assign(name, value); + sections[cSec].SetString(name, value); } } @@ -216,11 +216,6 @@ const CString* CIniFile::GetSectionName(std::size_t index) const noexcept return &(i->first); } -CString& CIniFileSection::AccessValueByName(const CString& valueName) -{ - return values[valueName]; -} - BOOL CIniFile::SaveFile(const CString& filename) const { return SaveFile(std::string(filename.GetString())); @@ -246,21 +241,21 @@ BOOL CIniFile::SaveFile(const std::string& Filename) const } -int CIniFileSection::FindValue(CString sval) const noexcept +int CIniFileSection::FindValue(CString val) const noexcept { for (auto idx = 0; idx < this->value_pairs.size(); ++idx) { - if (this->value_pairs[idx].second == sval) { + if (this->value_pairs[idx].second == val) { return idx; } } return -1; } -int CIniFileSection::FindName(CString sval) const noexcept +int CIniFileSection::FindIndex(const CString& key) const noexcept { - auto const it = this->value_pos.find(sval); + auto const it = this->value_pos.find(key); if (it != this->value_pos.end()) { return it->second; } diff --git a/MissionEditor/IniFile.h b/MissionEditor/IniFile.h index 01162be..65edaa9 100644 --- a/MissionEditor/IniFile.h +++ b/MissionEditor/IniFile.h @@ -59,12 +59,16 @@ public: virtual ~CIniFileSection(); [[deprecated("instead use GetString or TryGetString")]] - CString& AccessValueByName(const CString& name); + const CString& AccessValueByName(const CString& name) const { + return GetString(name); + } auto const& Nth(size_t index) const { ASSERT(index < value_pairs.size()); return this->value_pairs[index]; } + int FindIndex(const CString& key) const noexcept; + int FindValue(CString val) const noexcept; const CString* TryGetString(const CString& key) const { auto const it = value_pos.find(key); @@ -98,12 +102,15 @@ public: auto const it = value_pos.find(key); return it != value_pos.end(); } - - void Assign(const CString& key, const CString& value) { - return this->Assign(key, CString(value)); + bool HasValue(const CString& val) const { + return this->FindValue(val) >= 0; } - void Assign(const CString& key, CString&& value) { + void SetString(const CString& key, const CString& value) { + return this->SetString(key, CString(value)); + } + + void SetString(const CString& key, CString&& value) { auto const it = value_pos.find(key); // new, never had one if (it == value_pos.end()) { @@ -114,10 +121,6 @@ public: value_pairs[it->second].second = std::move(value); } - bool HasValue(const CString& val) const { - return this->FindValue(val) >= 0; - } - void RemoveAt(size_t idx) { ASSERT(idx < value_pairs.size()); for (auto affectedIdx = idx + 1; affectedIdx < value_pairs.size(); ++affectedIdx) { @@ -130,6 +133,18 @@ public: ASSERT(value_pos.erase(itErased->first), 1); } + void RemoveByKey(const CString& key) { + auto const idx = this->FindIndex(key); + if (idx >= 0) { + RemoveAt(idx); + } + } + + void RemoveValue(const CString& val) { + auto const idx = this->FindValue(val); + RemoveAt(idx); + } + auto begin() const noexcept { return value_pairs.begin(); @@ -144,13 +159,9 @@ public: int GetValueOrigPos(int index) const noexcept; [[deprecated("instead use iterators or for_each")]] - int FindName(CString sval) const noexcept; - - [[deprecated("instead use iterators or for_each")]] - int FindValue(CString sval) const noexcept; - - [[deprecated("instead use iterators or for_each")]] - const CString* GetValueName(std::size_t index) const noexcept; + const CString* GetValueName(std::size_t index) const noexcept { + return &Nth(index).first; + } private: map value_pos{}; @@ -160,9 +171,23 @@ private: class CIniFile { + using StorageMap = map; + static const CIniFileSection EmptySection; public: + [[deprecated("instead use GetString")]] + CString GetValueByName(const CString& sectionName, const CString& valueName, const CString& defaultValue) const; + void Clear(); + WORD InsertFile(const CString& filename, const char* Section, BOOL bNoSpaces = FALSE); + WORD InsertFile(const std::string& filename, const char* Section, BOOL bNoSpaces = FALSE); + BOOL SaveFile(const CString& Filename) const; + BOOL SaveFile(const std::string& Filename) const; + WORD LoadFile(const CString& filename, BOOL bNoSpaces = FALSE); + WORD LoadFile(const std::string& filename, BOOL bNoSpaces = FALSE); + + // ================ Section interfaces ================ + const CString* GetSectionName(std::size_t Index) const noexcept; const CIniFileSection* TryGetSection(std::size_t index) const; CIniFileSection* TryGetSection(std::size_t index); @@ -184,47 +209,54 @@ public: } const CIniFileSection& GetSection(const CString& section) const { - auto const it = sections.find(section); - if (it != sections.end()) { - return it->second; + if (auto found = this->TryGetSection(section)) { + return *found; } return EmptySection; } - CString GetValueByName(const CString& sectionName, const CString& valueName, const CString& defaultValue) const; - void Clear(); - WORD InsertFile(const CString& filename, const char* Section, BOOL bNoSpaces = FALSE); - WORD InsertFile(const std::string& filename, const char* Section, BOOL bNoSpaces = FALSE); - BOOL SaveFile(const CString& Filename) const; - BOOL SaveFile(const std::string& Filename) const; - WORD LoadFile(const CString& filename, BOOL bNoSpaces = FALSE); - WORD LoadFile(const std::string& filename, BOOL bNoSpaces = FALSE); + const CIniFileSection& operator[](const CString& section) const { + return this->GetSection(section); + } + bool DeleteSection(const CString& section) { + return sections.erase(section) > 0; + } + + typename StorageMap::iterator DeleteAt(const StorageMap::iterator pos) { + return sections.erase(pos); + } + + // ============= Reader and Helper converter ============================ const CString& GetString(const CString& section, const CString& key) const { - auto const it = sections.find(section); - if (it != sections.end()) { - return it->second.GetString(key); - } - return CIniFileSection::EmptyValue; + return GetSection(section).GetString(key); } const bool GetBool(const CString& section, const CString& key, bool def = false) const { auto const& str = this->GetString(section, key); - return INIHelper::StingToBool(str, def); + return INIHelper::StringToBool(str, def); + } + const int GetInteger(const CString& section, const CString& key, int def = 0) const { + return GetSection(section).GetInteger(key, def); } - void Assign(const CString& section, const CString& key, CString&& value) { + // ============== Writer and Helper converter ============================ + void SetSection(const CString& sectionName, const CIniFileSection& sec) { + sections.insert_or_assign(sectionName, sec); + } + + void SetString(const CString& section, const CString& key, CString&& value) { auto const it = sections.find(section); if (it != sections.end()) { - it->second.Assign(key, value); + it->second.SetString(key, value); return; } auto&& newSec = CIniFileSection{}; - newSec.Assign(key, value); + newSec.SetString(key, value); ASSERT(sections.insert({ key, std::move(newSec) }).second == true); } - void Assign(const CString& section, const CString& key, const CString& value) { - return this->Assign(section, key, CString(value)); + void SetString(const CString& section, const CString& key, const CString& value) { + return this->SetString(section, key, CString(value)); } auto begin() noexcept @@ -252,7 +284,7 @@ public: private: std::string m_filename; - map sections; + StorageMap sections; }; #endif // !defined(AFX_INIFILE_H__96455620_6528_11D3_99E0_DB2A1EF71411__INCLUDED_) diff --git a/MissionEditor/IniHelper.h b/MissionEditor/IniHelper.h index 3978793..e29b1b4 100644 --- a/MissionEditor/IniHelper.h +++ b/MissionEditor/IniHelper.h @@ -5,7 +5,7 @@ class INIHelper { public: - static bool StingToBool(const CString& str, bool def) + static bool StringToBool(const CString& str, bool def) { switch (toupper(static_cast(*str))) { case '1':