home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / viscobv6.zip / vac22os2 / ibmcobol / samples / sdu / sample2.c < prev    next >
Text File  |  1997-03-14  |  17KB  |  365 lines

  1. #pragma title ("SAMPLE2")
  2. /******************************************************************************/
  3. /*                                                                            */
  4. /* PRODUCT   = Data Description and Conversion for OS/2                       */
  5. /*                                                                            */
  6. /* SOURCE FILE NAME = Sample2.C                                               */
  7. /*                                                                            */
  8. /* DESCRIPTIVE NAME = Conversion Plan Executor sample                         */
  9. /*                                                                            */
  10. /* FUNCTION =  This sample program calls the functions of the                 */
  11. /*             conversion plan executor to convert data based on the          */
  12. /*             conversion plans created by the conversion plan builder        */
  13. /*             in program SAMPLE1.                                            */
  14. /*             In this sample the hex string C1C2C301234C will be converted   */
  15. /*             with specified plan COBOL_TO_C into the hex string             */
  16. /*             414243000D204. Then the hex string 414243000D204 will be       */
  17. /*             converted with the specified plan C_TO_COBOL into the hex      */
  18. /*             string C1C2C301234C. The result will be printed on screen.     */
  19. /*                                                                            */
  20. /* NOTES =                                                                    */
  21. /*                                                                            */
  22. /*   DEPENDENCIES = OS/2 Release 2.0 or later                                 */
  23. /*                                                                            */
  24. /*   RESTRICTIONS = None                                                      */
  25. /*                                                                            */
  26. /* ENTRY POINTS = main()                                                      */
  27. /*                                                                            */
  28. /*                                                                            */
  29. /******************************************************************************/
  30. #pragma page ()
  31. /******************************************************************************/
  32. /* Header Files.                                                              */
  33. /******************************************************************************/
  34.  
  35. /*---------------- OS/2 Base Header ------------------------------------------*/
  36.  
  37. #define INCL_BASE                      /* all of OS/2 Base                    */
  38. #define INCL_NOPMAPI                   /* no presentation manager functions   */
  39. #include <os2.h>
  40.  
  41. /*---------------- C Library Header ------------------------------------------*/
  42.  
  43. #include <stdio.h>
  44. #include <memory.h>
  45. #include <string.h>
  46. #include <stdlib.h>
  47. #include <ctype.h>
  48. #include <io.h>
  49.  
  50. #pragma page ()
  51. /******************************************************************************/
  52. /* DDC/2 global header file                                                   */
  53. /******************************************************************************/
  54. #define FMT_NO_DCLXLRIFC FMT_NO_DCLXLRIFC
  55.                                        /* exclude the Declaration Translator
  56.                                           and Generate function prototypes and
  57.                                           their declarations                  */
  58. #define FMT_NO_CPB   FMT_NO_CPB        /* exclude the Conversion Plan Builder
  59.                                           function prototypes and
  60.                                           their declarations                  */
  61. #define FMT_NO_LCF   FMT_NO_LCF        /* exclude the Low Level Conversion
  62.                                           Functions function prototypes and
  63.                                           their declarations                  */
  64.  
  65. #include  "FMT.H"
  66.  
  67. /******************************************************************************/
  68. /* Define length of input and output buffer                                   */
  69. /******************************************************************************/
  70. #define  BUFFER_LENGTH  6
  71.  
  72. /******************************************************************************/
  73. /* Enumeration to identify the appropriate CPEX function                      */
  74. /******************************************************************************/
  75. enum Execute { INIT,
  76.                CONVERT,
  77.                TERM
  78. };
  79.  
  80. /******************************************************************************/
  81. /*                                                                            */
  82. /* The function PrintCtok prints the condition token and the ADL              */
  83. /* communication area after an error occured in a conversion plan executor    */
  84. /* function.                                                                  */
  85. /*                                                                            */
  86. /******************************************************************************/
  87. void PrintCtok( PFMTCTOK  pFeedBack, PFMTADLCA pMyIsInfo, enum Execute Type )
  88. {
  89.  
  90.   switch ( Type )
  91.     {
  92.       case INIT    : printf("Error in Conversion Plan Executor Init.\n");
  93.         break;
  94.       case CONVERT : printf("Error in Conversion Plan Executor Convert.\n");
  95.         break;
  96.       case TERM    : printf("Error in Conversion Plan Executor Term.\n");
  97.         break;
  98.     } /* endswitch */
  99.  
  100.   printf("The Condition Token has the following contents:\n");
  101.  
  102.   printf("Message Severity %d Number %d\n",pFeedBack->Condition_ID.usMsgSev,
  103.                                            pFeedBack->Condition_ID.usMsgNo);
  104.   printf("Service Condition Case %d\n",    pFeedBack->fCase);
  105.   printf("Condition Severity     %d\n",    pFeedBack->fSeverity);
  106.   printf("Control                %d\n",    pFeedBack->fControl);
  107.   printf("Facility ID            %c%c%c\n",pFeedBack->uchFacility_ID[0],
  108.                                            pFeedBack->uchFacility_ID[1],
  109.                                            pFeedBack->uchFacility_ID[2]);
  110.  
  111.   /****************************************************************************/
  112.   /* Check whether an ADL exception occurred.                                 */
  113.   /****************************************************************************/
  114.   if ( pFeedBack->Condition_ID.usMsgNo == CPX_ADL_EXCEPTION_SEV2 ||
  115.        pFeedBack->Condition_ID.usMsgNo == CPX_ADL_EXCEPTION_SEV3 )
  116.    {
  117.      if ( Type == INIT )
  118.       {
  119.         printf("ADL exception         %d\n", pFeedBack->pI_S_Info.ulAdlExId );
  120.       } /* endif */
  121.      else
  122.       {
  123.         printf("The ADL communication area has the following contents:\n" );
  124.  
  125.         printf("ADL exception:             %d\n", pMyIsInfo->lExId );
  126.         printf("Severity of ADL exception: %d\n", pMyIsInfo->usSevCod );
  127.                                        /* The Severity of the ADL          */
  128.                                        /* exception has the same value as  */
  129.                                        /* the message severity             */
  130.                                        /* ( Feedback.Condition_ID.usMsgSev */
  131.  
  132.         printf("Name of processed plan:    %.255s\n",
  133.                                                   pMyIsInfo->PlanId.uchData );
  134.         printf("Number of processed PLAN statement: %d\n",
  135.                                                   pMyIsInfo->lPlanStmt );
  136.         printf("Input data portion that caused the error: %.255s\n"
  137.                , pMyIsInfo->InpErrDta.uchData );
  138.         printf("Source identifier of processed assignment statement: %.255s\n"
  139.                , pMyIsInfo->SrcFldId.uchData );
  140.         printf("Target identifier of processed assignment statement: %.255s\n"
  141.                , pMyIsInfo->TrgFldId.uchData );
  142.  
  143.       } /* endelse */
  144.    } /* endif */
  145.  
  146.   return;
  147.  
  148. }
  149.  
  150.  
  151.  
  152. /******************************************************************************/
  153. /*  MAIN function                                                             */
  154. /******************************************************************************/
  155. int main( )
  156. {
  157.   FMTCTOK     FeedBack;
  158.   FMTADLCA    MyIsInfo;
  159.   PVOID       pCnvPlnSpc;
  160.   FILE        *CnvPlnSpcHandle;
  161.   ULONG       ulLength;
  162.   ULONG       ulCnvPlnSpcHdl;
  163.   PBYTE       *ppInputData = 0;
  164.   PBYTE       *ppOutputData = 0;
  165.   PBYTE       pInValue = 0;
  166.   PBYTE       pOutValue = 0;
  167.   CHAR        Buffer[ BUFFER_LENGTH * 2 ];
  168.   CHAR        EBCDIC[] = { 0xC1,0xC2,0xC3,0x01,0x23,0x4C };
  169.   CHAR        ASCII[] = { 0x41,0x42,0x43,0x00,0xD2,0x04 };
  170.   int         k;
  171.  
  172.   /****************************************************************************/
  173.   /* Read the conversion plan space from file SAMPLE.SPC                      */
  174.   /****************************************************************************/
  175.  
  176.   CnvPlnSpcHandle = fopen( "SAMPLE.SPC","rb");
  177.   ulLength = _filelength( _fileno( CnvPlnSpcHandle ));
  178.   pCnvPlnSpc = (PVOID)calloc( ulLength, sizeof(CHAR));
  179.  
  180.   fread( pCnvPlnSpc, sizeof(CHAR), ulLength, CnvPlnSpcHandle );
  181.  
  182.   fclose( CnvPlnSpcHandle );
  183.  
  184.  
  185.   /****************************************************************************/
  186.   /* Call Conversion Plan Executor Initialisation                             */
  187.   /****************************************************************************/
  188.  
  189.   FMTCPXI( pCnvPlnSpc,                    // PBYTE    pCnvPlnSpc
  190.            &ulCnvPlnSpcHdl,               // PULONG   pulCnvPlnSpcHdl
  191.            &FeedBack );                   // PFMTCTOK pFeedback
  192.  
  193.  
  194.   /****************************************************************************/
  195.   /* Check the Condition Token                                                */
  196.   /****************************************************************************/
  197.   if ( FeedBack.Condition_ID.usMsgNo != CPX_NO_ERROR )
  198.    {
  199.      PrintCtok( &FeedBack, NULL, INIT );
  200.  
  201.    } /* endif */
  202.   else
  203.    {
  204.      /*************************************************************************/
  205.      /* Initialize ADL Communication Area                                     */
  206.      /*************************************************************************/
  207.      FeedBack.pI_S_Info.pAdlCommArea = &MyIsInfo;
  208.  
  209.      /*************************************************************************/
  210.      /* Alloc input and output buffer                                         */
  211.      /*************************************************************************/
  212.      pInValue     = malloc( BUFFER_LENGTH );
  213.      pOutValue    = malloc( BUFFER_LENGTH );
  214.      ppInputData  = malloc( sizeof( PBYTE ) );
  215.      ppOutputData = malloc( sizeof(PBYTE) );
  216.  
  217.      /*************************************************************************/
  218.      /* Call Conversion Plan Executor Convert                                 */
  219.      /* In this conversion the plan COBOL_TO_C is executed. The input data    */
  220.      /* are :                                                                 */
  221.      /*    "ABC"  in international EBCDIC format  -> '0xC1C2C3'               */
  222.      /*    1234   in PACKED PRECISION(5) format   -> '0x01234C'               */
  223.      /*                                                                       */
  224.      /* The output data after conversion should be:                           */
  225.      /*    "ABC"  in Latin PC Data format + suffix  -> '0x41424300'           */
  226.      /*    1234   in BINARY bytereversed format     -> '0xD204'               */
  227.      /*************************************************************************/
  228.  
  229.      memcpy( pInValue, EBCDIC, sizeof(EBCDIC));
  230.      memset( pOutValue, 0, BUFFER_LENGTH );
  231.  
  232.  
  233.      ppInputData[0] = pInValue;
  234.      ppOutputData[0] = pOutValue;
  235.  
  236.  
  237.      FMTCPXC(
  238.                ulCnvPlnSpcHdl,              // ULONG    ulCnvPlnSpcHdl
  239.                10,                          // ULONG    ulPlnNamLength
  240.                "COBOL_TO_C",                // PCHAR    pPlnNam
  241.                1,                           // ULONG    ulInputParmNum
  242.                ppInputData,                 // PBYTE    *ppInputData
  243.                1,                           // ULONG    ulOutputParmNum
  244.                ppOutputData,                // PBYTE    *ppInputData
  245.                &FeedBack );                 // PFMTCTOK pFeedBack
  246.  
  247.      if ( FeedBack.Condition_ID.usMsgNo != CPX_NO_ERROR )
  248.       {
  249.         /**********************************************************************/
  250.         /* An error occured. Print the condition token                        */
  251.         /**********************************************************************/
  252.         PrintCtok( &FeedBack, &MyIsInfo, CONVERT );
  253.  
  254.       } /* endif */
  255.      else
  256.       {
  257.         /**********************************************************************/
  258.         /* Print the converted value                                          */
  259.         /**********************************************************************/
  260.         printf(" Converted value for plan COBOL_TO_C:\n" );
  261.         for( k=0; k < BUFFER_LENGTH ;k++)
  262.         {
  263.           printf("%02x",pOutValue[k]);
  264.         }
  265.  
  266.       } /* endelse */
  267.  
  268.      /*************************************************************************/
  269.      /* Call Conversion Plan Executor Convert                                 */
  270.      /* In this conversion the plan C_TO_COBOL is executed. The input data    */
  271.      /* are :                                                                 */
  272.      /*    "ABC"  in Latin PC Data format + suffix  -> '0x41424300'           */
  273.      /*    1234   in BINARY bytereversed format     -> '0xD204'               */
  274.      /*                                                                       */
  275.      /* The output data after conversion should be:                           */
  276.      /*    "ABC"  in international EBCDIC format  -> '0xC1C2C3'               */
  277.      /*    1234   in PACKED PRECISION(5) format   -> '0x01234C'               */
  278.      /*************************************************************************/
  279.  
  280.      memcpy( pInValue, ASCII, sizeof(ASCII));
  281.      memset( pOutValue, 0, BUFFER_LENGTH );
  282.  
  283.      ppInputData[0] = pInValue;
  284.      ppOutputData[0] = pOutValue;
  285.  
  286.  
  287.      FMTCPXC(
  288.                ulCnvPlnSpcHdl,              // ULONG    ulCnvPlnSpcHdl
  289.                10,                          // ULONG    ulPlnNamLength
  290.                "C_TO_COBOL",                // PCHAR    pPlnNam
  291.                1,                           // ULONG    ulInputParmNum
  292.                ppInputData,                 // PBYTE   *apInputData
  293.                1,                           // ULONG    ulOutputParmNum
  294.                ppOutputData,                // PBYTE   *apInputData
  295.                &FeedBack );                 // PFMTCTOK pFeedBack
  296.  
  297.      if ( FeedBack.Condition_ID.usMsgNo != CPX_NO_ERROR )
  298.       {
  299.         /**********************************************************************/
  300.         /* An error occured. Print the condition token                        */
  301.         /**********************************************************************/
  302.         PrintCtok( &FeedBack, &MyIsInfo, CONVERT );
  303.  
  304.       } /* endif */
  305.      else
  306.       {
  307.         /**********************************************************************/
  308.         /* Print the converted value                                          */
  309.         /**********************************************************************/
  310.         printf("\n Converted value for plan C_TO_COBOL:\n" );
  311.         for( k=0; k < BUFFER_LENGTH ;k++)
  312.         {
  313.           printf("%02x",pOutValue[k]);
  314.         }
  315.       } /* endelse */
  316.  
  317.  
  318.  
  319.      /*************************************************************************/
  320.      /* Call Conversion Plan Executor Termination                             */
  321.      /*************************************************************************/
  322.  
  323.      FMTCPXT(
  324.               ulCnvPlnSpcHdl,               // ULONG    ulCnvPlnSpcHdl
  325.               &FeedBack );                  // PFMTCTOK pFeedBack
  326.  
  327.  
  328.      /*************************************************************************/
  329.      /* Check the Condition Token                                             */
  330.      /*************************************************************************/
  331.      if ( FeedBack.Condition_ID.usMsgNo != CPX_NO_ERROR )
  332.       {
  333.         PrintCtok( &FeedBack, NULL, TERM );
  334.       } /* endif */
  335.  
  336.  
  337.    } /* endelse */
  338.  
  339.   /****************************************************************************/
  340.   /* Free allocated resources                                                 */
  341.   /****************************************************************************/
  342.  
  343.   if ( pCnvPlnSpc != NULL ) {
  344.     free( pCnvPlnSpc );
  345.   } /* endif */
  346.  
  347.   if ( pInValue != NULL ) {
  348.     free( pInValue );
  349.   } /* endif */
  350.  
  351.   if ( pOutValue != NULL ) {
  352.     free( pOutValue );
  353.   } /* endif */
  354.  
  355.   if ( ppInputData != NULL) {
  356.     free( ppInputData );
  357.   } /* endif */
  358.  
  359.   if ( ppOutputData != NULL) {
  360.     free( ppOutputData );
  361.   } /* endif */
  362.  
  363.   return 0;
  364. }
  365.