home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Modules / _xdrmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-14  |  4.4 KB  |  182 lines  |  [TEXT/CWIE]

  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.  
  20. #include <rpc/rpc.h>
  21. #include <rpc/xdr.h>
  22.  
  23. static PyObject* xdr_error;
  24.  
  25.  
  26.  
  27. static PyObject*
  28. pack_float(self, args)
  29.      PyObject* self;
  30.      PyObject* args;
  31. {
  32.     XDR xdr;
  33.     float value;
  34.     union {                                  /* guarantees proper alignment */
  35.         long dummy;
  36.         char buffer[4];
  37.     } addr;
  38.     PyObject* rtn = NULL;
  39.  
  40.     if (!PyArg_ParseTuple(args, "f", &value))
  41.         return NULL;
  42.  
  43.     xdr.x_ops = NULL;
  44.     xdrmem_create(&xdr, addr.buffer, 4, XDR_ENCODE);
  45.     if( xdr.x_ops == NULL )
  46.         PyErr_SetString(xdr_error, "XDR stream initialization failed.");
  47.     else if (xdr_float(&xdr, &value))
  48.         rtn = PyString_FromStringAndSize(addr.buffer, 4);
  49.     else
  50.         PyErr_SetString(xdr_error, "conversion from float failed");
  51.  
  52.     xdr_destroy(&xdr);
  53.     return rtn;
  54. }
  55.     
  56.  
  57.  
  58. static PyObject*
  59. pack_double(self, args)
  60.      PyObject* self;
  61.      PyObject* args;
  62. {
  63.     XDR xdr;
  64.     double value;
  65.     union {                                  /* guarantees proper alignment */
  66.         long dummy;
  67.         char buffer[8];
  68.     } addr;
  69.     PyObject* rtn = NULL;
  70.  
  71.     if (!PyArg_ParseTuple(args, "d", &value))
  72.         return NULL;
  73.  
  74.     xdr.x_ops = NULL;
  75.     xdrmem_create(&xdr, addr.buffer, 8, XDR_ENCODE);
  76.     if( xdr.x_ops == NULL )
  77.         PyErr_SetString(xdr_error, "XDR stream initialization failed.");
  78.     else if (xdr_double(&xdr, &value))
  79.         rtn = PyString_FromStringAndSize(addr.buffer, 8);
  80.     else
  81.         PyErr_SetString(xdr_error, "conversion from double failed");
  82.  
  83.     xdr_destroy(&xdr);
  84.     return rtn;
  85. }
  86.  
  87.  
  88.  
  89. static PyObject*
  90. unpack_float(self, args)
  91.      PyObject* self;
  92.      PyObject* args;
  93. {
  94.     XDR xdr;
  95.     float value;
  96.     char* string;
  97.     int strlen;
  98.     PyObject* rtn = NULL;
  99.  
  100.     if (!PyArg_ParseTuple(args, "s#", &string, &strlen))
  101.         return NULL;
  102.  
  103.     if (strlen != 4) {
  104.         PyErr_SetString(PyExc_ValueError, "4 byte string expected");
  105.         return NULL;
  106.     }
  107.  
  108.         /* Python guarantees that the string is 4 byte aligned */
  109.     xdr.x_ops = NULL;
  110.     xdrmem_create(&xdr, (caddr_t)string, 4, XDR_DECODE);
  111.     if( xdr.x_ops == NULL )
  112.         PyErr_SetString(xdr_error, "XDR stream initialization failed.");
  113.     else if (xdr_float(&xdr, &value))
  114.         rtn = Py_BuildValue("f", value);
  115.     else
  116.         PyErr_SetString(xdr_error, "conversion to float failed");
  117.  
  118.     xdr_destroy(&xdr);
  119.     return rtn;
  120. }
  121.  
  122.     
  123.  
  124. static PyObject*
  125. unpack_double(self, args)
  126.      PyObject* self;
  127.      PyObject* args;
  128. {
  129.     XDR xdr;
  130.     double value;
  131.     char* string;
  132.     int strlen;
  133.     PyObject* rtn = NULL;
  134.  
  135.     if (!PyArg_ParseTuple(args, "s#", &string, &strlen))
  136.         return NULL;
  137.  
  138.     if (strlen != 8) {
  139.         PyErr_SetString(PyExc_ValueError, "8 byte string expected");
  140.         return NULL;
  141.     }
  142.  
  143.         /* Python guarantees that the string is 4 byte aligned */
  144.     xdr.x_ops = NULL;
  145.     xdrmem_create(&xdr, (caddr_t)string, 8, XDR_DECODE);
  146.     if( xdr.x_ops == NULL )
  147.         PyErr_SetString(xdr_error, "XDR stream initialization failed.");
  148.     else if (xdr_double(&xdr, &value))
  149.         rtn = Py_BuildValue("d", value);
  150.     else
  151.         PyErr_SetString(xdr_error, "conversion to double failed");
  152.  
  153.     xdr_destroy(&xdr);
  154.     return rtn;
  155. }
  156.  
  157.  
  158.  
  159. static struct PyMethodDef
  160. xdr_methods[] = {
  161.     {"pack_float",    pack_float,  1},
  162.     {"pack_double",   pack_double, 1},
  163.     {"unpack_float",  unpack_float, 1},
  164.     {"unpack_double", unpack_double, 1},
  165.     {NULL,          NULL,       0}           /* sentinel */
  166. };
  167.  
  168.  
  169.  
  170. void
  171. init_xdr()
  172. {
  173.     PyObject* module;
  174.     PyObject* dict;
  175.  
  176.     module = Py_InitModule("_xdr", xdr_methods);
  177.     dict = PyModule_GetDict(module);
  178.  
  179.     xdr_error = PyString_FromString("_xdr.error");
  180.     PyDict_SetItemString(dict, "error", xdr_error);
  181. }
  182.