From f028c892ad7bf382af5fff320fa34f14579df8bc Mon Sep 17 00:00:00 2001 From: kevin Date: Sun, 17 Dec 2017 13:41:25 -0500 Subject: [PATCH] cmRptFile.c : Initial commit. --- Makefile.am | 4 +- cmRptFile.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++ cmRptFile.h | 30 ++++++++++++ 3 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 cmRptFile.c create mode 100644 cmRptFile.h diff --git a/Makefile.am b/Makefile.am index 1a30418..6687f22 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3,8 +3,8 @@ cmHDR = cmSRC = -cmHDR += src/libcm/cmErr.h src/libcm/cmCtx.h src/libcm/cmRpt.h src/libcm/cmGlobal.h src/libcm/cmComplexTypes.h src/libcm/cmFloatTypes.h src/libcm/cmPrefix.h -cmSRC += src/libcm/cmErr.c src/libcm/cmCtx.c src/libcm/cmRpt.c src/libcm/cmGlobal.c src/libcm/cmComplexTypes.c +cmHDR += src/libcm/cmErr.h src/libcm/cmCtx.h src/libcm/cmRpt.h src/libcm/cmRptFile.h src/libcm/cmGlobal.h src/libcm/cmComplexTypes.h src/libcm/cmFloatTypes.h src/libcm/cmPrefix.h +cmSRC += src/libcm/cmErr.c src/libcm/cmCtx.c src/libcm/cmRpt.c src/libcm/cmRptFile.c src/libcm/cmGlobal.c src/libcm/cmComplexTypes.c cmHDR += src/libcm/cmSerialize.h src/libcm/cmSymTbl.h src/libcm/cmHashTbl.h src/libcm/cmFileSys.h src/libcm/cmFile.h cmSRC += src/libcm/cmSerialize.c src/libcm/cmSymTbl.c src/libcm/cmHashTbl.c src/libcm/cmFileSys.c src/libcm/cmFile.c diff --git a/cmRptFile.c b/cmRptFile.c new file mode 100644 index 0000000..1bfe22b --- /dev/null +++ b/cmRptFile.c @@ -0,0 +1,128 @@ +#include "cmPrefix.h" +#include "cmGlobal.h" +#include "cmRpt.h" +#include "cmErr.h" +#include "cmCtx.h" +#include "cmFile.h" +#include "cmMem.h" +#include "cmMallocDebug.h" +#include "cmRptFile.h" + +cmRptFileH_t cmRptFileNullHandle = cmSTATIC_NULL_HANDLE; + +typedef struct +{ + cmErr_t err; + cmFileH_t printFileH; + cmFileH_t errorFileH; + cmRpt_t rpt; + +} cmRptFile_t; + +cmRptFile_t* _cmRptFileHandleToPtr( cmRptFileH_t h ) +{ + cmRptFile_t* p = (cmRptFile_t*)h.h; + assert(p != NULL ); + return p; +} + +void _cmRptFilePrintFunc(void* cmRptUserPtr, const cmChar_t* text) +{ + cmRptFile_t* p = (cmRptFile_t*)cmRptUserPtr; + if( cmFileIsValid(p->printFileH)) + cmFilePrint(p->printFileH,text); +} + +void _cmRptFileErrorFunc(void* cmRptUserPtr, const cmChar_t* text) +{ + cmRptFile_t* p = (cmRptFile_t*)cmRptUserPtr; + if( cmFileIsValid(p->errorFileH)) + cmFilePrint(p->errorFileH,text); +} + +cmRfRC_t _cmRptFileClose( cmRptFile_t* p ) +{ + if( cmFileClose( &p->printFileH ) != kOkFileRC ) + cmErrMsg(&p->err,kFileFailRfRC,"Unable to close the print file."); + + if( cmFileClose( &p->errorFileH ) != kOkFileRC ) + cmErrMsg(&p->err,kFileFailRfRC,"Unable to close the error file."); + + cmMemFree(p); + + return kOkRfRC; +} + +cmRfRC_t cmRptFileCreate( cmCtx_t* ctx, cmRptFileH_t* hp, const cmChar_t* printFn, const cmChar_t* errorFn ) +{ + cmRptPrintFunc_t printFunc = NULL; + cmRptPrintFunc_t errorFunc = NULL; + cmRfRC_t rc; + if((rc = cmRptFileClose(hp)) != kOkRfRC ) + return rc; + + cmRptFile_t* p = cmMemAllocZ( cmRptFile_t, 1 ); + + cmErrSetup(&p->err,&ctx->rpt,"Rpt File"); + + if( printFn != NULL ) + { + if((rc = cmFileOpen(&p->printFileH,printFn,kWriteFileFl,p->err.rpt)) == kOkFileRC ) + printFunc = _cmRptFilePrintFunc; + else + { + rc = cmErrMsg(&p->err,kFileFailRfRC,"Unable to open the print file '%s'.", cmStringNullGuard(printFn)); + goto errLabel; + } + } + + if( errorFn != NULL ) + { + if((rc = cmFileOpen(&p->errorFileH,errorFn,kWriteFileFl,p->err.rpt)) == kOkFileRC ) + errorFunc = _cmRptFileErrorFunc; + else + { + rc = cmErrMsg(&p->err,kFileFailRfRC,"Unable to open the error file '%s'.", cmStringNullGuard(errorFn)); + goto errLabel; + } + } + + if( errorFunc == NULL ) + errorFunc = printFunc; + + cmRptSetup(&p->rpt,printFunc,errorFunc,p); + + hp->h = p; + + errLabel: + if( rc != kOkRfRC ) + _cmRptFileClose(p); + + return rc; +} + +cmRfRC_t cmRptFileClose( cmRptFileH_t* hp ) +{ + cmRfRC_t rc = kOkRfRC; + + if( hp==NULL || cmRptFileIsValid(*hp)==false) + return rc; + + cmRptFile_t* p = _cmRptFileHandleToPtr( *hp ); + + if((rc = _cmRptFileClose(p)) != kOkRfRC ) + return rc; + + hp->h = NULL; + + return rc; +} + +bool cmRptFileIsValid( cmRptFileH_t h ) +{ return h.h != NULL; } + +cmRpt_t* cmRptFileRpt( cmRptFileH_t h ) +{ + cmRptFile_t* p = _cmRptFileHandleToPtr( h ); + return &p->rpt; +} diff --git a/cmRptFile.h b/cmRptFile.h new file mode 100644 index 0000000..0821fd5 --- /dev/null +++ b/cmRptFile.h @@ -0,0 +1,30 @@ +#ifndef cmRptFile_h +#define cmRptFile_h + +#ifdef __cplusplus +extern "C" { +#endif + + enum + { + kOkRfRC = cmOkRC, + kFileFailRfRC + }; + + typedef unsigned cmRfRC_t; + typedef cmHandle_t cmRptFileH_t; + + extern cmRptFileH_t cmRptFileNullHandle; + + cmRfRC_t cmRptFileCreate( cmCtx_t* ctx, cmRptFileH_t* hp, const cmChar_t* printFn, const cmChar_t* errorFn ); + cmRfRC_t cmRptFileClose( cmRptFileH_t* hp ); + + bool cmRptFileIsValid( cmRptFileH_t h ); + + cmRpt_t* cmRptFileRpt( cmRptFileH_t h ); + +#ifdef __cplusplus +} +#endif + +#endif