Add: INR currency (#8136)
[openttd-github.git] / src / ini_load.cpp
blobdd7283130819244ade1940f1a0b4bab18420d348
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 */
8 /** @file ini_load.cpp Definition of the #IniLoadFile class, related to reading and storing '*.ini' files. */
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"
16 #include "safeguards.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 last the last element of the name of the item
24 IniItem::IniItem(IniGroup *parent, const char *name, const char *last) : next(nullptr), value(nullptr), comment(nullptr)
26 this->name = stredup(name, last);
27 str_validate(this->name, this->name + strlen(this->name));
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 = stredup(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 last the last element of the name of the group
59 IniGroup::IniGroup(IniLoadFile *parent, const char *name, const char *last) : next(nullptr), type(IGT_VARIABLES), item(nullptr), comment(nullptr)
61 this->name = stredup(name, last);
62 str_validate(this->name, this->name + strlen(this->name));
64 this->last_item = &this->item;
65 *parent->last_group = this;
66 parent->last_group = &this->next;
68 if (parent->list_group_names != nullptr) {
69 for (uint i = 0; parent->list_group_names[i] != nullptr; 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 != nullptr) {
77 for (uint i = 0; parent->seq_group_names[i] != nullptr; 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 nullptr if not found.
103 IniItem *IniGroup::GetItem(const char *name, bool create)
105 for (IniItem *item = this->item; item != nullptr; item = item->next) {
106 if (strcmp(item->name, name) == 0) return item;
109 if (!create) return nullptr;
111 /* otherwise make a new one */
112 return new IniItem(this, name, nullptr);
116 * Clear all items in the group
118 void IniGroup::Clear()
120 delete this->item;
121 this->item = nullptr;
122 this->last_item = &this->item;
126 * Construct a new in-memory Ini file representation.
127 * @param list_group_names A \c nullptr terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST
128 * @param seq_group_names A \c nullptr 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(nullptr),
132 comment(nullptr),
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 nullptr.
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 != nullptr; group = group->next) {
160 if (!strncmp(group->name, name, len) && group->name[len] == 0) {
161 return group;
165 if (!create_new) return nullptr;
167 /* otherwise make a new one */
168 IniGroup *group = new IniGroup(this, name, name + len - 1);
169 group->comment = stredup("\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 = nullptr;
181 IniGroup *group;
183 /* does it exist already? */
184 for (group = this->group; group != nullptr; prev = group, group = group->next) {
185 if (strncmp(group->name, name, len) == 0) {
186 break;
190 if (group == nullptr) return;
192 if (prev != nullptr) {
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 = nullptr;
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 = nullptr;
217 char *comment = nullptr;
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 == nullptr) 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 == nullptr || 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 - 1);
264 if (comment_size != 0) {
265 group->comment = stredup(comment, comment + comment_size - 1);
266 comment_size = 0;
268 } else if (group != nullptr) {
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 - 1);
272 if (comment_size) {
273 item->comment = stredup(comment, comment + comment_size - 1);
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 - 1);
290 if (comment_size != 0) {
291 item->comment = stredup(comment, comment + comment_size - 1);
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 nullptr */
307 item->value = (!quoted && e == t) ? nullptr : stredup(t);
308 if (item->value != nullptr) str_validate(item->value, item->value + strlen(item->value));
309 } else {
310 /* it's an orphan item */
311 this->ReportFileError("ini: '", buffer, "' outside of group");
315 if (comment_size > 0) {
316 this->comment = stredup(comment, comment + comment_size - 1);
317 comment_size = 0;
320 free(comment);
321 fclose(in);