Subversion Repositories filter_foundry

Rev

Rev 193 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
259 daniel-mar 1
/*
2
        This file is part of a common library
3
    Copyright (C) 2002-2011 Toby Thain, toby@telegraphics.com.au
4
 
5
    This program is free software; you can redistribute it and/or modify
6
    it under the terms of the GNU General Public License as published by  
7
    the Free Software Foundation; either version 2 of the License, or
8
    (at your option) any later version.
9
 
10
    This program is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU General Public License for more details.
14
 
15
    You should have received a copy of the GNU General Public License  
16
    along with this program; if not, write to the Free Software
17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
*/
19
 
20
/* BigEndian and LittleEndian file I/O for long words, short words, and bytes */
21
 
22
#include "file_io.h"
23
 
24
int platform_is_LittleEndian(){
25
        union{ int a; char b; } u;
26
        u.a = 1;
27
        return u.b;
28
}
29
 
30
OSErr read4B(FILEREF f, int32_t *v){
31
        unsigned char p[4];
32
        FILECOUNT count = 4;
33
        OSErr e = FSREAD(f,&count,p);
34
        if( !e )
35
                *v = ((long)p[0]<<24) | ((long)p[1]<<16) | ((long)p[2]<<8) | p[3];
36
        return e;
37
}
38
 
39
OSErr read2B(FILEREF f, int16_t *v){
40
        unsigned char p[2];
41
        FILECOUNT count = 2;
42
        OSErr e = FSREAD(f,&count,p);
43
        if( !e )
44
                *v = (p[0]<<8) | p[1];
45
        return e;
46
}
47
 
48
OSErr read4L(FILEREF f, int32_t *v){
49
        unsigned char p[4];
50
        FILECOUNT count = 4;
51
        OSErr e = FSREAD(f,&count,p);
52
        if( !e )
53
                *v = ((long)p[3]<<24) | ((long)p[2]<<16) | ((long)p[1]<<8) | p[0];
54
        return e;
55
}
56
 
57
OSErr read2L(FILEREF f, int16_t *v){
58
        unsigned char p[2];
59
        FILECOUNT count = 2;
60
        OSErr e = FSREAD(f,&count,p);
61
        if( !e )
62
                *v = (p[1]<<8) | p[0];
63
        return e;
64
}
65
 
66
OSErr read1(FILEREF f,unsigned char *v){
67
        FILECOUNT count = 1;
68
        return FSREAD(f,&count,v);
69
}
70
 
71
/*
72
        readdoubleL() - read a LittleEndian (e.g. Intel) 8-byte double floating point value from a file.
73
 
74
        This will only work correctly if the compiler's "double" type is IEEE 8-byte format!
75
        In particular, in Metrowerks 68K C this means setting the "8-byte doubles" option
76
        and linking with the appropriate library (e.g. "MathLib68K (4i_8d).A4.Lib")
77
*/
78
 
79
OSErr readdoubleL(FILEREF f, double *v){
80
        OSErr e;
81
        union {
82
                int32_t i[2];
83
                double d;
84
        } u;
85
        int swap = !platform_is_LittleEndian();
86
 
87
        if( !(e = read4L(f, u.i + swap)) && !(e = read4L(f, u.i + 1 - swap)) )
88
                *v = u.d;
89
        return e;
90
}
91
 
92
OSErr readfloatB(FILEREF f,float *v){
93
        OSErr e;
94
        union {
95
                int32_t i;
96
                float f;
97
        } u;
98
 
99
        if(!(e = read4B(f, &u.i)))
100
                *v = u.f;
101
        return e;
102
}
103
 
104
OSErr writedoubleL(FILEREF f, double v){
105
        OSErr e;
106
        union {
107
                int32_t i[2];
108
                double d;
109
        } u;
110
        int swap = !platform_is_LittleEndian();
111
 
112
        u.d = v;
113
        if( !(e = write4L(f, u.i[swap])) )
114
                e = write4L(f, u.i[1-swap]);
115
        return e;
116
}
117
 
118
OSErr write4B(FILEREF f, int32_t v){
119
        unsigned char p[4];
120
        FILECOUNT count = 4;
121
        p[3] = v;
122
        p[2] = v>>8;
123
        p[1] = v>>16;
124
        p[0] = v>>24;
125
        return FSWRITE(f,&count,p);
126
}
127
 
128
OSErr writefloatB(FILEREF f, float v){
129
        union {
130
                int32_t i;
131
                float f;
132
        } u;
133
 
134
        u.f = v;
135
        return write4B(f, u.i);
136
}
137
 
138
OSErr write2B(FILEREF f, int16_t v){
139
        unsigned char p[2];
140
        FILECOUNT count = 2;
141
        p[1] = v;
142
        p[0] = v>>8;
143
        return FSWRITE(f,&count,p);
144
}
145
 
146
OSErr write4L(FILEREF f, int32_t v){
147
        unsigned char p[4];
148
        FILECOUNT count = 4;
149
        p[0] = v;
150
        p[1] = v>>8;
151
        p[2] = v>>16;
152
        p[3] = v>>24;
153
        return FSWRITE(f,&count,p);
154
}
155
 
156
OSErr write2L(FILEREF f, int16_t v){
157
        unsigned char p[2];
158
        FILECOUNT count = 2;
159
        p[0] = v;
160
        p[1] = v>>8;
161
        return FSWRITE(f,&count,p);
162
}
163
 
164
OSErr write1(FILEREF f,unsigned char v){
165
        FILECOUNT count = 1;
166
        return FSWRITE(f,&count,&v);
167
}
168