Pioneer
IniConfig.h
Go to the documentation of this file.
1 // Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _INICONFIG_H
5 #define _INICONFIG_H
6 
7 #include <map>
8 #include <string>
9 
10 namespace FileSystem {
11  class FileData;
12  class FileSource;
13  class FileSourceFS;
14 } // namespace FileSystem
15 
16 class IniConfig {
17 public:
18  IniConfig() = default;
19 
20  // Read from a file on disk. If fs is a FileSourceFS, enables in-place saving
21  // of the IniConfig.
22  void Read(FileSystem::FileSource &fs, const std::string &path);
23 
24  // Write to a file on disk.
25  bool Write(FileSystem::FileSourceFS &fs, const std::string &path);
26 
27  // If a previous call to Read() was made using a writable file source,
28  // save the IniConfig in-place to that file.
29  bool Save();
30 
31  void SetInt(const std::string &section, const std::string &key, int val);
32  void SetFloat(const std::string &section, const std::string &key, float val);
33  void SetString(const std::string &section, const std::string &key, const std::string &val);
34 
35  int Int(const std::string &section, const std::string &key, int defval) const;
36  float Float(const std::string &section, const std::string &key, float defval) const;
37  std::string String(const std::string &section, const std::string &key, const std::string &defval) const;
38 
39  void SetInt(const std::string &key, int val) { SetInt("", key, val); }
40  void SetFloat(const std::string &key, float val) { SetFloat("", key, val); }
41  void SetString(const std::string &key, const std::string &val) { SetString("", key, val); }
42 
43  int Int(const std::string &key, int defval = 0) const { return Int("", key, defval); }
44  float Float(const std::string &key, float defval = 0.0f) const { return Float("", key, defval); }
45  std::string String(const std::string &key, const std::string &defval = std::string()) const { return String("", key, defval); }
46 
47  bool HasSection(const std::string &section) const
48  {
49  const auto it = m_map.find(section);
50  return (it != m_map.end()) && (!it->second.empty());
51  }
52 
53  bool HasEntry(const std::string &section, const std::string &key) const
54  {
55  const auto it = m_map.find(section);
56  return (it != m_map.end()) && it->second.count(key);
57  }
58  bool HasEntry(const std::string &key) const { return HasEntry("", key); }
59 
60 protected:
61  void Read(const FileSystem::FileData &data);
62 
63  typedef std::map<std::string, std::string> MapType;
64  typedef std::map<std::string, MapType> SectionMapType;
66 
68  std::string m_path;
69 };
70 
71 #endif /* _INICONFIG_H */
double val
Definition: PrecalcPath.cpp:40
Definition: FileSystem.h:158
Definition: FileSystem.h:220
Definition: FileSystem.h:196
Definition: IniConfig.h:16
bool HasEntry(const std::string &section, const std::string &key) const
Definition: IniConfig.h:53
void SetInt(const std::string &key, int val)
Definition: IniConfig.h:39
bool HasSection(const std::string &section) const
Definition: IniConfig.h:47
IniConfig()=default
bool Write(FileSystem::FileSourceFS &fs, const std::string &path)
Definition: IniConfig.cpp:134
bool HasEntry(const std::string &key) const
Definition: IniConfig.h:58
void Read(FileSystem::FileSource &fs, const std::string &path)
Definition: IniConfig.cpp:70
std::map< std::string, MapType > SectionMapType
Definition: IniConfig.h:64
std::map< std::string, std::string > MapType
Definition: IniConfig.h:63
std::string String(const std::string &key, const std::string &defval=std::string()) const
Definition: IniConfig.h:45
float Float(const std::string &section, const std::string &key, float defval) const
Definition: IniConfig.cpp:46
float Float(const std::string &key, float defval=0.0f) const
Definition: IniConfig.h:44
void SetFloat(const std::string &section, const std::string &key, float val)
Definition: IniConfig.cpp:19
void SetString(const std::string &section, const std::string &key, const std::string &val)
Definition: IniConfig.cpp:26
void SetString(const std::string &key, const std::string &val)
Definition: IniConfig.h:41
std::string m_path
Definition: IniConfig.h:68
SectionMapType m_map
Definition: IniConfig.h:65
int Int(const std::string &section, const std::string &key, int defval) const
Definition: IniConfig.cpp:31
bool Save()
Definition: IniConfig.cpp:159
FileSystem::FileSourceFS * m_fs
Definition: IniConfig.h:67
void SetInt(const std::string &section, const std::string &key, int val)
Definition: IniConfig.cpp:12
std::string String(const std::string &section, const std::string &key, const std::string &defval) const
Definition: IniConfig.cpp:61
int Int(const std::string &key, int defval=0) const
Definition: IniConfig.h:43
void SetFloat(const std::string &key, float val)
Definition: IniConfig.h:40
Definition: IniConfig.h:10