OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
settingsbase.hpp
Go to the documentation of this file.
1 #ifndef SETTINGSBASE_HPP
2 #define SETTINGSBASE_HPP
3 
4 #include <QTextStream>
5 #include <QStringList>
6 #include <QString>
7 #include <QRegExp>
8 #include <QMap>
9 
10 namespace Config
11 {
12  template <class Map>
14  {
15 
16  public:
17  SettingsBase() { mMultiValue = false; }
19 
20  inline QString value(const QString &key, const QString &defaultValue = QString()) const
21  {
22  return mSettings.value(key).isEmpty() ? defaultValue : mSettings.value(key);
23  }
24 
25  inline void setValue(const QString &key, const QString &value)
26  {
27  QStringList values = mSettings.values(key);
28  if (!values.contains(value))
29  mSettings.insert(key, value);
30  }
31 
32  inline void setMultiValue(const QString &key, const QString &value)
33  {
34  QStringList values = mSettings.values(key);
35  if (!values.contains(value))
36  mSettings.insertMulti(key, value);
37  }
38 
39  inline void setMultiValueEnabled(bool enable)
40  {
42  }
43 
44  inline void remove(const QString &key)
45  {
46  mSettings.remove(key);
47  }
48 
49  Map getSettings() const {return mSettings;}
50 
51  bool readFile(QTextStream &stream)
52  {
53  Map cache;
54 
55  QString sectionPrefix;
56 
57  QRegExp sectionRe("^\\[([^]]+)\\]");
58  QRegExp keyRe("^([^=]+)\\s*=\\s*(.+)$");
59 
60  while (!stream.atEnd()) {
61  QString line = stream.readLine();
62 
63  if (line.isEmpty() || line.startsWith("#"))
64  continue;
65 
66  if (sectionRe.exactMatch(line)) {
67  sectionPrefix = sectionRe.cap(1);
68  sectionPrefix.append("/");
69  continue;
70  }
71 
72  if (keyRe.indexIn(line) != -1) {
73 
74  QString key = keyRe.cap(1).trimmed();
75  QString value = keyRe.cap(2).trimmed();
76 
77  if (!sectionPrefix.isEmpty())
78  key.prepend(sectionPrefix);
79 
80  mSettings.remove(key);
81 
82  QStringList values = cache.values(key);
83 
84  if (!values.contains(value)) {
85  if (mMultiValue) {
86  cache.insertMulti(key, value);
87  } else {
88  cache.insert(key, value);
89  }
90  }
91  }
92  }
93 
94  if (mSettings.isEmpty()) {
95  mSettings = cache; // This is the first time we read a file
96  return true;
97  }
98 
99  // Merge the changed keys with those which didn't
100  mSettings.unite(cache);
101  return true;
102  }
103 
104  void clear()
105  {
106  mSettings.clear();
107  }
108 
109  private:
111 
113  };
114 }
115 #endif // SETTINGSBASE_HPP
void clear()
Definition: settingsbase.hpp:104
void setMultiValueEnabled(bool enable)
Definition: settingsbase.hpp:39
void setMultiValue(const QString &key, const QString &value)
Definition: settingsbase.hpp:32
Map getSettings() const
Definition: settingsbase.hpp:49
SettingsBase()
Definition: settingsbase.hpp:17
Definition: settingsbase.hpp:13
void setValue(const QString &key, const QString &value)
Definition: settingsbase.hpp:25
bool mMultiValue
Definition: settingsbase.hpp:112
bool readFile(QTextStream &stream)
Definition: settingsbase.hpp:51
~SettingsBase()
Definition: settingsbase.hpp:18
void enable(CodeContainer &code, Literals &literals, const std::string &id)
Definition: generator.cpp:870
Map mSettings
Definition: settingsbase.hpp:110
QString value(const QString &key, const QString &defaultValue=QString()) const
Definition: settingsbase.hpp:20