OpenTTD Source: src/ini_load.cpp Source File

OpenTTD Source 1.11.0-RC1

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 = str_validate(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 char *value)
42 {
43 if (value == nullptr) {
44 this->value.reset();
45 } else {
46 this->value.emplace(value);
47 }
48 }
49
55 IniGroup::IniGroup(IniLoadFile *parent, const std::string &name) : next(nullptr), type(IGT_VARIABLES), item(nullptr)
56 {
57 this->name = str_validate(name);
58
59 this->last_item = &this->item;
60 *parent->last_group = this;
61 parent->last_group = &this->next;
62
63 if (parent->list_group_names != nullptr) {
64 for (uint i = 0; parent->list_group_names[i] != nullptr; i++) {
65 if (this->name == parent->list_group_names[i]) {
66 this->type = IGT_LIST;
67 return;
68 }
69 }
70 }
71 if (parent->seq_group_names != nullptr) {
72 for (uint i = 0; parent->seq_group_names[i] != nullptr; i++) {
73 if (this->name == parent->seq_group_names[i]) {
74 this->type = IGT_SEQUENCE;
75 return;
76 }
77 }
78 }
79 }
80
82 IniGroup::~IniGroup()
83 {
84 delete this->item;
85 delete this->next;
86 }
87
95 IniItem *IniGroup::GetItem(const std::string &name, bool create)
96 {
97 for (IniItem *item = this->item; item != nullptr; item = item->next) {
98 if (item->name == name) return item;
99 }
100
101 if (!create) return nullptr;
102
103 /* otherwise make a new one */
104 return new IniItem(this, name);
105 }
106
110 void IniGroup::Clear()
111 {
112 delete this->item;
113 this->item = nullptr;
114 this->last_item = &this->item;
115 }
116
122 IniLoadFile::IniLoadFile(const char * const *list_group_names, const char * const *seq_group_names) :
123 group(nullptr),
124 list_group_names(list_group_names),
125 seq_group_names(seq_group_names)
126 {
127 this->last_group = &this->group;
128 }
129
131 IniLoadFile::~IniLoadFile()
132 {
133 delete this->group;
134 }
135
143 IniGroup *IniLoadFile::GetGroup(const std::string &name, bool create_new)
144 {
145 /* does it exist already? */
146 for (IniGroup *group = this->group; group != nullptr; group = group->next) {
147 if (group->name == name) return group;
148 }
149
150 if (!create_new) return nullptr;
151
152 /* otherwise make a new one */
153 IniGroup *group = new IniGroup(this, name);
154 group->comment = "\n";
155 return group;
156 }
157
162 void IniLoadFile::RemoveGroup(const char *name)
163 {
164 size_t len = strlen(name);
165 IniGroup *prev = nullptr;
166 IniGroup *group;
167
168 /* does it exist already? */
169 for (group = this->group; group != nullptr; prev = group, group = group->next) {
170 if (group->name.compare(0, len, name) == 0) {
171 break;
172 }
173 }
174
175 if (group == nullptr) return;
176
177 if (prev != nullptr) {
178 prev->next = prev->next->next;
179 if (this->last_group == &group->next) this->last_group = &prev->next;
180 } else {
181 this->group = this->group->next;
182 if (this->last_group == &group->next) this->last_group = &this->group;
183 }
184
185 group->next = nullptr;
186 delete group;
187 }
188
195 void IniLoadFile::LoadFromDisk(const std::string &filename, Subdirectory subdir)
196 {
197 assert(this->last_group == &this->group);
198
199 char buffer[1024];
200 IniGroup *group = nullptr;
201
202 char *comment = nullptr;
203 uint comment_size = 0;
204 uint comment_alloc = 0;
205
206 size_t end;
207 FILE *in = this->OpenFile(filename, subdir, &end);
208 if (in == nullptr) return;
209
210 end += ftell(in);
211
212 /* for each line in the file */
213 while ((size_t)ftell(in) < end && fgets(buffer, sizeof(buffer), in)) {
214 char c, *s;
215 /* trim whitespace from the left side */
216 for (s = buffer; *s == ' ' || *s == '\t'; s++) {}
217
218 /* trim whitespace from right side. */
219 char *e = s + strlen(s);
220 while (e > s && ((c = e[-1]) == '\n' || c == '\r' || c == ' ' || c == '\t')) e--;
221 *e = '\0';
222
223 /* Skip comments and empty lines outside IGT_SEQUENCE groups. */
224 if ((group == nullptr || group->type != IGT_SEQUENCE) && (*s == '#' || *s == ';' || *s == '\0')) {
225 uint ns = comment_size + (e - s + 1);
226 uint a = comment_alloc;
227 /* add to comment */
228 if (ns > a) {
229 a = std::max(a, 128U);
230 do a *= 2; while (a < ns);
231 comment = ReallocT(comment, comment_alloc = a);
232 }
233 uint pos = comment_size;
234 comment_size += (e - s + 1);
235 comment[pos + e - s] = '\n'; // comment newline
236 memcpy(comment + pos, s, e - s); // copy comment contents
237 continue;
238 }
239
240 /* it's a group? */
241 if (s[0] == '[') {
242 if (e[-1] != ']') {
243 this->ReportFileError("ini: invalid group name '", buffer, "'");
244 } else {
245 e--;
246 }
247 s++; // skip [
248 group = new IniGroup(this, std::string(s, e - s));
249 if (comment_size != 0) {
250 group->comment.assign(comment, comment_size);
251 comment_size = 0;
252 }
253 } else if (group != nullptr) {
254 if (group->type == IGT_SEQUENCE) {
255 /* A sequence group, use the line as item name without further interpretation. */
256 IniItem *item = new IniItem(group, std::string(buffer, e - buffer));
257 if (comment_size) {
258 item->comment.assign(comment, comment_size);
259 comment_size = 0;
260 }
261 continue;
262 }
263 char *t;
264 /* find end of keyname */
265 if (*s == '\"') {
266 s++;
267 for (t = s; *t != '\0' && *t != '\"'; t++) {}
268 if (*t == '\"') *t = ' ';
269 } else {
270 for (t = s; *t != '\0' && *t != '=' && *t != '\t' && *t != ' '; t++) {}
271 }
272
273 /* it's an item in an existing group */
274 IniItem *item = new IniItem(group, std::string(s, t - s));
275 if (comment_size != 0) {
276 item->comment.assign(comment, comment_size);
277 comment_size = 0;
278 }
279
280 /* find start of parameter */
281 while (*t == '=' || *t == ' ' || *t == '\t') t++;
282
283 bool quoted = (*t == '\"');
284 /* remove starting quotation marks */
285 if (*t == '\"') t++;
286 /* remove ending quotation marks */
287 e = t + strlen(t);
288 if (e > t && e[-1] == '\"') e--;
289 *e = '\0';
290
291 /* If the value was not quoted and empty, it must be nullptr */
292 if (!quoted && e == t) {
293 item->value.reset();
294 } else {
295 item->value = str_validate(std::string(t));
296 }
297 } else {
298 /* it's an orphan item */
299 this->ReportFileError("ini: '", buffer, "' outside of group");
300 }
301 }
302
303 if (comment_size > 0) {
304 this->comment.assign(comment, comment_size);
305 comment_size = 0;
306 }
307
308 free(comment);
309 fclose(in);
310 }
311
IniLoadFile::RemoveGroup
void RemoveGroup(const char *name)
Remove the group with the given name.
Definition: ini_load.cpp:162
IniLoadFile::~IniLoadFile
virtual ~IniLoadFile()
Free everything we loaded.
Definition: ini_load.cpp:131
str_validate
void str_validate(char *str, const char *last, StringValidationSettings settings)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
Definition: string.cpp:237
IniLoadFile::comment
std::string comment
last comment in file
Definition: ini_type.h:57
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::Clear
void Clear()
Clear all items in the group.
Definition: ini_load.cpp:110
IniLoadFile
Ini file that only supports loading.
Definition: ini_type.h:54
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:58
string_func.h
IniLoadFile::group
IniGroup * group
the first group in the ini
Definition: ini_type.h:55
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
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:82
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:56
IniItem::SetValue
void SetValue(const char *value)
Replace the current value with another value.
Definition: ini_load.cpp:41
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:122
IniLoadFile::seq_group_names
const char *const * seq_group_names
nullptr terminated list with group names that are sequences.
Definition: ini_type.h:59
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:95
IniGroup::IniGroup
IniGroup(struct IniLoadFile *parent, const std::string &name)
Construct a new in-memory group of an Ini file.
Definition: ini_load.cpp:55
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:454
IniLoadFile::LoadFromDisk
void LoadFromDisk(const std::string &filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition: ini_load.cpp:195
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:143
IniGroup::next
IniGroup * next
the next group within this file
Definition: ini_type.h:39