OpenTTD
ini_load.cpp
Go to the documentation of this file.
1 /*
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/>.
6  */
7 
10 #include "stdafx.h"
11 #include "core/alloc_func.hpp"
12 #include "core/mem_func.hpp"
13 #include "ini_type.h"
14 #include "string_func.h"
15 
16 #include "safeguards.h"
17 
24 IniItem::IniItem(IniGroup *parent, const char *name, const char *last) : next(nullptr), value(nullptr), comment(nullptr)
25 {
26  this->name = stredup(name, last);
27  str_validate(this->name, this->name + strlen(this->name));
28 
29  *parent->last_item = this;
30  parent->last_item = &this->next;
31 }
32 
35 {
36  free(this->name);
37  free(this->value);
38  free(this->comment);
39 
40  delete this->next;
41 }
42 
47 void IniItem::SetValue(const char *value)
48 {
49  free(this->value);
50  this->value = stredup(value);
51 }
52 
59 IniGroup::IniGroup(IniLoadFile *parent, const char *name, const char *last) : next(nullptr), type(IGT_VARIABLES), item(nullptr), comment(nullptr)
60 {
61  this->name = stredup(name, last);
62  str_validate(this->name, this->name + strlen(this->name));
63 
64  this->last_item = &this->item;
65  *parent->last_group = this;
66  parent->last_group = &this->next;
67 
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;
72  return;
73  }
74  }
75  }
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;
80  return;
81  }
82  }
83  }
84 }
85 
88 {
89  free(this->name);
90  free(this->comment);
91 
92  delete this->item;
93  delete this->next;
94 }
95 
103 IniItem *IniGroup::GetItem(const char *name, bool create)
104 {
105  for (IniItem *item = this->item; item != nullptr; item = item->next) {
106  if (strcmp(item->name, name) == 0) return item;
107  }
108 
109  if (!create) return nullptr;
110 
111  /* otherwise make a new one */
112  return new IniItem(this, name, nullptr);
113 }
114 
119 {
120  delete this->item;
121  this->item = nullptr;
122  this->last_item = &this->item;
123 }
124 
130 IniLoadFile::IniLoadFile(const char * const *list_group_names, const char * const *seq_group_names) :
131  group(nullptr),
132  comment(nullptr),
133  list_group_names(list_group_names),
134  seq_group_names(seq_group_names)
135 {
136  this->last_group = &this->group;
137 }
138 
141 {
142  free(this->comment);
143  delete this->group;
144 }
145 
154 IniGroup *IniLoadFile::GetGroup(const char *name, size_t len, bool create_new)
155 {
156  if (len == 0) len = strlen(name);
157 
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) {
161  return group;
162  }
163  }
164 
165  if (!create_new) return nullptr;
166 
167  /* otherwise make a new one */
168  IniGroup *group = new IniGroup(this, name, name + len - 1);
169  group->comment = stredup("\n");
170  return group;
171 }
172 
177 void IniLoadFile::RemoveGroup(const char *name)
178 {
179  size_t len = strlen(name);
180  IniGroup *prev = nullptr;
181  IniGroup *group;
182 
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) {
186  break;
187  }
188  }
189 
190  if (group == nullptr) return;
191 
192  if (prev != nullptr) {
193  prev->next = prev->next->next;
194  if (this->last_group == &group->next) this->last_group = &prev->next;
195  } else {
196  this->group = this->group->next;
197  if (this->last_group == &group->next) this->last_group = &this->group;
198  }
199 
200  group->next = nullptr;
201  delete group;
202 }
203 
210 void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir)
211 {
212  assert(this->last_group == &this->group);
213 
214  char buffer[1024];
215  IniGroup *group = nullptr;
216 
217  char *comment = nullptr;
218  uint comment_size = 0;
219  uint comment_alloc = 0;
220 
221  size_t end;
222  FILE *in = this->OpenFile(filename, subdir, &end);
223  if (in == nullptr) return;
224 
225  end += ftell(in);
226 
227  /* for each line in the file */
228  while ((size_t)ftell(in) < end && fgets(buffer, sizeof(buffer), in)) {
229  char c, *s;
230  /* trim whitespace from the left side */
231  for (s = buffer; *s == ' ' || *s == '\t'; s++) {}
232 
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--;
236  *e = '\0';
237 
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;
242  /* add to comment */
243  if (ns > a) {
244  a = max(a, 128U);
245  do a *= 2; while (a < ns);
246  comment = ReallocT(comment, comment_alloc = a);
247  }
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
252  continue;
253  }
254 
255  /* it's a group? */
256  if (s[0] == '[') {
257  if (e[-1] != ']') {
258  this->ReportFileError("ini: invalid group name '", buffer, "'");
259  } else {
260  e--;
261  }
262  s++; // skip [
263  group = new IniGroup(this, s, e - 1);
264  if (comment_size != 0) {
265  group->comment = stredup(comment, comment + comment_size - 1);
266  comment_size = 0;
267  }
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);
272  if (comment_size) {
273  item->comment = stredup(comment, comment + comment_size - 1);
274  comment_size = 0;
275  }
276  continue;
277  }
278  char *t;
279  /* find end of keyname */
280  if (*s == '\"') {
281  s++;
282  for (t = s; *t != '\0' && *t != '\"'; t++) {}
283  if (*t == '\"') *t = ' ';
284  } else {
285  for (t = s; *t != '\0' && *t != '=' && *t != '\t' && *t != ' '; t++) {}
286  }
287 
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);
292  comment_size = 0;
293  }
294 
295  /* find start of parameter */
296  while (*t == '=' || *t == ' ' || *t == '\t') t++;
297 
298  bool quoted = (*t == '\"');
299  /* remove starting quotation marks */
300  if (*t == '\"') t++;
301  /* remove ending quotation marks */
302  e = t + strlen(t);
303  if (e > t && e[-1] == '\"') e--;
304  *e = '\0';
305 
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));
309  } else {
310  /* it's an orphan item */
311  this->ReportFileError("ini: '", buffer, "' outside of group");
312  }
313  }
314 
315  if (comment_size > 0) {
316  this->comment = stredup(comment, comment + comment_size - 1);
317  comment_size = 0;
318  }
319 
320  free(comment);
321  fclose(in);
322 }
323 
A group within an ini file.
Definition: ini_type.h:36
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
char * comment
comment for group
Definition: ini_type.h:42
IniItem ** last_item
the last item in the group
Definition: ini_type.h:40
virtual ~IniLoadFile()
Free everything we loaded.
Definition: ini_load.cpp:140
IniItem * item
the first item in the group
Definition: ini_type.h:39
virtual FILE * OpenFile(const char *filename, Subdirectory subdir, size_t *size)=0
Open the INI file.
void RemoveGroup(const char *name)
Remove the group with the given name.
Definition: ini_load.cpp:177
IniGroup * GetGroup(const char *name, size_t len=0, bool create_new=true)
Get the group with the given name.
Definition: ini_load.cpp:154
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:24
IniItem * next
The next item in this group.
Definition: ini_type.h:24
char * comment
last comment in file
Definition: ini_type.h:55
~IniGroup()
Free everything we loaded.
Definition: ini_load.cpp:87
A single "line" in an ini file.
Definition: ini_type.h:23
Functions related to low-level strings.
IniGroup * group
the first group in the ini
Definition: ini_type.h:53
void str_validate(char *str, const char *last, StringValidationSettings settings)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
Definition: string.cpp:194
IniGroupType type
type of group
Definition: ini_type.h:38
Functions related to the allocation of memory.
void SetValue(const char *value)
Replace the current value with another value.
Definition: ini_load.cpp:47
void LoadFromDisk(const char *filename, Subdirectory subdir)
Load the Ini file&#39;s data from the disk.
Definition: ini_load.cpp:210
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
static T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type...
Definition: alloc_func.hpp:111
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:136
char * value
The value of this item.
Definition: ini_type.h:26
Values of the form "landscape = hilly".
Definition: ini_type.h:17
Types related to reading/writing &#39;*.ini&#39; files.
void Clear()
Clear all items in the group.
Definition: ini_load.cpp:118
IniGroup ** last_group
the last group in the ini
Definition: ini_type.h:54
IniLoadFile(const char *const *list_group_names=nullptr, const char *const *seq_group_names=nullptr)
Construct a new in-memory Ini file representation.
Definition: ini_load.cpp:130
A list of uninterpreted lines, terminated by the next group block.
Definition: ini_type.h:19
virtual void ReportFileError(const char *const pre, const char *const buffer, const char *const post)=0
Report an error about the file contents.
char * name
The name of this item.
Definition: ini_type.h:25
IniItem(struct IniGroup *parent, const char *name, const char *last=nullptr)
Construct a new in-memory item of an Ini file.
Definition: ini_load.cpp:24
IniItem * GetItem(const char *name, bool create)
Get the item with the given name, and if it doesn&#39;t exist and create is true it creates a new item...
Definition: ini_load.cpp:103
IniGroup(struct IniLoadFile *parent, const char *name, const char *last=nullptr)
Construct a new in-memory group of an Ini file.
Definition: ini_load.cpp:59
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
IniGroup * next
the next group within this file
Definition: ini_type.h:37
~IniItem()
Free everything we loaded.
Definition: ini_load.cpp:34
A list of values, separated by and terminated by the next group block.
Definition: ini_type.h:18
char * name
name of group
Definition: ini_type.h:41
Ini file that only supports loading.
Definition: ini_type.h:52
char * comment
The comment associated with this item.
Definition: ini_type.h:27
Functions related to memory operations.
const char *const * list_group_names
nullptr terminated list with group names that are lists
Definition: ini_type.h:56
const char *const * seq_group_names
nullptr terminated list with group names that are sequences.
Definition: ini_type.h:57