home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / xdr.zip / xdrtest.cpp < prev   
C/C++ Source or Header  |  1993-06-03  |  3KB  |  137 lines

  1. /*************************************************************************
  2. *
  3. * File Name     : XDRTEST.CPP
  4. *
  5. * Description: used for debugging XDR.CPP
  6. *
  7. * Exports     : main()
  8. *
  9. * Imports     : xdrmem_create <xdr.h>    xdr_int <xdr.h>
  10. *               xdr_string <xdr.h>        xdr_vector <xdr.h>
  11. *               xdr_union <xdr.h>        xdr_destroy <xdr.h>
  12. *              strcpy <string.h>        printf <stdio.h>
  13. *              xdrstdio_create <xdr.h>  fopen <stdio.h>
  14. *               fclose <stdio.h>
  15. *
  16. * copyright (c) Jörg Caumanns, 1993 [caumanns@cs.tu-berlin.de]
  17. *
  18. *      DISCLAIMER OF WARRANTIES.
  19. *      The code is provided "AS IS", without warranty of any kind.
  20. *
  21. *************************************************************************/
  22. #include <os2def.h>
  23. #include <xdr.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26.  
  27. /*
  28. * needed for xdr_union
  29. */
  30. enum disc_type    { END = 0, ERROR, INTEGER, CHARACTER };
  31. xdr_discrim filter_dir[] =    {
  32.     { ERROR,    (xdrproc_t)xdr_int        },
  33.     { INTEGER,    (xdrproc_t)xdr_int        },
  34.     { CHARACTER,(xdrproc_t)xdr_char        },
  35.     { END,        (xdrproc_t)NULL        }    };
  36.  
  37. /************************************************************************/
  38.  
  39. main(void)    {
  40.     XDR xdrE;
  41.  
  42. #ifdef MEMSTREAM
  43.     CHAR *pb = new CHAR[4096];
  44.  
  45.     /*
  46.     * create XDR memory stream
  47.     */
  48.     xdrmem_create(&xdrE, pb, 4096, XDR_ENCODE);
  49. #else
  50.     FILE *pf;
  51.     if((pf = fopen("test", "wb")) == (FILE *)NULL)
  52.         return 0;
  53.  
  54.     xdrstdio_create(&xdrE, pf, XDR_ENCODE);
  55. #endif
  56.  
  57.     /*
  58.     * encode some objects
  59.     */
  60.     INT i = 9;                      // encode integer value
  61.     xdr_int(&xdrE, &i);
  62.  
  63.     CHAR *str = new CHAR[32];        // encode string
  64.     strcpy(str, "test string");
  65.     xdr_string(&xdrE, &str, 256);
  66.  
  67.     INT aint[4];                    // encode array of integers
  68.     for(INT j = 0; j < 4; j++)
  69.         aint[j] = 100+j;
  70.     xdr_vector(&xdrE, (CHAR *)aint, 4, 4, (xdrproc_t)xdr_int);
  71.  
  72.     union    {                          // encode discriminated union
  73.         INT        error;
  74.         INT        integer;
  75.         CHAR    character;
  76.         } test_union;
  77.     INT disc;
  78.     test_union.character = 'c';
  79.     disc = CHARACTER;
  80.     xdr_union(&xdrE, &disc, (CHAR *)&test_union, filter_dir, NULL);
  81.  
  82.     /*
  83.     * destroy XDR stream handle
  84.     */
  85.     xdr_destroy(&xdrE);
  86.  
  87. #ifndef MEMSTREAM
  88.     fclose(pf);
  89. #endif
  90.  
  91.     /*
  92.     * invalidate variables
  93.     */
  94.     i = 0;
  95.     strcpy(str, "xxxxxxxxxxxxxxxxxx");
  96.     for(j = 0; j < 4; j++)
  97.         aint[j] = 0;
  98.     test_union.integer = 0;
  99.  
  100.     /*
  101.     * create XDR memory stream to read previously written data
  102.     */
  103.     XDR xdrD;
  104. #ifdef MEMSTREAM
  105.     xdrmem_create(&xdrD, pb, 4096, XDR_DECODE);
  106. #else
  107.     if((pf = fopen("test", "rb")) == (FILE *)NULL)
  108.         return 0;
  109.  
  110.     xdrstdio_create(&xdrD, pf, XDR_DECODE);
  111. #endif
  112.  
  113.     xdr_int(&xdrD, &i);
  114.     xdr_string(&xdrD, &str, 256);
  115.     xdr_vector(&xdrD, (CHAR *)aint, 4, 4, (xdrproc_t)xdr_int);
  116.     xdr_union(&xdrD, &disc, (CHAR *)&test_union, filter_dir, NULL);
  117.  
  118.     xdr_destroy(&xdrD);
  119.  
  120. #ifndef MEMSTREAM
  121.     fclose(pf);
  122. #endif
  123.  
  124.     /*
  125.     * display results
  126.     */
  127.     printf("i = %d\n", i);
  128.     printf("str = %s\n", str);
  129.     for(j = 0; j < 4; j++)
  130.         printf("aint[%d] = %d\n", j, aint[j]);
  131.     printf("test_union.character = %c\n", test_union.character);
  132.  
  133.     return 0;
  134.     }
  135.  
  136.  
  137.