28 this->name =
stredup(name, last);
29 str_validate(this->name, this->name + strlen(this->name));
63 this->name =
stredup(name, last);
64 str_validate(this->name, this->name + strlen(this->name));
111 if (!create)
return NULL;
114 return new IniItem(
this, name, NULL);
135 list_group_names(list_group_names),
136 seq_group_names(seq_group_names)
158 if (len == 0) len = strlen(name);
167 if (!create_new)
return NULL;
181 size_t len = strlen(name);
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;
198 this->group = this->group->
next;
220 uint comment_size = 0;
221 uint comment_alloc = 0;
224 FILE *in = this->
OpenFile(filename, subdir, &end);
225 if (in == NULL)
return;
230 while ((
size_t)ftell(in) < end && fgets(buffer,
sizeof(buffer), in)) {
233 for (s = buffer; *s ==
' ' || *s ==
'\t'; s++) {}
236 char *e = s + strlen(s);
237 while (e > s && ((c = e[-1]) ==
'\n' || c ==
'\r' || c ==
' ' || c ==
'\t')) e--;
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';
253 memcpy(comment + pos, s, e - s);
265 group =
new IniGroup(
this, s, e - 1);
266 if (comment_size != 0) {
270 }
else if (group != NULL) {
284 for (t = s; *t !=
'\0' && *t !=
'\"'; t++) {}
285 if (*t ==
'\"') *t =
' ';
287 for (t = s; *t !=
'\0' && *t !=
'=' && *t !=
'\t' && *t !=
' '; t++) {}
292 if (comment_size != 0) {
298 while (*t ==
'=' || *t ==
' ' || *t ==
'\t') t++;
300 bool quoted = (*t ==
'\"');
305 if (e > t && e[-1] ==
'\"') e--;
317 if (comment_size > 0) {
318 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.
IniLoadFile(const char *const *list_group_names=NULL, const char *const *seq_group_names=NULL)
Construct a new in-memory Ini file representation.
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
IniItem(struct IniGroup *parent, const char *name, const char *last=NULL)
Construct a new in-memory item of an Ini file.
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 * 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=NULL)
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
NULL terminated list with group names that are lists.
const char *const * seq_group_names
NULL terminated list with group names that are sequences.