CnC_Remastered_Collection

Command and Conquer: Red Alert
Log | Files | Refs | README | LICENSE

RAWFILE.H (15517B)


      1 //
      2 // Copyright 2020 Electronic Arts Inc.
      3 //
      4 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free 
      5 // software: you can redistribute it and/or modify it under the terms of 
      6 // the GNU General Public License as published by the Free Software Foundation, 
      7 // either version 3 of the License, or (at your option) any later version.
      8 
      9 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed 
     10 // in the hope that it will be useful, but with permitted additional restrictions 
     11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 
     12 // distributed with this program. You should have received a copy of the 
     13 // GNU General Public License along with permitted additional restrictions 
     14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
     15 
     16 /* $Header: /CounterStrike/RAWFILE.H 1     3/03/97 10:25a Joe_bostic $ */
     17 /***********************************************************************************************
     18  ***              C O N F I D E N T I A L  ---  W E S T W O O D  S T U D I O S               ***
     19  ***********************************************************************************************
     20  *                                                                                             *
     21  *                 Project Name : Westwood Library                                             *
     22  *                                                                                             *
     23  *                    File Name : RAWFILE.H                                                    *
     24  *                                                                                             *
     25  *                   Programmer : Joe L. Bostic                                                *
     26  *                                                                                             *
     27  *                   Start Date : August 8, 1994                                               *
     28  *                                                                                             *
     29  *                  Last Update : October 18, 1994   [JLB]                                     *
     30  *                                                                                             *
     31  *---------------------------------------------------------------------------------------------*
     32  * Functions:                                                                                  *
     33  *   RawFileClass::File_Name -- Returns with the filename associate with the file object.      *
     34  *   RawFileClass::RawFileClass -- Default constructor for a file object.                      *
     35  *   RawFileClass::~RawFileClass -- Default deconstructor for a file object.                   *
     36  *   RawFileClass::Is_Open -- Checks to see if the file is open or not.                        *
     37  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     38 
     39 #ifndef RAWFILE_H
     40 #define RAWFILE_H
     41 
     42 #include	<limits.h>
     43 #include	<errno.h>
     44 #include	<stddef.h>
     45 #include	<stdlib.h>
     46 
     47 #ifdef WIN32
     48 #include	<windows.h>
     49 
     50 #define	NULL_HANDLE		INVALID_HANDLE_VALUE
     51 #define	HANDLE_TYPE		HANDLE
     52 #else
     53 #define	NULL_HANDLE		-1
     54 #define	HANDLE_TYPE		int
     55 #endif
     56 
     57 #include	"wwfile.h"
     58 
     59 #ifdef NEVER
     60 	/*
     61 	**	This is a duplicate of the error numbers. The error handler for the RawFileClass handles
     62 	**	these errors. If the error routine is overridden and additional errors are defined, then
     63 	**	use numbers starting with 100. Note that these errors here are listed in numerical order.
     64 	**	These errors are defined in the standard header file "ERRNO.H".
     65 	*/
     66 	EZERO,				// Non-error.
     67 	EINVFNC,				// Invalid function number.
     68 	ENOFILE,				// File not found.
     69 	ENOENT=ENOFILE,	// No such file or directory.
     70 	ENOPATH,				// Path not found.
     71 	EMFILE,				// Too many open files.
     72 	EACCES,				// Permission denied.
     73 	EBADF,				// Bad file number.
     74 	ECONTR,				// Memory blocks destroyed.
     75 	ENOMEM,				// Not enough core memory.
     76 	EINVMEM,				// Invalid memory block address.
     77 	EINVENV,				// Invalid environment.
     78 	EINVFMT,				// Invalid format.
     79 	EINVACC,				// Invalid access code.
     80 	EINVDAT,				// Invalid data.
     81 	EFAULT,				// Unknown error.
     82 	EINVDRV,				// Invalid drive specified.
     83 	ENODEV=EINVDRV,	// No such device.
     84 	ECURDIR,				// Attempt to remove CurDir.
     85 	ENOTSAM,				// Not same device.
     86 	ENMFILE,				// No more files.
     87 	EINVAL,				// Invalid argument.
     88 	E2BIG,				// Argument list too long.
     89 	ENOEXEC,				// exec format error.
     90 	EXDEV,				// Cross-device link.
     91 	ENFILE,				// Too many open files.
     92 	ECHILD,				// No child process.
     93 	ENOTTY,				// not used
     94 	ETXTBSY,				// not used
     95 	EFBIG,				// not used
     96 	ENOSPC,				// No space left on device.
     97 	ESPIPE,				// Illegal seek.
     98 	EROFS,				// Read-only file system.
     99 	EMLINK,				// not used
    100 	EPIPE,				// Broken pipe.
    101 	EDOM,					// Math argument.
    102 	ERANGE,				// Result too large.
    103 	EEXIST,				// File already exists.
    104 	EDEADLOCK,			// Locking violation.
    105 	EPERM,				// Operation not permitted.
    106 	ESRCH,				// not used
    107 	EINTR,				// Interrupted function call.
    108 	EIO,					// Input/output error.
    109 	ENXIO,				// No such device or address.
    110 	EAGAIN,				// Resource temporarily unavailable.
    111 	ENOTBLK,				// not used
    112 	EBUSY,				// Resource busy.
    113 	ENOTDIR,				// not used
    114 	EISDIR,				// not used
    115 	EUCLEAN,				// not used
    116 #endif
    117 
    118 #ifndef WWERROR
    119 #define WWERROR	-1
    120 #endif
    121 
    122 /*
    123 **	This is the definition of the raw file class. It is derived from the abstract base FileClass
    124 **	and handles the interface to the low level DOS routines. This is the first class in the
    125 **	chain of derived file classes that actually performs a useful function. With this class,
    126 **	I/O is possible. More sophisticated features, such as packed files, CD-ROM support,
    127 **	file caching, and XMS/EMS memory support, are handled by derived classes.
    128 **
    129 **	Of particular importance is the need to override the error routine if more sophisticated
    130 **	error handling is required. This is more than likely if greater functionality is derived
    131 **	from this base class.
    132 */
    133 class RawFileClass : public FileClass
    134 {
    135 	public:
    136 
    137 		/*
    138 		**	This is a record of the access rights used to open the file. These rights are
    139 		**	used if the file object is duplicated.
    140 		*/
    141 		int Rights;
    142 
    143 		RawFileClass(char const *filename);
    144 		RawFileClass(void);
    145 		RawFileClass (RawFileClass const & f);
    146 		RawFileClass & operator = (RawFileClass const & f);
    147 		virtual ~RawFileClass(void);
    148 
    149 		virtual char const * File_Name(void) const;
    150 		virtual char const * Set_Name(char const *filename);
    151 		virtual int Create(void);
    152 		virtual int Delete(void);
    153 		virtual int Is_Available(int forced=false);
    154 		virtual int Is_Open(void) const;
    155 		virtual int Open(char const *filename, int rights=READ);
    156 		virtual int Open(int rights=READ);
    157 		virtual long Read(void *buffer, long size);
    158 		virtual long Seek(long pos, int dir=SEEK_CUR);
    159 		virtual long Size(void);
    160 		virtual long Write(void const *buffer, long size);
    161 		virtual void Close(void);
    162 		virtual unsigned long Get_Date_Time(void);
    163 		virtual bool Set_Date_Time(unsigned long datetime);
    164 		virtual void Error(int error, int canretry = false, char const * filename=NULL);
    165 
    166 		void Bias(int start, int length=-1);
    167 
    168 		HANDLE_TYPE Get_File_Handle(void) { return (Handle); };
    169 
    170 		/*
    171 		**	These bias values enable a sub-portion of a file to appear as if it
    172 		**	were the whole file. This comes in very handy for multi-part files such as
    173 		**	mixfiles.
    174 		*/
    175 		int BiasStart;
    176 		int BiasLength;
    177 
    178 	protected:
    179 
    180 		/*
    181 		**	This function returns the largest size a low level DOS read or write may
    182 		**	perform. Larger file transfers are performed in chunks of this size or less.
    183 		*/
    184 		long Transfer_Block_Size(void) {return (long)((unsigned)UINT_MAX)-16L;};
    185 
    186 		long Raw_Seek(long pos, int dir=SEEK_CUR);
    187 
    188 	private:
    189 
    190 		/*
    191 		**	This is the low level DOS handle. A -1 indicates an empty condition.
    192 		*/
    193 		HANDLE_TYPE Handle;
    194 
    195 		/*
    196 		**	This points to the filename as a NULL terminated string. It may point to either a
    197 		**	constant or an allocated string as indicated by the "Allocated" flag.
    198 		*/
    199 		const char *Filename;
    200 
    201 		//
    202 		// file date and time are in the following formats:
    203 		//
    204 		//      date   bits 0-4   day (0-31)
    205 		//             bits 5-8   month (1-12)
    206 		//             bits 9-15  year (0-119 representing 1980-2099)
    207 		//
    208 		//      time   bits 0-4   second/2 (0-29)
    209 		//             bits 5-10  minutes (0-59)
    210 		//             bits 11-15 hours (0-23)
    211 		//
    212 		unsigned short Date;
    213 		unsigned short Time;
    214 
    215 		/*
    216 		**	Filenames that were assigned as part of the construction process
    217 		**	are not allocated. It is assumed that the filename string is a
    218 		**	constant in that case and thus making duplication unnecessary.
    219 		**	This value will be non-zero if the filename has be allocated
    220 		**	(using strdup()).
    221 		*/
    222 		unsigned Allocated:1;
    223 };
    224 
    225 
    226 /***********************************************************************************************
    227  * RawFileClass::File_Name -- Returns with the filename associate with the file object.        *
    228  *                                                                                             *
    229  *    Use this routine to determine what filename is associated with this file object. If no   *
    230  *    filename has yet been assigned, then this routing will return NULL.                      *
    231  *                                                                                             *
    232  * INPUT:   none                                                                               *
    233  *                                                                                             *
    234  * OUTPUT:  Returns with a pointer to the file name associated with this file object or NULL   *
    235  *          if one doesn't exist.                                                              *
    236  *                                                                                             *
    237  * WARNINGS:   none                                                                            *
    238  *                                                                                             *
    239  * HISTORY:                                                                                    *
    240  *   10/18/1994 JLB : Created.                                                                 *
    241  *=============================================================================================*/
    242 inline char const * RawFileClass::File_Name(void) const
    243 {
    244 	return(Filename);
    245 }
    246 
    247 
    248 /***********************************************************************************************
    249  * RawFileClass::RawFileClass -- Default constructor for a file object.                        *
    250  *                                                                                             *
    251  *    This constructs a null file object. A null file object has no file handle or filename    *
    252  *    associated with it. In order to use a file object created in this fashion it must be     *
    253  *    assigned a name and then opened.                                                         *
    254  *                                                                                             *
    255  * INPUT:   none                                                                               *
    256  *                                                                                             *
    257  * OUTPUT:  none                                                                               *
    258  *                                                                                             *
    259  * WARNINGS:   none                                                                            *
    260  *                                                                                             *
    261  * HISTORY:                                                                                    *
    262  *   10/18/1994 JLB : Created.                                                                 *
    263  *=============================================================================================*/
    264 inline RawFileClass::RawFileClass(void) :
    265 	Rights(READ),
    266 	BiasStart(0),
    267 	BiasLength(-1),
    268 	#ifdef WIN32
    269 	Handle(INVALID_HANDLE_VALUE),
    270 	#else
    271 	Handle(-1),
    272 	#endif
    273 	Filename(0),
    274 	Date(0),
    275 	Time(0),
    276 	Allocated(false)
    277 {
    278 }
    279 
    280 
    281 /***********************************************************************************************
    282  * RawFileClass::~RawFileClass -- Default deconstructor for a file object.                     *
    283  *                                                                                             *
    284  *    This constructs a null file object. A null file object has no file handle or filename    *
    285  *    associated with it. In order to use a file object created in this fashion it must be     *
    286  *    assigned a name and then opened.                                                         *
    287  *                                                                                             *
    288  * INPUT:   none                                                                               *
    289  *                                                                                             *
    290  * OUTPUT:  none                                                                               *
    291  *                                                                                             *
    292  * WARNINGS:   none                                                                            *
    293  *                                                                                             *
    294  * HISTORY:                                                                                    *
    295  *   10/18/1994 JLB : Created.                                                                 *
    296  *=============================================================================================*/
    297 inline RawFileClass::~RawFileClass(void)
    298 {
    299 	Close();
    300 	if (Allocated && Filename) {
    301 		free((char *)Filename);
    302 		((char *&)Filename) = 0;
    303 		Allocated = false;
    304 	}
    305 }
    306 
    307 
    308 /***********************************************************************************************
    309  * RawFileClass::Is_Open -- Checks to see if the file is open or not.                          *
    310  *                                                                                             *
    311  *    Use this routine to determine if the file is open. It returns true if it is.             *
    312  *                                                                                             *
    313  * INPUT:   none                                                                               *
    314  *                                                                                             *
    315  * OUTPUT:  bool; Is the file open?                                                            *
    316  *                                                                                             *
    317  *                                                                                             *
    318  * WARNINGS:   none                                                                            *
    319  *                                                                                             *
    320  * HISTORY:                                                                                    *
    321  *   10/18/1994 JLB : Created.                                                                 *
    322  *=============================================================================================*/
    323 inline int RawFileClass::Is_Open(void) const
    324 {
    325 #ifdef WIN32
    326 	return(Handle != INVALID_HANDLE_VALUE);
    327 #else
    328 	return (Handle >= 0);
    329 #endif
    330 }
    331 
    332 #endif