OpenTTD Source: src/ini_load.cpp Source File

OpenTTD Source 13.1

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"
11 #include "core/alloc_func.hpp"
12 #include "core/mem_func.hpp"
13 #include "ini_type.h"
14 #include "string_func.h"
15
16 #include "safeguards.h"
17
23 IniItem::IniItem(IniGroup *parent, const std::string &name) : next(nullptr)
24 {
25 this->name = StrMakeValid(name);
26
27 *parent->last_item = this;
28 parent->last_item = &this->next;
29 }
30
32 IniItem::~IniItem()
33 {
34 delete this->next;
35 }
36
41 void IniItem::SetValue(const std::string_view value)
42 {
43 this->value.emplace(value);
44 }
45
51 IniGroup::IniGroup(IniLoadFile *parent, const std::string &name) : next(nullptr), type(IGT_VARIABLES), item(nullptr)
52 {
53 this->name = StrMakeValid(name);
54
55 this->last_item = &this->item;
56 *parent->last_group = this;
57 parent->last_group = &this->next;
58
59 if (parent->list_group_names != nullptr) {
60 for (uint i = 0; parent->list_group_names[i] != nullptr; i++) {
61 if (this->name == parent->list_group_names[i]) {
62 this->type = IGT_LIST;
63 return;
64 }
65 }
66 }
67 if (parent->seq_group_names != nullptr) {
68 for (uint i = 0; parent->seq_group_names[i] != nullptr; i++) {
69 if (this->name == parent->seq_group_names[i]) {
70 this->type = IGT_SEQUENCE;
71 return;
72 }
73 }
74 }
75 }
76
78 IniGroup::~IniGroup()
79 {
80 delete this->item;
81 delete this->next;
82 }
83
91 IniItem *IniGroup::GetItem(const std::string &name, bool create)
92 {
93 for (IniItem *item = this->item; item != nullptr; item = item->next) {
94 if (item->name == name) return item;
95 }
96
97 if (!create) return nullptr;
98
99 /* otherwise make a new one */
100 return new IniItem(this, name);
101 }
102
107 void IniGroup::RemoveItem(const std::string &name)
108 {
109 IniItem **prev = &this->item;
110
111 for (IniItem *item = this->item; item != nullptr; prev = &item->next, item = item->next) {
112 if (item->name != name) continue;
113
114 *prev = item->next;
115 /* "last_item" is a pointer to the "real-last-item"->next. */
116 if (this->last_item == &item->next) {
117 this->last_item = prev;
118 }
119
120 item->next = nullptr;
121 delete item;
122
123 return;
124 }
125 }
126
130 void IniGroup::Clear()
131 {
132 delete this->item;
133 this->item = nullptr;
134 this->last_item = &this->item;
135 }
136
142 IniLoadFile::IniLoadFile(const char * const *list_group_names, const char * const *seq_group_names) :
143 group(nullptr),
144 list_group_names(list_group_names),
145 seq_group_names(seq_group_names)
146 {
147 this->last_group = &this->group;
148 }
149
151 IniLoadFile::~IniLoadFile()
152 {
153 delete this->group;
154 }
155
163 IniGroup *IniLoadFile::GetGroup(const std::string &name, bool create_new)
164 {
165 /* does it exist already? */
166 for (IniGroup *group = this->group; group != nullptr; group = group->next) {
167 if (group->name == name) return group;
168 }
169
170 if (!create_new) return nullptr;
171
172 /* otherwise make a new one */
173 IniGroup *group = new IniGroup(this, name);
174 group->comment = "\n";
175 return group;
176 }
177
182 void IniLoadFile::RemoveGroup(const char *name)
183 {
184 size_t len = strlen(name);
185 IniGroup *prev = nullptr;
186 IniGroup *group;
187
188 /* does it exist already? */
189 for (group = this->group; group != nullptr; prev = group, group = group->next) {
190 if (group->name.compare(0, len, name) == 0) {
191 break;
192 }
193 }
194
195 if (group == nullptr) return;
196
197 if (prev != nullptr) {
198 prev->next = prev->next->next;
199 if (this->last_group == &group->next) this->last_group = &prev->next;
200 } else {
201 this->group = this->group->next;
202 if (this->last_group == &group->next) this->last_group = &this->group;
203 }
204
205 group->next = nullptr;
206 delete group;
207 }
208
215 void IniLoadFile::LoadFromDisk(const std::string &filename, Subdirectory subdir)
216 {
217 assert(this->last_group == &this->group);
218
219 char buffer[1024];
220 IniGroup *group = nullptr;
221
222 char *comment = nullptr;
223 uint comment_size = 0;
224 uint comment_alloc = 0;
225
226 size_t end;
227 FILE *in = this->OpenFile(filename, subdir, &end);
228 if (in == nullptr) return;
229
230 end += ftell(in);
231
232 /* for each line in the file */
233 while ((size_t)ftell(in) < end && fgets(buffer, sizeof(buffer), in)) {
234 char c, *s;
235 /* trim whitespace from the left side */
236 for (s = buffer; *s == ' ' || *s == '\t'; s++) {}
237
238 /* trim whitespace from right side. */
239 char *e = s + strlen(s);
240 while (e > s && ((c = e[-1]) == '\n' || c == '\r' || c == ' ' || c == '\t')) e--;
241 *e = '\0';
242
243 /* Skip comments and empty lines outside IGT_SEQUENCE groups. */
244 if ((group == nullptr || group->type != IGT_SEQUENCE) && (*s == '#' || *s == ';' || *s == '\0')) {
245 uint ns = comment_size + (e - s + 1);
246 uint a = comment_alloc;
247 /* add to comment */
248 if (ns > a) {
249 a = std::max(a, 128U);
250 do a *= 2; while (a < ns);
251 comment = ReallocT(comment, comment_alloc = a);
252 }
253 uint pos = comment_size;
254 comment_size += (e - s + 1);
255 comment[pos + e - s] = '\n'; // comment newline
256 memcpy(comment + pos, s, e - s); // copy comment contents
257 continue;
258 }
259
260 /* it's a group? */
261 if (s[0] == '[') {
262 if (e[-1] != ']') {
263 this->ReportFileError("ini: invalid group name '", buffer, "'");
264 } else {
265 e--;
266 }
267 s++; // skip [
268 group = new IniGroup(this, std::string(s, e - s));
269 if (comment_size != 0) {
270 group->comment.assign(comment, comment_size);
271 comment_size = 0;
272 }
273 } else if (group != nullptr) {
274 if (group->type == IGT_SEQUENCE) {
275 /* A sequence group, use the line as item name without further interpretation. */
276 IniItem *item = new IniItem(group, std::string(buffer, e - buffer));
277 if (comment_size) {
278 item->comment.assign(comment, comment_size);
279 comment_size = 0;
280 }
281 continue;
282 }
283 char *t;
284 /* find end of keyname */
285 if (*s == '\"') {
286 s++;
287 for (t = s; *t != '\0' && *t != '\"'; t++) {}
288 if (*t == '\"') *t = ' ';
289 } else {
290 for (t = s; *t != '\0' && *t != '=' && *t != '\t' && *t != ' '; t++) {}
291 }
292
293 /* it's an item in an existing group */
294 IniItem *item = new IniItem(group, std::string(s, t - s));
295 if (comment_size != 0) {
296 item->comment.assign(comment, comment_size);
297 comment_size = 0;
298 }
299
300 /* find start of parameter */
301 while (*t == '=' || *t == ' ' || *t == '\t') t++;
302
303 bool quoted = (*t == '\"');
304 /* remove starting quotation marks */
305 if (*t == '\"') t++;
306 /* remove ending quotation marks */
307 e = t + strlen(t);
308 if (e > t && e[-1] == '\"') e--;
309 *e = '\0';
310
311 /* If the value was not quoted and empty, it must be nullptr */
312 if (!quoted && e == t) {
313 item->value.reset();
314 } else {
315 item->value = StrMakeValid(std::string(t));
316 }
317 } else {
318 /* it's an orphan item */
319 this->ReportFileError("ini: '", buffer, "' outside of group");
320 }
321 }
322
323 if (comment_size > 0) {
324 this->comment.assign(comment, comment_size);
325 comment_size = 0;
326 }
327
328 free(comment);
329 fclose(in);
330 }
331
IniLoadFile::RemoveGroup
void RemoveGroup(const char *name)
Remove the group with the given name.
Definition: ini_load.cpp:182
IniLoadFile::~IniLoadFile
virtual ~IniLoadFile()
Free everything we loaded.
Definition: ini_load.cpp:151
IniItem::SetValue
void SetValue(const std::string_view value)
Replace the current value with another value.
Definition: ini_load.cpp:41
IniLoadFile::comment
std::string comment
last comment in file
Definition: ini_type.h:58
IniItem::next
IniItem * next
The next item in this group.
Definition: ini_type.h:26
mem_func.hpp
IGT_LIST
@ IGT_LIST
A list of values, separated by and terminated by the next group block.
Definition: ini_type.h:20
IniItem
A single "line" in an ini file.
Definition: ini_type.h:25
IniGroup
A group within an ini file.
Definition: ini_type.h:38
IniGroup::RemoveItem
void RemoveItem(const std::string &name)
Remove the item with the given name.
Definition: ini_load.cpp:107
IniGroup::Clear
void Clear()
Clear all items in the group.
Definition: ini_load.cpp:130
IniLoadFile
Ini file that only supports loading.
Definition: ini_type.h:55
IniLoadFile::OpenFile
virtual FILE * OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)=0
Open the INI file.
IniItem::value
std::optional< std::string > value
The value of this item.
Definition: ini_type.h:28
safeguards.h
IniGroup::type
IniGroupType type
type of group
Definition: ini_type.h:40
IGT_VARIABLES
@ IGT_VARIABLES
Values of the form "landscape = hilly".
Definition: ini_type.h:19
stdafx.h
IniGroup::last_item
IniItem ** last_item
the last item in the group
Definition: ini_type.h:42
IniLoadFile::list_group_names
const char *const * list_group_names
nullptr terminated list with group names that are lists
Definition: ini_type.h:59
string_func.h
IniLoadFile::group
IniGroup * group
the first group in the ini
Definition: ini_type.h:56
IniGroup::comment
std::string comment
comment for group
Definition: ini_type.h:44
IniGroup::name
std::string name
name of group
Definition: ini_type.h:43
StrMakeValid
std::string StrMakeValid(const std::string &str, StringValidationSettings settings)
Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).
Definition: string.cpp:299
IniItem::~IniItem
~IniItem()
Free everything we loaded.
Definition: ini_load.cpp:32
alloc_func.hpp
IniLoadFile::ReportFileError
virtual void ReportFileError(const char *const pre, const char *const buffer, const char *const post)=0
Report an error about the file contents.
IniGroup::~IniGroup
~IniGroup()
Free everything we loaded.
Definition: ini_load.cpp:78
IniItem::IniItem
IniItem(struct IniGroup *parent, const std::string &name)
Construct a new in-memory item of an Ini file.
Definition: ini_load.cpp:23
IGT_SEQUENCE
@ IGT_SEQUENCE
A list of uninterpreted lines, terminated by the next group block.
Definition: ini_type.h:21
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
IniLoadFile::last_group
IniGroup ** last_group
the last group in the ini
Definition: ini_type.h:57
ReallocT
static T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type.
Definition: alloc_func.hpp:111
IniItem::name
std::string name
The name of this item.
Definition: ini_type.h:27
IniGroup::item
IniItem * item
the first item in the group
Definition: ini_type.h:41
IniLoadFile::IniLoadFile
IniLoadFile(const char *const *list_group_names=nullptr, const char *const *seq_group_names=nullptr)
Construct a new in-memory Ini file representation.
Definition: ini_load.cpp:142
IniLoadFile::seq_group_names
const char *const * seq_group_names
nullptr terminated list with group names that are sequences.
Definition: ini_type.h:60
IniItem::comment
std::string comment
The comment associated with this item.
Definition: ini_type.h:29
IniGroup::GetItem
IniItem * GetItem(const std::string &name, bool create)
Get the item with the given name, and if it doesn't exist and create is true it creates a new item.
Definition: ini_load.cpp:91
IniGroup::IniGroup
IniGroup(struct IniLoadFile *parent, const std::string &name)
Construct a new in-memory group of an Ini file.
Definition: ini_load.cpp:51
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:470
IniLoadFile::LoadFromDisk
void LoadFromDisk(const std::string &filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition: ini_load.cpp:215
ini_type.h
IniLoadFile::GetGroup
IniGroup * GetGroup(const std::string &name, bool create_new=true)
Get the group with the given name.
Definition: ini_load.cpp:163
IniGroup::next
IniGroup * next
the next group within this file
Definition: ini_type.h:39