IniFile.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. FinalSun/FinalAlert 2 Mission Editor
  3. Copyright (C) 1999-2024 Electronic Arts, Inc.
  4. Authored by Matthias Wagner
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. // IniFile.h: Schnittstelle für die Klasse CIniFile.
  17. //
  18. //////////////////////////////////////////////////////////////////////
  19. #if !defined(AFX_INIFILE_H__96455620_6528_11D3_99E0_DB2A1EF71411__INCLUDED_)
  20. #define AFX_INIFILE_H__96455620_6528_11D3_99E0_DB2A1EF71411__INCLUDED_
  21. #if _MSC_VER > 1000
  22. #pragma once
  23. #endif // _MSC_VER > 1000
  24. #include <map>
  25. #include <CString>
  26. #include <fstream>
  27. #include <ios>
  28. using namespace std;
  29. class SortDummy
  30. {
  31. public:
  32. bool operator() (const CString&, const CString&) const;
  33. };
  34. class CIniFileSection
  35. {
  36. public:
  37. CIniFileSection();
  38. virtual ~CIniFileSection();
  39. CString GetValueByName(const CString& name, const CString& defaultValue = CString()) const;
  40. CString& AccessValueByName(const CString& name);
  41. auto begin() noexcept
  42. {
  43. return values.begin();
  44. }
  45. auto begin() const noexcept
  46. {
  47. return values.begin();
  48. }
  49. auto end() noexcept
  50. {
  51. return values.end();
  52. }
  53. auto end() const noexcept
  54. {
  55. return values.end();
  56. }
  57. [[deprecated("instead use iterators or for_each")]]
  58. int GetValueOrigPos(int index) const noexcept;
  59. [[deprecated("instead use iterators or for_each")]]
  60. int FindName(CString sval) const noexcept;
  61. [[deprecated("instead use iterators or for_each")]]
  62. int FindValue(CString sval) const noexcept;
  63. [[deprecated("instead use iterators or for_each")]]
  64. const CString* GetValueName(std::size_t index) const noexcept;
  65. [[deprecated("instead use iterators or for_each")]]
  66. const CString* GetValue(std::size_t index) const noexcept;
  67. [[deprecated("instead use iterators or for_each")]]
  68. CString* GetValue(std::size_t index) noexcept;
  69. public:
  70. map<CString, CString, SortDummy> values;
  71. map<CString, int, SortDummy> value_orig_pos;
  72. };
  73. class CIniFile
  74. {
  75. public:
  76. void DeleteEndingSpaces(BOOL bValueNames, BOOL bValues);
  77. void DeleteLeadingSpaces(BOOL bValueNames, BOOL bValues);
  78. const CString* GetSectionName(std::size_t Index) const noexcept;
  79. const CIniFileSection* GetSection(std::size_t index) const;
  80. CIniFileSection* GetSection(std::size_t index);
  81. const CIniFileSection* GetSection(const CString& section) const;
  82. CIniFileSection* GetSection(const CString& section);
  83. CString GetValueByName(const CString& sectionName, const CString& valueName, const CString& defaultValue) const;
  84. void Clear();
  85. WORD InsertFile(const CString& filename, const char* Section, BOOL bNoSpaces = FALSE);
  86. WORD InsertFile(const std::string& filename, const char* Section, BOOL bNoSpaces = FALSE);
  87. BOOL SaveFile(const CString& Filename) const;
  88. BOOL SaveFile(const std::string& Filename) const;
  89. WORD LoadFile(const CString& filename, BOOL bNoSpaces = FALSE);
  90. WORD LoadFile(const std::string& filename, BOOL bNoSpaces = FALSE);
  91. auto begin() noexcept
  92. {
  93. return sections.begin();
  94. }
  95. auto begin() const noexcept
  96. {
  97. return sections.begin();
  98. }
  99. auto end() noexcept
  100. {
  101. return sections.end();
  102. }
  103. auto end() const noexcept
  104. {
  105. return sections.end();
  106. }
  107. map<CString, CIniFileSection> sections;
  108. CIniFile();
  109. virtual ~CIniFile();
  110. private:
  111. std::string m_filename;
  112. };
  113. #endif // !defined(AFX_INIFILE_H__96455620_6528_11D3_99E0_DB2A1EF71411__INCLUDED_)