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_
26 #include "SC_Endian.h"
37 SC_IOStream() : s(0) {}
38 SC_IOStream(T inStream
) : s(inStream
) {}
40 void SetStream(T inStream
) { s
= inStream
; }
41 T
GetStream() { return s
; }
44 void readData(char *data
, size_t size
);
47 void writeData(char *data
, size_t 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));
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
)
87 void writeFloat_le(float inFloat
)
101 #if BYTE_ORDER == BIG_ENDIAN
102 void writeFloat_le(float inFloat
)
104 void writeFloat_be(float inFloat
)
119 #if BYTE_ORDER == BIG_ENDIAN
120 void writeDouble_be(double inDouble
)
122 void writeDouble_le(double inDouble
)
140 #if BYTE_ORDER == BIG_ENDIAN
141 void writeDouble_le(double inDouble
)
143 void writeDouble_be(double inDouble
)
164 return (int8
)readUInt8();
169 uint8 a
= readUInt8();
170 uint8 b
= readUInt8();
171 return (int16
)((a
<< 8) | b
);
176 uint8 a
= readUInt8();
177 uint8 b
= readUInt8();
178 return (int16
)((b
<< 8) | a
);
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
);
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
209 u
.c
[0] = readUInt8();
210 u
.c
[1] = readUInt8();
211 u
.c
[2] = readUInt8();
212 u
.c
[3] = readUInt8();
216 #if BYTE_ORDER == BIG_ENDIAN
226 u
.c
[3] = readUInt8();
227 u
.c
[2] = readUInt8();
228 u
.c
[1] = readUInt8();
229 u
.c
[0] = readUInt8();
234 #if BYTE_ORDER == BIG_ENDIAN
235 double readDouble_be()
237 double readDouble_le()
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();
255 #if BYTE_ORDER == BIG_ENDIAN
256 double readDouble_le()
258 double readDouble_be()
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();
276 void readSymbol(char *outString
)
278 int length
= readUInt8();
279 readData(outString
, length
);
280 outString
[length
] = 0;
283 void writeSymbol(char *inString
)
285 size_t length
= strlen(inString
);
286 writeUInt8((uint8
)length
);
287 writeData(inString
, length
);
294 inline void SC_IOStream
<FILE*>::readData(char *data
, size_t size
)
296 size_t read
= fread(data
, 1, size
, s
);
298 throw std::runtime_error("SC_IOStream<FILE*>::readData: read != size");
302 inline uint8 SC_IOStream
<FILE*>::readUInt8()
304 return (uint8
)fgetc(s
);
308 inline void SC_IOStream
<FILE*>::writeData(char *data
, size_t size
)
310 fwrite(data
, 1, size
, s
);
314 inline void SC_IOStream
<FILE*>::writeUInt8(uint8 inInt
)
321 inline void SC_IOStream
<char*>::readData(char *data
, size_t size
)
323 memcpy(data
, s
, size
);
328 inline uint8 SC_IOStream
<char*>::readUInt8()
334 inline void SC_IOStream
<char*>::writeData(char *data
, size_t size
)
336 memcpy(s
, data
, size
);
341 inline void SC_IOStream
<char*>::writeUInt8(uint8 inInt
)
343 *s
++ = (inInt
& 255);