Browse Source

cmRptFile.c : Initial commit.

master
kevin 7 years ago
parent
commit
f028c892ad
3 changed files with 160 additions and 2 deletions
  1. 2
    2
      Makefile.am
  2. 128
    0
      cmRptFile.c
  3. 30
    0
      cmRptFile.h

+ 2
- 2
Makefile.am View File

@@ -3,8 +3,8 @@
3 3
 cmHDR = 
4 4
 cmSRC = 
5 5
 
6
-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
7
-cmSRC += src/libcm/cmErr.c src/libcm/cmCtx.c src/libcm/cmRpt.c src/libcm/cmGlobal.c src/libcm/cmComplexTypes.c
6
+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
7
+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
8 8
 
9 9
 cmHDR += src/libcm/cmSerialize.h src/libcm/cmSymTbl.h src/libcm/cmHashTbl.h src/libcm/cmFileSys.h src/libcm/cmFile.h 
10 10
 cmSRC += src/libcm/cmSerialize.c src/libcm/cmSymTbl.c src/libcm/cmHashTbl.c src/libcm/cmFileSys.c src/libcm/cmFile.c 

+ 128
- 0
cmRptFile.c View File

@@ -0,0 +1,128 @@
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
+
11
+cmRptFileH_t cmRptFileNullHandle = cmSTATIC_NULL_HANDLE;
12
+
13
+typedef struct
14
+{
15
+  cmErr_t   err;
16
+  cmFileH_t printFileH;
17
+  cmFileH_t errorFileH;
18
+  cmRpt_t   rpt;
19
+  
20
+} cmRptFile_t;
21
+
22
+cmRptFile_t* _cmRptFileHandleToPtr( cmRptFileH_t h )
23
+{
24
+  cmRptFile_t* p = (cmRptFile_t*)h.h;
25
+  assert(p != NULL );
26
+  return p;
27
+}
28
+
29
+void _cmRptFilePrintFunc(void* cmRptUserPtr, const cmChar_t* text)
30
+{
31
+  cmRptFile_t* p = (cmRptFile_t*)cmRptUserPtr;
32
+  if( cmFileIsValid(p->printFileH))
33
+    cmFilePrint(p->printFileH,text);
34
+}
35
+
36
+void _cmRptFileErrorFunc(void* cmRptUserPtr, const cmChar_t* text)
37
+{
38
+  cmRptFile_t* p = (cmRptFile_t*)cmRptUserPtr;
39
+  if( cmFileIsValid(p->errorFileH))
40
+    cmFilePrint(p->errorFileH,text);
41
+}
42
+
43
+cmRfRC_t _cmRptFileClose( cmRptFile_t* p )
44
+{
45
+  if( cmFileClose( &p->printFileH ) != kOkFileRC )
46
+    cmErrMsg(&p->err,kFileFailRfRC,"Unable to close the print file.");
47
+
48
+  if( cmFileClose( &p->errorFileH ) != kOkFileRC )
49
+    cmErrMsg(&p->err,kFileFailRfRC,"Unable to close the error file.");
50
+  
51
+  cmMemFree(p);
52
+  
53
+  return kOkRfRC;
54
+}
55
+
56
+cmRfRC_t cmRptFileCreate( cmCtx_t* ctx, cmRptFileH_t* hp, const cmChar_t* printFn, const cmChar_t* errorFn )
57
+{
58
+  cmRptPrintFunc_t printFunc = NULL;
59
+  cmRptPrintFunc_t errorFunc = NULL;
60
+  cmRfRC_t rc;
61
+  if((rc = cmRptFileClose(hp)) != kOkRfRC )
62
+    return rc;
63
+
64
+  cmRptFile_t* p = cmMemAllocZ( cmRptFile_t, 1 );
65
+  
66
+  cmErrSetup(&p->err,&ctx->rpt,"Rpt File");
67
+  
68
+  if( printFn != NULL )
69
+  {
70
+    if((rc = cmFileOpen(&p->printFileH,printFn,kWriteFileFl,p->err.rpt)) == kOkFileRC )
71
+      printFunc = _cmRptFilePrintFunc;
72
+    else
73
+    {
74
+      rc = cmErrMsg(&p->err,kFileFailRfRC,"Unable to open the print file '%s'.", cmStringNullGuard(printFn));
75
+      goto errLabel;
76
+    }
77
+  }
78
+  
79
+  if( errorFn != NULL )
80
+  {
81
+    if((rc = cmFileOpen(&p->errorFileH,errorFn,kWriteFileFl,p->err.rpt)) == kOkFileRC )
82
+      errorFunc = _cmRptFileErrorFunc;
83
+    else
84
+    {
85
+      rc = cmErrMsg(&p->err,kFileFailRfRC,"Unable to open the error file '%s'.", cmStringNullGuard(errorFn));
86
+      goto errLabel;
87
+    }
88
+  }
89
+  
90
+  if( errorFunc == NULL )
91
+    errorFunc = printFunc;
92
+
93
+  cmRptSetup(&p->rpt,printFunc,errorFunc,p);
94
+
95
+  hp->h = p;
96
+  
97
+ errLabel:
98
+  if( rc != kOkRfRC )
99
+    _cmRptFileClose(p);
100
+
101
+  return rc;
102
+}
103
+
104
+cmRfRC_t     cmRptFileClose( cmRptFileH_t* hp )
105
+{
106
+  cmRfRC_t rc = kOkRfRC;
107
+  
108
+  if( hp==NULL || cmRptFileIsValid(*hp)==false)
109
+    return rc;
110
+
111
+  cmRptFile_t* p = _cmRptFileHandleToPtr( *hp );
112
+
113
+  if((rc = _cmRptFileClose(p)) != kOkRfRC )
114
+    return rc;
115
+
116
+  hp->h = NULL;
117
+  
118
+  return rc;
119
+}
120
+
121
+bool         cmRptFileIsValid( cmRptFileH_t h )
122
+{ return h.h != NULL; }
123
+    
124
+cmRpt_t*     cmRptFileRpt( cmRptFileH_t h )
125
+{
126
+  cmRptFile_t* p = _cmRptFileHandleToPtr( h );
127
+  return &p->rpt;
128
+}

+ 30
- 0
cmRptFile.h View File

@@ -0,0 +1,30 @@
1
+#ifndef cmRptFile_h
2
+#define cmRptFile_h
3
+
4
+#ifdef __cplusplus
5
+extern "C" {
6
+#endif
7
+
8
+  enum
9
+  {
10
+    kOkRfRC = cmOkRC,
11
+    kFileFailRfRC
12
+  };
13
+
14
+  typedef unsigned   cmRfRC_t;
15
+  typedef cmHandle_t cmRptFileH_t;
16
+
17
+  extern cmRptFileH_t cmRptFileNullHandle;
18
+
19
+  cmRfRC_t cmRptFileCreate( cmCtx_t* ctx, cmRptFileH_t* hp, const cmChar_t* printFn, const cmChar_t* errorFn );
20
+  cmRfRC_t cmRptFileClose( cmRptFileH_t* hp );
21
+  
22
+  bool     cmRptFileIsValid( cmRptFileH_t h );
23
+    
24
+  cmRpt_t* cmRptFileRpt( cmRptFileH_t h );
25
+
26
+#ifdef __cplusplus
27
+}
28
+#endif
29
+
30
+#endif

Loading…
Cancel
Save