aboutsummaryrefslogtreecommitdiff
path: root/sc4pd/headers/lang/ReadWriteMacros.h
blob: 2117fb6fe49f18e3fc7382c8024a3bba42325d74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
	SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


#ifndef _ReadWriteMacros_
#define _ReadWriteMacros_

#include "SC_Types.h"
#include "SC_Endian.h"
#include <stdio.h>
#include <string.h>

template <class T>
class SC_IOStream 
{
protected:
    T s;
public:
    SC_IOStream() : s(0) {}
    SC_IOStream(T inStream) : s(inStream) {}

    void SetStream(T inStream) { s = inStream; }
    T GetStream() { return s; }
    
    // core routines
    void readData(char *data, int size);
    uint8 readUInt8();

    void writeData(char *data, int size);
    void writeUInt8(uint8 inInt);
 
    // built using core routines
    void writeInt8(int8 inInt)
    {
        writeUInt8((uint8)inInt);
    }
    
    void writeInt16_be(int16 inInt)
    {
        writeUInt8((uint8)(inInt >> 8));
        writeUInt8(inInt);
    }
    
    void writeInt16_le(int16 inInt)
    {
        writeUInt8((uint8)inInt);
        writeUInt8((uint8)(inInt >> 8));
    }
    
    void writeInt32_be(int32 inInt)
    {
        writeUInt8((uint8)(inInt >> 24));
        writeUInt8((uint8)(inInt >> 16));
        writeUInt8((uint8)(inInt >> 8));
        writeUInt8((uint8)inInt);
    }
    
    void writeInt32_le(int32 inInt)
    {
        writeUInt8((uint8)inInt);
        writeUInt8((uint8)(inInt >> 8));
        writeUInt8((uint8)(inInt >> 16));
        writeUInt8((uint8)(inInt >> 24));
    }

#if BYTE_ORDER == BIG_ENDIAN
    void writeFloat_be(float inFloat)
#else
    void writeFloat_le(float inFloat)
#endif
    {
	union {
		float f;
		uint8 c[4];
	} u;
        u.f = inFloat;
        writeUInt8(u.c[0]);
        writeUInt8(u.c[1]);
        writeUInt8(u.c[2]);
        writeUInt8(u.c[3]);
    }

#if BYTE_ORDER == BIG_ENDIAN
    void writeFloat_le(float inFloat)
#else
    void writeFloat_be(float inFloat)
#endif
    {
	union {
		float f;
		uint8 c[4];
	} u;
        u.f = inFloat;
        writeUInt8(u.c[3]);
        writeUInt8(u.c[2]);
        writeUInt8(u.c[1]);
        writeUInt8(u.c[0]);
    }


#if BYTE_ORDER == BIG_ENDIAN
    void writeDouble_be(double inDouble)
#else
    void writeDouble_le(double inDouble)
#endif
    {
	union {
		double f;
		uint8 c[8];
	} u;
        u.f = inDouble;
        writeUInt8(u.c[0]);
        writeUInt8(u.c[1]);
        writeUInt8(u.c[2]);
        writeUInt8(u.c[3]);
        writeUInt8(u.c[4]);
        writeUInt8(u.c[5]);
        writeUInt8(u.c[6]);
        writeUInt8(u.c[7]);
    }

#if BYTE_ORDER == BIG_ENDIAN
    void writeDouble_le(double inDouble)
#else
    void writeDouble_be(double inDouble)
#endif
    {
	union {
		double f;
		uint8 c[8];
	} u;
        u.f = inDouble;
        writeUInt8(u.c[7]);
        writeUInt8(u.c[6]);
        writeUInt8(u.c[5]);
        writeUInt8(u.c[4]);
        writeUInt8(u.c[3]);
        writeUInt8(u.c[2]);
        writeUInt8(u.c[1]);
        writeUInt8(u.c[0]);
    }


    int8 readInt8()
    {
        return (int8)readUInt8();
    }
    
