home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / pslib / readfile.c < prev    next >
C/C++ Source or Header  |  1998-06-08  |  3KB  |  126 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13. /*
  14.  * $Source: f:/miner/source/pslib/rcs/readfile.c $
  15.  * $Revision: 1.8 $
  16.  * $Author: yuan $
  17.  * $Date: 1993/10/22 17:50:18 $
  18.  *
  19.  * ReadFileRaw, WriteFile, AppendFile, ReadFileBuf
  20.  * File and buffer i/o routines.
  21.  *
  22.  */
  23.  
  24. #pragma off (unreferenced)
  25. static char rcsid[] = "$Id: readfile.c 1.8 1993/10/22 17:50:18 yuan Exp $";
  26. #pragma on (unreferenced)
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <fcntl.h>
  31. #include <io.h>
  32. #include <sys\types.h>
  33. #include <sys\stat.h>
  34. #include <errno.h>
  35. #include <string.h>
  36.  
  37. #include "readfile.h"
  38. #include "error.h"
  39. #include "mem.h"
  40. //#include "mem2.h"
  41.  
  42. #define PACIFIER_COUNT 2047
  43.  
  44. void *ReadFile( char *filename, int *length )
  45. {
  46.     void *FileData;
  47.     int handle;
  48.  
  49.     handle = open( filename, O_RDONLY | O_BINARY );
  50.     if (handle == -1 )
  51.         Error("File %s, %s ",filename,strerror(errno)); 
  52.  
  53.     *length = filelength( handle );
  54.  
  55.     //FileData = (void * )malloc( *length );
  56.     MALLOC( FileData, ubyte, *length );
  57.  
  58.     if (FileData == NULL )  {
  59.         close( handle );
  60.         Error("File %s, cannot malloc memory",filename);
  61.    }
  62.  
  63.     if (read( handle, FileData, *length ) != *length )    {
  64.         free( FileData );
  65.         close( handle );
  66.       Error("File %s, %s ",filename,strerror(errno)); 
  67.     }
  68.     close( handle );
  69.  
  70.     return FileData;
  71.  
  72. }
  73.  
  74. int AppendFile( char *filename, void *data, int length )
  75. {
  76.     int handle;
  77.  
  78.     handle = open( filename, O_WRONLY | O_CREAT | O_APPEND | O_BINARY , S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );
  79.     if (handle == -1 ) {
  80.         return ERROR_OPENING_FILE;
  81.     }
  82.     if (write( handle, data, length )!=length)  {
  83.         close( handle );
  84.         return ERROR_WRITING_FILE;
  85.     }
  86.  
  87.     close( handle );
  88.     return 0;
  89. }
  90.  
  91. int WriteFile( char *filename, void *data, int length )
  92. {
  93.     int handle;
  94.  
  95.     handle = open( filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );
  96.     if (handle == -1 ) {
  97.         return ERROR_OPENING_FILE;
  98.     }
  99.     if (write( handle, data, length )!=length)  {
  100.         close( handle );
  101.         return ERROR_WRITING_FILE;
  102.     }
  103.  
  104.     close( handle );
  105.     return 0;
  106. }
  107.  
  108. int ReadFileBuf( char *filename, byte *buf, int bufsize )
  109. {
  110.     int length;
  111.     int handle;
  112.  
  113.     handle = open( filename, O_RDONLY | O_BINARY );
  114.     if (handle == -1 ) {
  115.         return ERROR_OPENING_FILE;
  116.     }
  117.  
  118.     if (length = read( handle, buf, bufsize ) != bufsize )    {
  119.         close( handle );
  120.         return ERROR_READING_DATA;
  121.     }
  122.     close( handle );
  123.  
  124.     return length;
  125. }
  126.