home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / dos / prg / midas / file.c < prev    next >
C/C++ Source or Header  |  1994-08-06  |  8KB  |  279 lines

  1. /*      FILE.C
  2.  *
  3.  * High-level file I/O for MIDAS Sound System
  4.  *
  5.  * Copyright 1994 Petteri Kangaslampi and Jarno Paananen
  6.  *
  7.  * This file is part of the MIDAS Sound System, and may only be
  8.  * used, modified and distributed under the terms of the MIDAS
  9.  * Sound System license, LICENSE.TXT. By continuing to use,
  10.  * modify or distribute this file you indicate that you have
  11.  * read the license and understand and accept it fully.
  12. */
  13.  
  14. /****************************************************************************\
  15. * Note! This module basicly calls directly the raw file I/O functions.
  16. * This kind of separation, however, is useful as it allows other kinds
  17. * of file systems, such as a compressed file library, to be added by
  18. * simply relinking MIDAS with another high-level file I/O module.
  19. \****************************************************************************/
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #include "lang.h"
  26. #include "mtypes.h"
  27. #include "errors.h"
  28. #include "mmem.h"
  29. #include "rawfile.h"
  30. #include "file.h"
  31.  
  32.  
  33.  
  34. /****************************************************************************\
  35. *
  36. * Function:     int fileOpen(char *fileName, int openMode, fileHandle *file);
  37. *
  38. * Description:  Opens a file for reading or writing
  39. *
  40. * Input:        char *fileName          name of file
  41. *               int openMode            file opening mode, see enum rfOpenMode
  42. *               fileHandle *file        pointer to file handle
  43. *
  44. * Returns:      MIDAS error code.
  45. *               File handle is stored in *file.
  46. *
  47. \****************************************************************************/
  48.  
  49. int CALLING fileOpen(char *fileName, int openMode, fileHandle *file)
  50. {
  51.     int         error;
  52.     int         rfMode;
  53.     fileHandle  hdl;
  54.  
  55.     /* allocate file structure: */
  56.     if ( (error = memAlloc(sizeof(fileFile), (void**) &hdl)) != OK )
  57.         PASSERROR(ID_fileOpen)
  58.  
  59.     switch ( openMode )
  60.     {
  61.         case fileOpenRead:      /* open file for reading */
  62.             rfMode = rfOpenRead;
  63.             break;
  64.  
  65.         case fileOpenWrite:     /* open file for writing */
  66.             rfMode = rfOpenWrite;
  67.             break;
  68.  
  69.         case fileOpenReadWrite: /* open file for reading and writing */
  70.             rfMode = rfOpenReadWrite;
  71.             break;
  72.  
  73.         default:
  74.             ERROR(ID_fileOpen, errInvalidArguments);
  75.             return errInvalidArguments;
  76.     }
  77.  
  78.     /* open file: */
  79.     if ( (error = rfOpen(fileName, rfMode, (rfHandle*) &hdl->rf)) != OK )
  80.         PASSERROR(ID_fileOpen)
  81.  
  82.     *file = hdl;
  83.  
  84.     return OK;
  85. }
  86.  
  87.  
  88.  
  89.  
  90. /****************************************************************************\
  91. *
  92. * Function:     int fileClose(fileHandle file);
  93. *
  94. * Description:  Closes a file opened with fileOpen().
  95. *
  96. * Input:        fileHandle file         handle of an open file
  97. *
  98. * Returns:      MIDAS error code
  99. *
  100. \****************************************************************************/
  101.  
  102. int CALLING fileClose(fileHandle file)
  103. {
  104.     int         error;
  105.  
  106.     /* close file: */
  107.     if ( (error = rfClose(file->rf)) != OK )
  108.         PASSERROR(ID_fileClose)
  109.  
  110.     /* deallocate file structure: */
  111.     if ( (error = memFree(file)) != OK )
  112.         PASSERROR(ID_fileClose)
  113.  
  114.     return OK;
  115. }
  116.  
  117.  
  118.  
  119.  
  120. /****************************************************************************\
  121. *
  122. * Function:     int fileGetSize(fileHandle file, long *fileSize);
  123. *
  124. * Description:  Get the size of a file
  125. *
  126. * Input:        fileHandle file         handle of an open file
  127. *               ulong *fileSize         pointer to file size
  128. *
  129. * Returns:      MIDAS error code.
  130. *               File size is stored in *fileSize.
  131. *
  132. \****************************************************************************/
  133.  
  134. int CALLING fileGetSize(fileHandle file, long *fileSize)
  135. {
  136.     int         error;
  137.  
  138.     /* get file size to *fileSize: */
  139.     if ( (error = rfGetSize(file->rf, fileSize)) != OK )
  140.         PASSERROR(ID_fileGetSize)
  141.  
  142.     return OK;
  143. }
  144.  
  145.  
  146.  
  147.  
  148. /****************************************************************************\
  149. *
  150. * Function:     int fileRead(fileHandle file, void *buffer, ulong numBytes);
  151. *
  152. * Description:  Reads binary data from a file
  153. *
  154. * Input:        fileHandle file         file handle
  155. *               void *buffer            reading buffer
  156. *               ulong numBytes          number of bytes to read
  157. *
  158. * Returns:      MIDAS error code.
  159. *               Read data is stored in *buffer, which must be large enough
  160. *               for it.
  161. *
  162. \****************************************************************************/
  163.  
  164. int CALLING fileRead(fileHandle file, void *buffer, ulong numBytes)
  165. {
  166.     int         error;
  167.  
  168.     /* read data from file: */
  169.     if ( (error = rfRead(file->rf, buffer, numBytes)) != OK )
  170.         PASSERROR(ID_fileRead)
  171.  
  172.     return OK;
  173. }
  174.  
  175.  
  176.  
  177.  
  178. /****************************************************************************\
  179. *
  180. * Function:     int fileWrite(fileHandle file, void *buffer, ulong numBytes);
  181. *
  182. * Description:  Writes binary data to a file
  183. *
  184. * Input:        fileHandle file         file handle
  185. *               void *buffer            pointer to data to be written
  186. *               ulong numBytes          number of bytes to write
  187. *
  188. * Returns:      MIDAS error code
  189. *
  190. \****************************************************************************/
  191.  
  192. int CALLING fileWrite(fileHandle file, void *buffer, ulong numBytes)
  193. {
  194.     int         error;
  195.  
  196.     /* write data to file: */
  197.     if ( (error = rfWrite(file->rf, buffer, numBytes)) != OK )
  198.         PASSERROR(ID_fileWrite)
  199.  
  200.     return OK;
  201. }
  202.  
  203.  
  204.  
  205.  
  206. /****************************************************************************\
  207. *
  208. * Function:     int fileSeek(fileHandle file, long newPosition, int seekMode);
  209. *
  210. * Description:  Seeks to a new position in file. Subsequent reads and writes
  211. *               go to the new position.
  212. *
  213. * Input:        fileHandle file         file handle
  214. *               long newPosition        new file position
  215. *               int seekMode            file seek mode, see enum rfSeekMode
  216. *
  217. * Returns:      MIDAS error code
  218. *
  219. \****************************************************************************/
  220.  
  221. int CALLING fileSeek(fileHandle file, long newPosition, int seekMode)
  222. {
  223.     int         error;
  224.     int         rfMode;
  225.  
  226.     switch ( seekMode )
  227.     {
  228.         case fileSeekAbsolute:      /* seek to an absolute position */
  229.             rfMode = rfSeekAbsolute;
  230.             break;
  231.  
  232.         case fileSeekRelative:      /* seek relative to current position */
  233.             rfMode = rfSeekRelative;
  234.             break;
  235.  
  236.         case fileSeekEnd:           /* seek from end of file */
  237.             rfMode = rfSeekEnd;
  238.             break;
  239.  
  240.         default:
  241.             ERROR(ID_fileSeek, errInvalidArguments);
  242.             return errInvalidArguments;
  243.     }
  244.  
  245.     /* seek to new file position: */
  246.     if ( (error = rfSeek(file->rf, newPosition, rfMode)) != OK )
  247.         PASSERROR(ID_fileSeek)
  248.  
  249.     return OK;
  250. }
  251.  
  252.  
  253.  
  254.  
  255. /****************************************************************************\
  256. *
  257. * Function:     int fileGetPosition(fileHandle file, long *position);
  258. *
  259. * Description:  Reads the current position in a file
  260. *
  261. * Input:        fileHandle file         file handle
  262. *               long *position          pointer to file position
  263. *
  264. * Returns:      MIDAS error code.
  265. *               Current file position is stored in *position.
  266. *
  267. \****************************************************************************/
  268.  
  269. int CALLING fileGetPosition(fileHandle file, long *position)
  270. {
  271.     int         error;
  272.  
  273.     /* get current file position to *position: */
  274.     if ( (error = rfGetPosition(file->rf, position)) != OK )
  275.         PASSERROR(ID_fileGetPosition)
  276.  
  277.     return OK;
  278. }
  279.