Skip to content

Commit 13408da

Browse files
committed
Move formatting to real functions to pave way for potential new formats
1 parent 53375db commit 13408da

File tree

14 files changed

+193
-91
lines changed

14 files changed

+193
-91
lines changed

bin/aix/convH

0 Bytes
Binary file not shown.

bin/aix/mqsmfcsv

-230 KB
Binary file not shown.

bin/linux/convH

0 Bytes
Binary file not shown.

bin/linux/mqsmfcsv

-216 KB
Binary file not shown.

bin/win/mqsmfcsv.exe

-122 KB
Binary file not shown.

mqsmfcsv.doc

0 Bytes
Binary file not shown.

src/Makefile.gcc.win

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ SRC = mqsmf.c \
77
smfDDL.c \
88
smfDate.c \
99
smfConv.c \
10+
smfPrint.c \
1011
printDEBUG.c \
1112
printQ5ST.c \
1213
printQCST.c \

src/Makefile.unix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CFLAGS= -I.
44
PLATFLAGS=
55
SRC = \
66
smfDDL.c \
7+
smfPrint.c \
78
smfDate.c \
89
smfConv.c \
910
printDEBUG.c \

src/Makefile.win

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ SRC = mqsmf.c \
44
smfDDL.c \
55
smfDate.c \
66
smfConv.c \
7+
smfPrint.c \
78
printDEBUG.c \
89
printQ5ST.c \
910
printQCST.c \

src/mqsmf.c

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,9 @@ int debugLevel = 0;
113113
BOOL addEquals = TRUE;
114114
BOOL printHeaders = TRUE;
115115
BOOL useRDW = TRUE;
116-
BOOL sqlMode = FALSE;
117116
commonFields_t commonF = {0};
117+
enum outputFormat_e outputFormat = OF_CSV;
118118

119-
char headings[HEADINGS_LEN]; /* Ensure these are big enough for any line*/
120-
char dataline[DATA_LEN];
121-
char tmpHead[64]; /* Working space for a column heading*/
122119
unsigned int recordType;
123120
unsigned short recordSubType;
124121

@@ -249,12 +246,14 @@ int main( int argc, char *argv[] )
249246
case 'f':
250247
for (i=0;i<strlen(mqoptarg);i++)
251248
mqoptarg[i] = toupper(mqoptarg[i]);
252-
if (!strcmp(mqoptarg,"NORDW"))
249+
if (strstr(mqoptarg,"NORDW"))
253250
useRDW = FALSE;
254-
else if (!strcmp(mqoptarg,"RDW"))
251+
else if (strstr(mqoptarg,"RDW"))
255252
useRDW = TRUE;
256-
else
257-
error = TRUE;
253+
if (strstr(mqoptarg,"JSON"))
254+
outputFormat = OF_JSON;
255+
else if (strstr(mqoptarg,"SQL"))
256+
outputFormat = OF_SQL;
258257
break;
259258
case 'h':
260259
for (i=0;i<strlen(mqoptarg);i++)
@@ -275,7 +274,7 @@ int main( int argc, char *argv[] )
275274
addEquals = 0;
276275
break;
277276
case 's':
278-
sqlMode = TRUE;
277+
outputFormat = OF_SQL;
279278
printHeaders = FALSE;
280279
addEquals = 0;
281280
break;
@@ -310,6 +309,12 @@ int main( int argc, char *argv[] )
310309
/* Windows requires the "b" option for binary input. And force */
311310
/* stdin to binary in case we need to read from that too. */
312311
/*********************************************************************/
312+
if (!inputFile && !useRDW)
313+
{
314+
printf("Cannot use NORDW format input via stdin piping\n");
315+
exit(1);
316+
}
317+
313318
#ifdef _WIN32
314319
setmode(fileno(stdin), O_BINARY);
315320
#endif
@@ -320,6 +325,11 @@ int main( int argc, char *argv[] )
320325
inputFile,strerror(errno),errno);
321326
goto mod_exit;
322327
}
328+
/* Make sure we've got a printable name */
329+
if (!inputFile)
330+
{
331+
inputFile = "<<stdin>>";
332+
}
323333

324334
fstat(fileno(fp),&statbuf);
325335
totalFileSize = statbuf.st_size;
@@ -361,9 +371,9 @@ int main( int argc, char *argv[] )
361371
/********************************************************************/
362372
currentOffset = startingOffset;
363373
totalRecords = startingRecords;
364-
seekRc = fseeko(fp,currentOffset,SEEK_SET);
374+
seekRc = (currentOffset!=0)?fseeko(fp,currentOffset,SEEK_SET):0;
365375
if (seekRc == -1) {
366-
printf("Cannot move to correct offset for input file");
376+
printf("Cannot move to correct offset for input file - error %s (%d)\n",strerror(errno),errno);
367377
goto mod_exit;
368378
}
369379

