1 /* $Id: ini_load.cpp 26206 2014-01-02 17:55:57Z frosch $ */
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/>.
10 /** @file ini_load.cpp Definition of the #IniLoadFile class, related to reading and storing '*.ini' files. */
13 #include "core/alloc_func.hpp"
14 #include "core/mem_func.hpp"
16 #include "string_func.h"
18 #include "safeguards.h"
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. */
46 * Replace the current value with another value.
47 * @param value the value to replace with.
49 void IniItem::SetValue(const char *value
)
52 this->value
= stredup(value
);
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
;
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
;
88 /** Free everything we loaded. */
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()
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
) :
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()
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) {
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");
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
;
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) {
192 if (group
== NULL
) return;
195 prev
->next
= prev
->next
->next
;
196 if (this->last_group
== &group
->next
) this->last_group
= &prev
->next
;
198 this->group
= this->group
->next
;
199 if (this->last_group
== &group
->next
) this->last_group
= &this->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
);
217 IniGroup
*group
= NULL
;
219 char *comment
= NULL
;
220 uint comment_size
= 0;
221 uint comment_alloc
= 0;
224 FILE *in
= this->OpenFile(filename
, subdir
, &end
);
225 if (in
== NULL
) return;
229 /* for each line in the file */
230 while ((size_t)ftell(in
) < end
&& fgets(buffer
, sizeof(buffer
), in
)) {
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
--;
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
;
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
260 this->ReportFileError("ini: invalid group name '", buffer
, "'");
265 group
= new IniGroup(this, s
, e
- 1);
266 if (comment_size
!= 0) {
267 group
->comment
= stredup(comment
, comment
+ comment_size
- 1);
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);
275 item
->comment
= stredup(comment
, comment
+ comment_size
- 1);
281 /* find end of keyname */
284 for (t
= s
; *t
!= '\0' && *t
!= '\"'; t
++) {}
285 if (*t
== '\"') *t
= ' ';
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);
297 /* find start of parameter */
298 while (*t
== '=' || *t
== ' ' || *t
== '\t') t
++;
300 bool quoted
= (*t
== '\"');
301 /* remove starting quotation marks */
303 /* remove ending quotation marks */
305 if (e
> t
&& e
[-1] == '\"') e
--;
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
));
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);