]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/bytestreamutils.h
refactored plugin api; refactored callback library; added signals library
[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 <algorithm>
26
27 #if defined(_MSC_VER)
28
29 typedef signed short int16_t;
30 typedef unsigned short uint16_t;
31 typedef signed int int32_t;
32 typedef unsigned int uint32_t;
33
34 #else
35
36 #define _ISOC9X_SOURCE  1
37 #define _ISOC99_SOURCE  1
38
39 #define __USE_ISOC9X    1
40 #define __USE_ISOC99    1
41
42 #include <stdint.h>
43
44 #endif
45
46
47 template<typename InputStreamType, typename Type>
48 inline void istream_read_little_endian(InputStreamType& istream, Type& value)
49 {
50   istream.read(reinterpret_cast<typename InputStreamType::byte_type*>(&value), sizeof(Type));
51 #if defined(__BIG_ENDIAN__)
52   std::reverse(reinterpret_cast<typename InputStreamType::byte_type*>(&value), reinterpret_cast<typename InputStreamType::byte_type*>(&value) + sizeof(Type));
53 #endif
54 }
55
56 template<typename InputStreamType, typename Type>
57 inline void istream_read_big_endian(InputStreamType& istream, Type& value)
58 {
59   istream.read(reinterpret_cast<typename InputStreamType::byte_type*>(&value), sizeof(Type));
60 #if !defined(__BIG_ENDIAN__)
61   std::reverse(reinterpret_cast<typename InputStreamType::byte_type*>(&value), reinterpret_cast<typename InputStreamType::byte_type*>(&value) + sizeof(Type));
62 #endif
63 }
64
65 template<typename InputStreamType>
66 inline void istream_read_byte(InputStreamType& istream, typename InputStreamType::byte_type& b)
67 {
68   istream.read(&b, 1);
69 }
70
71
72 template<typename InputStreamType>
73 inline int16_t istream_read_int16_le(InputStreamType& istream)
74 {
75   int16_t value;
76   istream_read_little_endian(istream, value);
77   return value;
78 }
79
80 template<typename InputStreamType>
81 inline uint16_t istream_read_uint16_le(InputStreamType& istream)
82 {
83   uint16_t value;
84   istream_read_little_endian(istream, value);
85   return value;
86 }
87
88 template<typename InputStreamType>
89 inline int32_t istream_read_int32_le(InputStreamType& istream)
90 {
91   int32_t value;
92   istream_read_little_endian(istream, value);
93   return value;
94 }
95
96 template<typename InputStreamType>
97 inline uint32_t istream_read_uint32_le(InputStreamType& istream)
98 {
99   uint32_t value;
100   istream_read_little_endian(istream, value);
101   return value;
102 }
103
104 template<typename InputStreamType>
105 inline float istream_read_float32_le(InputStreamType& istream)
106 {
107   float value;
108   istream_read_little_endian(istream, value);
109   return value;
110 }
111
112 template<typename InputStreamType>
113 inline typename InputStreamType::byte_type istream_read_byte(InputStreamType& istream)
114 {
115   typename InputStreamType::byte_type b;
116   istream.read(&b, sizeof(typename InputStreamType::byte_type));
117   return b;
118 }
119
120 #endif