Antares Simulator
Power System Simulator
inifile.hxx
1 /*
2 ** Copyright 2007-2025, 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 namespace Antares
24 {
25 inline bool IniFile::loaded() const
26 {
27  return not filename_.empty();
28 }
29 
30 inline bool IniFile::empty() const
31 {
32  return not firstSection;
33 }
34 
35 inline IniFile::Section::Section(const AnyString& name):
36  name(name)
37 {
38 }
39 
40 template<class U>
41 IniFile::Property::Property(const AnyString& key, const U& value):
42  key(key),
43  value(value)
44 {
45  this->key.trim();
46  this->key.toLower();
47  this->value.trim();
48 }
49 
50 inline bool IniFile::Section::empty() const
51 {
52  return !firstProperty;
53 }
54 
55 template<class U>
56 IniFile::Property* IniFile::Section::add(const AnyString& key, const U& value)
57 {
58  auto* p = new Property(key, value);
59  if (!lastProperty)
60  {
61  firstProperty = p;
62  }
63  else
64  {
65  lastProperty->next = p;
66  }
67  lastProperty = p;
68  return p;
69 }
70 
71 template<class U>
72 IniFile::Property* IniFile::Section::add(const AnyString& key, const std::optional<U>& value)
73 {
74  if (value.has_value())
75  {
76  return add(key, value.value());
77  }
78  return nullptr;
79 }
80 
81 template<class U, class StringT>
82 inline U IniFile::Section::read(const StringT& key, const U& defValue) const
83 {
84  auto* property = find(key);
85  return (property ? property->value.template to<U>() : defValue);
86 }
87 
88 inline IniFile::Section* IniFile::addSection(const AnyString& name)
89 {
90  return add(new Section(name));
91 }
92 
93 inline const std::string& IniFile::filename() const
94 {
95  return filename_;
96 }
97 
98 template<class CallbackT>
99 void IniFile::each(const CallbackT& callback)
100 {
101  for (auto* section = firstSection; section; section = section->next)
102  {
103  callback(*section);
104  }
105 }
106 
107 template<class CallbackT>
108 void IniFile::each(const CallbackT& callback) const
109 {
110  for (auto* section = firstSection; section; section = section->next)
111  {
112  callback(*section);
113  }
114 }
115 
116 template<class CallbackT>
117 void IniFile::Section::each(const CallbackT& callback)
118 {
119  for (auto* property = firstProperty; property; property = property->next)
120  {
121  callback(*property);
122  }
123 }
124 
125 template<class CallbackT>
126 void IniFile::Section::each(const CallbackT& callback) const
127 {
128  for (auto* property = firstProperty; property; property = property->next)
129  {
130  callback(*property);
131  }
132 }
133 
134 template<class CallbackT>
135 void IniFile::properties(const CallbackT& callback)
136 {
137  for (auto* section = firstSection; section; section = section->next)
138  {
139  for (auto* property = section->firstProperty; property; property = property->next)
140  {
141  callback(*property);
142  }
143  }
144 }
145 
146 template<class CallbackT>
147 void IniFile::properties(const CallbackT& callback) const
148 {
149  for (auto* section = firstSection; section; section = section->next)
150  {
151  for (auto* property = section->firstProperty; property; property = property->next)
152  {
153  callback(*property);
154  }
155  }
156 }
157 
158 } // namespace Antares
A single entry in an INI file.
Definition: inifile.h:43
A single section, with all its keys.
Definition: inifile.h:67
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
U read(const StringT &key, const U &defValue) const
Try to read a property.
Definition: inifile.hxx:82
Property * add(const AnyString &key, const U &value)
Add a new property.
bool empty() const
Get if the section is empty.
Definition: inifile.hxx:50
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
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
Section * find(const AnyString &name)
Try to find a section by its name.
Definition: inifile.cpp:327
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