[plist] Pass array index/label to the parse function

I ran into the need to get at the label of labeled array element and the
best way seemed to be by setting the name field of the plfield_t item
passed to the parser function, and then found that PL_ParseSymtab
already does this. I then decided passing the array index would also be
good, and the offset field made sense.
This commit is contained in:
Bill Currie 2023-02-10 00:51:06 +09:00
parent 35df90cb3f
commit d5b93c20b3
2 changed files with 13 additions and 0 deletions

View file

@ -107,6 +107,16 @@ typedef int (*plparser_t) (const struct plfield_s *field,
point to a \a plelement_t object. This allows all the parse functions to
be used directly as either a \a plfield_t or \a plelement_t object's
\a parser.
\a PL_ParseLabeledArray and \a PL_ParseSymtab pass to the parser a field
with \a name set to the key of the current item and \a offset set to 0.
For \a PL_ParseArray, \a name is set to null and \a offset is set to the
current index. \a PL_ParseLabeledArray also sets \a offset to the current
index. \a type, \a parser, and \a data are taken from the \a plelement_t
passed in to them.
\a PL_ParseStruct passes the currently parsed field without any
modifications.
*/
typedef struct plfield_s {
const char *name; ///< matched by dictionary key

View file

@ -1330,6 +1330,7 @@ PL_ParseArray (const plfield_t *field, const plitem_t *array, void *data,
plitem_t *item = plarray->values[i];
void *eledata = &arr->a[i * element->stride];
f.offset = i;
if (!PL_CheckType (element->type, item->type)) {
char index[16];
snprintf (index, sizeof(index) - 1, "%d", i);
@ -1381,6 +1382,8 @@ PL_ParseLabeledArray (const plfield_t *field, const plitem_t *item,
plitem_t *item = current->value;
void *eledata = &arr->a[i * element->stride];
f.name = current->key;
f.offset = i;
if (!PL_CheckType (element->type, item->type)) {
char index[16];
snprintf (index, sizeof(index) - 1, "%zd", i);