home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fns101.zip / Add-Ons / Fnorb / src / longlong.c < prev    next >
C/C++ Source or Header  |  1999-06-28  |  5KB  |  225 lines

  1. /***************************************************************
  2.  
  3.   Copyright (C) DSTC Pty Ltd (ACN 052 372 577) 1997, 1998, 1999
  4.   Unpublished work.  All Rights Reserved.
  5.  
  6.   The software contained on this media is the property of the
  7.   DSTC Pty Ltd.  Use of this software is strictly in accordance
  8.   with the license agreement in the accompanying LICENSE.HTML
  9.   file.  If your distribution of this software does not contain
  10.   a LICENSE.HTML file then you have no rights to use this
  11.   software in any manner and should contact DSTC at the address
  12.   below to determine an appropriate licensing arrangement.
  13.  
  14.      DSTC Pty Ltd
  15.      Level 7, Gehrmann Labs
  16.      University of Queensland
  17.      St Lucia, 4072
  18.      Australia
  19.      Tel: +61 7 3365 4310
  20.      Fax: +61 7 3365 4311
  21.      Email: enquiries@dstc.edu.au
  22.  
  23.   This software is being provided "AS IS" without warranty of
  24.   any kind.  In no event shall DSTC Pty Ltd be liable for
  25.   damage of any kind arising out of or in connection with
  26.   the use or performance of this software.
  27.  
  28.  
  29.   Project:      Fnorb
  30.   File:         $Source: /units/arch/src/Fnorb/src/RCS/longlong.c,v $
  31.   Version:      @(#)$RCSfile: longlong.c,v $ $Revision: 1.1 $
  32.  
  33.  
  34.   Description:    Implementation of CORBA 'long long's.
  35.  
  36. ****************************************************************/
  37.  
  38. #if !defined(lint)
  39. static const char rcsid[] = "@(#)$RCSfile: longlong.c,v $ $Revision: 1.1 $";
  40. #endif
  41.  
  42. /*
  43.  * Standard library includes (e.g., stdio)
  44.  */
  45.  
  46. /*
  47.  * Application library includes (e.g., X, DCE)
  48.  */
  49. #include "Python.h"                /* Python API        */
  50.  
  51. /*
  52.  * DSTC library includes (e.g., dstc_stdlib.h)
  53.  */
  54.  
  55. /*
  56.  * Project library includes              
  57.  */
  58. #include "longlong.h"
  59.  
  60. /*
  61.  * Local file includes
  62.  */
  63.  
  64. /*
  65.  * 'Methods' for CORBA 'unsigned long long's.
  66.  */
  67. void
  68. ulonglong_from_py_long(CORBA_unsigned_longlong* ulonglong, PyObject* py_long)
  69. {
  70.   PyObject* x;
  71.   PyObject* y;
  72.   PyObject* z;
  73.  
  74.   int i;
  75.  
  76.   /* 'Constants' ;^) */
  77.   PyObject* eight = PyLong_FromLong(8);
  78.   PyObject* mask  = PyLong_FromUnsignedLong(0xff);
  79.  
  80.   /* Starting with the Python long value. */
  81.   x = py_long;
  82.   Py_INCREF(x);
  83.  
  84. #ifdef __FNORB_BIG_ENDIAN
  85.   for (i = 7; i >= 0; i--) {
  86. #else
  87.   for (i = 0; i <= 7; i++) {
  88. #endif
  89.     /* Get the LSB. */
  90.     y = PyNumber_And(x, mask);
  91.     
  92.     /* Extract the LSB as an octet and add it to the result. */
  93.     ulonglong->octets[i] = (CORBA_octet) PyLong_AsLong(y);
  94.  
  95.     /* Shift right to get the next octet. */
  96.     z = PyNumber_Rshift(x, eight);
  97.  
  98.     /* Clean up. */
  99.     Py_DECREF(x);
  100.     Py_DECREF(y);
  101.  
  102.     x = z;
  103.   }
  104.  
  105.   /* Clean up the unused final shifted value. */
  106.   Py_DECREF(x);
  107.  
  108.   /* Clean up the constants. */
  109.   Py_DECREF(eight);
  110.   Py_DECREF(mask);
  111. }
  112.  
  113. PyObject*
  114. ulonglong_to_py_long(CORBA_unsigned_longlong* ulonglong)
  115. {
  116.   PyObject* x;
  117.   PyObject* y;
  118.   PyObject* z;
  119.  
  120.   int i;
  121.  
  122.   PyObject* result = PyLong_FromLong(0);
  123.  
  124.   /* 'Constants' ;^) */
  125.   PyObject* eight  = PyInt_FromLong(8);
  126.   PyObject* mask   = PyInt_FromLong(0xff);
  127.  
  128. #ifdef __FNORB_BIG_ENDIAN
  129.   for (i = 0; i <= 6; i++) {
  130. #else
  131.   for (i = 7; i >= 1; i--) {
  132. #endif
  133.     /* Get the MSB as a Python long. */
  134.     x = PyLong_FromLong((long) ulonglong->octets[i]);
  135.  
  136.     /* Add it to the result. */
  137.     y = PyNumber_Or(result, x);
  138.  
  139.     /* Shift right to 'make room' for the next octet! */
  140.     z = PyNumber_Lshift(y, eight);
  141.  
  142.     /* Clean up. */
  143.     Py_DECREF(result);
  144.     Py_DECREF(x);
  145.     Py_DECREF(y);
  146.  
  147.     result = z;
  148.   }
  149.  
  150.   /* Finishing up with the LSB. */
  151. #ifdef __FNORB_BIG_ENDIAN
  152.   x = PyLong_FromLong((long) ulonglong->octets[7]);
  153. #else
  154.   x = PyLong_FromLong((long) ulonglong->octets[0]);
  155. #endif
  156.  
  157.   /* Add it to the result. */
  158.   y = PyNumber_Or(result, x);
  159.   
  160.   /* Clean up. */
  161.   Py_DECREF(result);
  162.   Py_DECREF(x);
  163.  
  164.   result = y;
  165.  
  166.   /* Clean up */
  167.   Py_DECREF(eight);
  168.   Py_DECREF(mask);
  169.  
  170.   return result;
  171. }
  172.  
  173. /*
  174.  * 'Methods' for CORBA 'long long's.
  175.  */
  176. void
  177. longlong_from_py_long(CORBA_longlong* longlong, PyObject* py_long)
  178. {
  179.     ulonglong_from_py_long((CORBA_unsigned_longlong*) longlong, py_long);
  180. }
  181.  
  182. PyObject*
  183. longlong_to_py_long(CORBA_longlong* longlong)
  184. {
  185.   PyObject* result;
  186.   PyObject* x;
  187.   PyObject* y;
  188.  
  189.   /* 'Constants' ;^) */
  190.   PyObject* two         = PyLong_FromLong(2);
  191.   PyObject* sixty_three = PyLong_FromLong(63);
  192.   PyObject* big         = PyNumber_Power(two, sixty_three, Py_None);
  193.   PyObject* not_big     = PyNumber_Invert(big);
  194.  
  195.   /* Get the result as for an 'unsigned long long'. */
  196.   result = ulonglong_to_py_long((CORBA_unsigned_longlong*) longlong);
  197.  
  198.   /* Now check the sign bit! */
  199. #ifdef __FNORB_BIG_ENDIAN
  200.   if (longlong->octets[0] & 0x80) {
  201. #else
  202.   if (longlong->octets[7] & 0x80) {
  203. #endif
  204.     x = PyNumber_And(result, not_big);
  205.     y = PyNumber_Subtract(x, big);
  206.  
  207.     /* Clean up. */
  208.     Py_DECREF(x);
  209.     Py_DECREF(result);
  210.  
  211.     result = y;
  212.   }
  213.  
  214.   /* Clean up */
  215.   Py_DECREF(two);
  216.   Py_DECREF(sixty_three);
  217.   Py_DECREF(big);
  218.   Py_DECREF(not_big);
  219.  
  220.   return result;
  221. }
  222.  
  223. /***************************************************************/
  224. /* end of longlong.c */
  225.