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/>.
8 /** @file ini_load.cpp Definition of the #IniLoadFile class, related to reading and storing '*.ini' files. */
11 #include "core/alloc_func.hpp"
12 #include "core/mem_func.hpp"
14 #include "string_func.h"
16 #include "safeguards.h"
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. */
44 * Replace the current value with another value.
45 * @param value the value to replace with.
47 void IniItem::SetValue(const char *value
)
50 this->value
= stredup(value
);
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
;
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
;
86 /** Free everything we loaded. */
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()
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
) :
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()
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) {
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");
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;
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) {
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
;
196 this->group
= this->group
->next
;
197 if (this->last_group
== &group
->next
) this->last_group
= &this->group
;
200 group
->next
= nullptr;
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
);
215 IniGroup
*group
= nullptr;
217 char *comment
= nullptr;
218 uint comment_size
= 0;
219 uint comment_alloc
= 0;
222 FILE *in
= this->OpenFile(filename
, subdir
, &end
);
223 if (in
== nullptr) return;
227 /* for each line in the file */
228 while ((size_t)ftell(in
) < end
&& fgets(buffer
, sizeof(buffer
), in
)) {
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
--;
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
;
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
258 this->ReportFileError("ini: invalid group name '", buffer
, "'");
263 group
= new IniGroup(this, s
, e
- 1);
264 if (comment_size
!= 0) {
265 group
->comment
= stredup(comment
, comment
+ comment_size
- 1);
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);
273 item
->comment
= stredup(comment
, comment
+ comment_size
- 1);
279 /* find end of keyname */
282 for (t
= s
; *t
!= '\0' && *t
!= '\"'; t
++) {}
283 if (*t
== '\"') *t
= ' ';
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);
295 /* find start of parameter */
296 while (*t
== '=' || *t
== ' ' || *t
== '\t') t
++;
298 bool quoted
= (*t
== '\"');
299 /* remove starting quotation marks */
301 /* remove ending quotation marks */
303 if (e
> t
&& e
[-1] == '\"') e
--;
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
));
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);