home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 February / PCO_0299.ISO / filesbbs / linux / mikmod-3.000 / mikmod-3 / mikmod-3.1.2 / mmio / mmio.c < prev   
Encoding:
C/C++ Source or Header  |  1998-12-07  |  6.0 KB  |  251 lines

  1. /*    MikMod sound library
  2.     (c) 1998 Miodrag Vallat and others - see file AUTHORS for complete list
  3.  
  4.     This library is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU Library General Public License as
  6.     published by the Free Software Foundation; either version 2 of
  7.     the License, or (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU Library General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU Library General Public
  15.     License along with this library; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. /*==============================================================================
  20.  
  21.   $Id: mmio.c,v 1.14 1998/12/07 06:00:37 miod Exp $
  22.  
  23.   Portable file I/O routines
  24.  
  25. ==============================================================================*/
  26.  
  27. /*
  28.  
  29.     The way this module works :
  30.  
  31.     - _mm_fopen will call the errorhandler [see mmerror.c] in addition to
  32.       setting _mm_errno on exit.
  33.     - _mm_iobase is for internal use.  It is used by Player_LoadFP to
  34.       ensure that it works properly with wad files.
  35.     - _mm_read_I_* and _mm_read_M_* differ : the first is for reading data
  36.       written by a little endian (intel) machine, and the second is for reading
  37.       big endian (Mac, RISC, Alpha) machine data.
  38.     - _mm_write functions work the same as the _mm_read functions.
  39.     - _mm_read_string is for reading binary strings.  It is basically the same
  40.       as an fread of bytes.
  41.  
  42. */
  43.  
  44. #ifdef HAVE_CONFIG_H
  45. #include "config.h"
  46. #endif
  47.  
  48. #include <stdio.h>
  49. #include <string.h>
  50.  
  51. #include <mikmod_internals.h>
  52.  
  53. #define COPY_BUFSIZE  1024
  54.  
  55. static long _mm_iobase=0,temp_iobase=0;
  56.  
  57. FILE* _mm_fopen(CHAR* fname,CHAR* attrib)
  58. {
  59.     FILE *fp;
  60.  
  61.     if(!(fp=fopen(fname,attrib))) {
  62.         _mm_errno = MMERR_OPENING_FILE;
  63.         if(_mm_errorhandler) _mm_errorhandler();
  64.     }
  65.     return fp;
  66. }
  67.  
  68. int _mm_fseek(FILE* stream,long offset,int whence)
  69. {
  70.     return fseek(stream,(whence==SEEK_SET)?offset+_mm_iobase:offset,whence);
  71. }
  72.  
  73. long _mm_ftell(FILE* stream)
  74. {
  75.     return ftell(stream)-_mm_iobase;
  76. }
  77.  
  78. BOOL _mm_FileExists(CHAR* fname)
  79. {
  80.     FILE *fp;
  81.  
  82.     if(!(fp=fopen(fname,"r"))) return 0;
  83.     fclose(fp);
  84.  
  85.     return 1;
  86. }
  87.  
  88. /* Sets the current file-position as the new _mm_iobase */
  89. void _mm_iobase_setcur(FILE* fp)
  90. {
  91.     temp_iobase=_mm_iobase;  /* store old value in case of revert */
  92.     _mm_iobase=ftell(fp);
  93. }
  94.  
  95. /* Reverts to the last known _mm_iobase value. */
  96. void _mm_iobase_revert(void)
  97. {
  98.     _mm_iobase=temp_iobase;
  99. }
  100.  
  101.  
  102. /*========== Write functions */
  103.  
  104. void _mm_write_string(CHAR* data,FILE* fp)
  105. {
  106.     if(data)
  107.         _mm_write_UBYTES(data,strlen(data),fp);
  108. }
  109.  
  110. void _mm_write_M_UWORD(UWORD data,FILE* fp)
  111. {
  112.     _mm_write_UBYTE(data>>8,fp);
  113.     _mm_write_UBYTE(data&0xff,fp);
  114. }
  115.  
  116. void _mm_write_I_UWORD(UWORD data,FILE* fp)
  117. {
  118.     _mm_write_UBYTE(data&0xff,fp);
  119.     _mm_write_UBYTE(data>>8,fp);
  120. }
  121.  
  122. void _mm_write_M_ULONG(ULONG data,FILE* fp)
  123. {
  124.     _mm_write_M_UWORD(data>>16,fp);
  125.     _mm_write_M_UWORD(data&0xffff,fp);
  126. }
  127.  
  128. void _mm_write_I_ULONG(ULONG data,FILE* fp)
  129. {
  130.     _mm_write_I_UWORD(data&0xffff,fp);
  131.     _mm_write_I_UWORD(data>>16,fp);
  132. }
  133.  
  134. void _mm_write_M_SWORD(SWORD data,FILE* fp)
  135. {
  136.     _mm_write_M_UWORD((UWORD)data,fp);
  137. }
  138.  
  139. void _mm_write_I_SWORD(SWORD data,FILE* fp)
  140. {
  141.     _mm_write_I_UWORD((UWORD)data,fp);
  142. }
  143.  
  144. void _mm_write_M_SLONG(SLONG data,FILE* fp)
  145. {
  146.     _mm_write_M_ULONG((ULONG)data,fp);
  147. }
  148.  
  149. void _mm_write_I_SLONG(SLONG data,FILE* fp)
  150. {
  151.     _mm_write_I_ULONG((ULONG)data,fp);
  152. }
  153.  
  154. #define DEFINE_MULTIPLE_WRITE_FUNCTION(type_name,type)           \
  155. void _mm_write_##type_name##S (type *buffer,int number,FILE* fp) \
  156. {                                                                \
  157.     while(number-->0)                                            \
  158.         _mm_write_##type_name(*(buffer++),fp);                   \
  159. }
  160.  
  161. /*DEFINE_MULTIPLE_WRITE_FUNCTION(SBYTE,SBYTE)*/
  162. /*DEFINE_MULTIPLE_WRITE_FUNCTION(UBYTE,UBYTE)*/
  163.  
  164. DEFINE_MULTIPLE_WRITE_FUNCTION(M_SWORD,SWORD)
  165. DEFINE_MULTIPLE_WRITE_FUNCTION(M_UWORD,UWORD)
  166. DEFINE_MULTIPLE_WRITE_FUNCTION(I_SWORD,SWORD)
  167. DEFINE_MULTIPLE_WRITE_FUNCTION(I_UWORD,UWORD)
  168.  
  169. DEFINE_MULTIPLE_WRITE_FUNCTION(M_SLONG,SLONG)
  170. DEFINE_MULTIPLE_WRITE_FUNCTION(M_ULONG,ULONG)
  171. DEFINE_MULTIPLE_WRITE_FUNCTION(I_SLONG,SLONG)
  172. DEFINE_MULTIPLE_WRITE_FUNCTION(I_ULONG,ULONG)
  173.  
  174. /*========== Read functions */
  175.  
  176. int _mm_read_string(CHAR* buffer,int number,FILE* fp)
  177. {
  178.     fread(buffer,1,number,fp);
  179.     return !feof(fp);
  180. }
  181.  
  182. UWORD _mm_read_M_UWORD(FILE *fp)
  183. {
  184.     UWORD result=((UWORD)_mm_read_UBYTE(fp))<<8;
  185.     result|=_mm_read_UBYTE(fp);
  186.     return result;
  187. }
  188.  
  189. UWORD _mm_read_I_UWORD(FILE *fp)
  190. {
  191.     UWORD result=_mm_read_UBYTE(fp);
  192.     result|=((UWORD)_mm_read_UBYTE(fp))<<8;
  193.     return result;
  194. }
  195.  
  196. ULONG _mm_read_M_ULONG(FILE *fp)
  197. {
  198.     ULONG result=((ULONG)_mm_read_M_UWORD(fp))<<16;
  199.     result|=_mm_read_M_UWORD(fp);
  200.     return result;
  201. }
  202.  
  203. ULONG _mm_read_I_ULONG(FILE *fp)
  204. {
  205.     ULONG result=_mm_read_I_UWORD(fp);
  206.     result|=((ULONG)_mm_read_I_UWORD(fp))<<16;
  207.     return result;
  208. }
  209.  
  210. SWORD _mm_read_M_SWORD(FILE* fp)
  211. {
  212.     return((SWORD)_mm_read_M_UWORD(fp));
  213. }
  214.  
  215. SWORD _mm_read_I_SWORD(FILE* fp)
  216. {
  217.     return((SWORD)_mm_read_I_UWORD(fp));
  218. }
  219.  
  220. SLONG _mm_read_M_SLONG(FILE* fp)
  221. {
  222.     return((SLONG)_mm_read_M_ULONG(fp));
  223. }
  224.  
  225. SLONG _mm_read_I_SLONG(FILE* fp)
  226. {
  227.     return((SLONG)_mm_read_I_ULONG(fp));
  228. }
  229.  
  230. #define DEFINE_MULTIPLE_READ_FUNCTION(type_name,type)          \
  231. int _mm_read_##type_name##S (type *buffer,int number,FILE* fp) \
  232. {                                                              \
  233.     while(number-->0)                                          \
  234.         *(buffer++)=_mm_read_##type_name(fp);                  \
  235.     return !feof(fp);                                          \
  236. }
  237.  
  238. /*DEFINE_MULTIPLE_READ_FUNCTION(SBYTE,SBYTE)*/
  239. /*DEFINE_MULTIPLE_READ_FUNCTION(UBYTE,UBYTE)*/
  240.  
  241. DEFINE_MULTIPLE_READ_FUNCTION(M_SWORD,SWORD)
  242. DEFINE_MULTIPLE_READ_FUNCTION(M_UWORD,UWORD)
  243. DEFINE_MULTIPLE_READ_FUNCTION(I_SWORD,SWORD)
  244. DEFINE_MULTIPLE_READ_FUNCTION(I_UWORD,UWORD)
  245.  
  246. DEFINE_MULTIPLE_READ_FUNCTION(M_SLONG,SLONG)
  247. DEFINE_MULTIPLE_READ_FUNCTION(M_ULONG,ULONG)
  248. DEFINE_MULTIPLE_READ_FUNCTION(I_SLONG,SLONG)
  249. DEFINE_MULTIPLE_READ_FUNCTION(I_ULONG,ULONG)
  250.  
  251.