Antares Simulator
Power System Simulator
Loading...
Searching...
No Matches
inifile.h
1/*
2** Copyright 2007-2024, RTE (https://www.rte-france.com)
3** See AUTHORS.txt
4** SPDX-License-Identifier: MPL-2.0
5** This file is part of Antares-Simulator,
6** Adequacy and Performance assessment for interconnected energy networks.
7**
8** Antares_Simulator is free software: you can redistribute it and/or modify
9** it under the terms of the Mozilla Public Licence 2.0 as published by
10** the Mozilla Foundation, either version 2 of the License, or
11** (at your option) any later version.
12**
13** Antares_Simulator is distributed in the hope that it will be useful,
14** but WITHOUT ANY WARRANTY; without even the implied warranty of
15** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16** Mozilla Public Licence 2.0 for more details.
17**
18** You should have received a copy of the Mozilla Public Licence 2.0
19** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20*/
21#pragma once
22
23#include <optional>
24
25#include <yuni/yuni.h>
26#include <yuni/core/string.h>
27
28namespace Antares
29{
33class IniFile final
34{
35public:
42 class Property final
43 {
44 public:
45 Property() = default;
46 explicit Property(const AnyString& key);
47 template<class U>
48 Property(const AnyString& key, const U& value);
49 ~Property() = default;
50
51 void saveToStream(std::ostream& file, uint64_t& written) const;
52
53 public:
55 YString key;
57 YString value;
59 Property* next = nullptr;
60 };
61
66 class Section final
67 {
68 public:
69 Section() = default;
70 explicit Section(const AnyString& name);
71 ~Section();
72
76 template<class U>
77 Property* add(const AnyString& key, const U& value);
78
79 template<class U>
80 Property* add(const AnyString& key, const std::optional<U>& value);
81
82 void add(const Property& property);
83
84 void saveToStream(std::ostream& file, uint64_t& written) const;
85
86 Property* find(const AnyString& key);
87 const Property* find(const AnyString& key) const;
88
97 template<class U, class StringT>
98 U read(const StringT& key, const U& defValue) const;
99
119 template<class CallbackT>
120 void each(const CallbackT& callback);
121
141 template<class CallbackT>
142 void each(const CallbackT& callback) const;
143
145 bool empty() const;
146
148 uint size() const;
149
150 public:
152 Yuni::ShortString256 name;
158 Section* next = nullptr;
159
160 }; // class Section
161
162public:
164
165
168 IniFile() = default;
172 explicit IniFile(const std::filesystem::path& filename);
176 ~IniFile();
178
180
181
184 void clear();
185
189 bool loaded() const;
190
197 bool open(const std::string& filename, bool warnings = true);
198
199 bool open(const std::filesystem::path& filename, bool warnings = true);
200
201 bool readStream(std::istream& in_stream);
202
206 bool save(const AnyString& filename) const;
207 void saveToStream(std::ostream&, uint64_t&) const;
208
209 std::string toString() const;
210
212 const std::string& filename() const;
214
216
217
220 Section* add(Section* s);
221
225 Section* addSection(const AnyString& name);
226
230 bool empty() const;
231
235 Section* find(const AnyString& name);
236
240 const Section* find(const AnyString& name) const;
241
256 template<class CallbackT>
257 void each(const CallbackT& callback);
258
273 template<class CallbackT>
274 void each(const CallbackT& callback) const;
275
290 template<class CallbackT>
291 void properties(const CallbackT& callback);
292
307 template<class CallbackT>
308 void properties(const CallbackT& callback) const;
310
311public:
312 Section* firstSection = nullptr;
313 Section* lastSection = nullptr;
314
315private:
322 std::string filename_;
323
324}; // class IniFile
325
326} // namespace Antares
327
328#include "inifile.hxx"
A single entry in an INI file.
Definition inifile.h:43
YString value
Its associated value.
Definition inifile.h:57
YString key
The key.
Definition inifile.h:55
Property * next
The next value.
Definition inifile.h:59
A single section, with all its keys.
Definition inifile.h:67
Property * add(const AnyString &key, const U &value)
Add a new property.
IniFile::Property * lastProperty
The last property of the section.
Definition inifile.h:156
Yuni::ShortString256 name
The name of the section.
Definition inifile.h:152
IniFile::Property * firstProperty
The first property of the section.
Definition inifile.h:154
void each(const CallbackT &callback)
Iterate through all properties.
Definition inifile.hxx:117
Section * next
The next section.
Definition inifile.h:158
uint size() const
Get the number of properties.
Definition inifile.cpp:143
U read(const StringT &key, const U &defValue) const
Try to read a property.
Definition inifile.hxx:82
bool empty() const
Get if the section is empty.
Definition inifile.hxx:50
Data for an INI file.
Definition inifile.h:34
bool save(const AnyString &filename) const
Save the entire INI into a file.
Definition inifile.cpp:287
bool empty() const
Get if the inifile is empty.
Definition inifile.hxx:30
void each(const CallbackT &callback)
Iterate through all sections.
Definition inifile.hxx:99
bool open(const std::string &filename, bool warnings=true)
Load an INI file.
Definition inifile.cpp:238
Section * addSection(const AnyString &name)
Create a new section.
Definition inifile.hxx:88
Section * add(Section *s)
Add an existing section into the INI structure.
Definition inifile.cpp:109
const std::string & filename() const
Get the last filename saved or loaded.
Definition inifile.hxx:93
IniFile()=default
Default Constructor.
void clear()
Empty the INI File.
Definition inifile.cpp:126
~IniFile()
Destructor.
Definition inifile.cpp:94
Section * find(const AnyString &name)
Try to find a section by its name.
Definition inifile.cpp:327
IniFile(const std::filesystem::path &filename)
Load an INI file.
bool loaded() const
Get if the INI file has been loaded/written.
Definition inifile.hxx:25
void properties(const CallbackT &callback)
Iterate through all properties of all sections.
Definition inifile.hxx:135