From ce70ac871764ec835d5c83b3115d3a7236196838 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Wed, 21 May 2008 09:22:38 +0300 Subject: [PATCH] Add rpmtdDup() method for deep copying of tag containers --- lib/rpmtd.c | 26 ++++++++++++++++++++++++++ lib/rpmtd.h | 10 ++++++++++ 2 files changed, 36 insertions(+) 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 */ -- 2.7.4