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