IniFile.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <algorithm>
  5. #include <list>
  6. #include <map>
  7. #include <string>
  8. #include <string_view>
  9. #include <vector>
  10. #include "Common/CommonTypes.h"
  11. #include "Common/StringUtil.h"
  12. namespace Common
  13. {
  14. class IniFile
  15. {
  16. public:
  17. class Section
  18. {
  19. friend class IniFile;
  20. public:
  21. Section();
  22. explicit Section(std::string name_);
  23. bool Exists(std::string_view key) const;
  24. bool Delete(std::string_view key);
  25. void Set(const std::string& key, std::string new_value);
  26. template <typename T>
  27. void Set(const std::string& key, T&& new_value)
  28. {
  29. Set(key, ValueToString(std::forward<T>(new_value)));
  30. }
  31. template <typename T>
  32. void Set(const std::string& key, T&& new_value, const std::common_type_t<T>& default_value)
  33. {
  34. if (new_value != default_value)
  35. Set(key, std::forward<T>(new_value));
  36. else
  37. Delete(key);
  38. }
  39. bool Get(std::string_view key, std::string* value,
  40. const std::string& default_value = NULL_STRING) const;
  41. template <typename T>
  42. bool Get(std::string_view key, T* value, const std::common_type_t<T>& default_value = {}) const
  43. {
  44. std::string temp;
  45. bool retval = Get(key, &temp);
  46. if (retval && TryParse(temp, value))
  47. return true;
  48. *value = default_value;
  49. return false;
  50. }
  51. void SetLines(std::vector<std::string> lines);
  52. bool GetLines(std::vector<std::string>* lines, const bool remove_comments = true) const;
  53. bool operator<(const Section& other) const { return name < other.name; }
  54. using SectionMap = std::map<std::string, std::string, CaseInsensitiveLess>;
  55. const std::string& GetName() const { return name; }
  56. const SectionMap& GetValues() const { return values; }
  57. bool HasLines() const { return !m_lines.empty(); }
  58. protected:
  59. std::string name;
  60. std::vector<std::string> keys_order;
  61. SectionMap values;
  62. std::vector<std::string> m_lines;
  63. };
  64. IniFile();
  65. ~IniFile();
  66. /**
  67. * Loads sections and keys.
  68. * @param filename filename of the ini file which should be loaded
  69. * @param keep_current_data If true, "extends" the currently loaded list of sections and keys with
  70. * the loaded data (and replaces existing entries). If false, existing data will be erased.
  71. * @warning Using any other operations than "Get*" and "Exists" is untested and will behave
  72. * unexpectedly
  73. * @todo This really is just a hack to support having two levels of gameinis (defaults and
  74. * user-specified) and should eventually be replaced with a less stupid system.
  75. */
  76. bool Load(const std::string& filename, bool keep_current_data = false);
  77. bool Save(const std::string& filename);
  78. bool Exists(std::string_view section_name) const;
  79. // Returns true if key exists in section
  80. bool Exists(std::string_view section_name, std::string_view key) const;
  81. template <typename T>
  82. bool GetIfExists(std::string_view section_name, std::string_view key, T* value)
  83. {
  84. if (Exists(section_name, key))
  85. return GetOrCreateSection(section_name)->Get(key, value);
  86. return false;
  87. }
  88. template <typename T>
  89. bool GetIfExists(std::string_view section_name, std::string_view key, T* value, T default_value)
  90. {
  91. if (Exists(section_name, key))
  92. return GetOrCreateSection(section_name)->Get(key, value, default_value);
  93. *value = default_value;
  94. return false;
  95. }
  96. bool GetKeys(std::string_view section_name, std::vector<std::string>* keys) const;
  97. void SetLines(std::string_view section_name, std::vector<std::string> lines);
  98. bool GetLines(std::string_view section_name, std::vector<std::string>* lines,
  99. bool remove_comments = true) const;
  100. bool DeleteKey(std::string_view section_name, std::string_view key);
  101. bool DeleteSection(std::string_view section_name);
  102. void SortSections();
  103. Section* GetOrCreateSection(std::string_view section_name);
  104. const Section* GetSection(std::string_view section_name) const;
  105. Section* GetSection(std::string_view section_name);
  106. // This function is related to parsing data from lines of INI files
  107. // It's used outside of IniFile, which is why it is exposed publicly
  108. // In particular it is used in PostProcessing for its configuration
  109. static void ParseLine(std::string_view line, std::string* keyOut, std::string* valueOut);
  110. const std::list<Section>& GetSections() const { return sections; }
  111. private:
  112. std::list<Section> sections;
  113. static const std::string& NULL_STRING;
  114. };
  115. } // namespace Common