From: Panu Matilainen Date: Wed, 21 May 2008 06:22:38 +0000 (+0300) Subject: Add rpmtdDup() method for deep copying of tag containers X-Git-Tag: rpm-4.6.0-rc1~522 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ce70ac871764ec835d5c83b3115d3a7236196838;p=platform%2Fupstream%2Frpm.git Add rpmtdDup() method for deep copying of tag containers --- diff --git a/lib/rpmtd.c b/lib/rpmtd.c index 998dd32..8948233 100644 --- a/lib/rpmtd.c +++ b/lib/rpmtd.c @@ -205,3 +205,29 @@ int rpmtdFromArgi(rpmtd td, rpmTag tag, ARGI_t argi) td->data = argiData(argi); return 1; } + +rpmtd rpmtdDup(rpmtd td) +{ + rpmtd newtd = NULL; + char **data = NULL; + int i; + + assert(td != NULL); + /* TODO: permit other types too */ + if (td->type != RPM_STRING_ARRAY_TYPE && td->type != RPM_I18NSTRING_TYPE) { + return NULL; + } + + /* deep-copy container and data, drop immutable flag */ + newtd = rpmtdNew(); + memcpy(newtd, td, sizeof(*td)); + newtd->flags &= ~(RPMTD_IMMUTABLE); + + newtd->flags |= (RPMTD_ALLOCED | RPMTD_PTR_ALLOCED); + newtd->data = data = xmalloc(td->count * sizeof(*data)); + while ((i = rpmtdNext(td)) >= 0) { + data[i] = xstrdup(rpmtdGetString(td)); + } + + return newtd; +} diff --git a/lib/rpmtd.h b/lib/rpmtd.h index 1a441e4..c5e4a8f 100644 --- a/lib/rpmtd.h +++ b/lib/rpmtd.h @@ -179,4 +179,14 @@ int rpmtdFromArgv(rpmtd td, rpmTag tag, ARGV_t argv); */ int rpmtdFromArgi(rpmtd td, rpmTag tag, ARGI_t argi); +/* \ingroup rpmtd + * Perform deep copy of container. + * Create a modifiable copy of tag data container (on string arrays each + * string is separately allocated) + * @todo Only string arrays types are supported currently + * @param td Container to copy + * @return New container or NULL on error + */ +rpmtd rpmtdDup(rpmtd td); + #endif /* _RPMTD_H */