-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminput.cpp
More file actions
257 lines (187 loc) · 4.52 KB
/
minput.cpp
File metadata and controls
257 lines (187 loc) · 4.52 KB
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
/*
File: MINPUT.CPP
Description: -
Version: 1.00 - original
1.01 - included a string reading function
1.02 - added destructor to MINPUT_FN
*/
#include <stdio.h>
#include "minput.h"
MINPUT::MINPUT()
{
UWORD test=0x0102;
mode=MOTOROLA;
native=(((UBYTE *)&test)[0]==0x01) ? MOTOROLA : INTEL;
}
void MINPUT::rewind()
{
seek(0,SEEK_SET);
}
void MINPUT::wswap(UWORD *b,int n)
{
while(n--){
*b=(*b>>8) | (*b<<8);
b++;
}
}
void MINPUT::lswap(ULONG *b,int n)
{
while(n--){
*b=(*b>>24) |
(*b<<24) |
((*b & 0x00ff0000) >> 8) |
((*b & 0x0000ff00) << 8);
b++;
}
}
UWORD MINPUT::read_UWORD()
{
UWORD result;
read_UBYTES((UBYTE *)&result,2);
if(mode!=native) wswap(&result,1);
return result;
}
ULONG MINPUT::read_ULONG()
{
ULONG result;
read_UBYTES((UBYTE *)&result,4);
if(mode!=native) lswap(&result,1);
return result;
}
int MINPUT::read_UBYTES(UBYTE *b,int n)
{
while(n--) *(b++)=read_UBYTE();
return !eof();
}
int MINPUT::read_STRING(char *b,int n)
{
while(n--){
char c=read_UBYTE();
*(b++)=(c) ? c : ' ';
}
*(b++)=0;
return !eof();
}
int MINPUT::read_UWORDS(UWORD *b,int n)
{
read_UBYTES((UBYTE *)b,n<<1);
if(mode!=native) wswap(b,n);
return !eof();
}
int MINPUT::read_ULONGS(ULONG *b,int n)
{
read_UBYTES((UBYTE *)b,n<<2);
if(mode!=native) lswap(b,n);
return !eof();
}
/****************************************************************************
class: MINPUT_MEMORY
derived from: MINPUT
purpose: for reading platform independent data from a memory block
****************************************************************************/
MINPUT_MEMORY::MINPUT_MEMORY(void *a,ULONG l) : MINPUT()
{
if(a==NULL){
THROW MikModException("NULL memory pointer passed to MINPUT_MEMORY");
}
base=(UBYTE *)a;
end=base+l;
ptr=base;
}
int MINPUT_MEMORY::eof()
{
return ( ptr >= end );
}
void MINPUT_MEMORY::seek(long offset,int mode)
{
switch(mode){
case SEEK_SET:
ptr=base+offset;
break;
case SEEK_CUR:
ptr=ptr+offset;
break;
case SEEK_END:
ptr=end-offset;
break;
}
if(ptr>end) ptr=end;
else if(ptr<base) ptr=base;
}
UBYTE MINPUT_MEMORY::read_UBYTE()
{
return (ptr<end) ? *(ptr++) : 0;
}
/****************************************************************************
class: MINPUT_FP
derived from: MINPUT
purpose: for reading platform independent data from a filepointer
****************************************************************************/
MINPUT_FP::MINPUT_FP(FILE *f) : MINPUT()
{
if(f==NULL){
THROW MikModException("NULL filepointer passed to MINPUT_FP");
}
fp=f;
}
int MINPUT_FP::eof()
{
return feof(fp);
}
void MINPUT_FP::seek(long offset,int mode)
{
fseek(fp,offset,mode);
}
long MINPUT_FP::tell()
{
return ftell(fp);
}
UBYTE MINPUT_FP::read_UBYTE()
{
return fgetc(fp);
}
int MINPUT_FP::read_UBYTES(UBYTE *b,int n)
{
fread(b,1,n,fp);
return !eof();
}
/****************************************************************************
class: MINPUT_FN
derived from: MINPUT
purpose: for reading platform independent data from a filepointer
****************************************************************************/
MINPUT_FN::MINPUT_FN(char *s) : MINPUT()
{
if(s==NULL){
THROW MikModException("NULL filename passed to MINPUT_FN");
}
fp=fopen(s,"rb");
if(fp==NULL){
THROW MikModException("Can't open file");
}
}
MINPUT_FN::~MINPUT_FN()
{
if(fp) fclose(fp);
}
int MINPUT_FN::eof()
{
return feof(fp);
}
void MINPUT_FN::seek(long offset,int mode)
{
fseek(fp,offset,mode);
}
long MINPUT_FN::tell()
{
return ftell(fp);
}
UBYTE MINPUT_FN::read_UBYTE()
{
return fgetc(fp);
}
int MINPUT_FN::read_UBYTES(UBYTE *b,int n)
{
fread(b,1,n,fp);
return !eof();
}