|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
Convert from EBCDIC format |
« View previous topic :: View next topic » |
Author |
Message
|
somsengupta |
Posted: Fri Sep 08, 2006 5:13 am Post subject: Convert from EBCDIC format |
|
|
 Novice
Joined: 23 Jan 2006 Posts: 11
|
Hi,
I am getting messages from a partner, the logs of which is logged in from the receiver channel using a particular C program (i think that is service pack 6). This C program puts in the log of the message and also the time stamp, etc. The messages that we are receiving are EBCDIC Format. So, the log of the data written is also in the same format. I would like to transfer this data to the ASCII Format while logging itself. Please suggest changes that can be made into the C program to achieve this. The code is as follows:
MSGMON.C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <locale.h>
#ifdef AIX
#include <langinfo.h>
#endif
#include <sys/types.h>
#include <sys/timeb.h>
#include <time.h>
/* MQSeries headers */
#include <cmqc.h>
#include <cmqxc.h>
#include "channel.h"
char timebuf??(130??);
char Channel_Logfile??(129??);
char MsgBufFile??(129??);
char ini_filename??(129??);
FILE * OST = 0;
/* define output file and statement prototype */
#define DBGPRINTF(x) { \
OST=fopen(Channel_Logfile,"a+"); \
if(OST) fprintf x; \
fclose(OST); \
}
void writer (long *, char *); /* prototype for file writer function */
void getINI( void );
/* dummy function used as entry point to exit, only needed for AIX boxes */
void MQENTRY MQStart(void) {;}
void MQENTRY MsgExit( PMQCXP pChannelExitParams,
PMQCD pChannelDefinition,
PMQLONG pDataLength,
PMQLONG pAgentBufferLength,
PMQBYTE AgentBuffer,
PMQLONG pExitBufferLength,
PMQPTR pExitBufferAddr)
{
short i;
struct timeb t;
char millisec??(25??);
/*** Get time string to use in all messages ***/
time_t clock = time( (time_t*) NULL);
struct tm *tmptr = localtime(&clock);
strcpy(timebuf, asctime(tmptr));
ftime( &t );
memset( millisec, 0, sizeof( millisec ) );
sprintf( millisec, " Time:%ld.%d", t.time, t.millitm );
strcat( timebuf, millisec );
if ( strlen(pChannelExitParams->ExitData) )
{
memset( ini_filename, 0, sizeof( ini_filename ) );
strncpy( ini_filename, pChannelExitParams->ExitData, MQ_EXIT_DATA_LENGTH );
/*------------------------*/
/* remove space from data */
/*------------------------*/
for ( i = MQ_EXIT_DATA_LENGTH - 1; i > 0; i-- )
{
if ( ini_filename??(i??) != ' ' && ini_filename??(i??) )
break;
ini_filename??(i??) = 0;
}
}
getINI();
/*** Check call type - initialization, message, or exit ***/
/* */
switch( pChannelExitParams-> ExitReason )
{
case MQXR_INIT:
DBGPRINTF((OST,"\nChannel message exit called at MCA initialization %s",timebuf));
break;
case MQXR_MSG:
/*** Call subroutine to write record to output file ***/
/* */
writer (pDataLength, (char *)AgentBuffer);
DBGPRINTF((OST,"\nMessage written, length = %li %s \n",*pDataLength, timebuf));
break;
case MQXR_TERM:
DBGPRINTF((OST, "\nMessage channel exit called at MCA termination %s\n",timebuf));
break;
default:
DBGPRINTF((OST,"\nMessage channel exit called with invalid reason code: %d %s", pChannelExitParams-> ExitReason, timebuf));
break;
} /* switch */
/*** Set exit response = OK in all circumstances ***/
pChannelExitParams -> ExitResponse = MQXCC_OK;
return;
}/* end exit */
/*------------------------------------------------------------------
write() - writes a MQSeries message to a log file.
-------------------------------------------------------------------*/
void writer(long *lngth, char *bf)
{
long msgsize;
int n;
FILE *fp;
getINI();
fp = fopen(MsgBufFile, "ab");
if (fp == NULL)
{
printf("Error opening <%s> to write message. Error #: %d", MsgBufFile, errno );
return;
}
msgsize = *lngth;
n = fwrite(&msgsize, sizeof(msgsize), 1, fp); /* put record length */
n = fwrite(bf, msgsize, 1, fp); /* and message buffer */
fclose(fp);
} /* end writer */
/*---------------------------------------------------------
getINI() - read information from INI file
-----------------------------------------------------------*/
void getINI( void )
{
FILE *ini_fp;
char *ptr, *name_ptr;
char field_val??(129??), Ini_s??(129??), fn??(129??);
short len;
memset( Ini_s, 0, sizeof( Ini_s ) );
memset( field_val, 0, sizeof( field_val ) );
memset( MsgBufFile, 0, sizeof ( MsgBufFile ) );
memset( Channel_Logfile, 0, sizeof( Channel_Logfile ) );
if ( strlen( ini_filename ) ) /* if Channel Exit defined INI filename */
strcpy( fn, ini_filename ); /* use it */
else /* otherwise use default INI filename */
strcpy( fn, INI_FILENAME );
if ( (ini_fp = fopen( fn, "r" )) == NULL )
{
fprintf(stderr, "\nUnable to open %s. Error #: %d", fn, errno );
return;
}
/*-----------------------------*/
/* read the INI file until eof */
/*-----------------------------*/
while ( (ptr = fgets( Ini_s, sizeof(Ini_s) - 1, ini_fp )) != NULL )
{
len = strlen( Ini_s );
if ( Ini_s??(len -1??) == LINEFEED )
Ini_s??(len-1??) = 0; /* null out '\n' */
if ( (name_ptr = strchr( Ini_s, EQUAL )) == NULL ) /* no '=' found */
continue;
ptr = name_ptr + 1;
*name_ptr = 0; /* set null for s[] */
strcpy( field_val, ptr );
/*---------------------------------------------------*/
/* remove comments and unwanted characters from data */
/*---------------------------------------------------*/
while ( *ptr )
{
if ( *ptr == ' ' ) /* stop at first space */
{
*ptr = 0;
break;
}
ptr++;
}
if ( !strcmp( MESSAGE_BUFFER_FILE, Ini_s ) ) /* look for message buffer filename */
{
strcpy( MsgBufFile, field_val );
}
else
{
if ( !strcmp( CHANNEL_LOGFILE, Ini_s ) )
{
strcpy( Channel_Logfile, field_val );
}
}
memset( Ini_s, 0, sizeof( Ini_s ) );
memset( field_val, 0, sizeof( field_val ) );
}
fclose( ini_fp );
return;
}
PUTMSGQ.C
#include <string.h>
#include <stdlib.h>
#include <cmqc.h>
#include <cmqxc.h>
#include <stdio.h>
#include <errno.h>
#include "channel.h"
long msgsize;
char *bufr;
MQOD od = {MQOD_DEFAULT}; /* Object Descriptor */
MQMD md = {MQMD_DEFAULT}; /* Message Descriptor */
MQPMO pmo = {MQPMO_DEFAULT}; /* put message options */
MQHCONN Hcon; /* connection handle */
MQHOBJ Hobj; /* object handle */
MQLONG O_options; /* MQOPEN options */
MQLONG C_options; /* MQCLOSE options */
MQLONG CompCode; /* completion code */
MQLONG OpenCode; /* MQOPEN completion code */
MQLONG Reason; /* reason code */
MQLONG buflen; /* Length of message */
MQLONG CReason; /* reason code for MQCONN */
char QMName??(50??); /* queue manager name */
MQXQH *pMQXQH; /* message exit header */
MQMD *pMQMD; /* message descriptor */
PMQXQH pAgentBuf;
PMQMD pMsgDesc; /* Pointer to message descriptor */
char MsgBufFile??(129??);
char InQ??(129??);
char OutQ??(129??);
long offset;
FILE *msg_fp;
char buf??(10000??); /* buffer to receive messages */
MQXQH agentBuf;
char ini_filename??(129??);
/*** Function prototpe for read subroutine ***/
/* First argument is length of message, */
/* Second is returned buffer */
/* Returns int 0 if record read, 1 at EOF */
int reader(long *pmsg, char *bfr);
void getINI( void );
/*-----------------------------------------------
main
-------------------------------------------------*/
main( int argc, char ** argv )
{
long count = 0L;
memset( ini_filename, 0, sizeof( ini_filename ) );
if ( argc > 1 ) /* if INI filename specified, copy it */
strcpy( ini_filename, argv??(1??) );
getINI();
pMQXQH = buf; /* initialize pointers */
pMQMD = &pMQXQH->MsgDesc;
printf("\nConnecting to Queue Mgr: ??(%s??)", QMName );
MQCONN(QMName, /* queue manager */
&Hcon, /* connection handle */
&CompCode, /* completion code */
&CReason); /* reason code */
/* report reason and stop if it failed */
if (CompCode == MQCC_FAILED)
{
printf("MQCONN ended with reason code %ld\n", CReason);
exit( (int)CReason );
}
printf("\nConnected to %s", QMName);
/******************************************************************/
/* */
/* Open the target message queue for output */
/* */
/******************************************************************/
strcpy(od.ObjectName, InQ);
O_options = MQOO_OUTPUT /* open queue for output */
+ MQOO_FAIL_IF_QUIESCING; /* but not if MQM stopping */
MQOPEN(Hcon, /* connection handle */
&od, /* object descriptor for queue */
O_options, /* open options */
&Hobj, /* object handle */
&OpenCode, /* MQOPEN completion code */
&Reason); /* reason code */
/* report reason, if any; stop if failed */
if (Reason != MQRC_NONE)
{
printf("\nMQOPEN ended with reason code %ld\n", Reason);
exit( 1 );
}
if (OpenCode == MQCC_FAILED)
{
printf("\nUnable to open queue for output\n");
exit( 2 );
}
if ( (msg_fp = fopen( MsgBufFile, "rb" )) == NULL )
{
printf("\nError opening ??(%s??). Error code: %d", MsgBufFile, errno );
exit( 3 );
}
while (reader(&msgsize, buf) == 0)
{
pMQXQH = buf; /* set pointers 4-28-97 */
pMQMD = &pMQXQH->MsgDesc; /* 4-28-97 */
offset = sizeof(MQXQH);
buflen = msgsize - offset;
memcpy( &md, pMsgDesc, sizeof( md ) ); /* 4-28-97 */
/****************************************************************/
/* */
/* Put each buffer to the message queue */
/* */
/****************************************************************/
if (buflen)
{
strcpy(md.ReplyToQ,OutQ); /* Dummy reply-to's */
strcpy(md.ReplyToQMgr, QMName);
MQPUT(Hcon, /* connection handle */
Hobj, /* object handle */
&md, /* message descriptor */
&pmo, /* default options (datagram) */
buflen, /* buffer length */
&buf??(offset??), /* message buffer */
&CompCode, /* completion code */
&Reason); /* reason code */
/* report reason, if any */
if (Reason != MQRC_NONE)
printf("MQPUT ended with reason code %ld\n", Reason);
else
printf("\nMQPUT %ld message in ??(%s??)", ++count, InQ );
} /* end if */
} /* endwhile */
fclose( msg_fp );
return 0;
}
/*---------------------------------------------------------
reader() - read information from Message Bufer file and
writes it out to a queue
-----------------------------------------------------------*/
int reader(long *pmsg, char *bf)
{
long msglength;
char s1??(100??), s2??(100??);
fread (&msglength, sizeof(msglength), 1, msg_fp);
if (feof(msg_fp))
return(1);
*pmsg = msglength;
fread (bf, (short)msglength, 1, msg_fp);
pAgentBuf = bf;
pMsgDesc = &pAgentBuf->MsgDesc;
memset( s1, 0, sizeof( s1 ) );
memset( s2, 0, sizeof( s2 ) );
strncpy(s1, pAgentBuf->RemoteQName, sizeof( pAgentBuf->RemoteQName ));
strncpy(s2, pAgentBuf->RemoteQMgrName, sizeof( pAgentBuf->RemoteQMgrName ));
printf("\n\nRemoteQName: ??(%s??)\nQueue Manager: ??(%s??)", s1, s2);
memset( s1, 0, sizeof( s1 ) );
memset( s2, 0, sizeof( s2 ) );
strncpy(s1, pMsgDesc->ReplyToQ, sizeof( pMsgDesc->ReplyToQ ));
strncpy(s2, pMsgDesc->ReplyToQMgr, sizeof( pMsgDesc->ReplyToQMgr ));
printf("\nReplyToQ: ??(%s??)\nReplyToQMgr: ??(%s??)", s1, s2);
memset( s1, 0, sizeof( s1 ) );
memset( s2, 0, sizeof( s2 ) );
memcpy(s1, pMsgDesc->MsgId, sizeof( pMsgDesc->MsgId ) );
memcpy(s2, pMsgDesc->CorrelId, sizeof( pMsgDesc->CorrelId ) );
printf("\nMsg Type: ??(%ld??) MsgID: ??(%s??). CorrelID: ??(%s??) ",
pMsgDesc->MsgType,s1, s2 );
memset( s1, 0, sizeof( s1 ) );
memset( s2, 0, sizeof( s2 ) );
memcpy(s1, pMsgDesc->PutDate, sizeof( pMsgDesc->PutDate ) );
memcpy(s2, pMsgDesc->PutTime, sizeof( pMsgDesc->PutTime )) ;
printf("\nPutDate: ??(%s??) PutTime: ??(%s??)", s1, s2 );
return(0);
}
/*---------------------------------------------------------
getINI() - read information from INI file
-----------------------------------------------------------*/
void getINI( void )
{
FILE *ini_fp;
char *ptr, *name_ptr, field_val??(257??), Ini_s??(257??), fn??(129??);
int i, len;
memset( Ini_s, 0, sizeof( Ini_s ) );
memset( field_val, 0, sizeof( field_val ) );
memset( MsgBufFile, 0, sizeof ( MsgBufFile ) );
memset( InQ, 0, sizeof( InQ ) );
memset( OutQ, 0, sizeof( OutQ ) );
memset( QMName, 0, sizeof( QMName ) );
memset( fn, 0, sizeof( fn ) );
if ( strlen( ini_filename ) )
strcpy( fn, ini_filename );
else
strcpy( fn, INI_FILENAME );
if ( (ini_fp = fopen( fn, "r" )) == NULL )
{
fprintf( stderr, "\nUnable to open %s. Error #: %d", fn, errno );
return;
}
/*-----------------------------*/
/* read the INI file until eof */
/*-----------------------------*/
while ( (ptr = fgets( Ini_s, sizeof(Ini_s) - 1, ini_fp )) != NULL )
{
len = strlen( Ini_s );
if ( Ini_s??(len -1??) == LINEFEED )
Ini_s??(len-1??) = 0; /* null out CR */
if ( (name_ptr = strchr( Ini_s, EQUAL )) == NULL ) /* no '=' found */
continue;
ptr = name_ptr + 1;
*name_ptr = 0; /* set null for s??(??) */
strcpy( field_val, ptr );
/*---------------------------------------------------*/
/* remove comments and unwanted characters from data */
/*---------------------------------------------------*/
while ( *ptr )
{
if ( *ptr == ' ' ) /* stop at first space */
{
*ptr = 0;
break;
}
ptr++;
}
if ( !strcmp( MESSAGE_BUFFER_FILE, Ini_s ) ) /* look for message buffer filename */
{
strcpy( MsgBufFile, field_val );
}
else
if ( !strcmp( IN_QUE, Ini_s ) ) /* look for IN Queue name */
{
strcpy( InQ, field_val );
}
else
if ( !strcmp( OUT_QUE, Ini_s ) ) /* look for OUT Queue name */
{
strcpy( OutQ, field_val );
}
else
if ( !strcmp( QUE_MGR, Ini_s ) ) /* look for Queue Mgr name */
{
strcpy( QMName, field_val );
}
memset( Ini_s, 0, sizeof( Ini_s ) );
memset( field_val, 0, sizeof( field_val ) );
} /* while ( fgets() ) */
fclose( ini_fp );
return;
} |
|
Back to top |
|
 |
jefflowrey |
Posted: Fri Sep 08, 2006 5:17 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
Why don't you send an email to the author of the Support Pack that that code came in? _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
somsengupta |
Posted: Fri Sep 08, 2006 5:21 am Post subject: Convert from EBCDIC format |
|
|
 Novice
Joined: 23 Jan 2006 Posts: 11
|
The code contains the name of the person who has developed it. Its an IBM property. BUt there are no mail Ids etc given where I can connect.
Any suggestions? |
|
Back to top |
|
 |
jefflowrey |
Posted: Fri Sep 08, 2006 5:31 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
Read the documentation on data conversion and message exits? _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
Nigelg |
Posted: Mon Sep 11, 2006 12:10 am Post subject: |
|
|
Grand Master
Joined: 02 Aug 2004 Posts: 1046
|
After reading the msg, use the O/S iconv_open() and iconv() library calls to convert the msg data from EBCDIC to ASCII. _________________ MQSeries.net helps those who help themselves.. |
|
Back to top |
|
 |
|
|
 |
|
Page 1 of 1 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|