34 this->value.emplace(
value);
55 if (item.name ==
name)
return &item;
69 if (item.name ==
name)
return item;
83 return this->
items.emplace_back(name);
109 list_group_names(list_group_names),
110 seq_group_names(seq_group_names)
122 if (group.name == name)
return &group;
136 if (group.name == name)
return &group;
150 if (group.name == name)
return group;
165 if (std::find(this->
list_group_names.begin(), this->list_group_names.end(), name) != this->list_group_names.end()) type =
IGT_LIST;
166 if (std::find(this->
seq_group_names.begin(), this->seq_group_names.end(), name) != this->seq_group_names.end()) type =
IGT_SEQUENCE;
168 return this->
groups.emplace_back(name, type);
177 size_t len = name.length();
178 this->
groups.remove_if([&name, &len](
const IniGroup &group) {
return group.
name.compare(0, len, name) == 0; });
189 assert(this->
groups.empty());
197 auto in = this->
OpenFile(filename, subdir, &end);
198 if (!in.has_value())
return;
203 while (
static_cast<size_t>(ftell(*in)) < end && fgets(buffer,
sizeof(buffer), *in)) {
206 for (s = buffer; *s ==
' ' || *s ==
'\t'; s++) {}
209 char *e = s + strlen(s);
210 while (e > s && ((c = e[-1]) ==
'\n' || c ==
'\r' || c ==
' ' || c ==
'\t')) e--;
214 if ((group ==
nullptr || group->
type !=
IGT_SEQUENCE) && (*s ==
'#' || *s ==
';' || *s ==
'\0')) {
215 comment += std::string_view(s, e - s);
228 group = &this->
CreateGroup(std::string_view(s, e - s));
230 }
else if (group !=
nullptr) {
241 for (t = s; *t !=
'\0' && *t !=
'\"'; t++) {}
242 if (*t ==
'\"') *t =
' ';
244 for (t = s; *t !=
'\0' && *t !=
'=' && *t !=
'\t' && *t !=
' '; t++) {}
252 while (*t ==
'=' || *t ==
' ' || *t ==
'\t') t++;
254 bool quoted = (*t ==
'\"');
259 if (e > t && e[-1] ==
'\"') e--;
263 if (!quoted && e == t) {
274 this->comment = std::move(
comment);
Functions related to the allocation of memory.
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.
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.
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.
void LoadFromDisk(const std::string &filename, Subdirectory subdir)
Load the Ini file's data from the disk.
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(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
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.
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.