/**
 * @file   SCFCBCPY.c
 * @brief  Sample program in C to copy files via the FLUC byte interface
 * @author limes datentechnik gmbh
 **********************************************************************/

/* defines code page for literals for IBM mainframes to German */
#ifdef __ZOS__
#  pragma convert(1141)
#endif

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#include"FLCBYT.h"

/**
 * This test program takes four parameters to convert files via the byte
 * interface.
 *
 * @param argc must be 5
 * @param argv contains the strings for fcbopen
 * @return 8=parameter error, 6=open file error, 4=read write error, 2=close error, 0=Success
 */
int main(int argc, char * argv[])
{
   void*                   pvRed;
   void*                   pvWrt;
   static unsigned char    acBuf[65536];
   unsigned int            uiLen,uiErr,i;
   char*                   h;
   char*                   a[5]={"","","","",""};

/* defines environment variable LANG to German EBCDIC for IBM mainframes */
#ifdef __ZOS__
   __setenv("LANG","de_DE.IBM-1141",1);
#endif

   if (argc==2) { /* parse HOST parameter string */
      i=2; a[0]=argv[0]; a[1]=argv[1];
      h=strchr(a[1],',');
      if (h!=NULL) { h[0]=0x00; a[2]=h+1; i++; }
      h=strchr(a[2],',');
      if (h!=NULL) { h[0]=0x00; a[3]=h+1; i++; }
      h=strchr(a[3],',');
      if (h!=NULL) { h[0]=0x00; a[4]=h+1; i++; }
      argc=i; argv=a;
   }

   if (argc!=5) {
      fprintf(stderr,"Parameter count (%d) not correct to run this sample program\n",argc);
      fprintf(stderr,"Given parameters:\n");
      for (i=0;i<argc;i++) fprintf(stderr,"--> P%2.2u(%s)\n",i,argv[i]);
      fprintf(stderr,"Required parameters:\n");
      fprintf(stderr,"%s \"read file string\" \"read format string\" \"write file string\" \"write format string\"\n",argv[0]);
      fprintf(stderr,"READ FILE STRING   : \"%s\"\n",fcbsyntax(FLC_READ_FILE   ,1,NULL));
      fprintf(stderr,"READ FORMAT STRING : \"%s\"\n",fcbsyntax(FLC_READ_FORMAT ,1,NULL));
      fprintf(stderr,"WRITE FILE STRING  : \"%s\"\n",fcbsyntax(FLC_WRITE_FILE  ,1,NULL));
      fprintf(stderr,"WRITE FORMAT STRING: \"%s\"\n",fcbsyntax(FLC_WRITE_FORMAT,1,NULL));
      exit(8);
   }

   fcbopnlog("LOG(MESSAGE(WARNING))");

   pvRed=fcbopen(argv[1],argv[2]);
   if (pvRed==NULL) {
      fprintf(stderr,"Open file for read operation failed\n");
      fprintf(stderr,"pvRed=fcbopen(\"%s\",\"%s\");\n\n",argv[1],argv[2]);
      fprintf(stderr,"%s\n\n",fcberrms(fcberrno));
      fprintf(stderr,"%s\n\n",fcberrtr());
      const char* pcLog=NULL; fcbgetlog(&pcLog);
      if (pcLog!=NULL) fprintf(stderr,"%s\n\n",pcLog);
      fcbclslog();
      exit(6);
   }

   pvWrt=fcbopen(argv[3],argv[4]);
   if (pvWrt==NULL) {
      fprintf(stderr,"Open file for write operation failed\n");
      fprintf(stderr,"pvWrt=fcbopen(\"%s\",\"%s\");\n\n",argv[3],argv[4]);
      fprintf(stderr,"%s\n\n",fcberrms(fcberrno));
      fprintf(stderr,"%s\n\n",fcberrtr());
      const char* pcLog=NULL; fcbgetlog(&pcLog);
      if (pcLog!=NULL) fprintf(stderr,"%s\n\n",pcLog);
      fcbclslog();
      fcbclose(pvRed);
      exit(6);
   }

   for (uiLen=fcbread(acBuf,1,sizeof(acBuf),pvRed); uiLen && (fcberror(pvRed)==FLMRTC_OK || fcberror(pvRed)==FLMRTC_EOF);
        uiLen=fcbread(acBuf,1,sizeof(acBuf),pvRed)) {
      uiErr=fcbwrite(acBuf,1,uiLen,pvWrt);
      if (uiErr!=uiLen) {
         fprintf(stderr,"Write data to file failed\n");
         fprintf(stderr,"uiErr(%u)=fcbwrite(acBuf,1,uiLen(%u),pvWrt);\n\n",uiErr,uiLen);
         fprintf(stderr,"%s\n\n",fcberrms(fcberror(pvWrt)));
         fprintf(stderr,"%s\n\n",fcberrtr());
         const char* pcLog=NULL; fcbgetlog(&pcLog);
         if (pcLog!=NULL) fprintf(stderr,"%s\n\n",pcLog);
         fcbclslog();
         fcbclose(pvRed);
         fcbclose(pvWrt);
         exit(4);
      }
   }
   if (fcberror(pvRed)!=FLMRTC_OK && fcberror(pvRed)!=FLMRTC_EOF) {
      fprintf(stderr,"Read data from file failed\n");
      fprintf(stderr,"uiLen(%u)=fcbread(acBuf,1,sizeof(acBuf)(%u),pvRed);\n\n",uiLen,(unsigned int)sizeof(acBuf));
      fprintf(stderr,"%s\n\n",fcberrms(fcberror(pvRed)));
      fprintf(stderr,"%s\n\n",fcberrtr());
      const char* pcLog=NULL; fcbgetlog(&pcLog);
      if (pcLog!=NULL) fprintf(stderr,"%s\n\n",pcLog);
      fcbclslog();
      fcbclose(pvRed);
      fcbclose(pvWrt);
      exit(4);
   }

   if (fcbclose(pvRed)) {
      fprintf(stderr,"Close file for read operation failed\n");
      fprintf(stderr,"fcbclose(pvRed);\n\n");
      fprintf(stderr,"%s\n\n",fcberrms(fcberrno));
      fprintf(stderr,"%s\n\n",fcberrtr());
      const char* pcLog=NULL; fcbgetlog(&pcLog);
      if (pcLog!=NULL) fprintf(stderr,"%s\n\n",pcLog);
      fcbclslog();
      fcbclose(pvWrt);
      exit(2);
   }

   if (fcbclose(pvWrt)) {
      fprintf(stderr,"Close file for write operation failed\n");
      fprintf(stderr,"fcbclose(pvWrt);\n\n");
      fprintf(stderr,"%s\n\n",fcberrms(fcberrno));
      fprintf(stderr,"%s\n\n",fcberrtr());
      const char* pcLog=NULL; fcbgetlog(&pcLog);
      if (pcLog!=NULL) fprintf(stderr,"%s\n\n",pcLog);
      fcbclslog();
      exit(2);
   }
   fprintf(stderr,"Normal end\n\n"); fflush(stderr);
   const char* pcLog=NULL; fcbgetlog(&pcLog);
   if (pcLog!=NULL) fprintf(stderr,"%s\n\n",pcLog);
   fcbclslog();
   return(0);
}

/**********************************************************************/

