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
This commit is contained in:
Panu Matilainen 2008-05-13 14:40:55 +03:00
parent 7c4cae7030
commit 9de4354a12
2 changed files with 63 additions and 0 deletions

View File

@ -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;
}

View File

@ -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 */