RAWFILE.H 15 KB

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