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"
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 len the length of the name of the item
24 IniItem::IniItem(IniGroup
*parent
, const char *name
, size_t len
) : next(NULL
), value(NULL
), comment(NULL
)
26 if (len
== 0) len
= strlen(name
);
28 this->name
= strndup(name
, len
);
29 if (this->name
!= NULL
) str_validate(this->name
, this->name
+ len
);
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
= strdup(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 len the length of the name of the group
61 IniGroup::IniGroup(IniLoadFile
*parent
, const char *name
, size_t len
) : next(NULL
), type(IGT_VARIABLES
), item(NULL
), comment(NULL
)
63 if (len
== 0) len
= strlen(name
);
65 this->name
= strndup(name
, len
);
66 if (this->name
!= NULL
) str_validate(this->name
, this->name
+ len
);
68 this->last_item
= &this->item
;
69 *parent
->last_group
= this;
70 parent
->last_group
= &this->next
;
72 if (parent
->list_group_names
!= NULL
) {
73 for (uint i
= 0; parent
->list_group_names
[i
] != NULL
; i
++) {
74 if (strcmp(this->name
, parent
->list_group_names
[i
]) == 0) {
75 this->type
= IGT_LIST
;
80 if (parent
->seq_group_names
!= NULL
) {
81 for (uint i
= 0; parent
->seq_group_names
[i
] != NULL
; i
++) {
82 if (strcmp(this->name
, parent
->seq_group_names
[i
]) == 0) {
83 this->type
= IGT_SEQUENCE
;
90 /** Free everything we loaded. */
101 * Get the item with the given name, and if it doesn't exist
102 * and create is true it creates a new item.
103 * @param name name of the item to find.
104 * @param create whether to create an item when not found or not.
105 * @return the requested item or NULL if not found.
107 IniItem
*IniGroup::GetItem(const char *name
, bool create
)
109 for (IniItem
*item
= this->item
; item
!= NULL
; item
= item
->next
) {
110 if (strcmp(item
->name
, name
) == 0) return item
;
113 if (!create
) return NULL
;
115 /* otherwise make a new one */
116 return new IniItem(this, name
, strlen(name
));
120 * Clear all items in the group
122 void IniGroup::Clear()
126 this->last_item
= &this->item
;
130 * Construct a new in-memory Ini file representation.
131 * @param list_group_names A \c NULL terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST
132 * @param seq_group_names A \c NULL terminated list with group names that should be loaded as lists of names. @see IGT_SEQUENCE
134 IniLoadFile::IniLoadFile(const char * const *list_group_names
, const char * const *seq_group_names
) :
137 list_group_names(list_group_names
),
138 seq_group_names(seq_group_names
)
140 this->last_group
= &this->group
;
143 /** Free everything we loaded. */
144 IniLoadFile::~IniLoadFile()
151 * Get the group with the given name. If it doesn't exist
152 * and \a create_new is \c true create a new group.
153 * @param name name of the group to find.
154 * @param len the maximum length of said name (\c 0 means length of the string).
155 * @param create_new Allow creation of group if it does not exist.
156 * @return The requested group if it exists or was created, else \c NULL.
158 IniGroup
*IniLoadFile::GetGroup(const char *name
, size_t len
, bool create_new
)
160 if (len
== 0) len
= strlen(name
);
162 /* does it exist already? */
163 for (IniGroup
*group
= this->group
; group
!= NULL
; group
= group
->next
) {
164 if (!strncmp(group
->name
, name
, len
) && group
->name
[len
] == 0) {
169 if (!create_new
) return NULL
;
171 /* otherwise make a new one */
172 IniGroup
*group
= new IniGroup(this, name
, len
);
173 group
->comment
= strdup("\n");
178 * Remove the group with the given name.
179 * @param name name of the group to remove.
181 void IniLoadFile::RemoveGroup(const char *name
)
183 size_t len
= strlen(name
);
184 IniGroup
*prev
= NULL
;
187 /* does it exist already? */
188 for (group
= this->group
; group
!= NULL
; prev
= group
, group
= group
->next
) {
189 if (strncmp(group
->name
, name
, len
) == 0) {
194 if (group
== NULL
) return;
197 prev
->next
= prev
->next
->next
;
198 if (this->last_group
== &group
->next
) this->last_group
= &prev
->next
;
200 this->group
= this->group
->next
;
201 if (this->last_group
== &group
->next
) this->last_group
= &this->group
;
209 * Load the Ini file's data from the disk.
210 * @param filename the file to load.
211 * @param subdir the sub directory to load the file from.
212 * @pre nothing has been loaded yet.
214 void IniLoadFile::LoadFromDisk(const char *filename
, Subdirectory subdir
)
216 assert(this->last_group
== &this->group
);
219 IniGroup
*group
= NULL
;
221 char *comment
= NULL
;
222 uint comment_size
= 0;
223 uint comment_alloc
= 0;
226 FILE *in
= this->OpenFile(filename
, subdir
, &end
);
227 if (in
== NULL
) return;
231 /* for each line in the file */
232 while ((size_t)ftell(in
) < end
&& fgets(buffer
, sizeof(buffer
), in
)) {
234 /* trim whitespace from the left side */
235 for (s
= buffer
; *s
== ' ' || *s
== '\t'; s
++) {}
237 /* trim whitespace from right side. */
238 char *e
= s
+ strlen(s
);
239 while (e
> s
&& ((c
= e
[-1]) == '\n' || c
== '\r' || c
== ' ' || c
== '\t')) e
--;
242 /* Skip comments and empty lines outside IGT_SEQUENCE groups. */
243 if ((group
== NULL
|| group
->type
!= IGT_SEQUENCE
) && (*s
== '#' || *s
== ';' || *s
== '\0')) {
244 uint ns
= comment_size
+ (e
- s
+ 1);
245 uint a
= comment_alloc
;
249 do a
*= 2; while (a
< ns
);
250 comment
= ReallocT(comment
, comment_alloc
= a
);
252 uint pos
= comment_size
;
253 comment_size
+= (e
- s
+ 1);
254 comment
[pos
+ e
- s
] = '\n'; // comment newline
255 memcpy(comment
+ pos
, s
, e
- s
); // copy comment contents
262 this->ReportFileError("ini: invalid group name '", buffer
, "'");
267 group
= new IniGroup(this, s
, e
- s
);
268 if (comment_size
!= 0) {
269 group
->comment
= strndup(comment
, comment_size
);
272 } else if (group
!= NULL
) {
273 if (group
->type
== IGT_SEQUENCE
) {
274 /* A sequence group, use the line as item name without further interpretation. */
275 IniItem
*item
= new IniItem(group
, buffer
, e
- buffer
);
277 item
->comment
= strndup(comment
, comment_size
);
283 /* find end of keyname */
286 for (t
= s
; *t
!= '\0' && *t
!= '\"'; t
++) {}
287 if (*t
== '\"') *t
= ' ';
289 for (t
= s
; *t
!= '\0' && *t
!= '=' && *t
!= '\t' && *t
!= ' '; t
++) {}
292 /* it's an item in an existing group */
293 IniItem
*item
= new IniItem(group
, s
, t
- s
);
294 if (comment_size
!= 0) {
295 item
->comment
= strndup(comment
, comment_size
);
299 /* find start of parameter */
300 while (*t
== '=' || *t
== ' ' || *t
== '\t') t
++;
302 bool quoted
= (*t
== '\"');
303 /* remove starting quotation marks */
305 /* remove ending quotation marks */
307 if (e
> t
&& e
[-1] == '\"') e
--;
310 /* If the value was not quoted and empty, it must be NULL */
311 item
->value
= (!quoted
&& e
== t
) ? NULL
: strndup(t
, e
- t
);
312 if (item
->value
!= NULL
) str_validate(item
->value
, item
->value
+ strlen(item
->value
));
314 /* it's an orphan item */
315 this->ReportFileError("ini: '", buffer
, "' outside of group");
319 if (comment_size
> 0) {
320 this->comment
= strndup(comment
, comment_size
);