aboutsummaryrefslogtreecommitdiff
path: root/libs/installer_fomod_csharp/src/psettings.h
blob: 649c7daea28d90f790ea994372288c8078dddb1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef PSETTINGS_H
#define PSETTINGS_H

#include <QSettings>
#include <QString>
#include <map>

/**
 * This is a small class that can be used to store INI settings in memory since
 * QSettings is a pain to use without an actual file.
 *
 * It is a much simpler structure since it stores everything as string.
 */
struct PSettings
{

  /**
   *
   */
  PSettings() = default;

public:  // Value read/write.
  /**
   * @brief Set the value of the given section/key.
   *
   * @param section The section of the value.
   * @param key The key of the value.
   * @param value The value to set.
   */
  void setValue(QString section, QString key, QString value)
  {
    m_Values[std::make_pair(section, key)] = value;
  }

  /**
   * @brief Return the value at the given section/key.
   *
   * @param section The section of the value.
   * @param key The key of the value.
   *
   * @return the corresponding value, or an empty string if the section/key does not
   * exist.
   */
  QString value(QString section, QString key) const
  {
    auto it = m_Values.find(std::make_pair(section, key));
    return it == m_Values.end() ? QString() : it->second;
  }

  /**
   * @brief Check if the given section/key exists in these settings.
   *
   * @param section The section of the value.
   * @param key The key of the value.
   *
   * @return true if the section/key exist.
   */
  bool hasValue(QString section, QString key) const
  {
    return m_Values.find(std::make_pair(section, key)) != m_Values.end();
  }

public:  // Output:
  /**
   * @brief Convert this PSettings to a string.
   *
   * @return a string representing the content of a valid INI file corresponding
   *     to this PSettings.
   */
  QString toString() const
  {
    QString result = "";
    QString cSection;
    for (auto& p : m_Values) {
      if (cSection != p.first.first) {
        if (!cSection.isEmpty()) {
          result += '\n';
        }
        cSection = p.first.first;
        result += "[" + cSection + "]\n";
      }
      result += p.first.second + "=" + p.second + "\n";
    }
    return result;
  }

  /**
   * @brief Update the given QSettings with all the value in this.
   *
   * @param settings The settings to update.
   */
  void update(QSettings& settings) const
  {
    for (auto& p : m_Values) {
      if (p.first.first == "General") {
        settings.setValue(p.first.second, p.second);
      } else {
        settings.setValue(p.first.first + "/" + p.first.second, p.second);
      }
    }
  }

private:
  // Map from <section, key> to value:
  std::map<std::pair<QString, QString>, QString> m_Values;
};

#endif  // !PSETTINGS_H