From 4393d0d5a9189db8b64324a2e1dcfc61c1b23428 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Tue, 25 Oct 2016 12:18:54 +0300 Subject: [PATCH] Force header data formatting through a central function Make rpmHeaderFormatFuncByName() and rpmHeaderFormatFuncByValue() return an opaque pointer to the extension itself (instead of the actual function pointer) and drop Func from the function and type names to signify the point. Add rpmHeaderFormatCall() function through which all formatting has to be done now. This is not supposed to change anything in itself, but opens up possibilities for central error checking etc. --- lib/formats.c | 36 ++++++++++++++++++------------------ lib/headerfmt.c | 6 +++--- lib/misc.h | 10 +++++++--- lib/rpmtd.c | 6 +++--- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/lib/formats.c b/lib/formats.c index 68c623d77..e5690a228 100644 --- a/lib/formats.c +++ b/lib/formats.c @@ -21,11 +21,13 @@ #include "debug.h" +typedef char * (*headerTagFormatFunction) (rpmtd td); + /** \ingroup header * Define header tag output formats. */ -struct headerFormatFunc_s { +struct headerFmt_s { rpmtdFormats fmt; /*!< Value of extension */ const char *name; /*!< Name of extension. */ headerTagFormatFunction func; /*!< Pointer to formatter function. */ @@ -508,7 +510,7 @@ static char * expandFormat(rpmtd td) return val; } -static const struct headerFormatFunc_s rpmHeaderFormats[] = { +static const struct headerFmt_s rpmHeaderFormats[] = { { RPMTD_FORMAT_STRING, "string", stringFormat }, { RPMTD_FORMAT_ARMOR, "armor", armorFormat }, { RPMTD_FORMAT_BASE64, "base64", base64Format }, @@ -533,31 +535,29 @@ static const struct headerFormatFunc_s rpmHeaderFormats[] = { { -1, NULL, NULL } }; -headerTagFormatFunction rpmHeaderFormatFuncByName(const char *fmt) +headerFmt rpmHeaderFormatByName(const char *fmt) { - const struct headerFormatFunc_s * ext; - headerTagFormatFunction func = NULL; + const struct headerFmt_s * ext; for (ext = rpmHeaderFormats; ext->name != NULL; ext++) { - if (rstreq(ext->name, fmt)) { - func = ext->func; - break; - } + if (rstreq(ext->name, fmt)) + return ext; } - return func; + return NULL; } -headerTagFormatFunction rpmHeaderFormatFuncByValue(rpmtdFormats fmt) +headerFmt rpmHeaderFormatByValue(rpmtdFormats fmt) { - const struct headerFormatFunc_s * ext; - headerTagFormatFunction func = NULL; + const struct headerFmt_s * ext; for (ext = rpmHeaderFormats; ext->name != NULL; ext++) { - if (fmt == ext->fmt) { - func = ext->func; - break; - } + if (fmt == ext->fmt) + return ext; } - return func; + return NULL; } +char *rpmHeaderFormatCall(headerFmt fmt, rpmtd td) +{ + return (fmt != NULL) ? fmt->func(td) : NULL; +} diff --git a/lib/headerfmt.c b/lib/headerfmt.c index f6fd707bf..43311cd96 100644 --- a/lib/headerfmt.c +++ b/lib/headerfmt.c @@ -20,7 +20,7 @@ */ typedef struct sprintfTag_s * sprintfTag; struct sprintfTag_s { - headerTagFormatFunction fmt; + headerFmt fmt; rpmTagVal tag; int justOne; char * format; @@ -246,7 +246,7 @@ static int findTag(headerSprintfArgs hsa, sprintfToken token, const char * name) /* Search extensions for specific format. */ if (stag->type != NULL) - stag->fmt = rpmHeaderFormatFuncByName(stag->type); + stag->fmt = rpmHeaderFormatByName(stag->type); return stag->fmt ? 0 : 1; } @@ -625,7 +625,7 @@ static char * formatValue(headerSprintfArgs hsa, sprintfTag tag, int element) if ((td = getData(hsa, tag->tag)) && td->count > element) { td->ix = element; /* Ick, use iterators instead */ - val = tag->fmt(td); + val = rpmHeaderFormatCall(tag->fmt, td); } else { val = xstrdup("(none)"); } diff --git a/lib/misc.h b/lib/misc.h index c519afb8e..fcd258438 100644 --- a/lib/misc.h +++ b/lib/misc.h @@ -11,6 +11,8 @@ #include /* for headerGetFlags typedef, duh.. */ #include "lib/rpmfs.h" +typedef const struct headerFmt_s * headerFmt; + #ifdef __cplusplus extern "C" { #endif @@ -25,17 +27,19 @@ char * rpmVerifyString(uint32_t verifyResult, const char *pad); RPM_GNUC_INTERNAL char * rpmFFlagsString(uint32_t fflags, const char *pad); -typedef char * (*headerTagFormatFunction) (rpmtd td); typedef int (*headerTagTagFunction) (Header h, rpmtd td, headerGetFlags hgflags); RPM_GNUC_INTERNAL headerTagTagFunction rpmHeaderTagFunc(rpmTagVal tag); RPM_GNUC_INTERNAL -headerTagFormatFunction rpmHeaderFormatFuncByName(const char *fmt); +headerFmt rpmHeaderFormatByName(const char *fmt); RPM_GNUC_INTERNAL -headerTagFormatFunction rpmHeaderFormatFuncByValue(rpmtdFormats fmt); +headerFmt rpmHeaderFormatByValue(rpmtdFormats fmt); + +RPM_GNUC_INTERNAL +char * rpmHeaderFormatCall(headerFmt fmt, rpmtd td); RPM_GNUC_INTERNAL int headerFindSpec(Header h); diff --git a/lib/rpmtd.c b/lib/rpmtd.c index b933c8863..9ac476848 100644 --- a/lib/rpmtd.c +++ b/lib/rpmtd.c @@ -253,12 +253,12 @@ uint64_t rpmtdGetNumber(rpmtd td) char *rpmtdFormat(rpmtd td, rpmtdFormats fmt, const char *errmsg) { - headerTagFormatFunction func = rpmHeaderFormatFuncByValue(fmt); + headerFmt ext = rpmHeaderFormatByValue(fmt); const char *err = NULL; char *str = NULL; - if (func) { - str = func(td); + if (ext) { + str = rpmHeaderFormatCall(ext, td); } else { err = _("Unknown format"); }