    int16 readInt16_be()
    {
        uint8 a = readUInt8();
        uint8 b = readUInt8();
        return (int16)((a << 8) | b);
    }
    
    int16 readInt16_le()
    {
        uint8 a = readUInt8();
        uint8 b = readUInt8();
        return (int16)((b << 8) | a);
    }
    
    int32 readInt32_be()
    {
        uint8 a = readUInt8();
        uint8 b = readUInt8();
        uint8 c = readUInt8();
        uint8 d = readUInt8();
        return (int32)((a << 24) | (b << 16) | (c << 8) | d);
    }
    
    int32 readInt32_le()
    {
        uint8 a = readUInt8();
        uint8 b = readUInt8();
        uint8 c = readUInt8();
        uint8 d = readUInt8();
        return (int32)((d << 24) | (c << 16) | (b << 8) | a);
    }

#if BYTE_ORDER == BIG_ENDIAN
    float readFloat_be()
#else
    float readFloat_le()
#endif
    {
	union {
		float f;
		uint8 c[4];
	} u;
        u.c[0] = readUInt8();
        u.c[1] = readUInt8();
        u.c[2] = readUInt8();
        u.c[3] = readUInt8();
        return u.f;
    }

#if BYTE_ORDER == BIG_ENDIAN
    float readFloat_le()
#else
    float readFloat_be()
#endif
    {
	union {
		float f;
		uint8 c[4];
	} u;
        u.c[3] = readUInt8();
        u.c[2] = readUInt8();
        u.c[1] = readUInt8();
        u.c[0] = readUInt8();
        return u.f;
    }


#if BYTE_ORDER == BIG_ENDIAN
    double readDouble_be()
#else
    double readDouble_le()
#endif
    {
	union {
		double f;
		uint8 c[8];
	} u;
        u.c[0] = readUInt8();
        u.c[1] = readUInt8();
        u.c[2] = readUInt8();
        u.c[3] = readUInt8();
        u.c[4] = readUInt8();
        u.c[5] = readUInt8();
        u.c[6] = readUInt8();
        u.c[7] = readUInt8();
        return u.f;
    }

#if BYTE_ORDER == BIG_ENDIAN
    double readDouble_le()
#else
    double readDouble_be()
#endif
    {
	union {
		double f;
		uint8 c[8];
	} u;
        u.c[7] = readUInt8();
        u.c[6] = readUInt8();
        u.c[5] = readUInt8();
        u.c[4] = readUInt8();
        u.c[3] = readUInt8();
        u.c[2] = readUInt8();
        u.c[1] = readUInt8();
        u.c[0] = readUInt8();
        return u.f;
    }
    
    void readSymbol(char *outString)
    {
            int length = readUInt8();
            readData(outString, length);
            outString[length] = 0;
    }

    void writeSymbol(char *inString)
    {
            int32 length = strlen(inString);
            writeUInt8((uint8)length);
            writeData(inString, length);
    }
};


// core routines
inline void SC_IOStream<FILE*>::readData(char *data, int size)
{
    fread(data, 1, size, s);
}

inline uint8 SC_IOStream<FILE*>::readUInt8()
{
    return (uint8)fgetc(s);
}

inline void SC_IOStream<FILE*>::writeData(char *data, int size)
{
    fwrite(data, 1, size, s);
}

inline void SC_IOStream<FILE*>::writeUInt8(uint8 inInt)
{
    fputc(inInt, s);
}

// core routines
inline void SC_IOStream<char*>::readData(char *data, int size)
{
    memcpy(data, s, size);
    s += size;
}
inline uint8 SC_IOStream<char*>::readUInt8()
{
    return (uint8)*s++;
}

inline void SC_IOStream<char*>::writeData(char *data, int size)
{
    memcpy(s, data, size);
        s += size;
}

inline void SC_IOStream<char*>::writeUInt8(uint8 inInt)
{
    *s++ = (inInt & 255);
}


#endif