@@ -451,7 +461,11 @@ int main( int argc, char *argv[] )
451461
}
452462
else
453463
{
454-
fseeko(fp,currentOffset,SEEK_SET);
464+
seekRc = fseeko(fp,currentOffset,SEEK_SET);
465+
if (seekRc == -1) {
466+
printf("Cannot move to correct offset for input file - error %s (%d)\n",strerror(errno),errno);
467+
goto mod_exit;
468+
}
455469
bytesRead = fread(&pSMFRecord->Header.SMFRECFLG,1,32768,fp);
456470
offset = bytesRead;
457471
offsetCorrection = 4;
@@ -529,7 +543,7 @@ int main( int argc, char *argv[] )
529543
time = time - (min * 60);
530544
sec = time % 60;
531545

532-
if (sqlMode)
546+
if (outputFormat == OF_SQL)
533547
sep = "-";
534548
else
535549
sep = "/";

src/mqsmf.h

100644100755
Lines changed: 27 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -248,26 +248,24 @@ extern char *strCfStatType (int v);
248248
/* SMFPRINTSTOP; */
249249
/* } */
250250
/**********************************************************/
251-
#define ADDHEAD(h,type,len) \
252-
if (first && h) \
253-
{ \
254-
offsetH += snprintf(&headings[offsetH],HEADINGS_LEN - offsetH, "%s,", h); \
255-
if (offsetH > HEADINGS_LEN) \
256-
{ \
257-
printf("HEADINGS buffer needs to be bigger than %d bytes.\n",HEADINGS_LEN); \
258-
exit(1); \
259-
} \
260-
if (sqlMode) \
261-
printDDL(h,type,len); \
262-
}
251+
extern FILE *smfPrintStart(char *,void *,size_t,BOOL *,BOOL *);
252+
extern void smfPrintStop(FILE *,BOOL, BOOL *);
253+
extern void smfAddHead(BOOL first,char *h,int type,int len);
254+
extern void smfAddData(char *fmt,...);
263255

264-
#define ADDDATA(fmt,...) \
265-
offsetD+=snprintf(&dataline[offsetD],DATA_LEN-offsetD,fmt,__VA_ARGS__); \
266-
if (offsetD > DATA_LEN) \
267-
{ \
268-
printf("DATA buffer needs to be bigger than %d bytes.\n",DATA_LEN); \
269-
exit(1); \
270-
}
256+
#define SMFPRINTGLOB \
257+
static BOOL first = TRUE;\
258+
static BOOL newFile = TRUE;\
259+
static FILE *fp = NULL
260+
261+
#define SMFPRINTSTART(n,p,l) \
262+
fp = smfPrintStart(n,p,l,&first,&newFile);
263+
264+
#define SMFPRINTSTOP \
265+
smfPrintStop(fp, newFile, &first);
266+
267+
#define ADDHEAD(a,b,c) smfAddHead(first,a,b,c)
268+
#define ADDDATA(f,...) smfAddData(f,__VA_ARGS__)
271269

272270
#define ADDS64(h,v) \
273271
ADDHEAD(h,DDL_I64,0); \
@@ -332,15 +330,15 @@ extern char *strCfStatType (int v);
332330
ADDDATA("%s,",convDate(v))
333331

334332
#define ADDSTCK(h,v) \
335-
if (sqlMode) \
333+
if (outputFormat == OF_SQL) \
336334
sprintf(tmpHead,"%s(US)",h); \
337335
else\
338336
sprintf(tmpHead,"%s(S),%s(US)",h,h); \
339337
ADDHEAD(tmpHead,DDL_SUS,0); \
340338
ADDDATA("%s,",convSecUSec(v))
341339

342340
#define ADDSTCKIDX(h,idx,v) \
343-
if (sqlMode) \
341+
if (outputFormat == OF_SQL) \
344342
sprintf(tmpHead,"%s{%s}(US)",h,idx); \
345343
else \
346344
sprintf(tmpHead,"%s{%s}(S),%s{%s}(US)",h,idx,h,idx); \
@@ -349,11 +347,11 @@ extern char *strCfStatType (int v);
349347

350348
/* Add ASCII string, known length - underpins the other ADDSTRxx macros */
351349
#define ADDSTRN(h,v,l,maxlen) \
352-
ADDHEAD(h,DDL_C,maxlen); \
353-
{ char *equ=""; \
354-
if (looksLikeNum(l,v) && addEquals) equ="="; \
355-
ADDDATA("%s\"%-*.*s\",",equ,l,l,v) \
356-
}
350+
ADDHEAD(h,DDL_C,maxlen); \
351+
{ char *equ=""; \
352+
if (looksLikeNum(l,v) && addEquals) equ="="; \
353+
ADDDATA("%s\"%-*.*s\",",equ,l,l,v);\
354+
}
357355

358356
#define ADDSTR(h,v,maxlen) \
359357
ADDSTRN(h,v,strlen(v),maxlen) /* ASCII string null terminated*/
@@ -372,61 +370,12 @@ extern char *strCfStatType (int v);
372370
if (first) sprintf(tmpHead,"%s {%s}",h,idx); \
373371
ADDSTREN(tmpHead,v,l) /* EBCDIC string, known length*/
374372

375-
#define COMMON_BLOCK \
376-
ADDSTR ("Date",commonF.recordDate,8); \
377-
ADDSTR ("Time",commonF.recordTime,16); \
378-
ADDSTRN("LPAR",commonF.systemId,4,4); \
379-
ADDSTRN("QMgr",commonF.qMgr,4,4); \
380-
ADDSTRN("MQ_Version",commonF.mqVer,3,3) \
381-
if (recordType == 115 && commonF.intstart != 0) { \
382-
unsigned long long du = conv64(commonF.intduration)/1000000L; \
383-
ADDTIME("Interval_Start",commonF.intstart) \
384-
ADDHEAD("Interval_Duration",DDL_I,0); \
385-
if (recordSubType == 231) { \
386-
ADDDATA("%llu,",du/4096L); \
387-
} else { \
388-
ADDDATA("%llu,",du); \
389-
} \
390-
}
391-
392-
#define SMFPRINTSTART(n,p,l) \
393-
int offsetH=0; \
394-
int offsetD=0; \
395-
char *tableName = n; \
396-
if (debugLevel >=1 ) printDEBUG(n,p,(l));\
397-
if (first) { \
398-
fp = fopencsv(n,&newFile);\
399-
if (sqlMode) \
400-
openDDL(n); \
401-
} \
402-
if (!fp) \
403-
{ \
404-
exit(1); \
405-
} \
406-
COMMON_BLOCK
407373

408374
#define ADDINDEX(n) \
409375
if (first) { \
410376
addIndex(n); \
411377
}
412378

413-
#define SMFPRINTSTOP \
414-
if (first && newFile && printHeaders) \
415-
{ \
416-
fprintf(fp,"%s",headings);\
417-
fprintf(fp,"\n"); \
418-
} \
419-
if(first && sqlMode) \
420-
closeDDL(tableName); \
421-
first=FALSE; \
422-
fprintf(fp,"%s",dataline);\
423-
fprintf(fp,"\n")
424-
425-
#define SMFPRINTGLOB \
426-
static BOOL first = TRUE;\
427-
static BOOL newFile = TRUE;\
428-
static FILE *fp = NULL
429-
430379
/*****************************************/
431380
/* This isn't currently used. But it was */
432381
/* useful during initial debug. */
@@ -450,9 +399,11 @@ extern unsigned char EBCDIC_TO_ASCII[];
450399
extern int debugLevel;
451400
extern BOOL addEquals;
452401
extern BOOL printHeaders;
453-
extern BOOL sqlMode;
454402
extern unsigned int recordType;
455403
extern unsigned short recordSubType;
456404
extern commonFields_t commonF;
457405

406+
enum outputFormat_e { OF_SQL, OF_JSON, OF_CSV };
407+
extern enum outputFormat_e outputFormat;
408+
458409
#endif

src/smfConv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ char *convSecUSec(unsigned long long s)
172172
}
173173

174174
/* Separate fields by ',' for CSV formats */
175-
if (sqlMode)
175+
if (outputFormat == OF_SQL)
176176
sprintf(usecBuf,"%llu ",s);
177177
else
178178
sprintf(usecBuf,"%u, %u ",sec,usec);

src/smfDate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ char * convDate(unsigned long long stcki)
7979
char *emptyTime;
8080
size_t offset;
8181

82-
if (sqlMode)
82+
if (outputFormat == OF_SQL)
8383
{
8484
epochTime = sqlEpoch;
8585
emptyTime = sqlEpoch;

0 commit comments

Comments
 (0)