Pioneer
IniConfig.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 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  typedef std::map<std::string, std::string> MapType;
19  typedef std::map<std::string, MapType> SectionMapType;
20  IniConfig() = default;
21 
22  // Read from a file on disk. If fs is a FileSourceFS, enables in-place saving
23  // of the IniConfig.
24  void Read(FileSystem::FileSource &fs, const std::string &path);
25 
26  // Write to a file on disk.
27  bool Write(FileSystem::FileSourceFS &fs, const std::string &path);
28 
29  // If a previous call to Read() was made using a writable file source,
30  // save the IniConfig in-place to that file.
31  bool Save();
32 
33  void SetInt(const std::string &section, const std::string &key, int val);
34  void SetFloat(const std::string &section, const std::string &key, float val);
35  void SetString(const std::string &section, const std::string &key, const std::string &val);
36 
37  int Int(const std::string &section, const std::string &key, int defval) const;
38  float Float(const std::string &section, const std::string &key, float defval) const;
39  std::string String(const std::string &section, const std::string &key, const std::string &defval) const;
40 
41  void SetInt(const std::string &key, int val) { SetInt("", key, val); }
42  void SetFloat(const std::string &key, float val) { SetFloat("", key, val); }
43  void SetString(const std::string &key, const std::string &val) { SetString("", key, val); }
44 
45  int Int(const std::string &key, int defval = 0) const { return Int("", key, defval); }
46  float Float(const std::string &key, float defval = 0.0f) const { return Float("", key, defval); }
47  std::string String(const std::string &key, const std::string &defval = std::string()) const { return String("", key, defval); }
48 
49  bool HasSection(const std::string &section) const
50  {
51  const auto it = m_map.find(section);
52  return (it != m_map.end()) && (!it->second.empty());
53  }
54 
55  bool HasEntry(const std::string &section, const std::string &key) const
56  {
57  const auto it = m_map.find(section);
58  return (it != m_map.end()) && it->second.count(key);
59  }
60  bool HasEntry(const std::string &key) const { return HasEntry("", key); }
62 
63 protected:
64  void Read(const FileSystem::FileData &data);
65 
67 
69  std::string m_path;
70 };
71 
72 #endif /* _INICONFIG_H */
double val
Definition: PrecalcPath.cpp:40
Definition: FileSystem.h:158
Definition: FileSystem.h:221
Definition: FileSystem.h:197
Definition: IniConfig.h:16
bool HasEntry(const std::string &section, const std::string &key) const
Definition: IniConfig.h:55
void SetInt(const std::string &key, int val)
Definition: IniConfig.h:41
bool HasSection(const std::string &section) const
Definition: IniConfig.h:49
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:60
void Read(FileSystem::FileSource &fs, const std::string &path)
Definition: IniConfig.cpp:70
SectionMapType & GetSections()
Definition: IniConfig.h:61
std::map< std::string, MapType > SectionMapType
Definition: IniConfig.h:19
std::map< std::string, std::string > MapType
Definition: IniConfig.h:18
std::string String(const std::string &key, const std::string &defval=std::string()) const
Definition: IniConfig.h:47
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:46
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:43
std::string m_path
Definition: IniConfig.h:69
SectionMapType m_map
Definition: IniConfig.h:66
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:68
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:45
void SetFloat(const std::string &key, float val)
Definition: IniConfig.h:42
Definition: CityOnPlanet.h:27