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

myendian.h

Go to the documentation of this file.
00001 /****************************************************************************/
00002 #ifndef _MYENDIAN_H_
00003 #define _MYENDIAN_H_
00004 /****************************************************************************/
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <string.h>
00008 #include <iostream.h>
00009 #include <netinet/in.h>
00010 /****************************************************************************/
00011 static inline int isBigEndian(void)
00012 {
00013 short v = 0x4142;      // "AB"
00014 char *leftmostbyte = (char *) &v;
00015 
00016 if (*leftmostbyte == 0x41)
00017         return 1;    // Big-Endian
00018 if (*leftmostbyte == 0x42)
00019         return 0;    // Little-Endian
00020          
00021 // Should never reach here!
00022 cout << "Error!! Neither Big Endian nor Little Endian!!" << endl << flush;
00023 exit(-1);
00024 }
00025 /****************************************************************************/
00026 //static int IsBigEndian = isBigEndian();  // Optimized for Big-Endian Arch
00027 static int IsBigEndian = !isBigEndian();  // Optimized for Little-Endian Arch
00028 /****************************************************************************/
00029 template <class Etype>
00030 static inline int swapbytes(Etype *in)
00031 {
00032 char *p_in = (char *) in;
00033 static int size = sizeof(Etype);
00034 unsigned char temp;
00035 
00036 #define SWAP(a, b) {temp = a; a = b; b = temp;}
00037 for (int i = 0; i < size/2; i++) SWAP(p_in[i], p_in[size-1-i])
00038 #undef SWAP
00039 
00040 return 0;
00041 }
00042 /****************************************************************************/
00043 template <class Etype>
00044 static inline Etype swapbytes(Etype in)
00045 {
00046 Etype out;
00047 char *p_in = (char *) &in;
00048 char *p_out = (char *) &out;
00049 static int size = sizeof(Etype);
00050 
00051 for (int i = 0; i < size; i++)
00052         p_out[i] = p_in[size-1-i];
00053 
00054 return out;
00055 }
00056 /****************************************************************************/
00057 static inline int swapbytes(unsigned char *data, int numbytes)
00058 {
00059 unsigned char temp;
00060 
00061 #define SWAP(a, b) {temp = a; a = b; b = temp;}
00062 for (int i = 0; i < numbytes/2; i++) SWAP(data[i], data[numbytes-1-i])
00063 #undef SWAP
00064 
00065 return 0;
00066 }
00067 /****************************************************************************/
00068 static inline int setByteOrder(unsigned char *data, int numbytes, int elemsize)
00069 {
00070 if (elemsize == 1 || IsBigEndian) return 0;
00071 
00072 for (int i = 0; i < numbytes; i += elemsize)
00073         swapbytes(&data[i], elemsize);
00074         
00075 return 0;
00076 }
00077 /****************************************************************************/
00078 template <class Etype>
00079 static inline Etype setByteOrder(Etype in) // Convert to Big Endian
00080 {
00081 if (IsBigEndian) return in;
00082 
00083 return swapbytes(in);
00084 }
00085 //**************************************************************************
00086 template <class Etype>
00087 static inline void setByteOrder(Etype *in) // Convert to Big Endian
00088 {
00089 if (IsBigEndian) return;
00090 
00091 *in = swapbytes(*in);
00092 }
00093 //**************************************************************************
00094 template <class Etype>
00095 static inline void setByteOrder(Etype *in1D, int n1, Etype *out1D)
00096 {
00097 if (IsBigEndian)
00098         {
00099          for (int i = 0; i < n1; i++)
00100                 out1D[i] = in1D[i];
00101          return;
00102         }
00103 
00104 for (int i = 0; i < n1; i++)
00105         out1D[i] = swapbytes(in1D[i]);
00106 }
00107 //**************************************************************************
00108 template <class Etype>
00109 static inline void setByteOrder(Etype *in2D, int n1, int n2, Etype *out2D)
00110 {
00111 if (IsBigEndian)
00112         {
00113          for (int i = 0; i < n1; i++)
00114          for (int j = 0; j < n2; j++)
00115                 out2D[i][j] = in2D[i][j];
00116          return;
00117         }
00118 
00119 for (int i = 0; i < n1; i++)
00120 for (int j = 0; j < n2; j++)
00121         out2D[i][j] = swapbytes(in2D[i][j]);
00122 }
00123 //**************************************************************************
00124 template <class Etype>
00125 static inline void setByteOrder(Etype *in3D, int n1, int n2, int n3, Etype *out3D)
00126 {
00127 if (IsBigEndian)
00128         {
00129          for (int i = 0; i < n1; i++)
00130          for (int j = 0; j < n2; j++)
00131          for (int k = 0; k < n3; k++)
00132                 out3D[i][j][k] = in3D[i][j][k];
00133          return;
00134         }
00135 
00136 for (int i = 0; i < n1; i++)
00137 for (int j = 0; j < n2; j++)
00138 for (int k = 0; k < n3; k++)
00139         out3D[i][j][k] = swapbytes(in3D[i][j][k]);
00140 }
00141 //**************************************************************************
00142 template <class Etype>
00143 static inline void setByteOrder(Etype *in1D, int n1)
00144 {
00145 if (IsBigEndian) return;
00146 
00147 for (int i = 0; i < n1; i++)
00148         in1D[i] = swapbytes(in1D[i]);
00149 }
00150 //**************************************************************************
00151 template <class Etype>
00152 static inline void setByteOrder(Etype *in2D, int n1, int n2)
00153 {
00154 if (IsBigEndian) return;
00155 
00156 for (int i = 0; i < n1; i++)
00157 for (int j = 0; j < n2; j++)
00158         in2D[i][j] = swapbytes(in2D[i][j]);
00159 }
00160 //**************************************************************************
00161 template <class Etype>
00162 static inline void setByteOrder(Etype *in3D, int n1, int n2, int n3)
00163 {
00164 if (IsBigEndian) return;
00165 
00166 for (int i = 0; i < n1; i++)
00167 for (int j = 0; j < n2; j++)
00168 for (int k = 0; k < n3; k++)
00169         in3D[i][j][k] = swapbytes(in3D[i][j][k]);
00170 }
00171 //**************************************************************************
00172 template <class Etype>
00173 static inline void PACK_VARIABLE(Etype v, unsigned char *data, int *ptr)
00174 {
00175 static int size = sizeof(Etype);
00176 Etype temp = setByteOrder(v);
00177 memcpy(&data[*ptr], &temp, size);
00178 *ptr += size;
00179 }
00180 //**************************************************************************
00181 template <class Etype>
00182 static inline void UNPACK_VARIABLE(Etype *v, unsigned char *data, int *ptr)
00183 {
00184 static int size = sizeof(Etype);
00185 memcpy(v, &data[*ptr], size);
00186 setByteOrder(v);
00187 *ptr += size;
00188 }
00189 //**************************************************************************
00190 static inline void packArray(unsigned char *data, int size, int elemsize, unsigned char *newdata)
00191 {
00192 memcpy(newdata, data, size);
00193 setByteOrder(newdata, size, elemsize);
00194 }
00195 //**************************************************************************
00196 #define PACK_GENERIC_ARRAY(x, data, ptr, element) {\
00197         int _tempsize = sizeof(x); \
00198         packArray((unsigned char *) x, _tempsize, sizeof(x element), &data[*ptr]); \
00199         *(ptr) += _tempsize;}
00200 
00201 #define UNPACK_GENERIC_ARRAY(x, data, ptr, element) {\
00202         int _tempsize = sizeof(x); \
00203         packArray(&data[*ptr], _tempsize, sizeof(x element), (unsigned char *) x); \
00204         *(ptr) += _tempsize;}
00205 
00206 #define PACK_DYNAMIC_ARRAY(x, num_elem, data, ptr, element) {\
00207         int _tempsize = sizeof(x element); \
00208         packArray((unsigned char *) x, _tempsize*(num_elem), _tempsize, &data[*ptr]); \
00209         *(ptr) += _tempsize*(num_elem);}
00210 
00211 #define UNPACK_DYNAMIC_ARRAY(x, num_elem, data, ptr, element) {\
00212         int _tempsize = sizeof(x element); \
00213         packArray(&data[*ptr], _tempsize*(num_elem), _tempsize, (unsigned char *) x); \
00214         *(ptr) += _tempsize*(num_elem);}
00215 
00216 #define PACK_1D_ARRAY(x,data,ptr) PACK_GENERIC_ARRAY(x,data,ptr,[0])
00217 #define PACK_2D_ARRAY(x,data,ptr) PACK_GENERIC_ARRAY(x,data,ptr,[0][0])
00218 #define PACK_3D_ARRAY(x,data,ptr) PACK_GENERIC_ARRAY(x,data,ptr,[0][0][0])
00219 #define PACK_4D_ARRAY(x,data,ptr) PACK_GENERIC_ARRAY(x,data,ptr,[0][0][0][0])
00220 
00221 #define UNPACK_1D_ARRAY(x,data,ptr) UNPACK_GENERIC_ARRAY(x,data,ptr,[0])
00222 #define UNPACK_2D_ARRAY(x,data,ptr) UNPACK_GENERIC_ARRAY(x,data,ptr,[0][0])
00223 #define UNPACK_3D_ARRAY(x,data,ptr) UNPACK_GENERIC_ARRAY(x,data,ptr,[0][0][0])
00224 #define UNPACK_4D_ARRAY(x,data,ptr) UNPACK_GENERIC_ARRAY(x,data,ptr,[0][0][0][0])
00225 
00226 #define PACK_DYNAMIC_1D_ARRAY(x,n,data,ptr) PACK_DYNAMIC_ARRAY(x,n,data,ptr,[0])
00227 #define PACK_DYNAMIC_2D_ARRAY(x,n,data,ptr) PACK_DYNAMIC_ARRAY(x,n,data,ptr,[0][0])
00228 #define PACK_DYNAMIC_3D_ARRAY(x,n,data,ptr) PACK_DYNAMIC_ARRAY(x,n,data,ptr,[0][0][0])
00229 #define PACK_DYNAMIC_4D_ARRAY(x,n,data,ptr) PACK_DYNAMIC_ARRAY(x,n,data,ptr,[0][0][0][0])
00230 
00231 #define UNPACK_DYNAMIC_1D_ARRAY(x,n,data,ptr) UNPACK_DYNAMIC_ARRAY(x,n,data,ptr,[0])
00232 #define UNPACK_DYNAMIC_2D_ARRAY(x,n,data,ptr) UNPACK_DYNAMIC_ARRAY(x,n,data,ptr,[0][0])
00233 #define UNPACK_DYNAMIC_3D_ARRAY(x,n,data,ptr) UNPACK_DYNAMIC_ARRAY(x,n,data,ptr,[0][0][0])
00234 #define UNPACK_DYNAMIC_4D_ARRAY(x,n,data,ptr) UNPACK_DYNAMIC_ARRAY(x,n,data,ptr,[0][0][0][0])
00235 
00236 #define PACK_STRUCT(x, data, ptr) {\
00237         unsigned char *newdata; \
00238         int _tempsize = numbytes; \
00239         packStruct(&x, &newdata, _tempsize); \
00240         memcpy(&data[*(ptr)], newdata, _tempsize); \
00241         FREE1D(newdata, _tempsize); \
00242         *(ptr) += _tempsize;}
00243 
00244 #define PACK_DATA(startbyte, numbytes, data, ptr) {\
00245         int _tempsize = numbytes; \
00246         memcpy(&data[*(ptr)], startbyte, _tempsize); \
00247         *(ptr) += _tempsize;}
00248 
00249 #define UNPACK_STRUCT(x, data, ptr) {\
00250         int _tempsize = unpackStruct(x, &data[*(ptr)], _tempsize); \
00251         *(ptr) += _tempsize;}
00252 
00253 #define UNPACK_DATA(startbyte, numbytes, data, ptr) {\
00254         int _tempsize = numbytes; \
00255         memcpy(startbyte, &data[*(ptr)], _tempsize); \
00256         *(ptr) += _tempsize;}
00257 
00258 //**************************************************************************
00259 #endif  // _MYENDIAN_H_
00260 //**************************************************************************

Generated on Sun Jun 16 17:36:42 2002 for Anirudh's Vortex-Wake Simulation Code by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001