home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / OPENSTEP / Languages / Python / python-14-src / Modules / _xdrmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-17  |  4.5 KB  |  188 lines

  1. /* This module exports part of the C API to the XDR routines into Python.
  2.  * XDR is Sun's eXternal Data Representation, as described in RFC 1014.  This
  3.  * module is used by xdrlib.py to support the float and double data types
  4.  * which are too much of a pain to support in Python directly.  It is
  5.  * not required by xdrlib.py -- when not available, these types aren't
  6.  * supported at the Python layer.  Note that representations that can be
  7.  * implemented solely in Python, are *not* reproduced here.
  8.  *
  9.  * Module version number: 1.0
  10.  *
  11.  * See xdrlib.py for usage notes.
  12.  *
  13.  * Note: this has so far, only been tested on Solaris 2.5 and IRIX 5.3.  On
  14.  * these systems, you will need to link with -lnsl for these symbols to be
  15.  * defined.
  16.  */
  17.  
  18. #include "Python.h"
  19. #ifndef NeXT
  20. #include <netconfig.h>
  21. #endif
  22. #include <rpc/rpc.h>
  23. #include <rpc/xdr.h>
  24.  
  25. static PyObject* xdr_error;
  26.  
  27.  
  28.  
  29. static PyObject*
  30. pack_float(self, args)
  31.      PyObject* self;
  32.      PyObject* args;
  33. {
  34.     XDR xdr;
  35.     float value;
  36.     union {                                  /* guarantees proper alignment */
  37.         long dummy;
  38.         char buffer[4];
  39.     } addr;
  40.     PyObject* rtn = NULL;
  41.  
  42.     if (!PyArg_ParseTuple(args, "f", &value))
  43.         return NULL;
  44.  
  45.     xdr.x_ops = NULL;
  46.     xdrmem_create(&xdr, addr.buffer, 4, XDR_ENCODE);
  47.     if( xdr.x_ops == NULL )
  48.         PyErr_SetString(xdr_error, "XDR stream initialization failed.");
  49.     else if (xdr_float(&xdr, &value))
  50.         rtn = PyString_FromStringAndSize(addr.buffer, 4);
  51.     else
  52.         PyErr_SetString(xdr_error, "conversion from float failed");
  53.  
  54.     xdr_destroy(&xdr);
  55.     return rtn;
  56. }
  57.     
  58.  
  59.  
  60. static PyObject*
  61. pack_double(self, args)
  62.      PyObject* self;
  63.      PyObject* args;
  64. {
  65.     XDR xdr;
  66.     double value;
  67.     union {                                  /* guarantees proper alignment */
  68.         long dummy;
  69.         char buffer[8];
  70.     } addr;
  71.     PyObject* rtn = NULL;
  72.  
  73.     if (!PyArg_ParseTuple(args, "d", &value))
  74.         return NULL;
  75.  
  76.     xdr.x_ops = NULL;
  77.     xdrmem_create(&xdr, addr.buffer, 8, XDR_ENCODE);
  78.     if( xdr.x_ops == NULL )
  79.         PyErr_SetString(xdr_error, "XDR stream initialization failed.");
  80.     else if (xdr_double(&xdr, &value))
  81.         rtn = PyString_FromStringAndSize(addr.buffer, 8);
  82.     else
  83.         PyErr_SetString(xdr_error, "conversion from double failed");
  84.  
  85.     xdr_destroy(&xdr);
  86.     return rtn;
  87. }
  88.  
  89.  
  90.  
  91. static PyObject*
  92. unpack_float(self, args)
  93.      PyObject* self;
  94.      PyObject* args;
  95. {
  96.     XDR xdr;
  97.     float value;
  98.     char* string;
  99.     int strlen;
  100.     PyObject* rtn = NULL;
  101.  
  102.     if (!PyArg_ParseTuple(args, "s#", &string, &strlen))
  103.         return NULL;
  104.  
  105.     if (strlen != 4) {
  106.         PyErr_SetString(PyExc_ValueError, "4 byte string expected");
  107.         return NULL;
  108.     }
  109.  
  110.         /* Python guarantees that the string is 4 byte aligned */
  111.     xdr.x_ops = NULL;
  112.     xdrmem_create(&xdr, (caddr_t)string, 4, XDR_DECODE);
  113.     if( xdr.x_ops == NULL )
  114.         PyErr_SetString(xdr_error, "XDR stream initialization failed.");
  115.     else if (xdr_float(&xdr, &value))
  116.         rtn = Py_BuildValue("f", value);
  117.     else
  118.         PyErr_SetString(xdr_error, "conversion to float failed");
  119.  
  120.     xdr_destroy(&xdr);
  121.     return rtn;
  122. }
  123.  
  124.     
  125.  
  126. static PyObject*
  127. unpack_double(self, args)
  128.      PyObject* self;
  129.      PyObject* args;
  130. {
  131.     XDR xdr;
  132.     double value;
  133.     char* string;
  134.     int strlen;
  135.     PyObject* rtn = NULL;
  136.  
  137.     if (!PyArg_ParseTuple(args, "s#", &string, &strlen))
  138.         return NULL;
  139.  
  140.     if (strlen != 8) {
  141.         PyErr_SetString(PyExc_ValueError, "8 byte string expected");
  142.         return NULL;
  143.     }
  144.  
  145.         /* Python guarantees that the string is 4 byte aligned */
  146.     xdr.x_ops = NULL;
  147.     xdrmem_create(&xdr, (caddr_t)string, 8, XDR_DECODE);
  148.     if( xdr.x_ops == NULL ) {
  149.         PyErr_SetString(xdr_error, "XDR stream initialization failed.");
  150.     rtn = NULL;
  151.     }
  152.     else if (xdr_double(&xdr, &value))
  153.         rtn = Py_BuildValue("d", value);
  154.     else {
  155.         PyErr_SetString(xdr_error, "conversion to double failed");
  156.     rtn = NULL;
  157.     }
  158.  
  159.     xdr_destroy(&xdr);
  160.     return rtn;
  161. }
  162.  
  163.  
  164.  
  165. static struct PyMethodDef
  166. xdr_methods[] = {
  167.     {"pack_float",    pack_float,  1},
  168.     {"pack_double",   pack_double, 1},
  169.     {"unpack_float",  unpack_float, 1},
  170.     {"unpack_double", unpack_double, 1},
  171.     {NULL,          NULL,       0}           /* sentinel */
  172. };
  173.  
  174.  
  175.  
  176. void
  177. init_xdr()
  178. {
  179.     PyObject* module;
  180.     PyObject* dict;
  181.  
  182.     module = Py_InitModule("_xdr", xdr_methods);
  183.     dict = PyModule_GetDict(module);
  184.  
  185.     xdr_error = PyString_FromString("_xdr.error");
  186.     PyDict_SetItemString(dict, "error", xdr_error);
  187. }
  188.