C INMOD Example
This INMOD example reads the function code and executes different processing functions based on its value.
/* This program is for Teradata MultiLoad INMOD testing using C
user exit routine.
When this routine is activated it looks at the content of the
function code passed (a->code) and depending on its value, it
0) initializes, i.e., opens a file, etc...
1) reads a record
5) acknowledges "close inmod" request. The user exit routine
must return "return code"(a->code) and "length" (a->len). You
should send return code = zero when no errors occur and non-zero for
an error. Teradata MultiLoad expects length = zero
at the end of file. Then
it sends "CLOSE INMOD" request. THE USER EXIT routine must
explicitly return "return code" = ZERO to terminate the
conversation. */
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
typedef unsigned short Int16;
typedef unsigned char Int8;
typedef unsigned long int Int32;
/* PASSING parameter structures */
typedef struct {
Int32 code;
Int32 len;
Int8 buf[80];
} inmodbuf;
typedef struct {
Int32 seq;
Int16 len;
char param[80];
} inmodpty;
static FILE *IN;
static int count=0;
char *memcpy();
void _dynamn(a,b)
inmodbuf *a;
inmodpty *b;
{int code=0;
char tempbuf[80];
memcpy(tempbuf,a->buf,sizeof(a->buf));
tempbuf[79]='\0';
printf("BEGIN--> %d %d %s\n",a->code,a->len,tempbuf);
printf(" +++ %d %d %s\n",b->seq ,b->len,b->param);
code= (int) a->code;
switch (code) {
case 0:
/* Here you open the file and read the first record */
printf("## CODE=0, openinig...\n");
IN=fopen("ddn:INDATA","rb");
if (! ferror(IN)) {
if (! readrecord(a))
fclose(IN);
};
break;
case 1:
/* Teradata MultiLoad requested next record, read it */
printf("## CODE=1, reading...\n");
if (! readrecord(a))
fclose(IN);
break;
case 5:
/* Teradata MultiLoad is closing INMOD routine */
a->code=0;
a->len=0;
printf("## CODE=5, terminating...\n");
break;
default:
a->code=12; /* any number not = to zero */
a->len=0;
printf("##### UNKNOWN code ######\n");a->code=0;a->len=0;
};
memcpy(tempbuf,a->buf,sizeof(a->buf));
tempbuf[79]='\0';
printf("END --> %d %d %s\n",a->code,a->len,tempbuf);
printf(" +++ %d %d %s\n",b->seq ,b->len,b->param);
}
vint readrecord(a)
inmodbuf *a;
{
int rtn=0;
char tempbuf[80];
if (fread((char *)&(a->buf),sizeof(a->buf),1,IN)) {
count++;
memcpy(tempbuf,a->buf,sizeof(a->buf));
tempbuf[79]='\0';
printf(" %d %s \n",count,tempbuf);
a->len=80;
a->code=0;
rtn=1;
};
if ferror(IN) {
printf("==== error ====\n");
a->code=16; /* any non zero number */
a->len=0;
};
if feof(IN) { /* EOF, set length = zero */
printf("=== EOF ===\n");
a->code=9;
a->len=9;
};
return(rtn);
}