From 9de4354a1258abf81963f450a654275cfbc08c33 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Tue, 13 May 2008 14:40:55 +0300 Subject: [PATCH] Add support for iterating over tag data container - rpmtd iterator init + next methods - string accessor method for RPM_STRING_TYPE and RPM_STRING_ARRAY_TYPE --- lib/rpmtd.c | 39 +++++++++++++++++++++++++++++++++++++++ lib/rpmtd.h | 24 ++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/lib/rpmtd.c b/lib/rpmtd.c index 51b8a865b..5d46bf916 100644 --- a/lib/rpmtd.c +++ b/lib/rpmtd.c @@ -52,3 +52,42 @@ rpmTag rpmtdTag(rpmtd td) return td->tag; } +int rpmtdInit(rpmtd td) +{ + assert(td != NULL); + + /* XXX check that this is an array type? */ + td->ix = -1; + return 0; +} + +int rpmtdNext(rpmtd td) +{ + int i = -1; + + assert(td != NULL); + if (++td->ix >= 0) { + if (td->ix < td->count) { + i = td->ix; + } else { + td->ix = i; + } + } + return i; +} + +const char * rpmtdGetString(rpmtd td) +{ + const char *str = NULL; + + assert(td != NULL); + + if (td->type == RPM_STRING_TYPE) { + str = (const char *) td->data; + } else if (td->type == RPM_STRING_ARRAY_TYPE) { + /* XXX TODO: check for array bounds */ + int ix = (td->ix >= 0 ? td->ix : 0); + str = *((const char**) td->data + ix); + } + return str; +} diff --git a/lib/rpmtd.h b/lib/rpmtd.h index 27254c5cb..3bd1c9b65 100644 --- a/lib/rpmtd.h +++ b/lib/rpmtd.h @@ -56,4 +56,28 @@ rpm_count_t rpmtdCount(rpmtd td); */ rpmTag rpmtdTag(rpmtd td); +/** \ingroup rpmtd + * Initialize tag container for iteration + * @param td Tag data container + * @return 0 on success + */ +int rpmtdInit(rpmtd td); + +/** \ingroup rpmtd + * Iterate over tag data container. + * @param td Tag data container + * @return Tag data container iterator index, -1 on termination + */ +int rpmtdNext(rpmtd td); + +/** \ingroup rpmtd + * Return string data from tag container. + * For string types, just return the string. On string array types, + * return the string from current iteration index. If the tag container + * is not for a string type, NULL is returned. + * @param td Tag data container + * @return String constant from container, NULL on error + */ +const char * rpmtdGetString(rpmtd td); + #endif /* _RPMTD_H */