src/mspsh.c

filter: Statementstmt 24.6% 16 / 65 Branchbran 13.3% 4 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 21.0% 20 / 95 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/mspdmn.c ↑ Summary src/mspscada.c »
1 /****************************************************************************/
2 /* */
3 /* Program name: MQIsdp protocol C Language implementation */
4 /* */
5 /* Description: This source file contains common functions that are */
6 /* required by all aspects of the code. */
7 /* */
8 /* Statement: Licensed Materials - Property of IBM */
9 /* */
10 /* MQSeries SupportPac IA93 */
11 /* (C) Copyright IBM Corp. 2002 */
12 /* */
13 /****************************************************************************/
14 /* Version @(#) IA93/ship/mspsh.c, SupportPacs, S000 1.2 03/08/26 16:38:29 */
15 /* */
16 /* Function: */
17 /* */
18 /* The functions in the source file provide memory management, and logging */
19 /* of messages in both character and hex. */
20 /* */
21 /****************************************************************************/
22 /* */
23 /* Change history: */
24 /* */
25 /* V1.0 19-02-2003 IRH Initial release */
26 /* */
27 /*==========================================================================*/
28 /* Module Name: mspsh.c */
29 #include <mspsh.h>
30
31 /* Declare an sccsid variable that has a scope of the entire mqisdp shared */
32 /* library and is visible if the a utility such as UNIX strings is executed */
33 /* against the shared library. */
34 static char *sccsid = "WMQtt C library SCCSID: @(#) IA93/ship/mspsh.c, SupportPacs, S000 1.2 03/08/26 16:38:29";
35
36 /* Memory allocation function used throughout the reference code base. */
37 79 DllExport void* mspMalloc( MSPCMN *cData, size_t size ) {
38 79 void *m = malloc( size );
39
40 /* Zero the memory */
41 79 ▶ if (m != NULL) bzero(m, size);
Branch: 1 branch
BranchTrueFalseTotal
if (m != NULL) bzero(m, size); 79 0 79
42
43 #if MSP_DEBUG_MEM > 0
44 if ( m != NULL ) {
45 if ( cData != NULL ) {
46 cData->mc++;
47 cData->memCount += size;
48 if ( cData->memCount > cData->memMax ) {
49 cData->memMax = cData->memCount;
50 }
51 }
52 #if MSP_DEBUG_MEM == 2
53 printf( "Malloced:%p\n", m );
54 #endif
55 }
56 #endif
57
58 79 return m;
59 }
60
61 /* Memory re-allocation function used throughout the reference code base. */
62 0 DllExport void* mspRealloc( MSPCMN *cData, void *memBlock, size_t size, long freeSize ) {
63 void *m;
64 #if defined(MSP_NO_REALLOC) || (MSP_DEBUG_MEM > 0)
65 m = mspMalloc( cData, size );
66 if ( m != NULL ) {
67 memcpy( m, memBlock, freeSize );
68 }
69 mspFree( cData, memBlock, freeSize );
70 #else
71 0 m = realloc( memBlock, size );
72 #endif
73
74 #if MSP_DEBUG_MEM == 2
75 printf( "Realloced:%p to:%p\n", memBlock, m );
76 #endif
77 0 return m;
78 }
79
80 /* Memory free function used throughout the reference code base. */
81 52 DllExport void mspFree( MSPCMN *cData, void *memBlock, size_t freeSize ) {
82
83 52 ▶ if ( memBlock != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( memBlock != NULL ) { 52 0 52
84 #if MSP_DEBUG_MEM > 0
85 if ( cData != NULL ) {
86 cData->fc++;
87 cData->memCount -= freeSize;
88 }
89 #if MSP_DEBUG_MEM == 2
90 printf( "Freeing:%p\n", memBlock );
91 #endif
92 #endif
93 52 free( memBlock );
94 }
95 52 }
96
97 /* Memory logging function recording memory stats when debuging is enabled */
98 8 DllExport void mspLogMem( MSPCMN *cData, char *id, int correction ) {
99 #if MSP_DEBUG_MEM > 0
100 if ( cData != NULL ) {
101 mspLog( LOGNORMAL, cData, "%s:Calls to malloc : %ld\n", id, cData->mc );
102 mspLog( LOGNORMAL, cData, "%s:Calls to free : %ld\n", id, cData->fc );
103 mspLog( LOGNORMAL, cData, "%s:Current allocation: %ld\n", id, cData->memCount );
104 mspLog( LOGNORMAL, cData, "%s:Max allocation : %ld\n", id, cData->memMax + correction );
105 }
106 #endif
107 8 }
108
109 /* Generic function for dumping data in hex */
110 /* As it stands this function won't work with multibyte character */
111 /* sets, as it's formatting assumes 1 byte per character. */
112 0 DllExport void mspLogHex( int logLevel, MSPCMN *cData, int bufSize, char *buffer ) {
113 unsigned char *ptr;
114 char line1[ MQISDP_LINE_LENGTH + 1 ];
115 char line2[ MQISDP_LINE_LENGTH + 1 ];
116 char line3[ MQISDP_LINE_LENGTH + 1 ];
117 int bytesRemaining;
118 int i;
119 char hexstring[ 2 + 1 ];
120
121 /* Should we log this message? mspLogOptions is defined at the top of this file */
122 0 ▶ if ( logLevel & cData->mspLogOptions ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( logLevel & cData->mspLogOptions ) { 0 0 0
123 0 ptr = (unsigned char *) buffer;
124 0 bytesRemaining = bufSize;
125
126 0 ▶ if (bufSize > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (bufSize > 0 ) { 0 0 0
127 0 mspLog( logLevel, cData, "Logging %ld bytes:\n", bufSize );
128 }
129
130 0 ▶ while ( bytesRemaining > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( bytesRemaining > 0 ) { 0 0 0
131
132 0 i = 0;
133
134 0 ▶ while ( ( bytesRemaining > 0 ) && ( i < MQISDP_LINE_LENGTH ) ) {
Branch: 2 branches
BranchTrueFalseTotal
while ( ( bytesRemaining > 0 ) && ( i < MQISDP_LINE_LENGTH ) ) { 0 0 0
while ( ( bytesRemaining > 0 ) && ( i < MQISDP_LINE_LENGTH ) ) { 0 0 0
135
136 #ifdef MSP_NO_ISPRINT
137 line1[ i ] = *ptr;
138 #else
139 0 ▶ if ( isprint( *ptr ) )
Branch: 1 branch
BranchTrueFalseTotal
if ( isprint( *ptr ) ) 0 0 0
140 0 line1[ i ] = *ptr;
141 else
142 0 line1[ i ] = '.';
143 #endif
144
145 0 sprintf( hexstring, "%02x", *ptr );
146 0 line2[ i ] = hexstring[ 0 ];
147 0 line3[ i ] = hexstring[ 1 ];
148
149 0 i++;
150 0 ptr++;
151 0 bytesRemaining--;
152 }
153
154 0 line1[ i ] = '\0';
155 0 line2[ i ] = '\0';
156 0 line3[ i ] = '\0';
157
158 0 mspLog( logLevel, cData, "%s\n", line1 );
159 0 mspLog( logLevel, cData, "%s\n", line2 );
160 0 mspLog( logLevel, cData, "%s\n", line3 );
161 }
162 }
163 0 }
164
165 /* Generic log function, which currently writes log messges to stdout */
166 /* Can be customised to write data to appropriate location */
167 50 DllExport void mspLog( int logLevel, MSPCMN *cData, char *fmt, ... ) {
168 char buf[MSP_LOG_LINE_SZ];
169 char *writePtr;
170 va_list arg_ptr;
171 #ifndef MSP_NO_LOCALTIME
172 time_t timer;
173 struct tm *pCurTime;
174 #endif
175
176 /* Should we log this message? mspLogOptions is defined at the top of this file */
177 50 ▶ if ( logLevel & cData->mspLogOptions ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( logLevel & cData->mspLogOptions ) { 0 50 50
178 0 va_start( arg_ptr, fmt );
179
180 0 writePtr = buf;
181 #ifndef MSP_NO_LOCALTIME
182 0 time( &timer );
183 0 pCurTime = localtime( &timer );
184 0 sprintf( writePtr, "%02d/%02d/%04d %02d:%02d:%02d ", pCurTime->tm_mday, pCurTime->tm_mon + 1,
185 0 pCurTime->tm_year + 1900, pCurTime->tm_hour, pCurTime->tm_min,
186 pCurTime->tm_sec );
187 0 writePtr += 20;
188 #endif
189
190 0 ▶ if ( !(logLevel & LOGNORMAL) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !(logLevel & LOGNORMAL) ) { 0 0 0
191 0 ▶ if ( logLevel & LOGERROR ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( logLevel & LOGERROR ) { 0 0 0
192 0 sprintf( writePtr, "ERROR :" );
193 0 ▶ } else if ( logLevel & LOGSCADA ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( logLevel & LOGSCADA ) { 0 0 0
194 0 sprintf( writePtr, "MQISDP:" );
195 0 ▶ } else if ( logLevel & LOGIPC ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( logLevel & LOGIPC ) { 0 0 0
196 0 sprintf( writePtr, "IPC :" );
197 0 ▶ } else if ( logLevel & LOGTCPIP ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( logLevel & LOGTCPIP ) { 0 0 0
198 0 sprintf( writePtr, "TCPIP :" );
199 }
200 0 writePtr += 7;
201 }
202
203 0 vsprintf ( writePtr, fmt, arg_ptr );
204 0 va_end( arg_ptr );
205
206 0 printf( buf );
207 }
208
209 50 }
210
211 /* This function retruns the length of a buffer after repeats of the character, as */
212 /* identified by the first parameter, are trimmed off the end of the buffer. */
213 /* Mainly used for trimming whitespace of the end of data */
214 3 DllExport long mspCharTrim( char c, long len, char *buffer ) {
215
216 3 ▶ while ( buffer[len - 1] == c ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( buffer[len - 1] == c ) { 0 3 3
217 0 len--;
218 }
219
220 3 return len;
221 }
222
« src/mspdmn.c ↑ Summary src/mspscada.c »