OpenTTD Source 20250312-master-gcdcc6b491d
ini_load.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#include "stdafx.h"
11#include "core/mem_func.hpp"
12#include "ini_type.h"
13#include "string_func.h"
14
15#include "safeguards.h"
16
22IniItem::IniItem(std::string_view name)
23{
24 this->name = StrMakeValid(name);
25}
26
31void IniItem::SetValue(std::string_view value)
32{
33 this->value.emplace(value);
34}
35
41IniGroup::IniGroup(std::string_view name, IniGroupType type) : type(type), comment("\n")
42{
43 this->name = StrMakeValid(name);
44}
45
51const IniItem *IniGroup::GetItem(std::string_view name) const
52{
53 for (const IniItem &item : this->items) {
54 if (item.name == name) return &item;
55 }
56
57 return nullptr;
58}
59
65IniItem &IniGroup::GetOrCreateItem(std::string_view name)
66{
67 for (IniItem &item : this->items) {
68 if (item.name == name) return item;
69 }
70
71 /* Item doesn't exist, make a new one. */
72 return this->CreateItem(name);
73}
74
80IniItem &IniGroup::CreateItem(std::string_view name)
81{
82 return this->items.emplace_back(name);
83}
84
89void IniGroup::RemoveItem(std::string_view name)
90{
91 this->items.remove_if([&name](const IniItem &item) { return item.name == name; });
92}
93
98{
99 this->items.clear();
100}
101
107IniLoadFile::IniLoadFile(const IniGroupNameList &list_group_names, const IniGroupNameList &seq_group_names) :
108 list_group_names(list_group_names),
109 seq_group_names(seq_group_names)
110{
111}
112
118const IniGroup *IniLoadFile::GetGroup(std::string_view name) const
119{
120 for (const IniGroup &group : this->groups) {
121 if (group.name == name) return &group;
122 }
123
124 return nullptr;
125}
126
132IniGroup *IniLoadFile::GetGroup(std::string_view name)
133{
134 for (IniGroup &group : this->groups) {
135 if (group.name == name) return &group;
136 }
137
138 return nullptr;
139}
140
147{
148 for (IniGroup &group : this->groups) {
149 if (group.name == name) return group;
150 }
151
152 /* Group doesn't exist, make a new one. */
153 return this->CreateGroup(name);
154}
155
161IniGroup &IniLoadFile::CreateGroup(std::string_view name)
162{
164 if (std::ranges::find(this->list_group_names, name) != this->list_group_names.end()) type = IGT_LIST;
165 if (std::ranges::find(this->seq_group_names, name) != this->seq_group_names.end()) type = IGT_SEQUENCE;
166
167 return this->groups.emplace_back(name, type);
168}
169
174void IniLoadFile::RemoveGroup(std::string_view name)
175{
176 size_t len = name.length();
177 this->groups.remove_if([&name, &len](const IniGroup &group) { return group.name.compare(0, len, name) == 0; });
178}
179
186void IniLoadFile::LoadFromDisk(const std::string &filename, Subdirectory subdir)
187{
188 assert(this->groups.empty());
189
190 char buffer[1024];
191 IniGroup *group = nullptr;
192
193 std::string comment;
194
195 size_t end;
196 auto in = this->OpenFile(filename, subdir, &end);
197 if (!in.has_value()) return;
198
199 end += ftell(*in);
200
201 /* for each line in the file */
202 while (static_cast<size_t>(ftell(*in)) < end && fgets(buffer, sizeof(buffer), *in)) {
203 char c, *s;
204 /* trim whitespace from the left side */
205 for (s = buffer; *s == ' ' || *s == '\t'; s++) {}
206
207 /* trim whitespace from right side. */
208 char *e = s + strlen(s);
209 while (e > s && ((c = e[-1]) == '\n' || c == '\r' || c == ' ' || c == '\t')) e--;
210 *e = '\0';
211
212 /* Skip comments and empty lines outside IGT_SEQUENCE groups. */
213 if ((group == nullptr || group->type != IGT_SEQUENCE) && (*s == '#' || *s == ';' || *s == '\0')) {
214 comment += std::string_view(s, e - s);
215 comment += '\n'; // comment newline
216 continue;
217 }
218
219 /* it's a group? */
220 if (s[0] == '[') {
221 if (e[-1] != ']') {
222 this->ReportFileError("ini: invalid group name '", buffer, "'");
223 } else {
224 e--;
225 }
226 s++; // skip [
227 group = &this->CreateGroup(std::string_view(s, e - s));
228 group->comment = std::move(comment);
229 comment.clear(); // std::move leaves comment in a "valid but unspecified state" according to the specification.
230 } else if (group != nullptr) {
231 if (group->type == IGT_SEQUENCE) {
232 /* A sequence group, use the line as item name without further interpretation. */
233 IniItem &item = group->CreateItem(std::string_view(buffer, e - buffer));
234 item.comment = std::move(comment);
235 comment.clear(); // std::move leaves comment in a "valid but unspecified state" according to the specification.
236 continue;
237 }
238 char *t;
239 /* find end of keyname */
240 if (*s == '\"') {
241 s++;
242 for (t = s; *t != '\0' && *t != '\"'; t++) {}
243 if (*t == '\"') *t = ' ';
244 } else {
245 for (t = s; *t != '\0' && *t != '=' && *t != '\t' && *t != ' '; t++) {}
246 }
247
248 /* it's an item in an existing group */
249 IniItem &item = group->CreateItem(std::string_view(s, t - s));
250 item.comment = std::move(comment);
251 comment.clear(); // std::move leaves comment in a "valid but unspecified state" according to the specification.
252
253 /* find start of parameter */
254 while (*t == '=' || *t == ' ' || *t == '\t') t++;
255
256 bool quoted = (*t == '\"');
257 /* remove starting quotation marks */
258 if (*t == '\"') t++;
259 /* remove ending quotation marks */
260 e = t + strlen(t);
261 if (e > t && e[-1] == '\"') e--;
262 *e = '\0';
263
264 /* If the value was not quoted and empty, it must be nullptr */
265 if (!quoted && e == t) {
266 item.value.reset();
267 } else {
268 item.value = StrMakeValid(std::string_view(t));
269 }
270 } else {
271 /* it's an orphan item */
272 this->ReportFileError("ini: '", buffer, "' outside of group");
273 }
274 }
275
276 this->comment = std::move(comment);
277}
278
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Types related to reading/writing '*.ini' files.
IniGroupType
Types of groups.
Definition ini_type.h:16
@ IGT_SEQUENCE
A list of uninterpreted lines, terminated by the next group block.
Definition ini_type.h:19
@ IGT_VARIABLES
Values of the form "landscape = hilly".
Definition ini_type.h:17
@ IGT_LIST
A list of values, separated by and terminated by the next group block.
Definition ini_type.h:18
Functions related to memory operations.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
Definition string.cpp:125
Functions related to low-level strings.
A group within an ini file.
Definition ini_type.h:34
const IniItem * GetItem(std::string_view name) const
Get the item with the given name.
Definition ini_load.cpp:51
std::string comment
comment for group
Definition ini_type.h:38
IniGroup(std::string_view name, IniGroupType type)
Construct a new in-memory group of an Ini file.
Definition ini_load.cpp:41
IniGroupType type
type of group
Definition ini_type.h:36
void Clear()
Clear all items in the group.
Definition ini_load.cpp:97
void RemoveItem(std::string_view name)
Remove the item with the given name.
Definition ini_load.cpp:89
std::string name
name of group
Definition ini_type.h:37
IniItem & CreateItem(std::string_view name)
Create an item with the given name.
Definition ini_load.cpp:80
IniItem & GetOrCreateItem(std::string_view name)
Get the item with the given name, and if it doesn't exist create a new item.
Definition ini_load.cpp:65
std::list< IniItem > items
all items in the group
Definition ini_type.h:35
A single "line" in an ini file.
Definition ini_type.h:23
std::optional< std::string > value
The value of this item.
Definition ini_type.h:25
std::string name
The name of this item.
Definition ini_type.h:24
std::string comment
The comment associated with this item.
Definition ini_type.h:26
IniItem(std::string_view name)
Construct a new in-memory item of an Ini file.
Definition ini_load.cpp:22
void SetValue(std::string_view value)
Replace the current value with another value.
Definition ini_load.cpp:31
std::list< IniGroup > groups
all groups in the ini
Definition ini_type.h:53
void RemoveGroup(std::string_view name)
Remove the group with the given name.
Definition ini_load.cpp:174
const IniGroupNameList seq_group_names
list of group names that are sequences.
Definition ini_type.h:56
void LoadFromDisk(const std::string &filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition ini_load.cpp:186
IniLoadFile(const IniGroupNameList &list_group_names={}, const IniGroupNameList &seq_group_names={})
Construct a new in-memory Ini file representation.
Definition ini_load.cpp:107
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
Definition ini_load.cpp:118
virtual void ReportFileError(const char *const pre, const char *const buffer, const char *const post)=0
Report an error about the file contents.
std::string comment
last comment in file
Definition ini_type.h:54
virtual std::optional< FileHandle > OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)=0
Open the INI file.
IniGroup & CreateGroup(std::string_view name)
Create an group with the given name.
Definition ini_load.cpp:161
const IniGroupNameList list_group_names
list of group names that are lists
Definition ini_type.h:55
IniGroup & GetOrCreateGroup(std::string_view name)
Get the group with the given name, and if it doesn't exist create a new group.
Definition ini_load.cpp:146