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.
This commit is contained in:
Panu Matilainen 2016-10-25 12:18:54 +03:00
parent f3f0168526
commit 4393d0d5a9
4 changed files with 31 additions and 27 deletions

View File

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

View File

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

View File

@ -11,6 +11,8 @@
#include <rpm/header.h> /* 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);

View File

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