00001 00002 /****************** <SYN heading BEGIN do not edit this line> ***************** 00003 * 00004 * subsynth - modular audio synthesizer 00005 * subsynth is (C) Copyright 2001-2002 by Kevin Meinert 00006 * 00007 * Original Author: Kevin Meinert 00008 * 00009 * This library is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU Library General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2 of the License, or (at your option) any later version. 00013 * 00014 * This library is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * Library General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Library General Public 00020 * License along with this library; if not, write to the 00021 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00022 * Boston, MA 02111-1307, USA. 00023 * 00024 * ----------------------------------------------------------------- 00025 * File: $RCSfile: Endian.h,v $ 00026 * Date modified: $Date: 2002/01/04 16:20:58 $ 00027 * Version: $Revision: 1.3 $ 00028 * ----------------------------------------------------------------- 00029 * 00030 ****************** <SYN heading END do not edit this line> ******************/ 00031 00032 #ifndef AUDIOJUGGLER_ENDIAN_FUNCS 00033 #define AUDIOJUGGLER_ENDIAN_FUNCS 00034 00035 00036 namespace syn 00037 { 00038 //: Swap the bytes in any data type. 00039 // Motorola and Intel store their bytes in reversed formats <BR> 00040 // ex: An SGI image is native to the SGI system, <BR> 00041 // to be read on an intel machine, it's bytes need to be reversed <BR> 00042 // NOTE: chars aren't affected by this since it only <BR> 00043 // affects the order of bytes, not bits. 00044 template< class Type > 00045 inline void byteReverse(Type& bytes) 00046 { 00047 const int size = sizeof(Type); 00048 Type buf = 0; 00049 int x, y; 00050 00051 //we want to index the bytes in the buffer 00052 unsigned char* buffer = (unsigned char*) &buf; 00053 00054 for ( x = 0, y = size-1; 00055 x < size; 00056 ++x, --y ) 00057 { 00058 buffer[x] |= ((unsigned char*)&bytes)[y]; 00059 } 00060 bytes = buf; 00061 } 00062 00063 enum Endianness 00064 { 00065 BIG, LITTLE 00066 }; 00067 00068 template< class Type > 00069 inline void byteReverse( const syn::Endianness& e, Type& bytes ) 00070 { 00071 if (e == BIG && syn::isLittle()) 00072 byteReverse( bytes ); 00073 if (e == LITTLE && syn::isBig()) 00074 byteReverse( bytes ); 00075 } 00076 00077 //: check the system for endianess 00078 inline bool isLittle() 00079 { 00080 union 00081 { 00082 short val; 00083 char ch[sizeof(short)]; 00084 } un; 00085 00086 // initialize the memory that we'll probe 00087 un.val = 256; // 0x10 00088 00089 // If the 1 byte is in the low-order position (un.ch[1]), this is a 00090 // little-endian system. Otherwise, it is a big-endian system. 00091 return un.ch[1] == 1; 00092 } 00093 00094 //: check the system for endianess 00095 inline bool isBig() 00096 { 00097 return !syn::isLittle(); 00098 } 00099 00100 }; // end namespace. 00101 #endif