ScummVM API documentation
config-manager.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef COMMON_CONFIG_MANAGER_H
23 #define COMMON_CONFIG_MANAGER_H
24 
25 #include "common/array.h"
26 #include "common/hashmap.h"
27 #include "common/path.h"
28 #include "common/singleton.h"
29 #include "common/str.h"
30 #include "common/hash-str.h"
31 
32 namespace Common {
33 
44 class WriteStream;
45 class SeekableReadStream;
46 
55 class ConfigManager : public Singleton<ConfigManager> {
56 
57 public:
58 
59  class Domain {
60  private:
61  StringMap _entries;
62  StringMap _keyValueComments;
63  String _domainComment;
64 
65  public:
66  typedef StringMap::const_iterator const_iterator;
67  const_iterator begin() const { return _entries.begin(); }
68  const_iterator end() const { return _entries.end(); }
70  bool empty() const { return _entries.empty(); }
72  bool contains(const String &key) const { return _entries.contains(key); }
80  const String &operator[](const String &key) const { return _entries[key]; }
81 
82  void setVal(const String &key, const String &value) { _entries.setVal(key, value); }
84  String &getOrCreateVal(const String &key) { return _entries.getOrCreateVal(key); }
85  String &getVal(const String &key) { return _entries.getVal(key); }
86  const String &getVal(const String &key) const { return _entries.getVal(key); }
92  const String &getValOrDefault(const String &key) const { return _entries.getValOrDefault(key); }
93  bool tryGetVal(const String &key, String &out) const { return _entries.tryGetVal(key, out); }
94 
95  void clear() { _entries.clear(); }
97  void erase(const String &key) { _entries.erase(key); }
99  void setDomainComment(const String &comment);
100  const String &getDomainComment() const;
102  void setKVComment(const String &key, const String &comment);
103  const String &getKVComment(const String &key) const;
104  bool hasKVComment(const String &key) const;
105  };
106 
109 
111  static char const *const kApplicationDomain;
112 
114  static char const *const kTransientDomain;
115 
117  static char const *const kKeymapperDomain;
118 
122  static char const *const kSessionDomain;
123 
124 #ifdef USE_CLOUD
125 
126  static char const *const kCloudDomain;
127 #endif
128 
129  bool loadDefaultConfigFile(const Path &fallbackFilename);
130  bool loadConfigFile(const Path &filename, const Path &fallbackFilename);
137  Domain * getDomain(const String &domName);
138  const Domain *getDomain(const String &domName) const;
148  bool hasKey(const String &key) const;
149  const String &get(const String &key) const;
150  void set(const String &key, const String &value);
156  bool hasDefault(const String &key) const;
157 
162  void setAndFlush(const String &key, const Common::String &value);
163 
164 #if 1
165 
175  bool hasKey(const String &key, const String &domName) const;
176  const String &get(const String &key, const String &domName) const;
177  void set(const String &key, const String &value, const String &domName);
179  void removeKey(const String &key, const String &domName);
181 #endif
182 
188  int getInt(const String &key, const String &domName = String()) const;
189  bool getBool(const String &key, const String &domName = String()) const;
190  Path getPath(const String &key, const String &domName = String()) const;
191  float getFloat(const String &key, const String &domName = String()) const;
192  void setInt(const String &key, int value, const String &domName = String());
193  void setBool(const String &key, bool value, const String &domName = String());
194  void setPath(const String &key, const Path &value, const String &domName = String());
195  void setFloat(const String &key, float value, const String &domName = String());
197  void registerDefault(const String &key, const String &value);
198  void registerDefault(const String &key, const char *value);
199  void registerDefault(const String &key, int value);
200  void registerDefault(const String &key, bool value);
201  void registerDefault(const String &key, const Path &value);
203  void flushToDisk();
205  void setActiveDomain(const String &domName);
206  Domain *getActiveDomain() { return _activeDomain; }
207  const Domain *getActiveDomain() const { return _activeDomain; }
208  const String &getActiveDomainName() const { return _activeDomainName; }
210  void addGameDomain(const String &domName);
211  void removeGameDomain(const String &domName);
212  void renameGameDomain(const String &oldName, const String &newName);
214  void addMiscDomain(const String &domName);
215  void removeMiscDomain(const String &domName);
216  void renameMiscDomain(const String &oldName, const String &newName);
218  bool hasGameDomain(const String &domName) const;
219  bool hasMiscDomain(const String &domName) const;
221  bool isKeyTemporary(const String &key) const;
223  const DomainMap &getGameDomains() const { return _gameDomains; }
224  DomainMap::iterator beginGameDomains() { return _gameDomains.begin(); }
225  DomainMap::iterator endGameDomains() { return _gameDomains.end(); }
227  const Path &getCustomConfigFileName() { return _filename; }
229  static void defragment();
230  void copyFrom(ConfigManager &source);
232 private:
233  friend class Singleton<SingletonBaseType>;
234  ConfigManager();
235 
236  bool loadFallbackConfigFile(const Path &filename);
237  bool loadFromStream(SeekableReadStream &stream);
238  void addDomain(const String &domainName, const Domain &domain);
239  void writeDomain(WriteStream &stream, const String &name, const Domain &domain);
240  void renameDomain(const String &oldName, const String &newName, DomainMap &map);
241 
242  Domain _transientDomain;
243  DomainMap _gameDomains;
244  DomainMap _miscDomains; // Any other domains
245  Domain _appDomain;
246  Domain _defaultsDomain;
247 
248  Domain _keymapperDomain;
249 
250  Domain _sessionDomain;
251 
252 #ifdef USE_CLOUD
253  Domain _cloudDomain;
254 #endif
255 
256  Array<String> _domainSaveOrder;
257 
258  String _activeDomainName;
259  Domain * _activeDomain;
260 
261  Path _filename;
262 };
263 
266 } // End of namespace Common
267 
269 #define ConfMan Common::ConfigManager::instance()
270 
271 #endif
void clear(bool shrinkArray=0)
Definition: hashmap.h:427
Definition: str.h:59
const String & operator[](const String &key) const
Definition: config-manager.h:80
void setBool(const String &key, bool value, const String &domName=String())
Definition: stream.h:77
HashMap< String, Domain, IgnoreCase_Hash, IgnoreCase_EqualTo > DomainMap
Definition: config-manager.h:108
static char const *const kKeymapperDomain
Definition: config-manager.h:117
Val & getVal(const Key &key)
Definition: hashmap.h:633
const Domain * getActiveDomain() const
Definition: config-manager.h:207
const String & getDomainComment() const
Definition: path.h:52
DomainMap::iterator endGameDomains()
Definition: config-manager.h:225
Domain * getDomain(const String &domName)
const String & getVal(const String &key) const
Definition: config-manager.h:86
bool loadConfigFile(const Path &filename, const Path &fallbackFilename)
static void defragment()
void copyFrom(ConfigManager &source)
void addGameDomain(const String &domName)
Definition: stream.h:745
String & getVal(const String &key)
Definition: config-manager.h:85
bool hasGameDomain(const String &domName) const
const String & getKVComment(const String &key) const
DomainMap::iterator beginGameDomains()
Definition: config-manager.h:224
const String & getActiveDomainName() const
Definition: config-manager.h:208
void addMiscDomain(const String &domName)
void setAndFlush(const String &key, const Common::String &value)
void removeMiscDomain(const String &domName)
bool loadDefaultConfigFile(const Path &fallbackFilename)
static char const *const kTransientDomain
Definition: config-manager.h:114
void renameMiscDomain(const String &oldName, const String &newName)
const DomainMap & getGameDomains() const
Definition: config-manager.h:223
bool hasKVComment(const String &key) const
void setInt(const String &key, int value, const String &domName=String())
bool isKeyTemporary(const String &key) const
bool getBool(const String &key, const String &domName=String()) const
Definition: algorithm.h:29
bool empty() const
Definition: hashmap.h:299
bool tryGetVal(const Key &key, Val &out) const
Definition: hashmap.h:688
void setFloat(const String &key, float value, const String &domName=String())
Definition: config-manager.h:59
void registerDefault(const String &key, const String &value)
int getInt(const String &key, const String &domName=String()) const
const Path & getCustomConfigFileName()
Definition: config-manager.h:227
bool contains(const Key &key) const
Definition: hashmap.h:594
void setDomainComment(const String &comment)
bool hasKey(const String &key) const
Domain * getActiveDomain()
Definition: config-manager.h:206
void removeKey(const String &key, const String &domName)
bool hasDefault(const String &key) const
bool hasMiscDomain(const String &domName) const
void removeGameDomain(const String &domName)
void erase(iterator entry)
Definition: hashmap.h:710
void clear()
Definition: config-manager.h:95
Path getPath(const String &key, const String &domName=String()) const
void setVal(const String &key, const String &value)
Definition: config-manager.h:82
bool contains(const String &key) const
Definition: config-manager.h:72
Definition: config-manager.h:55
void renameGameDomain(const String &oldName, const String &newName)
static char const *const kSessionDomain
Definition: config-manager.h:122
void setPath(const String &key, const Path &value, const String &domName=String())
Val & getOrCreateVal(const Key &key)
Definition: hashmap.h:622
void setKVComment(const String &key, const String &comment)
void setActiveDomain(const String &domName)
static char const *const kApplicationDomain
Definition: config-manager.h:111
const_iterator begin() const
Definition: config-manager.h:67
const String & getValOrDefault(const String &key) const
Definition: config-manager.h:92
void erase(const String &key)
Definition: config-manager.h:97
const_iterator end() const
Definition: config-manager.h:68
float getFloat(const String &key, const String &domName=String()) const
Definition: singleton.h:42
bool empty() const
Definition: config-manager.h:70