32 this->value.emplace(
value);
53 if (item.name ==
name)
return &item;
67 if (item.name ==
name)
return item;
120 if (group.name == name)
return &group;
134 if (group.name == name)
return &group;
148 if (group.name == name)
return group;
166 return this->
groups.emplace_back(name, type);
175 this->
groups.remove_if([&name](
const IniGroup &group) {
return group.
name.starts_with(name); });
186 assert(this->
groups.empty());
194 auto in = this->
OpenFile(filename, subdir, &end);
195 if (!in.has_value())
return;
201 while (
static_cast<size_t>(ftell(*in)) < end && fgets(buffer,
sizeof(buffer), *in)) {
221 }
else if (group !=
nullptr) {
230 static const std::string_view key_parameter_separators =
"=\t ";
231 std::string_view key;
250 if (value.ends_with(
"\"")) value.remove_suffix(1);
264 this->comment = std::move(
comment);
Parse data from a string / buffer.
bool ReadCharIf(char c)
Check whether the next 8-bit char matches 'c', and skip it.
std::string_view GetOrigData() const noexcept
Get the original data, as passed to the constructor.
std::string_view ReadUntilChar(char c, SeparatorUsage sep)
Read data until the first occurrence of 8-bit char 'c', and advance reader.
@ 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.
std::optional< char > PeekCharIfIn(std::string_view chars) const
Check whether the next 8-bit char is in 'chars'.
bool AnyBytesLeft() const noexcept
Check whether any bytes left to read.
static const std::string_view WHITESPACE_OR_NEWLINE
ASCII whitespace characters, including new-line.
void SkipUntilCharNotIn(std::string_view chars)
Skip 8-bit chars, while they are in 'chars', until they are not.
std::string_view ReadUntilCharIn(std::string_view chars)
Read 8-bit chars, while they are not in 'chars', until they are; and advance reader.
std::string_view GetLeftData() const noexcept
Get data left to read.
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.