Explicitly include a boost "windows" folder even on linux
[supercollider.git] / include / lang / ReadWriteMacros.h
blobba502f2772884afccba8ac92fcd0569a7ec39fa3
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef _ReadWriteMacros_
23 #define _ReadWriteMacros_
25 #include "SC_Types.h"
26 #include "SC_Endian.h"
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdexcept>
31 template <class T>
32 class SC_IOStream
34 protected:
35 T s;
36 public:
37 SC_IOStream() : s(0) {}
38 SC_IOStream(T inStream) : s(inStream) {}
40 void SetStream(T inStream) { s = inStream; }
41 T GetStream() { return s; }
43 // core routines
44 void readData(char *data, int size);
45 uint8 readUInt8();
47 void writeData(char *data, int size);
48 void writeUInt8(uint8 inInt);
50 // built using core routines
51 void writeInt8(int8 inInt)
53 writeUInt8((uint8)inInt);
56 void writeInt16_be(int16 inInt)
58 writeUInt8((uint8)(inInt >> 8));
59 writeUInt8(inInt);
62 void writeInt16_le(int16 inInt)
64 writeUInt8((uint8)inInt);
65 writeUInt8((uint8)(inInt >> 8));
68 void writeInt32_be(int32 inInt)
70 writeUInt8((uint8)(inInt >> 24));
71 writeUInt8((uint8)(inInt >> 16));
72 writeUInt8((uint8)(inInt >> 8));
73 writeUInt8((uint8)inInt);
76 void writeInt32_le(int32 inInt)
78 writeUInt8((uint8)inInt);
79 writeUInt8((uint8)(inInt >> 8));
80 writeUInt8((uint8)(inInt >> 16));
81 writeUInt8((uint8)(inInt >> 24));
84 #if BYTE_ORDER == BIG_ENDIAN
85 void writeFloat_be(float inFloat)
86 #else
87 void writeFloat_le(float inFloat)
88 #endif
90 union {
91 float f;
92 uint8 c[4];
93 } u;
94 u.f = inFloat;
95 writeUInt8(u.c[0]);
96 writeUInt8(u.c[1]);
97 writeUInt8(u.c[2]);
98 writeUInt8(u.c[3]);
101 #if BYTE_ORDER == BIG_ENDIAN
102 void writeFloat_le(float inFloat)
103 #else
104 void writeFloat_be(float inFloat)
105 #endif
107 union {
108 float f;
109 uint8 c[4];
110 } u;
111 u.f = inFloat;
112 writeUInt8(u.c[3]);
113 writeUInt8(u.c[2]);
114 writeUInt8(u.c[1]);
115 writeUInt8(u.c[0]);
119 #if BYTE_ORDER == BIG_ENDIAN
120 void writeDouble_be(double inDouble)
121 #else
122 void writeDouble_le(double inDouble)
123 #endif
125 union {
126 double f;
127 uint8 c[8];
128 } u;
129 u.f = inDouble;
130 writeUInt8(u.c[0]);
131 writeUInt8(u.c[1]);
132 writeUInt8(u.c[2]);
133 writeUInt8(u.c[3]);
134 writeUInt8(u.c[4]);
135 writeUInt8(u.c[5]);
136 writeUInt8(u.c[6]);
137 writeUInt8(u.c[7]);
140 #if BYTE_ORDER == BIG_ENDIAN
141 void writeDouble_le(double inDouble)
142 #else
143 void writeDouble_be(double inDouble)
144 #endif
146 union {
147 double f;
148 uint8 c[8];
149 } u;
150 u.f = inDouble;
151 writeUInt8(u.c[7]);
152 writeUInt8(u.c[6]);
153 writeUInt8(u.c[5]);
154 writeUInt8(u.c[4]);
155 writeUInt8(u.c[3]);
156 writeUInt8(u.c[2]);
157 writeUInt8(u.c[1]);
158 writeUInt8(u.c[0]);
162 int8 readInt8()
164 return (int8)readUInt8();
167 int16 readInt16_be()
169 uint8 a = readUInt8();
170 uint8 b = readUInt8();
171 return (int16)((a << 8) | b);
174 int16 readInt16_le()
176 uint8 a = readUInt8();
177 uint8 b = readUInt8();
178 return (int16)((b << 8) | a);
181 int32 readInt32_be()
183 uint8 a = readUInt8();
184 uint8 b = readUInt8();
185 uint8 c = readUInt8();
186 uint8 d = readUInt8();
187 return (int32)((a << 24) | (b << 16) | (c << 8) | d);
190 int32 readInt32_le()
192 uint8 a = readUInt8();
193 uint8 b = readUInt8();
194 uint8 c = readUInt8();
195 uint8 d = readUInt8();
196 return (int32)((d << 24) | (c << 16) | (b << 8) | a);
199 #if BYTE_ORDER == BIG_ENDIAN
200 float readFloat_be()
201 #else
202 float readFloat_le()
203 #endif
205 union {
206 float f;
207 uint8 c[4];
208 } u;
209 u.c[0] = readUInt8();
210 u.c[1] = readUInt8();
211 u.c[2] = readUInt8();
212 u.c[3] = readUInt8();
213 return u.f;
216 #if BYTE_ORDER == BIG_ENDIAN
217 float readFloat_le()
218 #else
219 float readFloat_be()
220 #endif
222 union {
223 float f;
224 uint8 c[4];
225 } u;
226 u.c[3] = readUInt8();
227 u.c[2] = readUInt8();
228 u.c[1] = readUInt8();
229 u.c[0] = readUInt8();
230 return u.f;
234 #if BYTE_ORDER == BIG_ENDIAN
235 double readDouble_be()
236 #else
237 double readDouble_le()
238 #endif
240 union {
241 double f;
242 uint8 c[8];
243 } u;
244 u.c[0] = readUInt8();
245 u.c[1] = readUInt8();
246 u.c[2] = readUInt8();
247 u.c[3] = readUInt8();
248 u.c[4] = readUInt8();
249 u.c[5] = readUInt8();
250 u.c[6] = readUInt8();
251 u.c[7] = readUInt8();
252 return u.f;
255 #if BYTE_ORDER == BIG_ENDIAN
256 double readDouble_le()
257 #else
258 double readDouble_be()
259 #endif
261 union {
262 double f;
263 uint8 c[8];
264 } u;
265 u.c[7] = readUInt8();
266 u.c[6] = readUInt8();
267 u.c[5] = readUInt8();
268 u.c[4] = readUInt8();
269 u.c[3] = readUInt8();
270 u.c[2] = readUInt8();
271 u.c[1] = readUInt8();
272 u.c[0] = readUInt8();
273 return u.f;
276 void readSymbol(char *outString)
278 int length = readUInt8();
279 readData(outString, length);
280 outString[length] = 0;
283 void writeSymbol(char *inString)
285 int32 length = strlen(inString);
286 writeUInt8((uint8)length);
287 writeData(inString, length);
292 // core routines
293 template <>
294 inline void SC_IOStream<FILE*>::readData(char *data, int size)
296 size_t read = fread(data, 1, size, s);
297 if (read != size)
298 throw std::runtime_error("SC_IOStream<FILE*>::readData: read != size");
301 template <>
302 inline uint8 SC_IOStream<FILE*>::readUInt8()
304 return (uint8)fgetc(s);
307 template <>
308 inline void SC_IOStream<FILE*>::writeData(char *data, int size)
310 fwrite(data, 1, size, s);
313 template <>
314 inline void SC_IOStream<FILE*>::writeUInt8(uint8 inInt)
316 fputc(inInt, s);
319 // core routines
320 template <>
321 inline void SC_IOStream<char*>::readData(char *data, int size)
323 memcpy(data, s, size);
324 s += size;
327 template <>
328 inline uint8 SC_IOStream<char*>::readUInt8()
330 return (uint8)*s++;
333 template <>
334 inline void SC_IOStream<char*>::writeData(char *data, int size)
336 memcpy(s, data, size);
337 s += size;
340 template <>
341 inline void SC_IOStream<char*>::writeUInt8(uint8 inInt)
343 *s++ = (inInt & 255);
347 #endif