RAWFILE.H 15 KB

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