Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / ini_load.cpp
blob44eb2b70abdf63134c9bc256cf3f2a7c9ed4a245
1 /* $Id: ini_load.cpp 26206 2014-01-02 17:55:57Z frosch $ */
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 #include "safeguards.h"
20 /**
21 * Construct a new in-memory item of an Ini file.
22 * @param parent the group we belong to
23 * @param name the name of the item
24 * @param last the last element of the name of the item
26 IniItem::IniItem(IniGroup *parent, const char *name, const char *last) : next(NULL), value(NULL), comment(NULL)
28 this->name = stredup(name, last);
29 str_validate(this->name, this->name + strlen(this->name));
31 *parent->last_item = this;
32 parent->last_item = &this->next;
35 /** Free everything we loaded. */
36 IniItem::~IniItem()
38 free(this->name);
39 free(this->value);
40 free(this->comment);
42 delete this->next;
45 /**
46 * Replace the current value with another value.
47 * @param value the value to replace with.
49 void IniItem::SetValue(const char *value)
51 free(this->value);
52 this->value = stredup(value);
55 /**
56 * Construct a new in-memory group of an Ini file.
57 * @param parent the file we belong to
58 * @param name the name of the group
59 * @param last the last element of the name of the group
61 IniGroup::IniGroup(IniLoadFile *parent, const char *name, const char *last) : next(NULL), type(IGT_VARIABLES), item(NULL), comment(NULL)
63 this->name = stredup(name, last);
64 str_validate(this->name, this->name + strlen(this->name));
66 this->last_item = &this->item;
67 *parent->last_group = this;
68 parent->last_group = &this->next;
70 if (parent->list_group_names != NULL) {
71 for (uint i = 0; parent->list_group_names[i] != NULL; i++) {
72 if (strcmp(this->name, parent->list_group_names[i]) == 0) {
73 this->type = IGT_LIST;
74 return;
78 if (parent->seq_group_names != NULL) {
79 for (uint i = 0; parent->seq_group_names[i] != NULL; i++) {
80 if (strcmp(this->name, parent->seq_group_names[i]) == 0) {
81 this->type = IGT_SEQUENCE;
82 return;
88 /** Free everything we loaded. */
89 IniGroup::~IniGroup()
91 free(this->name);
92 free(this->comment);
94 delete this->item;
95 delete this->next;
98 /**
99 * Get the item with the given name, and if it doesn't exist
100 * and create is true it creates a new item.
101 * @param name name of the item to find.
102 * @param create whether to create an item when not found or not.
103 * @return the requested item or NULL if not found.
105 IniItem *IniGroup::GetItem(const char *name, bool create)
107 for (IniItem *item = this->item; item != NULL; item = item->next) {
108 if (strcmp(item->name, name) == 0) return item;
111 if (!create) return NULL;
113 /* otherwise make a new one */
114 return new IniItem(this, name, NULL);
118 * Clear all items in the group
120 void IniGroup::Clear()
122 delete this->item;
123 this->item = NULL;
124 this->last_item = &this->item;
128 * Construct a new in-memory Ini file representation.
129 * @param list_group_names A \c NULL terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST
130 * @param seq_group_names A \c NULL terminated list with group names that should be loaded as lists of names. @see IGT_SEQUENCE
132 IniLoadFile::IniLoadFile(const char * const *list_group_names, const char * const *seq_group_names) :
133 group(NULL),
134 comment(NULL),
135 list_group_names(list_group_names),
136 seq_group_names(seq_group_names)
138 this->last_group = &this->group;
141 /** Free everything we loaded. */
142 IniLoadFile::~IniLoadFile()
144 free(this->comment);
145 delete this->group;
149 * Get the group with the given name. If it doesn't exist
150 * and \a create_new is \c true create a new group.
151 * @param name name of the group to find.
152 * @param len the maximum length of said name (\c 0 means length of the string).
153 * @param create_new Allow creation of group if it does not exist.
154 * @return The requested group if it exists or was created, else \c NULL.
156 IniGroup *IniLoadFile::GetGroup(const char *name, size_t len, bool create_new)
158 if (len == 0) len = strlen(name);
160 /* does it exist already? */
161 for (IniGroup *group = this->group; group != NULL; group = group->next) {
162 if (!strncmp(group->name, name, len) && group->name[len] == 0) {
163 return group;
167 if (!create_new) return NULL;
169 /* otherwise make a new one */
170 IniGroup *group = new IniGroup(this, name, name + len - 1);
171 group->comment = stredup("\n");
172 return group;
176 * Remove the group with the given name.
177 * @param name name of the group to remove.
179 void IniLoadFile::RemoveGroup(const char *name)
181 size_t len = strlen(name);
182 IniGroup *prev = NULL;
183 IniGroup *group;
185 /* does it exist already? */
186 for (group = this->group; group != NULL; prev = group, group = group->next) {
187 if (strncmp(group->name, name, len) == 0) {
188 break;
192 if (group == NULL) return;
194 if (prev != NULL) {
195 prev->next = prev->next->next;
196 if (this->last_group == &group->next) this->last_group = &prev->next;
197 } else {
198 this->group = this->group->next;
199 if (this->last_group == &group->next) this->last_group = &this->group;
202 group->next = NULL;
203 delete group;
207 * Load the Ini file's data from the disk.
208 * @param filename the file to load.
209 * @param subdir the sub directory to load the file from.
210 * @pre nothing has been loaded yet.
212 void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir)
214 assert(this->last_group == &this->group);
216 char buffer[1024];
217 IniGroup *group = NULL;
219 char *comment = NULL;
220 uint comment_size = 0;
221 uint comment_alloc = 0;
223 size_t end;
224 FILE *in = this->OpenFile(filename, subdir, &end);
225 if (in == NULL) return;
227 end += ftell(in);
229 /* for each line in the file */
230 while ((size_t)ftell(in) < end && fgets(buffer, sizeof(buffer), in)) {
231 char c, *s;
232 /* trim whitespace from the left side */
233 for (s = buffer; *s == ' ' || *s == '\t'; s++) {}
235 /* trim whitespace from right side. */
236 char *e = s + strlen(s);
237 while (e > s && ((c = e[-1]) == '\n' || c == '\r' || c == ' ' || c == '\t')) e--;
238 *e = '\0';
240 /* Skip comments and empty lines outside IGT_SEQUENCE groups. */
241 if ((group == NULL || group->type != IGT_SEQUENCE) && (*s == '#' || *s == ';' || *s == '\0')) {
242 uint ns = comment_size + (e - s + 1);
243 uint a = comment_alloc;
244 /* add to comment */
245 if (ns > a) {
246 a = max(a, 128U);
247 do a *= 2; while (a < ns);
248 comment = ReallocT(comment, comment_alloc = a);
250 uint pos = comment_size;
251 comment_size += (e - s + 1);
252 comment[pos + e - s] = '\n'; // comment newline
253 memcpy(comment + pos, s, e - s); // copy comment contents
254 continue;
257 /* it's a group? */
258 if (s[0] == '[') {
259 if (e[-1] != ']') {
260 this->ReportFileError("ini: invalid group name '", buffer, "'");
261 } else {
262 e--;
264 s++; // skip [
265 group = new IniGroup(this, s, e - 1);
266 if (comment_size != 0) {
267 group->comment = stredup(comment, comment + comment_size - 1);
268 comment_size = 0;
270 } else if (group != NULL) {
271 if (group->type == IGT_SEQUENCE) {
272 /* A sequence group, use the line as item name without further interpretation. */
273 IniItem *item = new IniItem(group, buffer, e - 1);
274 if (comment_size) {
275 item->comment = stredup(comment, comment + comment_size - 1);
276 comment_size = 0;
278 continue;
280 char *t;
281 /* find end of keyname */
282 if (*s == '\"') {
283 s++;
284 for (t = s; *t != '\0' && *t != '\"'; t++) {}
285 if (*t == '\"') *t = ' ';
286 } else {
287 for (t = s; *t != '\0' && *t != '=' && *t != '\t' && *t != ' '; t++) {}
290 /* it's an item in an existing group */
291 IniItem *item = new IniItem(group, s, t - 1);
292 if (comment_size != 0) {
293 item->comment = stredup(comment, comment + comment_size - 1);
294 comment_size = 0;
297 /* find start of parameter */
298 while (*t == '=' || *t == ' ' || *t == '\t') t++;
300 bool quoted = (*t == '\"');
301 /* remove starting quotation marks */
302 if (*t == '\"') t++;
303 /* remove ending quotation marks */
304 e = t + strlen(t);
305 if (e > t && e[-1] == '\"') e--;
306 *e = '\0';
308 /* If the value was not quoted and empty, it must be NULL */
309 item->value = (!quoted && e == t) ? NULL : stredup(t);
310 if (item->value != NULL) str_validate(item->value, item->value + strlen(item->value));
311 } else {
312 /* it's an orphan item */
313 this->ReportFileError("ini: '", buffer, "' outside of group");
317 if (comment_size > 0) {
318 this->comment = stredup(comment, comment + comment_size - 1);
319 comment_size = 0;
322 free(comment);
323 fclose(in);