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
= str_validate(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 char *value
)
43 if (value
== nullptr) {
46 this->value
.emplace(value
);
51 * Construct a new in-memory group of an Ini file.
52 * @param parent the file we belong to
53 * @param name the name of the group
55 IniGroup::IniGroup(IniLoadFile
*parent
, const std::string
&name
) : next(nullptr), type(IGT_VARIABLES
), item(nullptr)
57 this->name
= str_validate(name
);
59 this->last_item
= &this->item
;
60 *parent
->last_group
= this;
61 parent
->last_group
= &this->next
;
63 if (parent
->list_group_names
!= nullptr) {
64 for (uint i
= 0; parent
->list_group_names
[i
] != nullptr; i
++) {
65 if (this->name
== parent
->list_group_names
[i
]) {
66 this->type
= IGT_LIST
;
71 if (parent
->seq_group_names
!= nullptr) {
72 for (uint i
= 0; parent
->seq_group_names
[i
] != nullptr; i
++) {
73 if (this->name
== parent
->seq_group_names
[i
]) {
74 this->type
= IGT_SEQUENCE
;
81 /** Free everything we loaded. */
89 * Get the item with the given name, and if it doesn't exist
90 * and create is true it creates a new item.
91 * @param name name of the item to find.
92 * @param create whether to create an item when not found or not.
93 * @return the requested item or nullptr if not found.
95 IniItem
*IniGroup::GetItem(const std::string
&name
, bool create
)
97 for (IniItem
*item
= this->item
; item
!= nullptr; item
= item
->next
) {
98 if (item
->name
== name
) return item
;
101 if (!create
) return nullptr;
103 /* otherwise make a new one */
104 return new IniItem(this, name
);
108 * Clear all items in the group
110 void IniGroup::Clear()
113 this->item
= nullptr;
114 this->last_item
= &this->item
;
118 * Construct a new in-memory Ini file representation.
119 * @param list_group_names A \c nullptr terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST
120 * @param seq_group_names A \c nullptr terminated list with group names that should be loaded as lists of names. @see IGT_SEQUENCE
122 IniLoadFile::IniLoadFile(const char * const *list_group_names
, const char * const *seq_group_names
) :
124 list_group_names(list_group_names
),
125 seq_group_names(seq_group_names
)
127 this->last_group
= &this->group
;
130 /** Free everything we loaded. */
131 IniLoadFile::~IniLoadFile()
137 * Get the group with the given name. If it doesn't exist
138 * and \a create_new is \c true create a new group.
139 * @param name name of the group to find.
140 * @param create_new Allow creation of group if it does not exist.
141 * @return The requested group if it exists or was created, else \c nullptr.
143 IniGroup
*IniLoadFile::GetGroup(const std::string
&name
, bool create_new
)
145 /* does it exist already? */
146 for (IniGroup
*group
= this->group
; group
!= nullptr; group
= group
->next
) {
147 if (group
->name
== name
) return group
;
150 if (!create_new
) return nullptr;
152 /* otherwise make a new one */
153 IniGroup
*group
= new IniGroup(this, name
);
154 group
->comment
= "\n";
159 * Remove the group with the given name.
160 * @param name name of the group to remove.
162 void IniLoadFile::RemoveGroup(const char *name
)
164 size_t len
= strlen(name
);
165 IniGroup
*prev
= nullptr;
168 /* does it exist already? */
169 for (group
= this->group
; group
!= nullptr; prev
= group
, group
= group
->next
) {
170 if (group
->name
.compare(0, len
, name
) == 0) {
175 if (group
== nullptr) return;
177 if (prev
!= nullptr) {
178 prev
->next
= prev
->next
->next
;
179 if (this->last_group
== &group
->next
) this->last_group
= &prev
->next
;
181 this->group
= this->group
->next
;
182 if (this->last_group
== &group
->next
) this->last_group
= &this->group
;
185 group
->next
= nullptr;
190 * Load the Ini file's data from the disk.
191 * @param filename the file to load.
192 * @param subdir the sub directory to load the file from.
193 * @pre nothing has been loaded yet.
195 void IniLoadFile::LoadFromDisk(const std::string
&filename
, Subdirectory subdir
)
197 assert(this->last_group
== &this->group
);
200 IniGroup
*group
= nullptr;
202 char *comment
= nullptr;
203 uint comment_size
= 0;
204 uint comment_alloc
= 0;
207 FILE *in
= this->OpenFile(filename
, subdir
, &end
);
208 if (in
== nullptr) return;
212 /* for each line in the file */
213 while ((size_t)ftell(in
) < end
&& fgets(buffer
, sizeof(buffer
), in
)) {
215 /* trim whitespace from the left side */
216 for (s
= buffer
; *s
== ' ' || *s
== '\t'; s
++) {}
218 /* trim whitespace from right side. */
219 char *e
= s
+ strlen(s
);
220 while (e
> s
&& ((c
= e
[-1]) == '\n' || c
== '\r' || c
== ' ' || c
== '\t')) e
--;
223 /* Skip comments and empty lines outside IGT_SEQUENCE groups. */
224 if ((group
== nullptr || group
->type
!= IGT_SEQUENCE
) && (*s
== '#' || *s
== ';' || *s
== '\0')) {
225 uint ns
= comment_size
+ (e
- s
+ 1);
226 uint a
= comment_alloc
;
229 a
= std::max(a
, 128U);
230 do a
*= 2; while (a
< ns
);
231 comment
= ReallocT(comment
, comment_alloc
= a
);
233 uint pos
= comment_size
;
234 comment_size
+= (e
- s
+ 1);
235 comment
[pos
+ e
- s
] = '\n'; // comment newline
236 memcpy(comment
+ pos
, s
, e
- s
); // copy comment contents
243 this->ReportFileError("ini: invalid group name '", buffer
, "'");
248 group
= new IniGroup(this, std::string(s
, e
- s
));
249 if (comment_size
!= 0) {
250 group
->comment
.assign(comment
, comment_size
);
253 } else if (group
!= nullptr) {
254 if (group
->type
== IGT_SEQUENCE
) {
255 /* A sequence group, use the line as item name without further interpretation. */
256 IniItem
*item
= new IniItem(group
, std::string(buffer
, e
- buffer
));
258 item
->comment
.assign(comment
, comment_size
);
264 /* find end of keyname */
267 for (t
= s
; *t
!= '\0' && *t
!= '\"'; t
++) {}
268 if (*t
== '\"') *t
= ' ';
270 for (t
= s
; *t
!= '\0' && *t
!= '=' && *t
!= '\t' && *t
!= ' '; t
++) {}
273 /* it's an item in an existing group */
274 IniItem
*item
= new IniItem(group
, std::string(s
, t
- s
));
275 if (comment_size
!= 0) {
276 item
->comment
.assign(comment
, comment_size
);
280 /* find start of parameter */
281 while (*t
== '=' || *t
== ' ' || *t
== '\t') t
++;
283 bool quoted
= (*t
== '\"');
284 /* remove starting quotation marks */
286 /* remove ending quotation marks */
288 if (e
> t
&& e
[-1] == '\"') e
--;
291 /* If the value was not quoted and empty, it must be nullptr */
292 if (!quoted
&& e
== t
) {
295 item
->value
= str_validate(std::string(t
));
298 /* it's an orphan item */
299 this->ReportFileError("ini: '", buffer
, "' outside of group");
303 if (comment_size
> 0) {
304 this->comment
.assign(comment
, comment_size
);