bytestreamutils.h

Go to the documentation of this file.
00001 /*
00002  Copyright (C) 2001-2006, William Joseph.
00003  All Rights Reserved.
00004 
00005  This file is part of GtkRadiant.
00006 
00007  GtkRadiant is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation; either version 2 of the License, or
00010  (at your option) any later version.
00011 
00012  GtkRadiant is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with GtkRadiant; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #if !defined(INCLUDED_BYTESTREAMUTILS_H)
00023 #define INCLUDED_BYTESTREAMUTILS_H
00024 
00025 #if defined(__GNUC__)
00026 
00027 #define _ISOC9X_SOURCE  1
00028 #define _ISOC99_SOURCE  1
00029 
00030 #define __USE_ISOC9X    1
00031 #define __USE_ISOC99    1
00032 
00033 #include <stdint.h>
00034 
00035 #endif
00036 
00037 #include <algorithm>
00038 
00039 // if C99 is unavailable, fall back to the types most likely to be the right sizes
00040 #if !defined(int16_t)
00041 typedef signed short int16_t;
00042 #endif
00043 #if !defined(uint16_t)
00044 typedef unsigned short uint16_t;
00045 #endif
00046 #if !defined(int32_t)
00047 typedef signed int int32_t;
00048 #endif
00049 #if !defined(uint32_t)
00050 typedef unsigned int uint32_t;
00051 #endif
00052 
00053 template<typename InputStreamType, typename Type>
00054 inline void istream_read_little_endian (InputStreamType& istream, Type& value)
00055 {
00056     istream.read(reinterpret_cast<typename InputStreamType::byte_type*> (&value), sizeof(Type));
00057 #if defined(__BIG_ENDIAN__)
00058     std::reverse(reinterpret_cast<typename InputStreamType::byte_type*>(&value), reinterpret_cast<typename InputStreamType::byte_type*>(&value) + sizeof(Type));
00059 #endif
00060 }
00061 
00062 template<typename InputStreamType, typename Type>
00063 inline void istream_read_big_endian (InputStreamType& istream, Type& value)
00064 {
00065     istream.read(reinterpret_cast<typename InputStreamType::byte_type*> (&value), sizeof(Type));
00066 #if !defined(__BIG_ENDIAN__)
00067     std::reverse(reinterpret_cast<typename InputStreamType::byte_type*> (&value),
00068             reinterpret_cast<typename InputStreamType::byte_type*> (&value) + sizeof(Type));
00069 #endif
00070 }
00071 
00072 template<typename InputStreamType>
00073 inline void istream_read_byte (InputStreamType& istream, typename InputStreamType::byte_type& b)
00074 {
00075     istream.read(&b, 1);
00076 }
00077 
00078 template<typename InputStreamType>
00079 inline int16_t istream_read_int16_le (InputStreamType& istream)
00080 {
00081     int16_t value;
00082     istream_read_little_endian(istream, value);
00083     return value;
00084 }
00085 
00086 template<typename InputStreamType>
00087 inline uint16_t istream_read_uint16_le (InputStreamType& istream)
00088 {
00089     uint16_t value;
00090     istream_read_little_endian(istream, value);
00091     return value;
00092 }
00093 
00094 template<typename InputStreamType>
00095 inline int32_t istream_read_int32_le (InputStreamType& istream)
00096 {
00097     int32_t value;
00098     istream_read_little_endian(istream, value);
00099     return value;
00100 }
00101 
00102 template<typename InputStreamType>
00103 inline uint32_t istream_read_uint32_le (InputStreamType& istream)
00104 {
00105     uint32_t value;
00106     istream_read_little_endian(istream, value);
00107     return value;
00108 }
00109 
00110 template<typename InputStreamType>
00111 inline float istream_read_float32_le (InputStreamType& istream)
00112 {
00113     float value;
00114     istream_read_little_endian(istream, value);
00115     return value;
00116 }
00117 
00118 template<typename InputStreamType>
00119 inline typename InputStreamType::byte_type istream_read_byte (InputStreamType& istream)
00120 {
00121     typename InputStreamType::byte_type b;
00122     istream.read(&b, sizeof(typename InputStreamType::byte_type));
00123     return b;
00124 }
00125 
00126 #endif

Generated by  doxygen 1.6.2