From 177c2d8c1ed33391711524948fef5313c8624c75 Mon Sep 17 00:00:00 2001 From: Markus Mittendrein Date: Mon, 19 Aug 2019 13:42:48 +0200 Subject: Allow the warning handling callback to be set to a custom callback --- src/cc4group.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/cc4group.c') diff --git a/src/cc4group.c b/src/cc4group.c index df8854a..3c921e5 100644 --- a/src/cc4group.c +++ b/src/cc4group.c @@ -238,18 +238,20 @@ static char* cc4group_noerrorFormatter(int32_t const code, const char* const met return strdup("No Error"); } -// TODO: allow the user to set a warning callback with falling back to fprintf(stderr) by default -static void cc4group_warn(const CC4Group* const this, const char* const message, ...) +static void cc4group_printToStderrWarningCallback(const CC4Group* const this, const char* const format, ...) { (void)this; va_list ap; - va_start(ap, message); + va_start(ap, format); fputs("WARNING: ", stderr); - vfprintf(stderr, message, ap); + vfprintf(stderr, format, ap); fputs("\n", stderr); va_end (ap); } +static const CC4Group_WarningCallback cc4group_defaultWarningCallback = cc4group_printToStderrWarningCallback; +static CC4Group_WarningCallback cc4group_warn = cc4group_defaultWarningCallback; + #define AddCleanUpJob(func, data) CleanUpJobListPrepend(this->cleanupJobs, (CC4Group_CleanupJob){(CC4Group_CleanupFunc)func, data}); static void memScrambleHeader(uint8_t* const data) @@ -2185,6 +2187,11 @@ static void cc4group_setTmpMemoryStrategy(const CC4Group_TmpMemoryStrategy strat cc4group_tmpMemoryStrategy = strategy; } +static void cc4group_setWarningCallback(const CC4Group_WarningCallback warningCallback) +{ + cc4group_warn = (warningCallback == NULL) ? cc4group_defaultWarningCallback : warningCallback; +} + static const char* cc4group_getErrorMessage(CC4Group* const this) { assert(this); @@ -3244,6 +3251,7 @@ CC4Group_API cc4group = { }, .setTmpMemoryStrategy = cc4group_setTmpMemoryStrategy, + .setWarningCallback = cc4group_setWarningCallback, .new = cc4group_new, -- cgit v1.2.3-54-g00ecf