File.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __FILE_H__
  21. #define __FILE_H__
  22. /*
  23. ==============================================================
  24. File Streams.
  25. ==============================================================
  26. */
  27. // mode parm for Seek
  28. typedef enum {
  29. FS_SEEK_CUR,
  30. FS_SEEK_END,
  31. FS_SEEK_SET
  32. } fsOrigin_t;
  33. class idFileSystemLocal;
  34. class idFile {
  35. public:
  36. virtual ~idFile() {};
  37. // Get the name of the file.
  38. virtual const char * GetName() const;
  39. // Get the full file path.
  40. virtual const char * GetFullPath() const;
  41. // Read data from the file to the buffer.
  42. virtual int Read( void *buffer, int len );
  43. // Write data from the buffer to the file.
  44. virtual int Write( const void *buffer, int len );
  45. // Returns the length of the file.
  46. virtual int Length() const;
  47. // Return a time value for reload operations.
  48. virtual ID_TIME_T Timestamp() const;
  49. // Returns offset in file.
  50. virtual int Tell() const;
  51. // Forces flush on files being writting to.
  52. virtual void ForceFlush();
  53. // Causes any buffered data to be written to the file.
  54. virtual void Flush();
  55. // Seek on a file.
  56. virtual int Seek( long offset, fsOrigin_t origin );
  57. // Go back to the beginning of the file.
  58. virtual void Rewind();
  59. // Like fprintf.
  60. virtual int Printf( VERIFY_FORMAT_STRING const char *fmt, ... );
  61. // Like fprintf but with argument pointer
  62. virtual int VPrintf( const char *fmt, va_list arg );
  63. // Write a string with high precision floating point numbers to the file.
  64. virtual int WriteFloatString( VERIFY_FORMAT_STRING const char *fmt, ... );
  65. // Endian portable alternatives to Read(...)
  66. virtual int ReadInt( int &value );
  67. virtual int ReadUnsignedInt( unsigned int &value );
  68. virtual int ReadShort( short &value );
  69. virtual int ReadUnsignedShort( unsigned short &value );
  70. virtual int ReadChar( char &value );
  71. virtual int ReadUnsignedChar( unsigned char &value );
  72. virtual int ReadFloat( float &value );
  73. virtual int ReadBool( bool &value );
  74. virtual int ReadString( idStr &string );
  75. virtual int ReadVec2( idVec2 &vec );
  76. virtual int ReadVec3( idVec3 &vec );
  77. virtual int ReadVec4( idVec4 &vec );
  78. virtual int ReadVec6( idVec6 &vec );
  79. virtual int ReadMat3( idMat3 &mat );
  80. // Endian portable alternatives to Write(...)
  81. virtual int WriteInt( const int value );
  82. virtual int WriteUnsignedInt( const unsigned int value );
  83. virtual int WriteShort( const short value );
  84. virtual int WriteUnsignedShort( unsigned short value );
  85. virtual int WriteChar( const char value );
  86. virtual int WriteUnsignedChar( const unsigned char value );
  87. virtual int WriteFloat( const float value );
  88. virtual int WriteBool( const bool value );
  89. virtual int WriteString( const char *string );
  90. virtual int WriteVec2( const idVec2 &vec );
  91. virtual int WriteVec3( const idVec3 &vec );
  92. virtual int WriteVec4( const idVec4 &vec );
  93. virtual int WriteVec6( const idVec6 &vec );
  94. virtual int WriteMat3( const idMat3 &mat );
  95. template<class type> ID_INLINE size_t ReadBig( type &c ) {
  96. size_t r = Read( &c, sizeof( c ) );
  97. idSwap::Big( c );
  98. return r;
  99. }
  100. template<class type> ID_INLINE size_t ReadBigArray( type *c, int count ) {
  101. size_t r = Read( c, sizeof( c[0] ) * count );
  102. idSwap::BigArray( c, count );
  103. return r;
  104. }
  105. template<class type> ID_INLINE size_t WriteBig( const type &c ) {
  106. type b = c;
  107. idSwap::Big( b );
  108. return Write( &b, sizeof( b ) );
  109. }
  110. template<class type> ID_INLINE size_t WriteBigArray( const type *c, int count ) {
  111. size_t r = 0;
  112. for ( int i = 0; i < count; i++ ) {
  113. r += WriteBig( c[i] );
  114. }
  115. return r;
  116. }
  117. };
  118. /*
  119. ================================================
  120. idFile_Memory
  121. ================================================
  122. */
  123. class idFile_Memory : public idFile {
  124. friend class idFileSystemLocal;
  125. public:
  126. idFile_Memory(); // file for writing without name
  127. idFile_Memory( const char *name ); // file for writing
  128. idFile_Memory( const char *name, char *data, int length ); // file for writing
  129. idFile_Memory( const char *name, const char *data, int length ); // file for reading
  130. virtual ~idFile_Memory();
  131. virtual const char * GetName() const { return name.c_str(); }
  132. virtual const char * GetFullPath() const { return name.c_str(); }
  133. virtual int Read( void *buffer, int len );
  134. virtual int Write( const void *buffer, int len );
  135. virtual int Length() const;
  136. virtual void SetLength( size_t len );
  137. virtual ID_TIME_T Timestamp() const;
  138. virtual int Tell() const;
  139. virtual void ForceFlush();
  140. virtual void Flush();
  141. virtual int Seek( long offset, fsOrigin_t origin );
  142. // Set the given length and don't allow the file to grow.
  143. void SetMaxLength( size_t len );
  144. // changes memory file to read only
  145. void MakeReadOnly();
  146. // Change the file to be writable
  147. void MakeWritable();
  148. // clear the file
  149. virtual void Clear( bool freeMemory = true );
  150. // set data for reading
  151. void SetData( const char *data, int length );
  152. // returns const pointer to the memory buffer
  153. const char * GetDataPtr() const { return filePtr; }
  154. // returns pointer to the memory buffer
  155. char * GetDataPtr() { return filePtr; }
  156. // set the file granularity
  157. void SetGranularity( int g ) { assert( g > 0 ); granularity = g; }
  158. void PreAllocate( size_t len );
  159. // Doesn't change how much is allocated, but allows you to set the size of the file to smaller than it should be.
  160. // Useful for stripping off a checksum at the end of the file
  161. void TruncateData( size_t len );
  162. void TakeDataOwnership();
  163. size_t GetMaxLength() { return maxSize; }
  164. size_t GetAllocated() { return allocated; }
  165. protected:
  166. idStr name; // name of the file
  167. private:
  168. int mode; // open mode
  169. size_t maxSize; // maximum size of file
  170. size_t fileSize; // size of the file
  171. size_t allocated; // allocated size
  172. int granularity; // file granularity
  173. char * filePtr; // buffer holding the file data
  174. char * curPtr; // current read/write pointer
  175. };
  176. class idFile_BitMsg : public idFile {
  177. friend class idFileSystemLocal;
  178. public:
  179. idFile_BitMsg( idBitMsg &msg );
  180. idFile_BitMsg( const idBitMsg &msg );
  181. virtual ~idFile_BitMsg();
  182. virtual const char * GetName() const { return name.c_str(); }
  183. virtual const char * GetFullPath() const { return name.c_str(); }
  184. virtual int Read( void *buffer, int len );
  185. virtual int Write( const void *buffer, int len );
  186. virtual int Length() const;
  187. virtual ID_TIME_T Timestamp() const;
  188. virtual int Tell() const;
  189. virtual void ForceFlush();
  190. virtual void Flush();
  191. virtual int Seek( long offset, fsOrigin_t origin );
  192. private:
  193. idStr name; // name of the file
  194. int mode; // open mode
  195. idBitMsg * msg;
  196. };
  197. class idFile_Permanent : public idFile {
  198. friend class idFileSystemLocal;
  199. public:
  200. idFile_Permanent();
  201. virtual ~idFile_Permanent();
  202. virtual const char * GetName() const { return name.c_str(); }
  203. virtual const char * GetFullPath() const { return fullPath.c_str(); }
  204. virtual int Read( void *buffer, int len );
  205. virtual int Write( const void *buffer, int len );
  206. virtual int Length() const;
  207. virtual ID_TIME_T Timestamp() const;
  208. virtual int Tell() const;
  209. virtual void ForceFlush();
  210. virtual void Flush();
  211. virtual int Seek( long offset, fsOrigin_t origin );
  212. // returns file pointer
  213. idFileHandle GetFilePtr() { return o; }
  214. private:
  215. idStr name; // relative path of the file - relative path
  216. idStr fullPath; // full file path - OS path
  217. int mode; // open mode
  218. int fileSize; // size of the file
  219. idFileHandle o; // file handle
  220. bool handleSync; // true if written data is immediately flushed
  221. };
  222. class idFile_Cached : public idFile_Permanent {
  223. friend class idFileSystemLocal;
  224. public:
  225. idFile_Cached();
  226. virtual ~idFile_Cached();
  227. void CacheData( uint64 offset, uint64 length );
  228. virtual int Read( void *buffer, int len );
  229. virtual int Tell() const;
  230. virtual int Seek( long offset, fsOrigin_t origin );
  231. private:
  232. uint64 internalFilePos;
  233. uint64 bufferedStartOffset;
  234. uint64 bufferedEndOffset;
  235. byte * buffered;
  236. };
  237. class idFile_InZip : public idFile {
  238. friend class idFileSystemLocal;
  239. public:
  240. idFile_InZip();
  241. virtual ~idFile_InZip();
  242. virtual const char * GetName() const { return name.c_str(); }
  243. virtual const char * GetFullPath() const { return fullPath.c_str(); }
  244. virtual int Read( void *buffer, int len );
  245. virtual int Write( const void *buffer, int len );
  246. virtual int Length() const;
  247. virtual ID_TIME_T Timestamp() const;
  248. virtual int Tell() const;
  249. virtual void ForceFlush();
  250. virtual void Flush();
  251. virtual int Seek( long offset, fsOrigin_t origin );
  252. private:
  253. idStr name; // name of the file in the pak
  254. idStr fullPath; // full file path including pak file name
  255. int zipFilePos; // zip file info position in pak
  256. int fileSize; // size of the file
  257. void * z; // unzip info
  258. };
  259. #if 1
  260. class idFile_InnerResource : public idFile {
  261. friend class idFileSystemLocal;
  262. public:
  263. idFile_InnerResource( const char *_name, idFile *rezFile, int _offset, int _len );
  264. virtual ~idFile_InnerResource();
  265. virtual const char * GetName() const { return name.c_str(); }
  266. virtual const char * GetFullPath() const { return name.c_str(); }
  267. virtual int Read( void *buffer, int len );
  268. virtual int Write( const void *buffer, int len ) { assert( false ); return 0; }
  269. virtual int Length() const { return length; }
  270. virtual ID_TIME_T Timestamp() const { return 0; }
  271. virtual int Tell() const;
  272. virtual int Seek( long offset, fsOrigin_t origin );
  273. void SetResourceBuffer( byte * buf ) {
  274. resourceBuffer = buf;
  275. internalFilePos = 0;
  276. }
  277. private:
  278. idStr name; // name of the file in the pak
  279. int offset; // offset in the resource file
  280. int length; // size
  281. idFile * resourceFile; // actual file
  282. int internalFilePos; // seek offset
  283. byte * resourceBuffer; // if using the temp save memory
  284. };
  285. #endif
  286. /*
  287. ================================================
  288. idFileLocal is a FileStream wrapper that automatically closes a file when the
  289. class variable goes out of scope. Note that the pointer passed in to the constructor can be for
  290. any type of File Stream that ultimately inherits from idFile, and that this is not actually a
  291. SmartPointer, as it does not keep a reference count.
  292. ================================================
  293. */
  294. class idFileLocal {
  295. public:
  296. // Constructor that accepts and stores the file pointer.
  297. idFileLocal( idFile *_file ) : file( _file ) {
  298. }
  299. // Destructor that will destroy (close) the file when this wrapper class goes out of scope.
  300. ~idFileLocal();
  301. // Cast to a file pointer.
  302. operator idFile * () const {
  303. return file;
  304. }
  305. // Member access operator for treating the wrapper as if it were the file, itself.
  306. idFile * operator -> () const {
  307. return file;
  308. }
  309. protected:
  310. idFile *file; // The managed file pointer.
  311. };
  312. #endif /* !__FILE_H__ */