Skip to content

Latest commit

 

History

History
360 lines (291 loc) · 8.77 KB

mmio.c

File metadata and controls

360 lines (291 loc) · 8.77 KB
 
Dec 27, 1999
Dec 27, 1999
1
2
3
/* MikMod sound library
(c) 1998, 1999 Miodrag Vallat and others - see file AUTHORS for
complete list.
Oct 21, 1999
Oct 21, 1999
4
Dec 27, 1999
Dec 27, 1999
5
6
7
8
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
Oct 21, 1999
Oct 21, 1999
9
Dec 27, 1999
Dec 27, 1999
10
11
12
13
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 Library General Public License for more details.
Oct 21, 1999
Oct 21, 1999
14
Dec 27, 1999
Dec 27, 1999
15
16
17
18
19
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
Oct 21, 1999
Oct 21, 1999
20
Dec 27, 1999
Dec 27, 1999
21
/*==============================================================================
Oct 21, 1999
Oct 21, 1999
22
Dec 27, 1999
Dec 27, 1999
23
$Id$
Oct 21, 1999
Oct 21, 1999
24
Dec 27, 1999
Dec 27, 1999
25
Portable file I/O routines
Oct 21, 1999
Oct 21, 1999
26
Dec 27, 1999
Dec 27, 1999
27
==============================================================================*/
Oct 21, 1999
Oct 21, 1999
28
Dec 27, 1999
Dec 27, 1999
29
/*
Oct 21, 1999
Oct 21, 1999
30
Dec 27, 1999
Dec 27, 1999
31
The way this module works:
Oct 21, 1999
Oct 21, 1999
32
Dec 27, 1999
Dec 27, 1999
33
34
35
36
37
38
39
40
41
42
- _mm_fopen will call the errorhandler [see mmerror.c] in addition to
setting _mm_errno on exit.
- _mm_iobase is for internal use. It is used by Player_LoadFP to
ensure that it works properly with wad files.
- _mm_read_I_* and _mm_read_M_* differ : the first is for reading data
written by a little endian (intel) machine, and the second is for reading
big endian (Mac, RISC, Alpha) machine data.
- _mm_write functions work the same as the _mm_read functions.
- _mm_read_string is for reading binary strings. It is basically the same
as an fread of bytes.
Oct 21, 1999
Oct 21, 1999
43
Dec 27, 1999
Dec 27, 1999
44
*/
Oct 21, 1999
Oct 21, 1999
45
Dec 27, 1999
Dec 27, 1999
46
47
48
49
/* FIXME
the _mm_iobase variable ought to be MREADER-specific. It will eventually
become a private field of the MREADER structure, but this will require a
soname version bump.
Oct 21, 1999
Oct 21, 1999
50
Dec 27, 1999
Dec 27, 1999
51
52
53
In the meantime, the drawback is that if you use the xxx_LoadFP functions,
you can't have several MREADER objects with different iobase values.
*/
Oct 21, 1999
Oct 21, 1999
54
Dec 27, 1999
Dec 27, 1999
55
56
57
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Oct 21, 1999
Oct 21, 1999
58
Dec 27, 1999
Dec 27, 1999
59
60
#include <stdio.h>
#include <string.h>
Oct 21, 1999
Oct 21, 1999
61
Dec 27, 1999
Dec 27, 1999
62
#include "mikmod_internals.h"
Oct 21, 1999
Oct 21, 1999
63
Dec 27, 1999
Dec 27, 1999
64
#define COPY_BUFSIZE 1024
Oct 21, 1999
Oct 21, 1999
65
Dec 27, 1999
Dec 27, 1999
66
static long _mm_iobase=0,temp_iobase=0;
Oct 21, 1999
Oct 21, 1999
67
Dec 27, 1999
Dec 27, 1999
68
FILE* _mm_fopen(CHAR* fname,CHAR* attrib)
Oct 21, 1999
Oct 21, 1999
69
{
Dec 27, 1999
Dec 27, 1999
70
FILE *fp;
Oct 21, 1999
Oct 21, 1999
71
Dec 27, 1999
Dec 27, 1999
72
73
74
75
76
if(!(fp=fopen(fname,attrib))) {
_mm_errno = MMERR_OPENING_FILE;
if(_mm_errorhandler) _mm_errorhandler();
}
return fp;
Oct 21, 1999
Oct 21, 1999
77
78
}
Dec 27, 1999
Dec 27, 1999
79
BOOL _mm_FileExists(CHAR* fname)
Oct 21, 1999
Oct 21, 1999
80
{
Dec 27, 1999
Dec 27, 1999
81
FILE *fp;
Oct 21, 1999
Oct 21, 1999
82
Dec 27, 1999
Dec 27, 1999
83
84
if(!(fp=fopen(fname,"r"))) return 0;
fclose(fp);
Oct 21, 1999
Oct 21, 1999
85
Dec 27, 1999
Dec 27, 1999
86
return 1;
Oct 21, 1999
Oct 21, 1999
87
88
}
Dec 27, 1999
Dec 27, 1999
89
90
/* Sets the current file-position as the new _mm_iobase */
void _mm_iobase_setcur(MREADER* reader)
Oct 21, 1999
Oct 21, 1999
91
{
Dec 27, 1999
Dec 27, 1999
92
93
temp_iobase=_mm_iobase; /* store old value in case of revert */
_mm_iobase=reader->Tell(reader);
Oct 21, 1999
Oct 21, 1999
94
95
}
Dec 27, 1999
Dec 27, 1999
96
97
/* Reverts to the last known _mm_iobase value. */
void _mm_iobase_revert(void)
Oct 21, 1999
Oct 21, 1999
98
{
Dec 27, 1999
Dec 27, 1999
99
_mm_iobase=temp_iobase;
Oct 21, 1999
Oct 21, 1999
100
101
}
Dec 27, 1999
Dec 27, 1999
102
/*========== File Reader */
Oct 21, 1999
Oct 21, 1999
103
Dec 27, 1999
Dec 27, 1999
104
105
106
107
typedef struct MFILEREADER {
MREADER core;
FILE* file;
} MFILEREADER;
Oct 21, 1999
Oct 21, 1999
108
Dec 27, 1999
Dec 27, 1999
109
static BOOL _mm_FileReader_Eof(MREADER* reader)
Oct 21, 1999
Oct 21, 1999
110
{
Dec 27, 1999
Dec 27, 1999
111
return feof(((MFILEREADER*)reader)->file);
Oct 21, 1999
Oct 21, 1999
112
113
}
Dec 27, 1999
Dec 27, 1999
114
static BOOL _mm_FileReader_Read(MREADER* reader,void* ptr,size_t size)
Oct 21, 1999
Oct 21, 1999
115
{
Dec 27, 1999
Dec 27, 1999
116
return fread(ptr,size,1,((MFILEREADER*)reader)->file);
Oct 21, 1999
Oct 21, 1999
117
118
}
Dec 27, 1999
Dec 27, 1999
119
static int _mm_FileReader_Get(MREADER* reader)
Oct 21, 1999
Oct 21, 1999
120
{
Dec 27, 1999
Dec 27, 1999
121
return fgetc(((MFILEREADER*)reader)->file);
Oct 21, 1999
Oct 21, 1999
122
123
}
Dec 27, 1999
Dec 27, 1999
124
static BOOL _mm_FileReader_Seek(MREADER* reader,long offset,int whence)
Oct 21, 1999
Oct 21, 1999
125
{
Dec 27, 1999
Dec 27, 1999
126
127
return fseek(((MFILEREADER*)reader)->file,
(whence==SEEK_SET)?offset+_mm_iobase:offset,whence);
Oct 21, 1999
Oct 21, 1999
128
129
}
Dec 27, 1999
Dec 27, 1999
130
static long _mm_FileReader_Tell(MREADER* reader)
Oct 21, 1999
Oct 21, 1999
131
{
Dec 27, 1999
Dec 27, 1999
132
return ftell(((MFILEREADER*)reader)->file)-_mm_iobase;
Oct 21, 1999
Oct 21, 1999
133
134
}
Dec 27, 1999
Dec 27, 1999
135
MREADER *_mm_new_file_reader(FILE* fp)
Oct 21, 1999
Oct 21, 1999
136
{
Dec 27, 1999
Dec 27, 1999
137
138
139
140
141
142
143
144
145
146
MFILEREADER* reader=(MFILEREADER*)_mm_malloc(sizeof(MFILEREADER));
if (reader) {
reader->core.Eof =&_mm_FileReader_Eof;
reader->core.Read=&_mm_FileReader_Read;
reader->core.Get =&_mm_FileReader_Get;
reader->core.Seek=&_mm_FileReader_Seek;
reader->core.Tell=&_mm_FileReader_Tell;
reader->file=fp;
}
return (MREADER*)reader;
Oct 21, 1999
Oct 21, 1999
147
148
}
Dec 27, 1999
Dec 27, 1999
149
void _mm_delete_file_reader (MREADER* reader)
Oct 21, 1999
Oct 21, 1999
150
{
Dec 27, 1999
Dec 27, 1999
151
if(reader) free(reader);
Oct 21, 1999
Oct 21, 1999
152
153
}
Dec 27, 1999
Dec 27, 1999
154
/*========== File Writer */
Oct 21, 1999
Oct 21, 1999
155
Dec 27, 1999
Dec 27, 1999
156
157
158
159
typedef struct MFILEWRITER {
MWRITER core;
FILE* file;
} MFILEWRITER;
Oct 21, 1999
Oct 21, 1999
160
Dec 27, 1999
Dec 27, 1999
161
static BOOL _mm_FileWriter_Seek(MWRITER* writer,long offset,int whence)
Oct 21, 1999
Oct 21, 1999
162
{
Dec 27, 1999
Dec 27, 1999
163
return fseek(((MFILEWRITER*)writer)->file,offset,whence);
Oct 21, 1999
Oct 21, 1999
164
165
}
Dec 27, 1999
Dec 27, 1999
166
static long _mm_FileWriter_Tell(MWRITER* writer)
Oct 21, 1999
Oct 21, 1999
167
{
Dec 27, 1999
Dec 27, 1999
168
return ftell(((MFILEWRITER*)writer)->file);
Oct 21, 1999
Oct 21, 1999
169
170
}
Dec 27, 1999
Dec 27, 1999
171
static BOOL _mm_FileWriter_Write(MWRITER* writer,void* ptr,size_t size)
Oct 21, 1999
Oct 21, 1999
172
{
Dec 27, 1999
Dec 27, 1999
173
return (fwrite(ptr,size,1,((MFILEWRITER*)writer)->file)==size);
Oct 21, 1999
Oct 21, 1999
174
175
}
Dec 27, 1999
Dec 27, 1999
176
static BOOL _mm_FileWriter_Put(MWRITER* writer,int value)
Oct 21, 1999
Oct 21, 1999
177
{
Dec 27, 1999
Dec 27, 1999
178
return fputc(value,((MFILEWRITER*)writer)->file);
Oct 21, 1999
Oct 21, 1999
179
180
}
Dec 27, 1999
Dec 27, 1999
181
MWRITER *_mm_new_file_writer(FILE* fp)
Oct 21, 1999
Oct 21, 1999
182
{
Dec 27, 1999
Dec 27, 1999
183
184
185
186
187
188
189
190
191
MFILEWRITER* writer=(MFILEWRITER*)_mm_malloc(sizeof(MFILEWRITER));
if (writer) {
writer->core.Seek =&_mm_FileWriter_Seek;
writer->core.Tell =&_mm_FileWriter_Tell;
writer->core.Write=&_mm_FileWriter_Write;
writer->core.Put =&_mm_FileWriter_Put;
writer->file=fp;
}
return (MWRITER*) writer;
Oct 21, 1999
Oct 21, 1999
192
193
}
Dec 27, 1999
Dec 27, 1999
194
void _mm_delete_file_writer (MWRITER* writer)
Oct 21, 1999
Oct 21, 1999
195
{
Dec 27, 1999
Dec 27, 1999
196
if(writer) free (writer);
Oct 21, 1999
Oct 21, 1999
197
198
}
Dec 27, 1999
Dec 27, 1999
199
/*========== Write functions */
Oct 21, 1999
Oct 21, 1999
200
Dec 27, 1999
Dec 27, 1999
201
void _mm_write_string(CHAR* data,MWRITER* writer)
Oct 21, 1999
Oct 21, 1999
202
{
Dec 27, 1999
Dec 27, 1999
203
204
if(data)
_mm_write_UBYTES(data,strlen(data),writer);
Oct 21, 1999
Oct 21, 1999
205
206
}
Dec 27, 1999
Dec 27, 1999
207
void _mm_write_M_UWORD(UWORD data,MWRITER* writer)
Oct 21, 1999
Oct 21, 1999
208
{
Dec 27, 1999
Dec 27, 1999
209
210
_mm_write_UBYTE(data>>8,writer);
_mm_write_UBYTE(data&0xff,writer);
Oct 21, 1999
Oct 21, 1999
211
212
}
Dec 27, 1999
Dec 27, 1999
213
void _mm_write_I_UWORD(UWORD data,MWRITER* writer)
Oct 21, 1999
Oct 21, 1999
214
{
Dec 27, 1999
Dec 27, 1999
215
216
_mm_write_UBYTE(data&0xff,writer);
_mm_write_UBYTE(data>>8,writer);
Oct 21, 1999
Oct 21, 1999
217
218
}
Dec 27, 1999
Dec 27, 1999
219
void _mm_write_M_ULONG(ULONG data,MWRITER* writer)
Oct 21, 1999
Oct 21, 1999
220
{
Dec 27, 1999
Dec 27, 1999
221
222
_mm_write_M_UWORD(data>>16,writer);
_mm_write_M_UWORD(data&0xffff,writer);
Oct 21, 1999
Oct 21, 1999
223
224
}
Dec 27, 1999
Dec 27, 1999
225
void _mm_write_I_ULONG(ULONG data,MWRITER* writer)
Oct 21, 1999
Oct 21, 1999
226
{
Dec 27, 1999
Dec 27, 1999
227
228
_mm_write_I_UWORD(data&0xffff,writer);
_mm_write_I_UWORD(data>>16,writer);
Oct 21, 1999
Oct 21, 1999
229
230
}
Dec 27, 1999
Dec 27, 1999
231
void _mm_write_M_SWORD(SWORD data,MWRITER* writer)
Oct 21, 1999
Oct 21, 1999
232
{
Dec 27, 1999
Dec 27, 1999
233
_mm_write_M_UWORD((UWORD)data,writer);
Oct 21, 1999
Oct 21, 1999
234
235
}
Dec 27, 1999
Dec 27, 1999
236
void _mm_write_I_SWORD(SWORD data,MWRITER* writer)
Oct 21, 1999
Oct 21, 1999
237
{
Dec 27, 1999
Dec 27, 1999
238
_mm_write_I_UWORD((UWORD)data,writer);
Oct 21, 1999
Oct 21, 1999
239
240
}
Dec 27, 1999
Dec 27, 1999
241
void _mm_write_M_SLONG(SLONG data,MWRITER* writer)
Oct 21, 1999
Oct 21, 1999
242
{
Dec 27, 1999
Dec 27, 1999
243
_mm_write_M_ULONG((ULONG)data,writer);
Oct 21, 1999
Oct 21, 1999
244
245
}
Dec 27, 1999
Dec 27, 1999
246
void _mm_write_I_SLONG(SLONG data,MWRITER* writer)
Oct 21, 1999
Oct 21, 1999
247
{
Dec 27, 1999
Dec 27, 1999
248
_mm_write_I_ULONG((ULONG)data,writer);
Oct 21, 1999
Oct 21, 1999
249
250
}
Dec 27, 1999
Dec 27, 1999
251
252
253
254
255
256
#ifdef __STDC__
#define DEFINE_MULTIPLE_WRITE_FUNCTION(type_name,type) \
void _mm_write_##type_name##S (type *buffer,int number,MWRITER* writer) \
{ \
while(number-->0) \
_mm_write_##type_name(*(buffer++),writer); \
Oct 21, 1999
Oct 21, 1999
257
}
Dec 27, 1999
Dec 27, 1999
258
259
260
261
262
263
#else
#define DEFINE_MULTIPLE_WRITE_FUNCTION(type_name,type) \
void _mm_write_/**/type_name/**/S (type *buffer,int number,MWRITER* writer) \
{ \
while(number-->0) \
_mm_write_/**/type_name(*(buffer++),writer); \
Oct 21, 1999
Oct 21, 1999
264
}
Dec 27, 1999
Dec 27, 1999
265
#endif
Oct 21, 1999
Oct 21, 1999
266
Dec 27, 1999
Dec 27, 1999
267
268
269
270
DEFINE_MULTIPLE_WRITE_FUNCTION(M_SWORD,SWORD)
DEFINE_MULTIPLE_WRITE_FUNCTION(M_UWORD,UWORD)
DEFINE_MULTIPLE_WRITE_FUNCTION(I_SWORD,SWORD)
DEFINE_MULTIPLE_WRITE_FUNCTION(I_UWORD,UWORD)
Oct 21, 1999
Oct 21, 1999
271
Dec 27, 1999
Dec 27, 1999
272
273
274
275
276
277
DEFINE_MULTIPLE_WRITE_FUNCTION(M_SLONG,SLONG)
DEFINE_MULTIPLE_WRITE_FUNCTION(M_ULONG,ULONG)
DEFINE_MULTIPLE_WRITE_FUNCTION(I_SLONG,SLONG)
DEFINE_MULTIPLE_WRITE_FUNCTION(I_ULONG,ULONG)
/*========== Read functions */
Oct 21, 1999
Oct 21, 1999
278
Dec 27, 1999
Dec 27, 1999
279
int _mm_read_string(CHAR* buffer,int number,MREADER* reader)
Oct 21, 1999
Oct 21, 1999
280
{
Dec 27, 1999
Dec 27, 1999
281
return reader->Read(reader,buffer,number);
Oct 21, 1999
Oct 21, 1999
282
283
}
Dec 27, 1999
Dec 27, 1999
284
UWORD _mm_read_M_UWORD(MREADER* reader)
Oct 21, 1999
Oct 21, 1999
285
{
Dec 27, 1999
Dec 27, 1999
286
287
288
UWORD result=((UWORD)_mm_read_UBYTE(reader))<<8;
result|=_mm_read_UBYTE(reader);
return result;
Oct 21, 1999
Oct 21, 1999
289
290
}
Dec 27, 1999
Dec 27, 1999
291
UWORD _mm_read_I_UWORD(MREADER* reader)
Oct 21, 1999
Oct 21, 1999
292
{
Dec 27, 1999
Dec 27, 1999
293
294
295
UWORD result=_mm_read_UBYTE(reader);
result|=((UWORD)_mm_read_UBYTE(reader))<<8;
return result;
Oct 21, 1999
Oct 21, 1999
296
297
}
Dec 27, 1999
Dec 27, 1999
298
ULONG _mm_read_M_ULONG(MREADER* reader)
Oct 21, 1999
Oct 21, 1999
299
{
Dec 27, 1999
Dec 27, 1999
300
301
302
ULONG result=((ULONG)_mm_read_M_UWORD(reader))<<16;
result|=_mm_read_M_UWORD(reader);
return result;
Oct 21, 1999
Oct 21, 1999
303
304
}
Dec 27, 1999
Dec 27, 1999
305
ULONG _mm_read_I_ULONG(MREADER* reader)
Oct 21, 1999
Oct 21, 1999
306
{
Dec 27, 1999
Dec 27, 1999
307
308
309
ULONG result=_mm_read_I_UWORD(reader);
result|=((ULONG)_mm_read_I_UWORD(reader))<<16;
return result;
Oct 21, 1999
Oct 21, 1999
310
311
}
Dec 27, 1999
Dec 27, 1999
312
SWORD _mm_read_M_SWORD(MREADER* reader)
Oct 21, 1999
Oct 21, 1999
313
{
Dec 27, 1999
Dec 27, 1999
314
return((SWORD)_mm_read_M_UWORD(reader));
Oct 21, 1999
Oct 21, 1999
315
316
}
Dec 27, 1999
Dec 27, 1999
317
SWORD _mm_read_I_SWORD(MREADER* reader)
Oct 21, 1999
Oct 21, 1999
318
{
Dec 27, 1999
Dec 27, 1999
319
return((SWORD)_mm_read_I_UWORD(reader));
Oct 21, 1999
Oct 21, 1999
320
321
}
Dec 27, 1999
Dec 27, 1999
322
SLONG _mm_read_M_SLONG(MREADER* reader)
Oct 21, 1999
Oct 21, 1999
323
{
Dec 27, 1999
Dec 27, 1999
324
return((SLONG)_mm_read_M_ULONG(reader));
Oct 21, 1999
Oct 21, 1999
325
326
}
Dec 27, 1999
Dec 27, 1999
327
SLONG _mm_read_I_SLONG(MREADER* reader)
Oct 21, 1999
Oct 21, 1999
328
{
Dec 27, 1999
Dec 27, 1999
329
return((SLONG)_mm_read_I_ULONG(reader));
Oct 21, 1999
Oct 21, 1999
330
331
}
Dec 27, 1999
Dec 27, 1999
332
333
334
335
336
337
338
#ifdef __STDC__
#define DEFINE_MULTIPLE_READ_FUNCTION(type_name,type) \
int _mm_read_##type_name##S (type *buffer,int number,MREADER* reader) \
{ \
while(number-->0) \
*(buffer++)=_mm_read_##type_name(reader); \
return !reader->Eof(reader); \
Oct 21, 1999
Oct 21, 1999
339
}
Dec 27, 1999
Dec 27, 1999
340
341
342
343
344
345
346
347
348
#else
#define DEFINE_MULTIPLE_READ_FUNCTION(type_name,type) \
int _mm_read_/**/type_name/**/S (type *buffer,int number,MREADER* reader) \
{ \
while(number-->0) \
*(buffer++)=_mm_read_/**/type_name(reader); \
return !reader->Eof(reader); \
}
#endif
Oct 21, 1999
Oct 21, 1999
349
Dec 27, 1999
Dec 27, 1999
350
351
352
353
DEFINE_MULTIPLE_READ_FUNCTION(M_SWORD,SWORD)
DEFINE_MULTIPLE_READ_FUNCTION(M_UWORD,UWORD)
DEFINE_MULTIPLE_READ_FUNCTION(I_SWORD,SWORD)
DEFINE_MULTIPLE_READ_FUNCTION(I_UWORD,UWORD)
Oct 21, 1999
Oct 21, 1999
354
Dec 27, 1999
Dec 27, 1999
355
356
357
358
DEFINE_MULTIPLE_READ_FUNCTION(M_SLONG,SLONG)
DEFINE_MULTIPLE_READ_FUNCTION(M_ULONG,ULONG)
DEFINE_MULTIPLE_READ_FUNCTION(I_SLONG,SLONG)
DEFINE_MULTIPLE_READ_FUNCTION(I_ULONG,ULONG)
Oct 21, 1999
Oct 21, 1999
359
Dec 27, 1999
Dec 27, 1999
360
/* ex:set ts=4: */