26 this->name =
stredup(name, last);
27 str_validate(this->name, this->name + strlen(this->name));
61 this->name =
stredup(name, last);
62 str_validate(this->name, this->name + strlen(this->name));
109 if (!create)
return nullptr;
112 return new IniItem(
this, name,
nullptr);
121 this->
item =
nullptr;
133 list_group_names(list_group_names),
134 seq_group_names(seq_group_names)
156 if (len == 0) len = strlen(name);
165 if (!create_new)
return nullptr;
179 size_t len = strlen(name);
184 for (group = this->group; group !=
nullptr; prev =
group, group = group->
next) {
185 if (strncmp(group->name, name, len) == 0) {
190 if (group ==
nullptr)
return;
192 if (prev !=
nullptr) {
196 this->group = this->group->
next;
200 group->
next =
nullptr;
218 uint comment_size = 0;
219 uint comment_alloc = 0;
222 FILE *in = this->
OpenFile(filename, subdir, &end);
223 if (in ==
nullptr)
return;
228 while ((
size_t)ftell(in) < end && fgets(buffer,
sizeof(buffer), in)) {
231 for (s = buffer; *s ==
' ' || *s ==
'\t'; s++) {}
234 char *e = s + strlen(s);
235 while (e > s && ((c = e[-1]) ==
'\n' || c ==
'\r' || c ==
' ' || c ==
'\t')) e--;
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;
245 do a *= 2;
while (a < ns);
246 comment =
ReallocT(comment, comment_alloc = a);
248 uint pos = comment_size;
249 comment_size += (e - s + 1);
250 comment[pos + e - s] =
'\n';
251 memcpy(comment + pos, s, e - s);
263 group =
new IniGroup(
this, s, e - 1);
264 if (comment_size != 0) {
268 }
else if (group !=
nullptr) {
282 for (t = s; *t !=
'\0' && *t !=
'\"'; t++) {}
283 if (*t ==
'\"') *t =
' ';
285 for (t = s; *t !=
'\0' && *t !=
'=' && *t !=
'\t' && *t !=
' '; t++) {}
290 if (comment_size != 0) {
296 while (*t ==
'=' || *t ==
' ' || *t ==
'\t') t++;
298 bool quoted = (*t ==
'\"');
303 if (e > t && e[-1] ==
'\"') e--;
307 item->
value = (!quoted && e == t) ?
nullptr :
stredup(t);
315 if (comment_size > 0) {
316 this->comment =
stredup(comment, comment + comment_size - 1);
A group within an ini file.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
char * comment
comment for group
IniItem ** last_item
the last item in the group
virtual ~IniLoadFile()
Free everything we loaded.
IniItem * item
the first item in the group
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.
IniGroup * GetGroup(const char *name, size_t len=0, bool create_new=true)
Get the group with the given name.
static T max(const T a, const T b)
Returns the maximum of two values.
IniItem * next
The next item in this group.
char * comment
last comment in file
~IniGroup()
Free everything we loaded.
A single "line" in an ini file.
Functions related to low-level strings.
IniGroup * group
the first group in the ini
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...
IniGroupType type
type of group
Functions related to the allocation of memory.
void SetValue(const char *value)
Replace the current value with another value.
void LoadFromDisk(const char *filename, Subdirectory subdir)
Load the Ini file's data from the disk.
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...
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
char * value
The value of this item.
Values of the form "landscape = hilly".
Types related to reading/writing '*.ini' files.
void Clear()
Clear all items in the group.
IniGroup ** last_group
the last group in the ini
IniLoadFile(const char *const *list_group_names=nullptr, const char *const *seq_group_names=nullptr)
Construct a new in-memory Ini file representation.
A list of uninterpreted lines, terminated by the next group block.
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.
IniItem(struct IniGroup *parent, const char *name, const char *last=nullptr)
Construct a new in-memory item of an Ini file.
IniItem * GetItem(const char *name, bool create)
Get the item with the given name, and if it doesn't exist and create is true it creates a new item...
IniGroup(struct IniLoadFile *parent, const char *name, const char *last=nullptr)
Construct a new in-memory group of an Ini file.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
IniGroup * next
the next group within this file
~IniItem()
Free everything we loaded.
A list of values, separated by and terminated by the next group block.
Ini file that only supports loading.
char * comment
The comment associated with this item.
Functions related to memory operations.
const char *const * list_group_names
nullptr terminated list with group names that are lists
const char *const * seq_group_names
nullptr terminated list with group names that are sequences.