Debian Codegraph @

io/inifile.h

raw symbols SCIP
1 #ifndef IOUTILITIES_INIFILE_H
2 #define IOUTILITIES_INIFILE_H
3
4 #include "../global.h"
5 #include "../misc/flagenumclass.h"
6
7 #include <algorithm>
8 #include <iosfwd>
9 #include <map>
10 #include <optional>
11 #include <string>
12 #include <vector>
13
14 namespace CppUtilities {
15
16 class CPP_UTILITIES_EXPORT IniFile {
17 public:
18 using ScopeName = std::string;
19 using ScopeData = std::multimap<std::string, std::string>;
20 using Scope = std::pair<ScopeName, ScopeData>;
21 using ScopeList = std::vector<Scope>;
22
23 IniFile();
24 ScopeList &data();
25 const ScopeList &data() const;
26 void parse(std::istream &inputStream);
27 void make(std::ostream &outputStream);
28
29 private:
30 ScopeList m_data;
31 };
32
33 /*!
34 * \brief Constructs an empty ini file.
35 */
36 inline IniFile::IniFile()
37 {
38 }
39
40 /*!
41 * \brief Returns the data of the file.
42 * \remarks
43 * - The returned pairs represent the [scope names] and the contained "key = value"-pairs.
44 * - The data might be modified and then saved using the make() method.
45 */
46 inline IniFile::ScopeList &IniFile::data()
47 {
48 return m_data;
49 }
50
51 /*!
52 * \brief Returns the data of the file.
53 * \remarks The returned pairs represent the [scope names] and the contained "key = value"-pairs.
54 */
55 inline const IniFile::ScopeList &IniFile::data() const
56 {
57 return m_data;
58 }
59
60 enum class IniFileParseOptions {
61 None = 0,
62 };
63
64 enum class IniFileMakeOptions {
65 None = 0,
66 };
67
68 enum class IniFileFieldFlags {
69 None = 0,
70 HasValue = (1 << 0),
71 };
72
73 enum class IniFileSectionFlags {
74 None = 0,
75 Implicit = (1 << 0),
76 Truncated = (1 << 1),
77 };
78
79 struct CPP_UTILITIES_EXPORT AdvancedIniFile {
80 struct Field {
81 std::string key;
82 std::string value;
83 std::string precedingCommentBlock;
84 std::string followingInlineComment;
85 std::size_t paddedKeyLength = 0;
86 IniFileFieldFlags flags = IniFileFieldFlags::HasValue;
87 };
88 using FieldList = std::vector<Field>;
89 struct Section {
90 FieldList::iterator findField(std::string_view key);
91 FieldList::const_iterator findField(std::string_view key) const;
92 FieldList::iterator findField(FieldList::iterator after, std::string_view key);
93 FieldList::const_iterator findField(FieldList::iterator after, std::string_view key) const;
94 FieldList::iterator fieldEnd();
95 FieldList::const_iterator fieldEnd() const;
96
97 std::string name;
98 FieldList fields;
99 std::string precedingCommentBlock;
100 std::string followingInlineComment;
101 IniFileSectionFlags flags = IniFileSectionFlags::None;
102 };
103 using SectionList = std::vector<Section>;
104
105 SectionList::iterator findSection(std::string_view sectionName);
106 SectionList::const_iterator findSection(std::string_view sectionName) const;
107 SectionList::iterator findSection(SectionList::iterator after, std::string_view sectionName);
108 SectionList::const_iterator findSection(SectionList::iterator after, std::string_view sectionName) const;
109 SectionList::iterator sectionEnd();
110 SectionList::const_iterator sectionEnd() const;
111 std::optional<FieldList::iterator> findField(std::string_view sectionName, std::string_view key);
112 std::optional<FieldList::const_iterator> findField(std::string_view sectionName, std::string_view key) const;
113 void parse(std::istream &inputStream, IniFileParseOptions options = IniFileParseOptions::None);
114 void make(std::ostream &outputStream, IniFileMakeOptions options = IniFileMakeOptions::None);
115
116 SectionList sections;
117 };
118
119 /*!
120 * \brief Returns an iterator to the first section with the name \a sectionName.
121 */
122 inline AdvancedIniFile::SectionList::iterator AdvancedIniFile::findSection(std::string_view sectionName)
123 {
124 return std::find_if(sections.begin(), sections.end(), [&sectionName](const auto &scope) { return scope.name == sectionName; });
125 }
126
127 /*!
128 * \brief Returns an iterator to the first section with the name \a sectionName.
129 */
130 inline AdvancedIniFile::SectionList::const_iterator AdvancedIniFile::findSection(std::string_view sectionName) const
131 {
132 return const_cast<AdvancedIniFile *>(this)->findSection(sectionName);
133 }
134
135 /*!
136 * \brief Returns an iterator to the first section with the name \a sectionName which comes after \a after.
137 */
138 inline AdvancedIniFile::SectionList::iterator AdvancedIniFile::findSection(SectionList::iterator after, std::string_view sectionName)
139 {
140 return std::find_if(after + 1, sections.end(), [&sectionName](const auto &scope) { return scope.name == sectionName; });
141 }
142
143 /*!
144 * \brief Returns an iterator to the first section with the name \a sectionName which comes after \a after.
145 */
146 inline AdvancedIniFile::SectionList::const_iterator AdvancedIniFile::findSection(SectionList::iterator after, std::string_view sectionName) const
147 {
148 return const_cast<AdvancedIniFile *>(this)->findSection(after, sectionName);
149 }
150
151 /*!
152 * \brief Returns an iterator that points one past the last section.
153 */
154 inline AdvancedIniFile::SectionList::iterator AdvancedIniFile::sectionEnd()
155 {
156 return sections.end();
157 }
158
159 /*!
160 * \brief Returns an iterator that points one past the last section.
161 */
162 inline AdvancedIniFile::SectionList::const_iterator AdvancedIniFile::sectionEnd() const
163 {
164 return sections.end();
165 }
166
167 /*!
168 * \brief Returns an iterator to the first field within the first section with matching \a sectionName and \a key.
169 */
170 inline std::optional<AdvancedIniFile::FieldList::iterator> AdvancedIniFile::findField(std::string_view sectionName, std::string_view key)
171 {
172 const SectionList::iterator scope = findSection(sectionName);
173 if (scope == sectionEnd()) {
174 return std::nullopt;
175 }
176 const FieldList::iterator field = scope->findField(key);
177 if (field == scope->fieldEnd()) {
178 return std::nullopt;
179 }
180 return field;
181 }
182
183 /*!
184 * \brief Returns an iterator to the first field within the first section with matching \a sectionName and \a key.
185 */
186 inline std::optional<AdvancedIniFile::FieldList::const_iterator> AdvancedIniFile::findField(std::string_view sectionName, std::string_view key) const
187 {
188 return const_cast<AdvancedIniFile *>(this)->findField(sectionName, key);
189 }
190
191 /*!
192 * \brief Returns an iterator to the first field with the key \a key.
193 */
194 inline AdvancedIniFile::FieldList::iterator AdvancedIniFile::Section::findField(std::string_view key)
195 {
196 return std::find_if(fields.begin(), fields.end(), [&key](const auto &field) { return field.key == key; });
197 }
198
199 /*!
200 * \brief Returns an iterator to the first field with the key \a key.
201 */
202 inline AdvancedIniFile::FieldList::const_iterator AdvancedIniFile::Section::findField(std::string_view key) const
203 {
204 return const_cast<Section *>(this)->findField(key);
205 }
206
207 /*!
208 * \brief Returns an iterator to the first field with the key \a key which comes after \a after.
209 */
210 inline AdvancedIniFile::FieldList::iterator AdvancedIniFile::Section::findField(FieldList::iterator after, std::string_view key)
211 {
212 return std::find_if(after + 1, fields.end(), [&key](const auto &field) { return field.key == key; });
213 }
214
215 /*!
216 * \brief Returns an iterator to the first field with the key \a key which comes after \a after.
217 */
218 inline AdvancedIniFile::FieldList::const_iterator AdvancedIniFile::Section::findField(FieldList::iterator after, std::string_view key) const
219 {
220 return const_cast<Section *>(this)->findField(after, key);
221 }
222
223 /*!
224 * \brief Returns an iterator that points one past the last field.
225 */
226 inline AdvancedIniFile::FieldList::iterator AdvancedIniFile::Section::fieldEnd()
227 {
228 return fields.end();
229 }
230
231 /*!
232 * \brief Returns an iterator that points one past the last field.
233 */
234 inline AdvancedIniFile::FieldList::const_iterator AdvancedIniFile::Section::fieldEnd() const
235 {
236 return fields.end();
237 }
238
239 } // namespace CppUtilities
240
241 CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(CppUtilities, IniFileParseOptions);
242 CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(CppUtilities, IniFileMakeOptions);
243 CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(CppUtilities, IniFileFieldFlags);
244 CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(CppUtilities, IniFileSectionFlags);
245
246 #endif // IOUTILITIES_INIFILE_H