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
23 IniItem::IniItem(IniGroup
*parent
, const std::string
&name
) : next(nullptr)
25 this->name
= StrMakeValid(name
);
27 *parent
->last_item
= this;
28 parent
->last_item
= &this->next
;
31 /** Free everything we loaded. */
38 * Replace the current value with another value.
39 * @param value the value to replace with.
41 void IniItem::SetValue(const std::string_view value
)
43 this->value
.emplace(value
);
47 * Construct a new in-memory group of an Ini file.
48 * @param parent the file we belong to
49 * @param name the name of the group
51 IniGroup::IniGroup(IniLoadFile
*parent
, const std::string
&name
) : next(nullptr), type(IGT_VARIABLES
), item(nullptr)
53 this->name
= StrMakeValid(name
);
55 this->last_item
= &this->item
;
56 *parent
->last_group
= this;
57 parent
->last_group
= &this->next
;
59 if (parent
->list_group_names
!= nullptr) {
60 for (uint i
= 0; parent
->list_group_names
[i
] != nullptr; i
++) {
61 if (this->name
== parent
->list_group_names
[i
]) {
62 this->type
= IGT_LIST
;
67 if (parent
->seq_group_names
!= nullptr) {
68 for (uint i
= 0; parent
->seq_group_names
[i
] != nullptr; i
++) {
69 if (this->name
== parent
->seq_group_names
[i
]) {
70 this->type
= IGT_SEQUENCE
;
77 /** Free everything we loaded. */
85 * Get the item with the given name, and if it doesn't exist
86 * and create is true it creates a new item.
87 * @param name name of the item to find.
88 * @param create whether to create an item when not found or not.
89 * @return the requested item or nullptr if not found.
91 IniItem
*IniGroup::GetItem(const std::string
&name
, bool create
)
93 for (IniItem
*item
= this->item
; item
!= nullptr; item
= item
->next
) {
94 if (item
->name
== name
) return item
;
97 if (!create
) return nullptr;
99 /* otherwise make a new one */
100 return new IniItem(this, name
);
104 * Remove the item with the given name.
105 * @param name Name of the item to remove.
107 void IniGroup::RemoveItem(const std::string
&name
)
109 IniItem
**prev
= &this->item
;
111 for (IniItem
*item
= this->item
; item
!= nullptr; prev
= &item
->next
, item
= item
->next
) {
112 if (item
->name
!= name
) continue;
115 /* "last_item" is a pointer to the "real-last-item"->next. */
116 if (this->last_item
== &item
->next
) {
117 this->last_item
= prev
;
120 item
->next
= nullptr;
128 * Clear all items in the group
130 void IniGroup::Clear()
133 this->item
= nullptr;
134 this->last_item
= &this->item
;
138 * Construct a new in-memory Ini file representation.
139 * @param list_group_names A \c nullptr terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST
140 * @param seq_group_names A \c nullptr terminated list with group names that should be loaded as lists of names. @see IGT_SEQUENCE
142 IniLoadFile::IniLoadFile(const char * const *list_group_names
, const char * const *seq_group_names
) :
144 list_group_names(list_group_names
),
145 seq_group_names(seq_group_names
)
147 this->last_group
= &this->group
;
150 /** Free everything we loaded. */
151 IniLoadFile::~IniLoadFile()
157 * Get the group with the given name. If it doesn't exist
158 * and \a create_new is \c true create a new group.
159 * @param name name of the group to find.
160 * @param create_new Allow creation of group if it does not exist.
161 * @return The requested group if it exists or was created, else \c nullptr.
163 IniGroup
*IniLoadFile::GetGroup(const std::string
&name
, bool create_new
)
165 /* does it exist already? */
166 for (IniGroup
*group
= this->group
; group
!= nullptr; group
= group
->next
) {
167 if (group
->name
== name
) return group
;
170 if (!create_new
) return nullptr;
172 /* otherwise make a new one */
173 IniGroup
*group
= new IniGroup(this, name
);
174 group
->comment
= "\n";
179 * Remove the group with the given name.
180 * @param name name of the group to remove.
182 void IniLoadFile::RemoveGroup(const char *name
)
184 size_t len
= strlen(name
);
185 IniGroup
*prev
= nullptr;
188 /* does it exist already? */
189 for (group
= this->group
; group
!= nullptr; prev
= group
, group
= group
->next
) {
190 if (group
->name
.compare(0, len
, name
) == 0) {
195 if (group
== nullptr) return;
197 if (prev
!= nullptr) {
198 prev
->next
= prev
->next
->next
;
199 if (this->last_group
== &group
->next
) this->last_group
= &prev
->next
;
201 this->group
= this->group
->next
;
202 if (this->last_group
== &group
->next
) this->last_group
= &this->group
;
205 group
->next
= nullptr;
210 * Load the Ini file's data from the disk.
211 * @param filename the file to load.
212 * @param subdir the sub directory to load the file from.
213 * @pre nothing has been loaded yet.
215 void IniLoadFile::LoadFromDisk(const std::string
&filename
, Subdirectory subdir
)
217 assert(this->last_group
== &this->group
);
220 IniGroup
*group
= nullptr;
222 char *comment
= nullptr;
223 uint comment_size
= 0;
224 uint comment_alloc
= 0;
227 FILE *in
= this->OpenFile(filename
, subdir
, &end
);
228 if (in
== nullptr) return;
232 /* for each line in the file */
233 while ((size_t)ftell(in
) < end
&& fgets(buffer
, sizeof(buffer
), in
)) {
235 /* trim whitespace from the left side */
236 for (s
= buffer
; *s
== ' ' || *s
== '\t'; s
++) {}
238 /* trim whitespace from right side. */
239 char *e
= s
+ strlen(s
);
240 while (e
> s
&& ((c
= e
[-1]) == '\n' || c
== '\r' || c
== ' ' || c
== '\t')) e
--;
243 /* Skip comments and empty lines outside IGT_SEQUENCE groups. */
244 if ((group
== nullptr || group
->type
!= IGT_SEQUENCE
) && (*s
== '#' || *s
== ';' || *s
== '\0')) {
245 uint ns
= comment_size
+ (e
- s
+ 1);
246 uint a
= comment_alloc
;
249 a
= std::max(a
, 128U);
250 do a
*= 2; while (a
< ns
);
251 comment
= ReallocT(comment
, comment_alloc
= a
);
253 uint pos
= comment_size
;
254 comment_size
+= (e
- s
+ 1);
255 comment
[pos
+ e
- s
] = '\n'; // comment newline
256 memcpy(comment
+ pos
, s
, e
- s
); // copy comment contents
263 this->ReportFileError("ini: invalid group name '", buffer
, "'");
268 group
= new IniGroup(this, std::string(s
, e
- s
));
269 if (comment_size
!= 0) {
270 group
->comment
.assign(comment
, comment_size
);
273 } else if (group
!= nullptr) {
274 if (group
->type
== IGT_SEQUENCE
) {
275 /* A sequence group, use the line as item name without further interpretation. */
276 IniItem
*item
= new IniItem(group
, std::string(buffer
, e
- buffer
));
278 item
->comment
.assign(comment
, comment_size
);
284 /* find end of keyname */
287 for (t
= s
; *t
!= '\0' && *t
!= '\"'; t
++) {}
288 if (*t
== '\"') *t
= ' ';
290 for (t
= s
; *t
!= '\0' && *t
!= '=' && *t
!= '\t' && *t
!= ' '; t
++) {}
293 /* it's an item in an existing group */
294 IniItem
*item
= new IniItem(group
, std::string(s
, t
- s
));
295 if (comment_size
!= 0) {
296 item
->comment
.assign(comment
, comment_size
);
300 /* find start of parameter */
301 while (*t
== '=' || *t
== ' ' || *t
== '\t') t
++;
303 bool quoted
= (*t
== '\"');
304 /* remove starting quotation marks */
306 /* remove ending quotation marks */
308 if (e
> t
&& e
[-1] == '\"') e
--;
311 /* If the value was not quoted and empty, it must be nullptr */
312 if (!quoted
&& e
== t
) {
315 item
->value
= StrMakeValid(std::string(t
));
318 /* it's an orphan item */
319 this->ReportFileError("ini: '", buffer
, "' outside of group");
323 if (comment_size
> 0) {
324 this->comment
.assign(comment
, comment_size
);