Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
IniFile.h
Go to the documentation of this file.
1 // This file is part of CoreLibrary containing useful reusable utility
2 // classes.
3 //
4 // Copyright (C) 2014 to present, Duncan Crutchley
5 // Contact <dac1976github@outlook.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published
9 // by 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 and GNU Lesser General Public License
16 // for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // and GNU Lesser General Public License along with this program. If
20 // not, see <http://www.gnu.org/licenses/>.
21 
27 #ifndef INIFILE
28 #define INIFILE
29 
31 #include <utility>
32 #include <map>
33 #include <cstdint>
34 #include "IniFileSectionDetails.h"
35 
37 namespace core_lib
38 {
40 namespace ini_file
41 {
42 
72 class CORE_LIBRARY_DLL_SHARED_API IniFile final
73 {
74 public:
75 #ifdef USE_DEFAULT_CONSTRUCTOR_
76 
77  IniFile();
78 #else
79 
80  IniFile() = default;
81 #endif
82 
83  IniFile(const IniFile&) = default;
90  explicit IniFile(const std::string& iniFilePath);
92  ~IniFile() = default;
94  IniFile& operator=(const IniFile&) = default;
95 #ifdef USE_EXPLICIT_MOVE_
96 
97  IniFile(IniFile&& ini);
99  IniFile& operator=(IniFile&& ini);
100 #else
101 
102  IniFile(IniFile&&) = default;
104  IniFile& operator=(IniFile&&) = default;
105 #endif
106 
112  void LoadFile(const std::string& iniFilePath);
119  void UpdateFile(const std::string& overridePath = "") const;
124  std::list<std::string> GetSections() const;
130  keys_list GetSection(const std::string& section) const;
136  bool SectionExists(const std::string& section) const;
143  bool KeyExists(const std::string& section, const std::string& key) const;
151  bool ReadBool(const std::string& section, const std::string& key,
152  bool defaultValue = false) const;
160  int32_t ReadInt32(const std::string& section, const std::string& key,
161  int32_t defaultValue = 0) const;
169  int64_t ReadInt64(const std::string& section, const std::string& key,
170  int64_t defaultValue = 0L) const;
178  double ReadDouble(const std::string& section, const std::string& key,
179  double defaultValue = 0.0) const;
187  long double ReadLongDouble(const std::string& section, const std::string& key,
188  long double defaultValue = 0.0L) const;
196  std::string ReadString(const std::string& section, const std::string& key,
197  const std::string& defaultValue = "") const;
198 
205  void WriteBool(const std::string& section, const std::string& key, bool value);
212  void WriteInt32(const std::string& section, const std::string& key, int32_t value);
219  void WriteInt64(const std::string& section, const std::string& key, int64_t value);
226  void WriteDouble(const std::string& section, const std::string& key, double value);
233  void WriteLongDouble(const std::string& section, const std::string& key, long double value);
240  void WriteString(const std::string& section, const std::string& key, const std::string& value);
248  void EraseSection(const std::string& section);
254  void EraseSections();
265  void EraseKey(const std::string& section, const std::string& key);
274  void EraseKeys(const std::string& section);
275 
276 private:
277 #ifdef USE_DEFAULT_CONSTRUCTOR_
278 
279  mutable bool m_changesMade;
281  std::string m_iniFilePath;
282 #else
283 
284  mutable bool m_changesMade{false};
286  std::string m_iniFilePath{"config.ini"};
287 #endif
288 
289  using section_map = std::map<std::string, if_private::SectionDetails>;
293  using section_iter = section_map::iterator;
295  using section_citer = section_map::const_iterator;
298 
306  std::string ReadValueString(const std::string& section, const std::string& key,
307  const std::string& defaultValue) const;
314  void WriteValueString(const std::string& section, const std::string& key,
315  const std::string& value);
316 };
317 
318 } // namespace ini_file
319 } // namespace core_lib
320 
321 #endif // INIFILE
std::list< std::pair< std::string, std::string > > keys_list
Typedef defining the key-value pair list for section entries.
Definition: IniFileLines.h:44
section_map::const_iterator section_citer
Section map const iterator typedef.
Definition: IniFile.h:295
std::list< std::shared_ptr< Line > > line_list
Line list typedef.
Definition: IniFileLines.h:244
File containing declarations relating the IniFile support classes.
The core_lib namespace.
Definition: AsioDefines.h:59
Ini file class.
Definition: IniFile.h:72
section_map::iterator section_iter
Section map iterator typedef.
Definition: IniFile.h:293
if_private::line_list m_lines
Line list.
Definition: IniFile.h:297
std::map< std::string, if_private::SectionDetails > section_map
Section map typedef.
Definition: IniFile.h:289
File containing platform specific definitions.
section_map m_sectionMap
Sectin map.
Definition: IniFile.h:291