libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmRptFile.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. #include "cmPrefix.h"
  4. #include "cmGlobal.h"
  5. #include "cmRpt.h"
  6. #include "cmErr.h"
  7. #include "cmCtx.h"
  8. #include "cmFile.h"
  9. #include "cmMem.h"
  10. #include "cmMallocDebug.h"
  11. #include "cmRptFile.h"
  12. cmRptFileH_t cmRptFileNullHandle = cmSTATIC_NULL_HANDLE;
  13. typedef struct
  14. {
  15. cmErr_t err;
  16. cmFileH_t printFileH;
  17. cmFileH_t errorFileH;
  18. cmRpt_t rpt;
  19. } cmRptFile_t;
  20. cmRptFile_t* _cmRptFileHandleToPtr( cmRptFileH_t h )
  21. {
  22. cmRptFile_t* p = (cmRptFile_t*)h.h;
  23. assert(p != NULL );
  24. return p;
  25. }
  26. void _cmRptFilePrintFunc(void* cmRptUserPtr, const cmChar_t* text)
  27. {
  28. cmRptFile_t* p = (cmRptFile_t*)cmRptUserPtr;
  29. if( cmFileIsValid(p->printFileH))
  30. cmFilePrint(p->printFileH,text);
  31. }
  32. void _cmRptFileErrorFunc(void* cmRptUserPtr, const cmChar_t* text)
  33. {
  34. cmRptFile_t* p = (cmRptFile_t*)cmRptUserPtr;
  35. if( cmFileIsValid(p->errorFileH))
  36. cmFilePrint(p->errorFileH,text);
  37. }
  38. cmRfRC_t _cmRptFileClose( cmRptFile_t* p )
  39. {
  40. if( cmFileClose( &p->printFileH ) != kOkFileRC )
  41. cmErrMsg(&p->err,kFileFailRfRC,"Unable to close the print file.");
  42. if( cmFileClose( &p->errorFileH ) != kOkFileRC )
  43. cmErrMsg(&p->err,kFileFailRfRC,"Unable to close the error file.");
  44. cmMemFree(p);
  45. return kOkRfRC;
  46. }
  47. cmRfRC_t cmRptFileCreate( cmCtx_t* ctx, cmRptFileH_t* hp, const cmChar_t* printFn, const cmChar_t* errorFn )
  48. {
  49. cmRptPrintFunc_t printFunc = NULL;
  50. cmRptPrintFunc_t errorFunc = NULL;
  51. cmRfRC_t rc;
  52. if((rc = cmRptFileClose(hp)) != kOkRfRC )
  53. return rc;
  54. cmRptFile_t* p = cmMemAllocZ( cmRptFile_t, 1 );
  55. cmErrSetup(&p->err,&ctx->rpt,"Rpt File");
  56. if( printFn != NULL )
  57. {
  58. if((rc = cmFileOpen(&p->printFileH,printFn,kWriteFileFl,p->err.rpt)) == kOkFileRC )
  59. printFunc = _cmRptFilePrintFunc;
  60. else
  61. {
  62. rc = cmErrMsg(&p->err,kFileFailRfRC,"Unable to open the print file '%s'.", cmStringNullGuard(printFn));
  63. goto errLabel;
  64. }
  65. }
  66. if( errorFn != NULL )
  67. {
  68. if((rc = cmFileOpen(&p->errorFileH,errorFn,kWriteFileFl,p->err.rpt)) == kOkFileRC )
  69. errorFunc = _cmRptFileErrorFunc;
  70. else
  71. {
  72. rc = cmErrMsg(&p->err,kFileFailRfRC,"Unable to open the error file '%s'.", cmStringNullGuard(errorFn));
  73. goto errLabel;
  74. }
  75. }
  76. if( errorFunc == NULL )
  77. errorFunc = printFunc;
  78. cmRptSetup(&p->rpt,printFunc,errorFunc,p);
  79. hp->h = p;
  80. errLabel:
  81. if( rc != kOkRfRC )
  82. _cmRptFileClose(p);
  83. return rc;
  84. }
  85. cmRfRC_t cmRptFileClose( cmRptFileH_t* hp )
  86. {
  87. cmRfRC_t rc = kOkRfRC;
  88. if( hp==NULL || cmRptFileIsValid(*hp)==false)
  89. return rc;
  90. cmRptFile_t* p = _cmRptFileHandleToPtr( *hp );
  91. if((rc = _cmRptFileClose(p)) != kOkRfRC )
  92. return rc;
  93. hp->h = NULL;
  94. return rc;
  95. }
  96. bool cmRptFileIsValid( cmRptFileH_t h )
  97. { return h.h != NULL; }
  98. cmRpt_t* cmRptFileRpt( cmRptFileH_t h )
  99. {
  100. cmRptFile_t* p = _cmRptFileHandleToPtr( h );
  101. return &p->rpt;
  102. }