mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2024-11-10 15:22:04 +00:00
[util] Add code to parse a plist array to an array
This commit is contained in:
parent
0945f30731
commit
c6a8829a52
2 changed files with 57 additions and 2 deletions
|
@ -99,6 +99,14 @@ typedef struct plfield_s {
|
||||||
void *data; ///< additional data for \a parser
|
void *data; ///< additional data for \a parser
|
||||||
} plfield_t;
|
} plfield_t;
|
||||||
|
|
||||||
|
typedef struct plelement_s {
|
||||||
|
pltype_t type; ///< the required type of the array elements
|
||||||
|
size_t stride; ///< the size of each element
|
||||||
|
void *(*alloc) (size_t size); ///< allocator for array memory
|
||||||
|
plparser_t parser; ///< custom parser function
|
||||||
|
void *data; ///< additional data for \a parser
|
||||||
|
} plelement_t;
|
||||||
|
|
||||||
/** Create an in-memory representation of the contents of a property list.
|
/** Create an in-memory representation of the contents of a property list.
|
||||||
|
|
||||||
\param string the saved plist, as read from a file.
|
\param string the saved plist, as read from a file.
|
||||||
|
@ -316,6 +324,8 @@ void PL_Free (plitem_t *item);
|
||||||
*/
|
*/
|
||||||
int PL_ParseDictionary (const plfield_t *fields, const plitem_t *dict,
|
int PL_ParseDictionary (const plfield_t *fields, const plitem_t *dict,
|
||||||
void *data, plitem_t *messages);
|
void *data, plitem_t *messages);
|
||||||
|
int PL_ParseArray (const plfield_t *fields, const plitem_t *dict,
|
||||||
|
void *data, plitem_t *messages);
|
||||||
|
|
||||||
///@}
|
///@}
|
||||||
|
|
||||||
|
|
|
@ -1130,8 +1130,7 @@ PL_ParseDictionary (const plfield_t *fields, const plitem_t *dict, void *data,
|
||||||
void **list, **l;
|
void **list, **l;
|
||||||
dictkey_t *current;
|
dictkey_t *current;
|
||||||
int result;
|
int result;
|
||||||
int (*parser) (const plfield_t *, const plitem_t *, void *,
|
plparser_t parser;
|
||||||
plitem_t *);
|
|
||||||
|
|
||||||
if (dict->type != QFDictionary) {
|
if (dict->type != QFDictionary) {
|
||||||
pl_message (messages, dict, "error: not a dictionary object");
|
pl_message (messages, dict, "error: not a dictionary object");
|
||||||
|
@ -1177,3 +1176,49 @@ PL_ParseDictionary (const plfield_t *fields, const plitem_t *dict, void *data,
|
||||||
free (list);
|
free (list);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VISIBLE int
|
||||||
|
PL_ParseArray (const plfield_t *field, const plitem_t *array, void *data,
|
||||||
|
plitem_t *messages)
|
||||||
|
{
|
||||||
|
int result;
|
||||||
|
plparser_t parser;
|
||||||
|
plarray_t *plarray = (plarray_t *) array->data;
|
||||||
|
plelement_t *element = (plelement_t *) field->data;
|
||||||
|
typedef struct arr_s DARRAY_TYPE(byte) arr_t;
|
||||||
|
arr_t *arr;
|
||||||
|
plfield_t f = { 0, 0, element->type, element->parser, element->data };
|
||||||
|
|
||||||
|
if (array->type != QFArray) {
|
||||||
|
pl_message (messages, array, "error: not an array object");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (field->parser) {
|
||||||
|
parser = field->parser;
|
||||||
|
} else {
|
||||||
|
parser = pl_default_parser;
|
||||||
|
}
|
||||||
|
|
||||||
|
arr = DARRAY_ALLOCFIXED (arr_t, plarray->numvals * element->stride,
|
||||||
|
element->alloc);
|
||||||
|
|
||||||
|
for (int i = 0; i < plarray->numvals; i++) {
|
||||||
|
plitem_t *item = plarray->values[i];
|
||||||
|
void *eledata = &arr->a[i * element->stride];
|
||||||
|
|
||||||
|
if (item->type != element->type) {
|
||||||
|
pl_message (messages, item,
|
||||||
|
"error: element %d is the wrong type"
|
||||||
|
" Got %s, expected %s", i,
|
||||||
|
pl_types[element->type],
|
||||||
|
pl_types[item->type]);
|
||||||
|
result = 0;
|
||||||
|
} else {
|
||||||
|
if (!parser (&f, item, eledata, messages)) {
|
||||||
|
result = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*(arr_t **) data = arr;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue