GpCoreTools/ByteOrder.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of GpCoreTools.
00004 **
00005 **  This file may be distributed and/or modified under the terms of the
00006 **  GNU General Public License version 2 or 3 as published by the Free
00007 **  Software Foundation and appearing in the file LICENSE.GPL included
00008 **  in the packaging of this file.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
00013 **  more details.
00014 **
00015 **  You should have received a copy of the GNU General Public License
00016 **  along with this program. If not, see <http://www.gnu.org/licenses/>.
00017 **
00018 **  See http://www.geopsy.org for more information.
00019 **
00020 **  Created: 2011-05-30
00021 **  Authors:
00022 **    Marc Wathelet (ISTerre, Grenoble, France)
00023 **
00024 ***************************************************************************/
00025 
00026 #ifndef BYTEORDER_H
00027 #define BYTEORDER_H
00028 
00029 #include "GpCoreToolsDLLExport.h"
00030 
00031 //
00032 // Can be BIG_ENDIAN or LITTLE_ENDIAN, set with -D option in gcc command line
00033 //
00034 #ifndef BYTE_ORDER
00035 #  define BYTE_ORDER LITTLE_ENDIAN
00036 #endif
00037 
00038 namespace GpCoreTools {
00039 
00040   class GPCORETOOLS_EXPORT ByteOrder
00041   {
00042   public:
00043     static inline int bigEndianToNative(int v);
00044     static inline int littleEndianToNative(int v);
00045 
00046     static inline short bigEndianToNative(const unsigned short& v);
00047     static inline short littleEndianToNative(const unsigned short& v);
00048 
00049     static int swap(int v);
00050     static short swap(const unsigned short& v);
00051     static inline void swap(char& v1, char& v2);
00052     static inline void swap(unsigned char& v1, unsigned char& v2);
00053     static inline void swap(char * v);
00054     static inline void swap(unsigned char * v);
00055   };
00056 
00057   inline int ByteOrder::bigEndianToNative(int v)
00058   {
00059 #if BYTE_ORDER == LITTLE_ENDIAN
00060     return swap(v);
00061 #else
00062     return v;
00063 #endif
00064   }
00065 
00066   inline int ByteOrder::littleEndianToNative(int v)
00067   {
00068 #if BYTE_ORDER == BIG_ENDIAN
00069     return swap(v);
00070 #else
00071     return v;
00072 #endif
00073   }
00074 
00075   inline short ByteOrder::bigEndianToNative(const unsigned short& v)
00076   {
00077 #if BYTE_ORDER == LITTLE_ENDIAN
00078     return swap(v);
00079 #else
00080     return v;
00081 #endif
00082   }
00083 
00084   inline short ByteOrder::littleEndianToNative(const unsigned short& v)
00085   {
00086 #if BYTE_ORDER == BIG_ENDIAN
00087     return swap(v);
00088 #else
00089     return v;
00090 #endif
00091   }
00092 
00093   inline void ByteOrder::swap(char& v1, char& v2)
00094   {
00095     char tmp=v1;
00096     v1=v2;
00097     v2=tmp;
00098   }
00099 
00100   inline void ByteOrder::swap(unsigned char& v1, unsigned char& v2)
00101   {
00102     char tmp=v1;
00103     v1=v2;
00104     v2=tmp;
00105   }
00106 
00107   inline void ByteOrder::swap(char * v)
00108   {
00109     swap(v[0], v[3]);
00110     swap(v[1], v[2]);
00111   }
00112 
00113   inline void ByteOrder::swap(unsigned char * v)
00114   {
00115     swap(v[0], v[3]);
00116     swap(v[1], v[2]);
00117   }
00118 
00119 } // namespace GpCoreTools
00120 
00121 #endif // BYTEORDER_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines