libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmRptFile.c 2.8KB

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