allow undo “make detail/structural”, <3 @SpiKe, thanks @Garux, fix #76
[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 #include "globaldefs.h"
26
27 #if GDEF_COMPILER_GNU
28
29 #define _ISOC9X_SOURCE  1
30 #define _ISOC99_SOURCE  1
31
32 #define __USE_ISOC9X    1
33 #define __USE_ISOC99    1
34
35 #include <stdint.h>
36
37 #endif
38
39 #include <algorithm>
40
41 // if C99 is unavailable, fall back to the types most likely to be the right sizes
42 #if !defined( int16_t )
43 typedef signed short int16_t;
44 #endif
45 #if !defined( uint16_t )
46 typedef unsigned short uint16_t;
47 #endif
48 #if !defined( int32_t )
49 typedef signed int int32_t;
50 #endif
51 #if !defined( uint32_t )
52 typedef unsigned int uint32_t;
53 #endif
54
55
56
57
58 template<typename InputStreamType, typename Type>
59 inline void istream_read_little_endian( InputStreamType& istream, Type& value ){
60         istream.read(reinterpret_cast<typename InputStreamType::byte_type *>( &value ), sizeof(Type));
61         if (GDEF_ARCH_ENDIAN_BIG) {
62                 std::reverse(reinterpret_cast<typename InputStreamType::byte_type *>( &value ),
63                                          reinterpret_cast<typename InputStreamType::byte_type *>( &value ) + sizeof(Type));
64         }
65 }
66
67 template<typename InputStreamType, typename Type>
68 inline void istream_read_big_endian( InputStreamType& istream, Type& value ){
69         istream.read(reinterpret_cast<typename InputStreamType::byte_type *>( &value ), sizeof(Type));
70         if (!GDEF_ARCH_ENDIAN_BIG) {
71                 std::reverse(reinterpret_cast<typename InputStreamType::byte_type *>( &value ),
72                                          reinterpret_cast<typename InputStreamType::byte_type *>( &value ) + sizeof(Type));
73         }
74 }
75
76 template<typename InputStreamType>
77 inline void istream_read_byte( InputStreamType& istream, typename InputStreamType::byte_type& b ){
78         istream.read( &b, 1 );
79 }
80
81
82 template<typename InputStreamType>
83 inline int16_t istream_read_int16_le( InputStreamType& istream ){
84         int16_t value;
85         istream_read_little_endian( istream, value );
86         return value;
87 }
88
89 template<typename InputStreamType>
90 inline int16_t istream_read_int16_be( InputStreamType& istream ){
91         int16_t value;
92         istream_read_big_endian( istream, value );
93         return value;
94 }
95
96 template<typename InputStreamType>
97 inline uint16_t istream_read_uint16_le( InputStreamType& istream ){
98         uint16_t value;
99         istream_read_little_endian( istream, value );
100         return value;
101 }
102
103 template<typename InputStreamType>
104 inline uint16_t istream_read_uint16_be( InputStreamType& istream ){
105         uint16_t value;
106         istream_read_big_endian( istream, value );
107         return value;
108 }
109
110 template<typename InputStreamType>
111 inline int32_t istream_read_int32_le( InputStreamType& istream ){
112         int32_t value;
113         istream_read_little_endian( istream, value );
114         return value;
115 }
116
117 template<typename InputStreamType>
118 inline int32_t istream_read_int32_be( InputStreamType& istream ){
119         int32_t value;
120         istream_read_big_endian( istream, value );
121         return value;
122 }
123
124 template<typename InputStreamType>
125 inline uint32_t istream_read_uint32_le( InputStreamType& istream ){
126         uint32_t value;
127         istream_read_little_endian( istream, value );
128         return value;
129 }
130
131 template<typename InputStreamType>
132 inline uint32_t istream_read_uint32_be( InputStreamType& istream ){
133         uint32_t value;
134         istream_read_big_endian( istream, value );
135         return value;
136 }
137
138 template<typename InputStreamType>
139 inline float istream_read_float32_le( InputStreamType& istream ){
140         float value;
141         istream_read_little_endian( istream, value );
142         return value;
143 }
144
145 template<typename InputStreamType>
146 inline float istream_read_float32_be( InputStreamType& istream ){
147         float value;
148         istream_read_big_endian( istream, value );
149         return value;
150 }
151
152 template<typename InputStreamType>
153 inline typename InputStreamType::byte_type istream_read_byte( InputStreamType& istream ){
154         typename InputStreamType::byte_type b;
155         istream.read( &b, sizeof( typename InputStreamType::byte_type ) );
156         return b;
157 }
158
159 #endif