Handle invalid strings from game scripts more leniently
[openttd/fttd.git] / src / ini_load.cpp
blob6a208730380ff102b0853de89d1fbae45abaad22
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file ini_load.cpp Definition of the #IniLoadFile class, related to reading and storing '*.ini' files. */
12 #include "stdafx.h"
13 #include "core/alloc_func.hpp"
14 #include "core/mem_func.hpp"
15 #include "ini_type.h"
16 #include "string_func.h"
18 /**
19 * Construct a new in-memory item of an Ini file.
20 * @param parent the group we belong to
21 * @param name the name of the item
22 * @param len the length of the name of the item
24 IniItem::IniItem(IniGroup *parent, const char *name, size_t len) : next(NULL), value(NULL), comment(NULL)
26 if (len == 0) len = strlen(name);
28 this->name = strndup(name, len);
29 *parent->last_item = this;
30 parent->last_item = &this->next;
33 /** Free everything we loaded. */
34 IniItem::~IniItem()
36 free(this->name);
37 free(this->value);
38 free(this->comment);
40 delete this->next;
43 /**
44 * Replace the current value with another value.
45 * @param value the value to replace with.
47 void IniItem::SetValue(const char *value)
49 free(this->value);
50 this->value = strdup(value);
53 /**
54 * Construct a new in-memory group of an Ini file.
55 * @param parent the file we belong to
56 * @param name the name of the group
57 * @param len the length of the name of the group
59 IniGroup::IniGroup(IniLoadFile *parent, const char *name, size_t len) : next(NULL), type(IGT_VARIABLES), item(NULL), comment(NULL)
61 if (len == 0) len = strlen(name);
63 this->name = strndup(name, len);
64 this->last_item = &this->item;
65 *parent->last_group = this;
66 parent->last_group = &this->next;
68 if (parent->list_group_names != NULL) {
69 for (uint i = 0; parent->list_group_names[i] != NULL; i++) {
70 if (strcmp(this->name, parent->list_group_names[i]) == 0) {
71 this->type = IGT_LIST;
72 return;
76 if (parent->seq_group_names != NULL) {
77 for (uint i = 0; parent->seq_group_names[i] != NULL; i++) {
78 if (strcmp(this->name, parent->seq_group_names[i]) == 0) {
79 this->type = IGT_SEQUENCE;
80 return;
86 /** Free everything we loaded. */
87 IniGroup::~IniGroup()
89 free(this->name);
90 free(this->comment);
92 delete this->item;
93 delete this->next;
96 /**
97 * Get the item with the given name, and if it doesn't exist
98 * and create is true it creates a new item.
99 * @param name name of the item to find.
100 * @param create whether to create an item when not found or not.
101 * @return the requested item or NULL if not found.
103 IniItem *IniGroup::GetItem(const char *name, bool create)
105 for (IniItem *item = this->item; item != NULL; item = item->next) {
106 if (strcmp(item->name, name) == 0) return item;
109 if (!create) return NULL;
111 /* otherwise make a new one */
112 return new IniItem(this, name, strlen(name));
116 * Clear all items in the group
118 void IniGroup::Clear()
120 delete this->item;
121 this->item = NULL;
122 this->last_item = &this->item;
126 * Construct a new in-memory Ini file representation.
127 * @param list_group_names A \c NULL terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST
128 * @param seq_group_names A \c NULL terminated list with group names that should be loaded as lists of names. @see IGT_SEQUENCE
130 IniLoadFile::IniLoadFile(const char * const *list_group_names, const char * const *seq_group_names) :
131 group(NULL),
132 comment(NULL),
133 list_group_names(list_group_names),
134 seq_group_names(seq_group_names)
136 this->last_group = &this->group;
139 /** Free everything we loaded. */
140 IniLoadFile::~IniLoadFile()
142 free(this->comment);
143 delete this->group;
147 * Get the group with the given name. If it doesn't exist
148 * and \a create_new is \c true create a new group.
149 * @param name name of the group to find.
150 * @param len the maximum length of said name (\c 0 means length of the string).
151 * @param create_new Allow creation of group if it does not exist.
152 * @return The requested group if it exists or was created, else \c NULL.
154 IniGroup *IniLoadFile::GetGroup(const char *name, size_t len, bool create_new)
156 if (len == 0) len = strlen(name);
158 /* does it exist already? */
159 for (IniGroup *group = this->group; group != NULL; group = group->next) {
160 if (!strncmp(group->name, name, len) && group->name[len] == 0) {
161 return group;
165 if (!create_new) return NULL;
167 /* otherwise make a new one */
168 IniGroup *group = new IniGroup(this, name, len);
169 group->comment = strdup("\n");
170 return group;
174 * Remove the group with the given name.
175 * @param name name of the group to remove.
177 void IniLoadFile::RemoveGroup(const char *name)
179 size_t len = strlen(name);
180 IniGroup *prev = NULL;
181 IniGroup *group;
183 /* does it exist already? */
184 for (group = this->group; group != NULL; prev = group, group = group->next) {
185 if (strncmp(group->name, name, len) == 0) {
186 break;
190 if (group == NULL) return;
192 if (prev != NULL) {
193 prev->next = prev->next->next;
194 if (this->last_group == &group->next) this->last_group = &prev->next;
195 } else {
196 this->group = this->group->next;
197 if (this->last_group == &group->next) this->last_group = &this->group;
200 group->next = NULL;
201 delete group;
205 * Load the Ini file's data from the disk.
206 * @param filename the file to load.
207 * @param subdir the sub directory to load the file from.
208 * @pre nothing has been loaded yet.
210 void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir)
212 assert(this->last_group == &this->group);
214 char buffer[1024];
215 IniGroup *group = NULL;
217 char *comment = NULL;
218 uint comment_size = 0;
219 uint comment_alloc = 0;
221 size_t end;
222 FILE *in = this->OpenFile(filename, subdir, &end);
223 if (in == NULL) return;
225 end += ftell(in);
227 /* for each line in the file */
228 while ((size_t)ftell(in) < end && fgets(buffer, sizeof(buffer), in)) {
229 char c, *s;
230 /* trim whitespace from the left side */
231 for (s = buffer; *s == ' ' || *s == '\t'; s++) {}
233 /* trim whitespace from right side. */
234 char *e = s + strlen(s);
235 while (e > s && ((c = e[-1]) == '\n' || c == '\r' || c == ' ' || c == '\t')) e--;
236 *e = '\0';
238 /* Skip comments and empty lines outside IGT_SEQUENCE groups. */
239 if ((group == NULL || group->type != IGT_SEQUENCE) && (*s == '#' || *s == ';' || *s == '\0')) {
240 uint ns = comment_size + (e - s + 1);
241 uint a = comment_alloc;
242 /* add to comment */
243 if (ns > a) {
244 a = max(a, 128U);
245 do a *= 2; while (a < ns);
246 comment = ReallocT(comment, comment_alloc = a);
248 uint pos = comment_size;
249 comment_size += (e - s + 1);
250 comment[pos + e - s] = '\n'; // comment newline
251 memcpy(comment + pos, s, e - s); // copy comment contents
252 continue;
255 /* it's a group? */
256 if (s[0] == '[') {
257 if (e[-1] != ']') {
258 this->ReportFileError("ini: invalid group name '", buffer, "'");
259 } else {
260 e--;
262 s++; // skip [
263 group = new IniGroup(this, s, e - s);
264 if (comment_size != 0) {
265 group->comment = strndup(comment, comment_size);
266 comment_size = 0;
268 } else if (group != NULL) {
269 if (group->type == IGT_SEQUENCE) {
270 /* A sequence group, use the line as item name without further interpretation. */
271 IniItem *item = new IniItem(group, buffer, e - buffer);
272 if (comment_size) {
273 item->comment = strndup(comment, comment_size);
274 comment_size = 0;
276 continue;
278 char *t;
279 /* find end of keyname */
280 if (*s == '\"') {
281 s++;
282 for (t = s; *t != '\0' && *t != '\"'; t++) {}
283 if (*t == '\"') *t = ' ';
284 } else {
285 for (t = s; *t != '\0' && *t != '=' && *t != '\t' && *t != ' '; t++) {}
288 /* it's an item in an existing group */
289 IniItem *item = new IniItem(group, s, t - s);
290 if (comment_size != 0) {
291 item->comment = strndup(comment, comment_size);
292 comment_size = 0;
295 /* find start of parameter */
296 while (*t == '=' || *t == ' ' || *t == '\t') t++;
298 bool quoted = (*t == '\"');
299 /* remove starting quotation marks */
300 if (*t == '\"') t++;
301 /* remove ending quotation marks */
302 e = t + strlen(t);
303 if (e > t && e[-1] == '\"') e--;
304 *e = '\0';
306 /* If the value was not quoted and empty, it must be NULL */
307 item->value = (!quoted && e == t) ? NULL : strndup(t, e - t);
308 } else {
309 /* it's an orphan item */
310 this->ReportFileError("ini: '", buffer, "' outside of group");
314 if (comment_size > 0) {
315 this->comment = strndup(comment, comment_size);
316 comment_size = 0;
319 free(comment);
320 fclose(in);