Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

GenTypes.h

00001 #ifndef _LCORE_GenTypes_H_
00002 
00003 /*
00004  * Copyright (C) 1991-9 Eric M. Hopper <hopper@omnifarious.mn.org>
00005  * 
00006  *     This program is free software; you can redistribute it and/or modify it
00007  *     under the terms of the GNU Lesser General Public License as published
00008  *     by the Free Software Foundation; either version 2 of the License, or
00009  *     (at your option) any later version.
00010  * 
00011  *     This program is distributed in the hope that it will be useful, but
00012  *     WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *     Lesser General Public License for more details.
00015  * 
00016  *     You should have received a copy of the GNU Lesser General Public
00017  *     License along with this program; if not, write to the Free Software
00018  *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 /* $Header: /home/hopper/src/cvs/C++/LCore/LCore/GenTypes.h,v 1.8 2002/05/09 21:55:21 hopper Exp $ */
00022 
00023 // $Revision: 1.8 $
00024 
00025 // For a log, see ../ChangeLog
00026 //
00027 // Revision 1.4  1998/02/08 04:40:35  hopper
00028 // Untested efficiency improvements.
00029 //
00030 // Revision 1.3  1996/07/08 02:26:47  hopper
00031 // Added include of byteorder file on 386 architectures.
00032 // Added const in a lot of places that needed it.
00033 //
00034 // Revision 1.2  1996/03/26 16:22:59  hopper
00035 // Moved ReferenceCounting class over from Container library.
00036 // Fixed up class numbering system so LCore uses a seperate library number
00037 // from the container library.
00038 //
00039 // Revision 1.1.1.1  1995/07/22 04:09:24  hopper
00040 // Imported sources
00041 //
00042 // Revision 0.3  1994/10/30  04:40:26  hopper
00043 // Moved various things into the new LCore library.
00044 //
00045 // Revision 0.2  1994/10/05  06:00:57  hopper
00046 // Some changes to make it work better with gcc.
00047 //
00048 // Revision 0.1  1994/07/21  05:38:24  hopper
00049 // Genesis!
00050 //
00051 
00052 #ifdef __GNUG__
00053 #  pragma interface
00054 #endif
00055 
00056 #include <netinet/in.h>
00057 
00058 #define _LCORE_GenTypes_H_
00059 
00060 namespace strmod {
00061 namespace lcore {
00062 
00063 #ifdef BYTE_ORDER_DEFINED
00064 #   if !(defined(ORDER_UNIX) || defined(ORDER_XINU))
00065 #      error Byte order defined macro is defined, but no valid byte orderings \
00066              are defined.
00067 #   endif
00068 #else
00069 #   if defined(ORDER_UNIX) || defined(ORDER_XINU)
00070 #      error "Byte order defined macro isn't defined, but a valid order is."
00071 #   endif
00072 #endif
00073 
00074 #if defined(__MSDOS__) || defined(MSDOS)
00075    typedef unsigned long  U4Byte;
00076    typedef long           S4Byte;
00077    typedef unsigned int   U2Byte;
00078    typedef int            S2Byte;
00079    typedef unsigned char  U1Byte;
00080    typedef signed char    S1Byte;
00081    typedef float          IEEE_Float32;
00082    typedef double         IEEE_Float64;
00083    #if !defined(BYTE_ORDER_DEFINED)
00084       #define ORDER_XINU   // All 8x86 or 80x86 MSDOS machines are ORDER_XINU
00085       #define BYTE_ORDER_DEFINED
00086    #endif
00087 #else // UNIX is default   defined(UNIX)
00088 #ifdef __alpha
00089    typedef unsigned int   U4Byte;
00090 #else
00091    typedef unsigned long  U4Byte;
00092 #endif
00093 #ifdef __alpha
00094    typedef int            S4Byte;
00095 #else
00096    typedef long           S4Byte;
00097 #endif
00098    typedef unsigned short U2Byte;
00099    typedef signed short   S2Byte;
00100    typedef unsigned char  U1Byte;
00101    typedef signed char    S1Byte;
00102    typedef float          IEEE_Float32;
00103    typedef double         IEEE_Float64;
00104    #if !defined(BYTE_ORDER_DEFINED)
00105       #define ORDER_UNIX
00106       #define BYTE_ORDER_DEFINED
00107    #endif
00108 #endif
00109 
00110 
00111 #ifdef __cplusplus
00112 
00113 // These guys are out here for consistency. You never really have to mix
00114 // around a 1 byte value. (I hope)
00115 
00116 #ifndef BYTE_ORDER_DEFINED
00117 #   error No one has defined a byte ordering.
00118 #endif
00119 
00120    inline void hton(const U1Byte n, U1Byte *c)
00121    {
00122       c[0] = n;
00123    }
00124    inline void ntoh(const U1Byte *c, U1Byte &n)
00125    {
00126       n = c[0];
00127    }
00128 
00129    inline void hton(const S1Byte n, U1Byte *c)
00130    {
00131       *((S1Byte *)(c)) = n;
00132    }
00133    inline void ntoh(const U1Byte *c, S1Byte &n)
00134    {
00135       n = *((const S1Byte *)(c));
00136    }
00137 
00138    #ifdef ORDER_UNIX
00139       inline void hton(const U4Byte n, U1Byte *c)
00140       {
00141          *((U4Byte *)(c)) = n;
00142       }
00143       inline void ntoh(const U1Byte *c, U4Byte &n)
00144       {
00145          n = *((const U4Byte *)(c));
00146       }
00147 
00148       inline void hton(const S4Byte n, U1Byte *c)
00149       {
00150          *((S4Byte *)(c)) = n;
00151       }
00152       inline void ntoh(const U1Byte *c, S4Byte &n)
00153       {
00154          n = *((const S4Byte *)(c));
00155       }
00156 
00157 
00158       inline void hton(const U2Byte n, U1Byte *c)
00159       {
00160          *((U2Byte *)(c)) = n;
00161       }
00162       inline void ntoh(const U1Byte *c, U2Byte &n)
00163       {
00164          n = *((const U2Byte *)(c));
00165       }
00166 
00167       inline void hton(const S2Byte n, U1Byte *c)
00168       {
00169          *((S2Byte *)(c)) = n;
00170       }
00171       inline void ntoh(const U1Byte *c, S2Byte &n)
00172       {
00173          n = *((const S2Byte *)(c));
00174       }
00175 
00176 
00177       inline void hton(const IEEE_Float32 n, U1Byte *c)
00178       {
00179          *((IEEE_Float32 *)(c)) = n;
00180       }
00181       inline void hton(const U1Byte *c, IEEE_Float32 &n)
00182       {
00183          n = *((const IEEE_Float32 *)(c));
00184       }
00185 
00186       inline void hton(const IEEE_Float64 n, U1Byte *c)
00187       {
00188          *((IEEE_Float64 *)(c)) = n;
00189       }
00190       inline void hton(const U1Byte *c, IEEE_Float64 &n)
00191       {
00192          n = *((const IEEE_Float64 *)(c));
00193       }
00194 
00195    #elif defined(ORDER_XINU)
00196 
00197 #   ifdef __GNUG__
00198       inline void hton(const U4Byte n, U1Byte *c)
00199       {
00200          *((U4Byte *)(c)) = htonl(n);
00201       }
00202       inline void ntoh(const U1Byte *c, U4Byte &n)
00203       {
00204          n = ntohl(*((const U4Byte *)(c)));
00205       }
00206       inline void hton(const S4Byte n, U1Byte *c)
00207       {
00208          *((S4Byte *)(c)) = htonl(n);
00209       }
00210       inline void ntoh(const U1Byte *c, S4Byte &n)
00211       {
00212          n = ntohl(*((const S4Byte *)(c)));
00213       }
00214 
00215       inline void hton(const U2Byte n, U1Byte *c)
00216       {
00217          *((U2Byte *)(c)) = htons(n);
00218       }
00219       inline void ntoh(const U1Byte *c, U2Byte &n)
00220       {
00221          n = ntohs(*((const U2Byte *)(c)));
00222       }
00223       inline void hton(const S2Byte n, U1Byte *c)
00224       {
00225          *((S2Byte *)(c)) = htons(n);
00226       }
00227       inline void ntoh(const U1Byte *c, S2Byte &n)
00228       {
00229          n = ntohs(*((const S2Byte *)(c)));
00230       }
00231 #else
00232       inline void hton(const U4Byte n, U1Byte *c)
00233       {
00234          *c++ = U1Byte((n >> 24) & 0xffU);
00235          *c++ = U1Byte((n >> 16) & 0xffU);
00236          *c++ = U1Byte((n >> 8) & 0xffU);
00237          *c = U1Byte(n & 0xffU);
00238       }
00239       inline void ntoh(const U1Byte *c, U4Byte &n)
00240       {
00241          n = (U4Byte(c[0] & 0xffU) << 24) | (U4Byte(c[1] & 0xffU) << 16) |
00242              (U4Byte(c[2] & 0xffU) << 8)  |  U4Byte(c[3] & 0xffU);
00243       }
00244 
00245       inline void hton(const S4Byte n, U1Byte *c)
00246       {
00247          hton(U4Byte(n), c);  // Essentially the same operation...
00248       }
00249       inline void ntoh(const U1Byte *c, S4Byte &n)  {
00250          n = (U4Byte(c[0] & 0xffU) << 24) | (U4Byte(c[1] & 0xffU) << 16) |
00251              (U4Byte(c[2] & 0xffU) << 8)  |  U4Byte(c[3] & 0xffU);
00252       }
00253 
00254 
00255       inline void hton(const U2Byte n, U1Byte *c)
00256       {
00257          *c++ = U1Byte((n >> 8) & 0xffU);
00258          *c = (n & 0xffU);
00259       }
00260       inline void ntoh(const U1Byte *c, U2Byte &n)  {
00261          n = (U2Byte(c[0] & 0xffU) << 8) | U2Byte(c[1] & 0xffU);
00262       }
00263 
00264       inline void hton(const S2Byte n, U1Byte *c)
00265       {
00266          hton(U2Byte(n), c);
00267       }
00268       inline void ntoh(const U1Byte *c, S2Byte &n)
00269       {
00270          n = S2Byte((U2Byte(c[0] & 0xffU) << 8) | U2Byte(c[1] & 0xffU));
00271       }
00272 #endif
00273 
00274 
00275       inline void hton(const IEEE_Float32 &n, U1Byte *c)
00276       {
00277          const U1Byte * const from = ((const U1Byte *)(&n));
00278 
00279          c[3] = from[0];
00280          c[2] = from[1];
00281          c[1] = from[2];
00282          c[0] = from[3];
00283       }
00284       inline void ntoh(const U1Byte *c, IEEE_Float32 &n)
00285       {
00286          U1Byte * const to = ((U1Byte *)(&n));
00287 
00288          to[0] = c[3];
00289          to[1] = c[2];
00290          to[2] = c[1];
00291          to[3] = c[0];
00292       }
00293 
00294       inline void hton(const IEEE_Float64 &n, U1Byte *c)
00295       {
00296          const U1Byte * const from = ((const U1Byte *)(&n));
00297 
00298          c[7] = from[0];
00299          c[6] = from[1];
00300          c[5] = from[2];
00301          c[4] = from[3];
00302          c[3] = from[4];
00303          c[2] = from[5];
00304          c[1] = from[6];
00305          c[0] = from[7];
00306       }
00307       inline void ntoh(const U1Byte *c, IEEE_Float64 &n)
00308       {
00309          U1Byte * const to = ((U1Byte *)(&n));
00310 
00311          to[0] = c[7];
00312          to[1] = c[6];
00313          to[2] = c[5];
00314          to[3] = c[4];
00315          to[4] = c[3];
00316          to[5] = c[2];
00317          to[6] = c[1];
00318          to[7] = c[0];
00319       }
00320    #endif
00321 #endif
00322 
00323 } // namespace lcore
00324 } // namespace strmod
00325 
00326 #endif

Generated on Wed Jan 29 00:32:43 2003 for libNet by doxygen1.3-rc1