OpenTTD Source 20250529-master-g10c159a79f
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"
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(std::string_view 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 size_t line = 0;
202 /* for each line in the file */
203 while (static_cast<size_t>(ftell(*in)) < end && fgets(buffer, sizeof(buffer), *in)) {
204 ++line;
205 StringConsumer consumer{StrTrimView(buffer, StringConsumer::WHITESPACE_OR_NEWLINE)};
206
207 /* Skip comments and empty lines outside IGT_SEQUENCE groups. */
208 if ((group == nullptr || group->type != IGT_SEQUENCE) && (!consumer.AnyBytesLeft() || consumer.PeekCharIfIn("#;"))) {
209 comment += consumer.GetOrigData();
210 comment += "\n";
211 continue;
212 }
213
214 /* it's a group? */
215 if (consumer.ReadCharIf('[')) {
216 std::string_view group_name = consumer.ReadUntilChar(']', StringConsumer::KEEP_SEPARATOR);
217 if (!consumer.ReadCharIf(']') || consumer.AnyBytesLeft()) {
218 this->ReportFileError(fmt::format("ini [{}]: invalid group name '{}'", line, consumer.GetOrigData()));
219 }
220 group = &this->CreateGroup(group_name);
221 group->comment = std::move(comment);
222 comment.clear(); // std::move leaves comment in a "valid but unspecified state" according to the specification.
223 } else if (group != nullptr) {
224 if (group->type == IGT_SEQUENCE) {
225 /* A sequence group, use the line as item name without further interpretation. */
226 IniItem &item = group->CreateItem(consumer.GetOrigData());
227 item.comment = std::move(comment);
228 comment.clear(); // std::move leaves comment in a "valid but unspecified state" according to the specification.
229 continue;
230 }
231
232 static const std::string_view key_parameter_separators = "=\t ";
233 std::string_view key;
234 /* find end of keyname */
235 if (consumer.ReadCharIf('\"')) {
236 key = consumer.ReadUntilChar('\"', StringConsumer::SKIP_ONE_SEPARATOR);
237 } else {
238 key = consumer.ReadUntilCharIn(key_parameter_separators);
239 }
240
241 /* it's an item in an existing group */
242 IniItem &item = group->CreateItem(key);
243 item.comment = std::move(comment);
244 comment.clear(); // std::move leaves comment in a "valid but unspecified state" according to the specification.
245
246 /* find start of parameter */
247 consumer.SkipUntilCharNotIn(key_parameter_separators);
248
249 if (consumer.ReadCharIf('\"')) {
250 /* There is no escaping in our loader, so we just remove the first and last quote. */
251 std::string_view value = consumer.GetLeftData();
252 if (value.ends_with("\"")) value.remove_suffix(1);
253 item.value = StrMakeValid(value);
254 } else if (!consumer.AnyBytesLeft()) {
255 /* If the value was not quoted and empty, it must be nullptr */
256 item.value.reset();
257 } else {
258 item.value = StrMakeValid(consumer.GetLeftData());
259 }
260 } else {
261 /* it's an orphan item */
262 this->ReportFileError(fmt::format("ini [{}]: '{}' is outside of group", line, consumer.GetOrigData()));
263 }
264 }
265
266 this->comment = std::move(comment);
267}
268
Parse data from a string / buffer.
@ SKIP_ONE_SEPARATOR
Read and discard one separator, do not include it in the result.
@ KEEP_SEPARATOR
Keep the separator in the data as next value to be read.
static const std::string_view WHITESPACE_OR_NEWLINE
ASCII whitespace characters, including new-line.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:87
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
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Definition string.cpp:117
Parse strings.
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
virtual std::optional< FileHandle > OpenFile(std::string_view filename, Subdirectory subdir, size_t *size)=0
Open the INI file.
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(std::string_view message)=0
Report an error about the file contents.
std::string comment
last comment in file
Definition ini_type.h:54
void LoadFromDisk(std::string_view filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition ini_load.cpp:186
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