reorganized the source code
[dosdemo] / libs / oldmik / src / mmio.c
1 /*
2
3 Name:
4 MMIO.C
5
6 Description:
7 Miscellaneous I/O routines.. used to solve some portability issues
8 (like big/little endian machines and word alignment in structures )
9 Also includes mikmod's ingenious error handling variable + some much
10 used error strings.
11
12 Portability:
13 All systems - all compilers
14
15 */
16 #include <stdio.h>
17 #include "mikmod.h"
18
19 char *ERROR_ALLOC_STRUCT="Error allocating structure";
20 char *ERROR_LOADING_PATTERN="Error loading pattern";
21 char *ERROR_LOADING_TRACK="Error loading track";
22 char *ERROR_LOADING_HEADER="Error loading header";
23 char *ERROR_NOT_A_MODULE="Unknown module format";
24 char *ERROR_LOADING_SAMPLEINFO="Error loading sampleinfo";
25 char *ERROR_OUT_OF_HANDLES="Out of sample-handles";
26 char *ERROR_SAMPLE_TOO_BIG="Sample too big, out of memory";
27
28 char *myerr;
29
30 static long _mm_iobase=0;
31
32 int _mm_fseek(FILE *stream, long offset, int whence)
33 {
34         return fseek(stream,
35                                  (whence==SEEK_SET) ? offset+_mm_iobase : offset,
36                                  whence);
37 }
38
39 long _mm_ftell(FILE *stream)
40 {
41         return ftell(stream)-_mm_iobase;
42 }
43
44 void _mm_setiobase(long iobase)
45 {
46         _mm_iobase=iobase;
47 }
48
49 void _mm_write_SBYTE(SBYTE data,FILE *fp)
50 {
51         fputc(data,fp);
52 }
53
54 void _mm_write_UBYTE(UBYTE data,FILE *fp)
55 {
56         fputc(data,fp);
57 }
58
59 void _mm_write_M_UWORD(UWORD data,FILE *fp)
60 {
61         _mm_write_UBYTE(data>>8,fp);
62         _mm_write_UBYTE(data&0xff,fp);
63 }
64
65 void _mm_write_I_UWORD(UWORD data,FILE *fp)
66 {
67         _mm_write_UBYTE(data&0xff,fp);
68         _mm_write_UBYTE(data>>8,fp);
69 }
70
71 void _mm_write_M_SWORD(SWORD data,FILE *fp)
72 {
73         _mm_write_M_UWORD((UWORD)data,fp);
74 }
75
76 void _mm_write_I_SWORD(SWORD data,FILE *fp)
77 {
78         _mm_write_I_UWORD((UWORD)data,fp);
79 }
80
81 void _mm_write_M_ULONG(ULONG data,FILE *fp)
82 {
83         _mm_write_M_UWORD(data>>16,fp);
84         _mm_write_M_UWORD(data&0xffff,fp);
85 }
86
87 void _mm_write_I_ULONG(ULONG data,FILE *fp)
88 {
89         _mm_write_I_UWORD(data&0xffff,fp);
90         _mm_write_I_UWORD(data>>16,fp);
91 }
92
93 void _mm_write_M_SLONG(SLONG data,FILE *fp)
94 {
95         _mm_write_M_ULONG((ULONG)data,fp);
96 }
97
98 void _mm_write_I_SLONG(SLONG data,FILE *fp)
99 {
100         _mm_write_I_ULONG((ULONG)data,fp);
101 }
102
103
104 #define DEFINE_MULTIPLE_WRITE_FUNCTION(type_name, type)        \
105 void                                                           \
106 _mm_write_##type_name##S (type *buffer, int number, FILE *fp)  \
107 {                                                              \
108         while(number>0){                                           \
109                 _mm_write_##type_name(*(buffer++),fp);                             \
110                 number--;                                                                                          \
111         }                                                                                                                  \
112 }
113
114 DEFINE_MULTIPLE_WRITE_FUNCTION ( SBYTE,  SBYTE)
115 DEFINE_MULTIPLE_WRITE_FUNCTION (UBYTE, UBYTE)
116
117 DEFINE_MULTIPLE_WRITE_FUNCTION (M_SWORD,   SWORD)
118 DEFINE_MULTIPLE_WRITE_FUNCTION (M_UWORD, UWORD)
119 DEFINE_MULTIPLE_WRITE_FUNCTION (I_SWORD,   SWORD)
120 DEFINE_MULTIPLE_WRITE_FUNCTION (I_UWORD, UWORD)
121
122 DEFINE_MULTIPLE_WRITE_FUNCTION (M_SLONG,   SLONG)
123 DEFINE_MULTIPLE_WRITE_FUNCTION (M_ULONG, ULONG)
124 DEFINE_MULTIPLE_WRITE_FUNCTION (I_SLONG,   SLONG)
125 DEFINE_MULTIPLE_WRITE_FUNCTION (I_ULONG, ULONG)
126
127
128 SBYTE _mm_read_SBYTE(FILE *fp)
129 {
130         return(fgetc(fp));
131 }
132
133 UBYTE _mm_read_UBYTE(FILE *fp)
134 {
135         return(fgetc(fp));
136 }
137
138 UWORD _mm_read_M_UWORD(FILE *fp)
139 {
140         UWORD result=((UWORD)_mm_read_UBYTE(fp))<<8;
141         result|=_mm_read_UBYTE(fp);
142         return result;
143 }
144
145 UWORD _mm_read_I_UWORD(FILE *fp)
146 {
147         UWORD result=_mm_read_UBYTE(fp);
148         result|=((UWORD)_mm_read_UBYTE(fp))<<8;
149         return result;
150 }
151
152 SWORD _mm_read_M_SWORD(FILE *fp)
153 {
154         return((SWORD)_mm_read_M_UWORD(fp));
155 }
156
157 SWORD _mm_read_I_SWORD(FILE *fp)
158 {
159         return((SWORD)_mm_read_I_UWORD(fp));
160 }
161
162 ULONG _mm_read_M_ULONG(FILE *fp)
163 {
164         ULONG result=((ULONG)_mm_read_M_UWORD(fp))<<16;
165         result|=_mm_read_M_UWORD(fp);
166         return result;
167 }
168
169 ULONG _mm_read_I_ULONG(FILE *fp)
170 {
171         ULONG result=_mm_read_I_UWORD(fp);
172         result|=((ULONG)_mm_read_I_UWORD(fp))<<16;
173         return result;
174 }
175
176 SLONG _mm_read_M_SLONG(FILE *fp)
177 {
178         return((SLONG)_mm_read_M_ULONG(fp));
179 }
180
181 SLONG _mm_read_I_SLONG(FILE *fp)
182 {
183         return((SLONG)_mm_read_I_ULONG(fp));
184 }
185
186
187 int _mm_read_str(char *buffer,int number,FILE *fp)
188 {
189         fread(buffer,1,number,fp);
190         return !feof(fp);
191 }
192
193
194 #define DEFINE_MULTIPLE_READ_FUNCTION(type_name, type)         \
195 int                                                            \
196 _mm_read_##type_name##S (type *buffer, int number, FILE *fp)   \
197 {                                                              \
198         while(number>0){                                           \
199                 *(buffer++)=_mm_read_##type_name(fp);                              \
200                 number--;                                                                                          \
201         }                                                                                                                  \
202         return !feof(fp);                                                                                  \
203 }
204
205 DEFINE_MULTIPLE_READ_FUNCTION ( SBYTE,  SBYTE)
206 DEFINE_MULTIPLE_READ_FUNCTION (UBYTE, UBYTE)
207
208 DEFINE_MULTIPLE_READ_FUNCTION (M_SWORD,   SWORD)
209 DEFINE_MULTIPLE_READ_FUNCTION (M_UWORD, UWORD)
210 DEFINE_MULTIPLE_READ_FUNCTION (I_SWORD,   SWORD)
211 DEFINE_MULTIPLE_READ_FUNCTION (I_UWORD, UWORD)
212
213 DEFINE_MULTIPLE_READ_FUNCTION (M_SLONG,   SLONG)
214 DEFINE_MULTIPLE_READ_FUNCTION (M_ULONG, ULONG)
215 DEFINE_MULTIPLE_READ_FUNCTION (I_SLONG,   SLONG)
216 DEFINE_MULTIPLE_READ_FUNCTION (I_ULONG, ULONG)