00001 /* endian - Manages the endian-ness of data. 00002 00003 PIRL CVS ID: $Id: endian.hh,v 1.18 2009/10/03 00:00:39 castalia Exp $ 00004 00005 Copyright (C) 2003, 2004, 2005 Arizona Board of Regents 00006 on behalf of the Planetary Image Research Laboratory, 00007 Lunar and Planetary Laboratory at the University of Arizona. 00008 00009 This library is free software; you can redistribute it and/or modify it 00010 under the terms of the GNU Lesser General Public License, version 2.1, 00011 as published by the Free Software Foundation. 00012 00013 This library is distributed in the hope that it will be useful, but 00014 WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 00016 General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public License 00019 along with this library; if not, write to the Free Software Foundation, 00020 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00021 */ 00022 #ifndef _endian_h 00023 #define _endian_h 00024 00025 namespace PIRL 00026 { 00037 bool host_is_high_endian (); 00038 00043 inline bool high_endian_host () {return host_is_high_endian ();} 00044 00049 inline bool big_endian_host () {return host_is_high_endian ();} 00050 00055 inline bool low_endian_host () {return ! host_is_high_endian ();} 00056 00061 inline bool little_endian_host () {return ! host_is_high_endian ();} 00062 00073 void reorder_bytes (unsigned char* data, const unsigned long amount); 00074 00087 void reorder_bytes (unsigned char* data, const unsigned long groups, 00088 const unsigned int size); 00089 00105 void 00106 swap_bytes 00107 ( 00108 unsigned char* data, 00109 const unsigned int groups, 00110 const unsigned int size 00111 ); 00112 00135 template<typename T> 00136 T& 00137 MSB_native 00138 ( 00139 T& value 00140 ) 00141 { 00142 if (! host_is_high_endian ()) 00143 reorder_bytes 00144 (reinterpret_cast<unsigned char*>(&value), sizeof (T)); 00145 return value; 00146 } 00148 #define MSB_value MSB_native 00149 00172 template<typename T> 00173 T& 00174 LSB_native 00175 ( 00176 T& value 00177 ) 00178 { 00179 if (host_is_high_endian ()) 00180 reorder_bytes 00181 (reinterpret_cast<unsigned char*>(&value), sizeof (T)); 00182 return value; 00183 } 00185 #define LSB_value LSB_native 00186 00187 00188 } // PIRL namespace 00189 #endif