Merge branch 'master' of https://gitlab.com/jdolan/netradiant
[xonotic/netradiant.git] / libs / bytestreamutils.h
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #if !defined( INCLUDED_BYTESTREAMUTILS_H )
23 #define INCLUDED_BYTESTREAMUTILS_H
24
25 #if defined( __GNUC__ )
26
27 #define _ISOC9X_SOURCE  1
28 #define _ISOC99_SOURCE  1
29
30 #define __USE_ISOC9X    1
31 #define __USE_ISOC99    1
32
33 #include <stdint.h>
34
35 #endif
36
37 #include <algorithm>
38
39 // if C99 is unavailable, fall back to the types most likely to be the right sizes
40 #if !defined( int16_t )
41 typedef signed short int16_t;
42 #endif
43 #if !defined( uint16_t )
44 typedef unsigned short uint16_t;
45 #endif
46 #if !defined( int32_t )
47 typedef signed int int32_t;
48 #endif
49 #if !defined( uint32_t )
50 typedef unsigned int uint32_t;
51 #endif
52
53
54
55
56 template<typename InputStreamType, typename Type>
57 inline void istream_read_little_endian( InputStreamType& istream, Type& value ){
58         istream.read( reinterpret_cast<typename InputStreamType::byte_type*>( &value ), sizeof( Type ) );
59 #if defined( __BIG_ENDIAN__ )
60         std::reverse( reinterpret_cast<typename InputStreamType::byte_type*>( &value ), reinterpret_cast<typename InputStreamType::byte_type*>( &value ) + sizeof( Type ) );
61 #endif
62 }
63
64 template<typename InputStreamType, typename Type>
65 inline void istream_read_big_endian( InputStreamType& istream, Type& value ){
66         istream.read( reinterpret_cast<typename InputStreamType::byte_type*>( &value ), sizeof( Type ) );
67 #if !defined( __BIG_ENDIAN__ )
68         std::reverse( reinterpret_cast<typename InputStreamType::byte_type*>( &value ), reinterpret_cast<typename InputStreamType::byte_type*>( &value ) + sizeof( Type ) );
69 #endif
70 }
71
72 template<typename InputStreamType>
73 inline void istream_read_byte( InputStreamType& istream, typename InputStreamType::byte_type& b ){
74         istream.read( &b, 1 );
75 }
76
77
78 template<typename InputStreamType>
79 inline int16_t istream_read_int16_le( InputStreamType& istream ){
80         int16_t value;
81         istream_read_little_endian( istream, value );
82         return value;
83 }
84
85 template<typename InputStreamType>
86 inline int16_t istream_read_int16_be( InputStreamType& istream ){
87         int16_t value;
88         istream_read_big_endian( istream, value );
89         return value;
90 }
91
92 template<typename InputStreamType>
93 inline uint16_t istream_read_uint16_le( InputStreamType& istream ){
94         uint16_t value;
95         istream_read_little_endian( istream, value );
96         return value;
97 }
98
99 template<typename InputStreamType>
100 inline uint16_t istream_read_uint16_be( InputStreamType& istream ){
101         uint16_t value;
102         istream_read_big_endian( istream, value );
103         return value;
104 }
105
106 template<typename InputStreamType>
107 inline int32_t istream_read_int32_le( InputStreamType& istream ){
108         int32_t value;
109         istream_read_little_endian( istream, value );
110         return value;
111 }
112
113 template<typename InputStreamType>
114 inline int32_t istream_read_int32_be( InputStreamType& istream ){
115         int32_t value;
116         istream_read_big_endian( istream, value );
117         return value;
118 }
119
120 template<typename InputStreamType>
121 inline uint32_t istream_read_uint32_le( InputStreamType& istream ){
122         uint32_t value;
123         istream_read_little_endian( istream, value );
124         return value;
125 }
126
127 template<typename InputStreamType>
128 inline uint32_t istream_read_uint32_be( InputStreamType& istream ){
129         uint32_t value;
130         istream_read_big_endian( istream, value );
131         return value;
132 }
133
134 template<typename InputStreamType>
135 inline float istream_read_float32_le( InputStreamType& istream ){
136         float value;
137         istream_read_little_endian( istream, value );
138         return value;
139 }
140
141 template<typename InputStreamType>
142 inline float istream_read_float32_be( InputStreamType& istream ){
143         float value;
144         istream_read_big_endian( istream, value );
145         return value;
146 }
147
148 template<typename InputStreamType>
149 inline typename InputStreamType::byte_type istream_read_byte( InputStreamType& istream ){
150         typename InputStreamType::byte_type b;
151         istream.read( &b, sizeof( typename InputStreamType::byte_type ) );
152         return b;
153 }
154
155 #endif