From d5b93c20b39e3d94c7100b6d996d2f66ef0d8bec Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Fri, 10 Feb 2023 00:51:06 +0900 Subject: [PATCH] [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. --- include/QF/plist.h | 10 ++++++++++ libs/util/plist.c | 3 +++ 2 files changed, 13 insertions(+) diff --git a/include/QF/plist.h b/include/QF/plist.h index 9e68ed0bb..a663b0344 100644 --- a/include/QF/plist.h +++ b/include/QF/plist.h @@ -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 diff --git a/libs/util/plist.c b/libs/util/plist.c index be3cc2d47..9f4ca3292 100644 --- a/libs/util/plist.c +++ b/libs/util/plist.c @@ -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);