mirror of https://github.com/OpenTTD/OpenTTD
(svn r22170) -Add: Add IGT_SEQUENCE type for loading ini group lines without further interpretation.
parent
6a88af662b
commit
fa9c193539
|
@ -65,8 +65,7 @@ IniGroup::IniGroup(IniLoadFile *parent, const char *name, size_t len) : next(NUL
|
||||||
*parent->last_group = this;
|
*parent->last_group = this;
|
||||||
parent->last_group = &this->next;
|
parent->last_group = &this->next;
|
||||||
|
|
||||||
if (parent->list_group_names == NULL) return;
|
if (parent->list_group_names != NULL) {
|
||||||
|
|
||||||
for (uint i = 0; parent->list_group_names[i] != NULL; i++) {
|
for (uint i = 0; parent->list_group_names[i] != NULL; i++) {
|
||||||
if (strcmp(this->name, parent->list_group_names[i]) == 0) {
|
if (strcmp(this->name, parent->list_group_names[i]) == 0) {
|
||||||
this->type = IGT_LIST;
|
this->type = IGT_LIST;
|
||||||
|
@ -74,6 +73,15 @@ IniGroup::IniGroup(IniLoadFile *parent, const char *name, size_t len) : next(NUL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (parent->seq_group_names != NULL) {
|
||||||
|
for (uint i = 0; parent->seq_group_names[i] != NULL; i++) {
|
||||||
|
if (strcmp(this->name, parent->seq_group_names[i]) == 0) {
|
||||||
|
this->type = IGT_SEQUENCE;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Free everything we loaded. */
|
/** Free everything we loaded. */
|
||||||
IniGroup::~IniGroup()
|
IniGroup::~IniGroup()
|
||||||
|
@ -116,10 +124,14 @@ void IniGroup::Clear()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new in-memory Ini file representation.
|
* Construct a new in-memory Ini file representation.
|
||||||
* @param list_group_names A NULL terminated list with groups that should be
|
* @param list_group_names A \c NULL terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST
|
||||||
* loaded as lists instead of variables.
|
* @param seq_group_names A \c NULL terminated list with group names that should be loaded as lists of names. @see IGT_SEQUENCE
|
||||||
*/
|
*/
|
||||||
IniLoadFile::IniLoadFile(const char * const *list_group_names) : group(NULL), comment(NULL), list_group_names(list_group_names)
|
IniLoadFile::IniLoadFile(const char * const *list_group_names, const char * const *seq_group_names) :
|
||||||
|
group(NULL),
|
||||||
|
comment(NULL),
|
||||||
|
list_group_names(list_group_names),
|
||||||
|
seq_group_names(seq_group_names)
|
||||||
{
|
{
|
||||||
this->last_group = &this->group;
|
this->last_group = &this->group;
|
||||||
}
|
}
|
||||||
|
@ -222,8 +234,8 @@ void IniLoadFile::LoadFromDisk(const char *filename)
|
||||||
while (e > s && ((c = e[-1]) == '\n' || c == '\r' || c == ' ' || c == '\t')) e--;
|
while (e > s && ((c = e[-1]) == '\n' || c == '\r' || c == ' ' || c == '\t')) e--;
|
||||||
*e = '\0';
|
*e = '\0';
|
||||||
|
|
||||||
/* skip comments and empty lines */
|
/* Skip comments and empty lines outside IGT_SEQUENCE groups. */
|
||||||
if (*s == '#' || *s == ';' || *s == '\0') {
|
if ((group == NULL || group->type != IGT_SEQUENCE) && (*s == '#' || *s == ';' || *s == '\0')) {
|
||||||
uint ns = comment_size + (e - s + 1);
|
uint ns = comment_size + (e - s + 1);
|
||||||
uint a = comment_alloc;
|
uint a = comment_alloc;
|
||||||
/* add to comment */
|
/* add to comment */
|
||||||
|
@ -253,6 +265,15 @@ void IniLoadFile::LoadFromDisk(const char *filename)
|
||||||
comment_size = 0;
|
comment_size = 0;
|
||||||
}
|
}
|
||||||
} else if (group != NULL) {
|
} else if (group != NULL) {
|
||||||
|
if (group->type == IGT_SEQUENCE) {
|
||||||
|
/* A sequence group, use the line as item name without further interpretation. */
|
||||||
|
IniItem *item = new IniItem(group, buffer, e - buffer);
|
||||||
|
if (comment_size) {
|
||||||
|
item->comment = strndup(comment, comment_size);
|
||||||
|
comment_size = 0;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
char *t;
|
char *t;
|
||||||
/* find end of keyname */
|
/* find end of keyname */
|
||||||
if (*s == '\"') {
|
if (*s == '\"') {
|
||||||
|
|
|
@ -14,8 +14,9 @@
|
||||||
|
|
||||||
/** Types of groups */
|
/** Types of groups */
|
||||||
enum IniGroupType {
|
enum IniGroupType {
|
||||||
IGT_VARIABLES = 0, ///< values of the form "landscape = hilly"
|
IGT_VARIABLES = 0, ///< Values of the form "landscape = hilly".
|
||||||
IGT_LIST = 1, ///< a list of values, seperated by \n and terminated by the next group block
|
IGT_LIST = 1, ///< A list of values, separated by \n and terminated by the next group block.
|
||||||
|
IGT_SEQUENCE = 2, ///< A list of uninterpreted lines, terminated by the next group block.
|
||||||
};
|
};
|
||||||
|
|
||||||
/** A single "line" in an ini file. */
|
/** A single "line" in an ini file. */
|
||||||
|
@ -53,8 +54,9 @@ struct IniLoadFile {
|
||||||
IniGroup **last_group; ///< the last group in the ini
|
IniGroup **last_group; ///< the last group in the ini
|
||||||
char *comment; ///< last comment in file
|
char *comment; ///< last comment in file
|
||||||
const char * const *list_group_names; ///< NULL terminated list with group names that are lists
|
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.
|
||||||
|
|
||||||
IniLoadFile(const char * const *list_group_names = NULL);
|
IniLoadFile(const char * const *list_group_names = NULL, const char * const *seq_group_names = NULL);
|
||||||
virtual ~IniLoadFile();
|
virtual ~IniLoadFile();
|
||||||
|
|
||||||
IniGroup *GetGroup(const char *name, size_t len = 0, bool create_new = true);
|
IniGroup *GetGroup(const char *name, size_t len = 0, bool create_new = true);
|
||||||
|
|
Loading…
Reference in New Issue