33 this->value.emplace(
value);
54 if (item.name ==
name)
return &item;
68 if (item.name ==
name)
return item;
82 return this->
items.emplace_back(name);
108 list_group_names(list_group_names),
109 seq_group_names(seq_group_names)
121 if (group.name == name)
return &group;
135 if (group.name == name)
return &group;
149 if (group.name == name)
return group;
167 return this->
groups.emplace_back(name, type);
176 size_t len = name.length();
177 this->
groups.remove_if([&name, &len](
const IniGroup &group) {
return group.
name.compare(0, len, name) == 0; });
188 assert(this->
groups.empty());
196 auto in = this->
OpenFile(filename, subdir, &end);
197 if (!in.has_value())
return;
203 while (
static_cast<size_t>(ftell(*in)) < end && fgets(buffer,
sizeof(buffer), *in)) {
208 if ((group ==
nullptr || group->
type !=
IGT_SEQUENCE) && (!consumer.AnyBytesLeft() || consumer.PeekCharIfIn(
"#;"))) {
209 comment += consumer.GetOrigData();
215 if (consumer.ReadCharIf(
'[')) {
217 if (!consumer.ReadCharIf(
']') || consumer.AnyBytesLeft()) {
218 this->
ReportFileError(fmt::format(
"ini [{}]: invalid group name '{}'", line, consumer.GetOrigData()));
223 }
else if (group !=
nullptr) {
232 static const std::string_view key_parameter_separators =
"=\t ";
233 std::string_view key;
235 if (consumer.ReadCharIf(
'\"')) {
238 key = consumer.ReadUntilCharIn(key_parameter_separators);
247 consumer.SkipUntilCharNotIn(key_parameter_separators);
249 if (consumer.ReadCharIf(
'\"')) {
251 std::string_view value = consumer.GetLeftData();
252 if (value.ends_with(
"\"")) value.remove_suffix(1);
254 }
else if (!consumer.AnyBytesLeft()) {
262 this->
ReportFileError(fmt::format(
"ini [{}]: '{}' is outside of group", line, consumer.GetOrigData()));
266 this->comment = std::move(
comment);
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.
Types related to reading/writing '*.ini' files.
IniGroupType
Types of groups.
@ IGT_SEQUENCE
A list of uninterpreted lines, terminated by the next group block.
@ IGT_VARIABLES
Values of the form "landscape = hilly".
@ IGT_LIST
A list of values, separated by and terminated by the next group block.
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.
Functions related to low-level strings.
A group within an ini file.
const IniItem * GetItem(std::string_view name) const
Get the item with the given name.
std::string comment
comment for group
IniGroup(std::string_view name, IniGroupType type)
Construct a new in-memory group of an Ini file.
IniGroupType type
type of group
void Clear()
Clear all items in the group.
void RemoveItem(std::string_view name)
Remove the item with the given name.
std::string name
name of group
IniItem & CreateItem(std::string_view name)
Create an item with the given name.
IniItem & GetOrCreateItem(std::string_view name)
Get the item with the given name, and if it doesn't exist create a new item.
std::list< IniItem > items
all items in the group
A single "line" in an ini file.
std::optional< std::string > value
The value of this item.
std::string name
The name of this item.
std::string comment
The comment associated with this item.
IniItem(std::string_view name)
Construct a new in-memory item of an Ini file.
void SetValue(std::string_view value)
Replace the current value with another value.
std::list< IniGroup > groups
all groups in the ini
void RemoveGroup(std::string_view name)
Remove the group with the given name.
const IniGroupNameList seq_group_names
list of group names that are sequences.
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.
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
virtual void ReportFileError(std::string_view message)=0
Report an error about the file contents.
std::string comment
last comment in file
void LoadFromDisk(std::string_view filename, Subdirectory subdir)
Load the Ini file's data from the disk.
IniGroup & CreateGroup(std::string_view name)
Create an group with the given name.
const IniGroupNameList list_group_names
list of group names that are lists
IniGroup & GetOrCreateGroup(std::string_view name)
Get the group with the given name, and if it doesn't exist create a new group.