2011-03-03 20:50:24 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file ini_load.cpp Definition of the #IniLoadFile class, related to reading and storing '*.ini' files. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "core/alloc_func.hpp"
|
|
|
|
#include "core/mem_func.hpp"
|
|
|
|
#include "ini_type.h"
|
2012-01-03 21:47:01 +00:00
|
|
|
#include "string_func.h"
|
2011-03-03 20:50:24 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2011-03-03 20:50:24 +00:00
|
|
|
/**
|
|
|
|
* Construct a new in-memory item of an Ini file.
|
|
|
|
* @param parent the group we belong to
|
|
|
|
* @param name the name of the item
|
|
|
|
*/
|
2020-05-17 21:32:03 +00:00
|
|
|
IniItem::IniItem(IniGroup *parent, const std::string &name) : next(nullptr)
|
2011-03-03 20:50:24 +00:00
|
|
|
{
|
2021-05-29 09:21:38 +00:00
|
|
|
this->name = StrMakeValid(name);
|
2014-01-02 17:55:57 +00:00
|
|
|
|
2011-03-03 20:50:24 +00:00
|
|
|
*parent->last_item = this;
|
|
|
|
parent->last_item = &this->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Free everything we loaded. */
|
|
|
|
IniItem::~IniItem()
|
|
|
|
{
|
|
|
|
delete this->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the current value with another value.
|
|
|
|
* @param value the value to replace with.
|
|
|
|
*/
|
2021-04-29 17:04:27 +00:00
|
|
|
void IniItem::SetValue(const std::string_view value)
|
2011-03-03 20:50:24 +00:00
|
|
|
{
|
2021-04-29 17:04:27 +00:00
|
|
|
this->value.emplace(value);
|
2011-03-03 20:50:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new in-memory group of an Ini file.
|
|
|
|
* @param parent the file we belong to
|
|
|
|
* @param name the name of the group
|
|
|
|
*/
|
2020-05-17 21:32:03 +00:00
|
|
|
IniGroup::IniGroup(IniLoadFile *parent, const std::string &name) : next(nullptr), type(IGT_VARIABLES), item(nullptr)
|
2011-03-03 20:50:24 +00:00
|
|
|
{
|
2021-05-29 09:21:38 +00:00
|
|
|
this->name = StrMakeValid(name);
|
2014-01-02 17:55:57 +00:00
|
|
|
|
2011-03-03 20:50:24 +00:00
|
|
|
this->last_item = &this->item;
|
|
|
|
*parent->last_group = this;
|
|
|
|
parent->last_group = &this->next;
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (parent->list_group_names != nullptr) {
|
|
|
|
for (uint i = 0; parent->list_group_names[i] != nullptr; i++) {
|
2020-05-17 21:32:03 +00:00
|
|
|
if (this->name == parent->list_group_names[i]) {
|
2011-03-03 20:56:33 +00:00
|
|
|
this->type = IGT_LIST;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 21:07:06 +00:00
|
|
|
if (parent->seq_group_names != nullptr) {
|
|
|
|
for (uint i = 0; parent->seq_group_names[i] != nullptr; i++) {
|
2020-05-17 21:32:03 +00:00
|
|
|
if (this->name == parent->seq_group_names[i]) {
|
2011-03-03 20:56:33 +00:00
|
|
|
this->type = IGT_SEQUENCE;
|
|
|
|
return;
|
|
|
|
}
|
2011-03-03 20:50:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Free everything we loaded. */
|
|
|
|
IniGroup::~IniGroup()
|
|
|
|
{
|
|
|
|
delete this->item;
|
|
|
|
delete this->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the item with the given name, and if it doesn't exist
|
|
|
|
* and create is true it creates a new item.
|
|
|
|
* @param name name of the item to find.
|
|
|
|
* @param create whether to create an item when not found or not.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return the requested item or nullptr if not found.
|
2011-03-03 20:50:24 +00:00
|
|
|
*/
|
2020-05-17 21:32:03 +00:00
|
|
|
IniItem *IniGroup::GetItem(const std::string &name, bool create)
|
2011-03-03 20:50:24 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
for (IniItem *item = this->item; item != nullptr; item = item->next) {
|
2020-05-17 21:32:03 +00:00
|
|
|
if (item->name == name) return item;
|
2011-03-03 20:50:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (!create) return nullptr;
|
2011-03-03 20:50:24 +00:00
|
|
|
|
|
|
|
/* otherwise make a new one */
|
2020-05-17 21:32:03 +00:00
|
|
|
return new IniItem(this, name);
|
2011-03-03 20:50:24 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 14:39:48 +00:00
|
|
|
/**
|
|
|
|
* Remove the item with the given name.
|
|
|
|
* @param name Name of the item to remove.
|
|
|
|
*/
|
|
|
|
void IniGroup::RemoveItem(const std::string &name)
|
|
|
|
{
|
|
|
|
IniItem **prev = &this->item;
|
|
|
|
|
|
|
|
for (IniItem *item = this->item; item != nullptr; prev = &item->next, item = item->next) {
|
|
|
|
if (item->name != name) continue;
|
|
|
|
|
|
|
|
*prev = item->next;
|
2021-07-17 15:26:50 +00:00
|
|
|
/* "last_item" is a pointer to the "real-last-item"->next. */
|
|
|
|
if (this->last_item == &item->next) {
|
|
|
|
this->last_item = prev;
|
2021-06-28 14:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
item->next = nullptr;
|
|
|
|
delete item;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-03 20:50:24 +00:00
|
|
|
/**
|
|
|
|
* Clear all items in the group
|
|
|
|
*/
|
|
|
|
void IniGroup::Clear()
|
|
|
|
{
|
|
|
|
delete this->item;
|
2019-04-10 21:07:06 +00:00
|
|
|
this->item = nullptr;
|
2011-03-03 20:50:24 +00:00
|
|
|
this->last_item = &this->item;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new in-memory Ini file representation.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param list_group_names A \c nullptr terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST
|
|
|
|
* @param seq_group_names A \c nullptr terminated list with group names that should be loaded as lists of names. @see IGT_SEQUENCE
|
2011-03-03 20:50:24 +00:00
|
|
|
*/
|
2011-03-03 20:56:33 +00:00
|
|
|
IniLoadFile::IniLoadFile(const char * const *list_group_names, const char * const *seq_group_names) :
|
2019-04-10 21:07:06 +00:00
|
|
|
group(nullptr),
|
2011-03-03 20:56:33 +00:00
|
|
|
list_group_names(list_group_names),
|
|
|
|
seq_group_names(seq_group_names)
|
2011-03-03 20:50:24 +00:00
|
|
|
{
|
|
|
|
this->last_group = &this->group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Free everything we loaded. */
|
|
|
|
IniLoadFile::~IniLoadFile()
|
|
|
|
{
|
|
|
|
delete this->group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-03 20:55:06 +00:00
|
|
|
* Get the group with the given name. If it doesn't exist
|
|
|
|
* and \a create_new is \c true create a new group.
|
2011-03-03 20:50:24 +00:00
|
|
|
* @param name name of the group to find.
|
2011-03-03 20:55:06 +00:00
|
|
|
* @param create_new Allow creation of group if it does not exist.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return The requested group if it exists or was created, else \c nullptr.
|
2011-03-03 20:50:24 +00:00
|
|
|
*/
|
2020-05-17 21:32:03 +00:00
|
|
|
IniGroup *IniLoadFile::GetGroup(const std::string &name, bool create_new)
|
2011-03-03 20:50:24 +00:00
|
|
|
{
|
|
|
|
/* does it exist already? */
|
2019-04-10 21:07:06 +00:00
|
|
|
for (IniGroup *group = this->group; group != nullptr; group = group->next) {
|
2020-05-17 21:32:03 +00:00
|
|
|
if (group->name == name) return group;
|
2011-03-03 20:50:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (!create_new) return nullptr;
|
2011-03-03 20:55:06 +00:00
|
|
|
|
2011-03-03 20:50:24 +00:00
|
|
|
/* otherwise make a new one */
|
2020-05-17 21:32:03 +00:00
|
|
|
IniGroup *group = new IniGroup(this, name);
|
|
|
|
group->comment = "\n";
|
2011-03-03 20:50:24 +00:00
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the group with the given name.
|
|
|
|
* @param name name of the group to remove.
|
|
|
|
*/
|
|
|
|
void IniLoadFile::RemoveGroup(const char *name)
|
|
|
|
{
|
|
|
|
size_t len = strlen(name);
|
2019-04-10 21:07:06 +00:00
|
|
|
IniGroup *prev = nullptr;
|
2011-03-03 20:50:24 +00:00
|
|
|
IniGroup *group;
|
|
|
|
|
|
|
|
/* does it exist already? */
|
2019-04-10 21:07:06 +00:00
|
|
|
for (group = this->group; group != nullptr; prev = group, group = group->next) {
|
2020-05-17 21:32:03 +00:00
|
|
|
if (group->name.compare(0, len, name) == 0) {
|
2011-03-03 20:50:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (group == nullptr) return;
|
2011-03-03 20:50:24 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (prev != nullptr) {
|
2011-03-03 20:50:24 +00:00
|
|
|
prev->next = prev->next->next;
|
|
|
|
if (this->last_group == &group->next) this->last_group = &prev->next;
|
|
|
|
} else {
|
|
|
|
this->group = this->group->next;
|
|
|
|
if (this->last_group == &group->next) this->last_group = &this->group;
|
|
|
|
}
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
group->next = nullptr;
|
2011-03-03 20:50:24 +00:00
|
|
|
delete group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the Ini file's data from the disk.
|
|
|
|
* @param filename the file to load.
|
2011-08-24 13:38:26 +00:00
|
|
|
* @param subdir the sub directory to load the file from.
|
2011-03-03 20:50:24 +00:00
|
|
|
* @pre nothing has been loaded yet.
|
|
|
|
*/
|
2020-12-06 20:11:42 +00:00
|
|
|
void IniLoadFile::LoadFromDisk(const std::string &filename, Subdirectory subdir)
|
2011-03-03 20:50:24 +00:00
|
|
|
{
|
|
|
|
assert(this->last_group == &this->group);
|
|
|
|
|
|
|
|
char buffer[1024];
|
2019-04-10 21:07:06 +00:00
|
|
|
IniGroup *group = nullptr;
|
2011-03-03 20:50:24 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
char *comment = nullptr;
|
2011-03-03 20:50:24 +00:00
|
|
|
uint comment_size = 0;
|
|
|
|
uint comment_alloc = 0;
|
|
|
|
|
|
|
|
size_t end;
|
2020-12-06 20:11:49 +00:00
|
|
|
FILE *in = this->OpenFile(filename, subdir, &end);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (in == nullptr) return;
|
2011-03-03 20:50:24 +00:00
|
|
|
|
|
|
|
end += ftell(in);
|
|
|
|
|
|
|
|
/* for each line in the file */
|
|
|
|
while ((size_t)ftell(in) < end && fgets(buffer, sizeof(buffer), in)) {
|
|
|
|
char c, *s;
|
|
|
|
/* trim whitespace from the left side */
|
|
|
|
for (s = buffer; *s == ' ' || *s == '\t'; s++) {}
|
|
|
|
|
|
|
|
/* trim whitespace from right side. */
|
|
|
|
char *e = s + strlen(s);
|
|
|
|
while (e > s && ((c = e[-1]) == '\n' || c == '\r' || c == ' ' || c == '\t')) e--;
|
|
|
|
*e = '\0';
|
|
|
|
|
2011-03-03 20:56:33 +00:00
|
|
|
/* Skip comments and empty lines outside IGT_SEQUENCE groups. */
|
2019-04-10 21:07:06 +00:00
|
|
|
if ((group == nullptr || group->type != IGT_SEQUENCE) && (*s == '#' || *s == ';' || *s == '\0')) {
|
2011-03-03 20:50:24 +00:00
|
|
|
uint ns = comment_size + (e - s + 1);
|
|
|
|
uint a = comment_alloc;
|
|
|
|
/* add to comment */
|
|
|
|
if (ns > a) {
|
2021-01-08 10:16:18 +00:00
|
|
|
a = std::max(a, 128U);
|
2011-03-03 20:50:24 +00:00
|
|
|
do a *= 2; while (a < ns);
|
|
|
|
comment = ReallocT(comment, comment_alloc = a);
|
|
|
|
}
|
|
|
|
uint pos = comment_size;
|
|
|
|
comment_size += (e - s + 1);
|
|
|
|
comment[pos + e - s] = '\n'; // comment newline
|
|
|
|
memcpy(comment + pos, s, e - s); // copy comment contents
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it's a group? */
|
|
|
|
if (s[0] == '[') {
|
|
|
|
if (e[-1] != ']') {
|
2011-03-03 20:53:09 +00:00
|
|
|
this->ReportFileError("ini: invalid group name '", buffer, "'");
|
2011-03-03 20:50:24 +00:00
|
|
|
} else {
|
|
|
|
e--;
|
|
|
|
}
|
|
|
|
s++; // skip [
|
2020-05-17 21:32:03 +00:00
|
|
|
group = new IniGroup(this, std::string(s, e - s));
|
2011-03-03 20:50:24 +00:00
|
|
|
if (comment_size != 0) {
|
2020-05-17 21:32:03 +00:00
|
|
|
group->comment.assign(comment, comment_size);
|
2011-03-03 20:50:24 +00:00
|
|
|
comment_size = 0;
|
|
|
|
}
|
2019-04-10 21:07:06 +00:00
|
|
|
} else if (group != nullptr) {
|
2011-03-03 20:56:33 +00:00
|
|
|
if (group->type == IGT_SEQUENCE) {
|
|
|
|
/* A sequence group, use the line as item name without further interpretation. */
|
2020-05-17 21:32:03 +00:00
|
|
|
IniItem *item = new IniItem(group, std::string(buffer, e - buffer));
|
2011-03-03 20:56:33 +00:00
|
|
|
if (comment_size) {
|
2020-05-17 21:32:03 +00:00
|
|
|
item->comment.assign(comment, comment_size);
|
2011-03-03 20:56:33 +00:00
|
|
|
comment_size = 0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2011-03-03 20:50:24 +00:00
|
|
|
char *t;
|
|
|
|
/* find end of keyname */
|
|
|
|
if (*s == '\"') {
|
|
|
|
s++;
|
|
|
|
for (t = s; *t != '\0' && *t != '\"'; t++) {}
|
|
|
|
if (*t == '\"') *t = ' ';
|
|
|
|
} else {
|
|
|
|
for (t = s; *t != '\0' && *t != '=' && *t != '\t' && *t != ' '; t++) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it's an item in an existing group */
|
2020-05-17 21:32:03 +00:00
|
|
|
IniItem *item = new IniItem(group, std::string(s, t - s));
|
2011-03-03 20:50:24 +00:00
|
|
|
if (comment_size != 0) {
|
2020-05-17 21:32:03 +00:00
|
|
|
item->comment.assign(comment, comment_size);
|
2011-03-03 20:50:24 +00:00
|
|
|
comment_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find start of parameter */
|
|
|
|
while (*t == '=' || *t == ' ' || *t == '\t') t++;
|
|
|
|
|
|
|
|
bool quoted = (*t == '\"');
|
|
|
|
/* remove starting quotation marks */
|
|
|
|
if (*t == '\"') t++;
|
|
|
|
/* remove ending quotation marks */
|
|
|
|
e = t + strlen(t);
|
|
|
|
if (e > t && e[-1] == '\"') e--;
|
|
|
|
*e = '\0';
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
/* If the value was not quoted and empty, it must be nullptr */
|
2020-05-17 21:32:03 +00:00
|
|
|
if (!quoted && e == t) {
|
|
|
|
item->value.reset();
|
|
|
|
} else {
|
2021-05-29 09:21:38 +00:00
|
|
|
item->value = StrMakeValid(std::string(t));
|
2020-05-17 21:32:03 +00:00
|
|
|
}
|
2011-03-03 20:50:24 +00:00
|
|
|
} else {
|
|
|
|
/* it's an orphan item */
|
2011-03-03 20:53:09 +00:00
|
|
|
this->ReportFileError("ini: '", buffer, "' outside of group");
|
2011-03-03 20:50:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comment_size > 0) {
|
2020-05-17 21:32:03 +00:00
|
|
|
this->comment.assign(comment, comment_size);
|
2011-03-03 20:50:24 +00:00
|
|
|
comment_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(comment);
|
|
|
|
fclose(in);
|
|
|
|
}
|
|
|
|
|