File.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 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 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( void ) {};
  37. // Get the name of the file.
  38. virtual const char * GetName( void );
  39. // Get the full file path.
  40. virtual const char * GetFullPath( void );
  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( void );
  47. // Return a time value for reload operations.
  48. virtual ID_TIME_T Timestamp( void );
  49. // Returns offset in file.
  50. virtual int Tell( void );
  51. // Forces flush on files being writting to.
  52. virtual void ForceFlush( void );
  53. // Causes any buffered data to be written to the file.
  54. virtual void Flush( void );
  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( void );
  59. // Like fprintf.
  60. virtual int Printf( const char *fmt, ... ) id_attribute((format(printf,2,3)));
  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( const char *fmt, ... ) id_attribute((format(printf,2,3)));
  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. };
  96. class idFile_Memory : public idFile {
  97. friend class idFileSystemLocal;
  98. public:
  99. idFile_Memory( void ); // file for writing without name
  100. idFile_Memory( const char *name ); // file for writing
  101. idFile_Memory( const char *name, char *data, int length ); // file for writing
  102. idFile_Memory( const char *name, const char *data, int length ); // file for reading
  103. virtual ~idFile_Memory( void );
  104. virtual const char * GetName( void ) { return name.c_str(); }
  105. virtual const char * GetFullPath( void ) { return name.c_str(); }
  106. virtual int Read( void *buffer, int len );
  107. virtual int Write( const void *buffer, int len );
  108. virtual int Length( void );
  109. virtual ID_TIME_T Timestamp( void );
  110. virtual int Tell( void );
  111. virtual void ForceFlush( void );
  112. virtual void Flush( void );
  113. virtual int Seek( long offset, fsOrigin_t origin );
  114. // changes memory file to read only
  115. virtual void MakeReadOnly( void );
  116. // clear the file
  117. virtual void Clear( bool freeMemory = true );
  118. // set data for reading
  119. void SetData( const char *data, int length );
  120. // returns const pointer to the memory buffer
  121. const char * GetDataPtr( void ) const { return filePtr; }
  122. // set the file granularity
  123. void SetGranularity( int g ) { assert( g > 0 ); granularity = g; }
  124. private:
  125. idStr name; // name of the file
  126. int mode; // open mode
  127. int maxSize; // maximum size of file
  128. int fileSize; // size of the file
  129. int allocated; // allocated size
  130. int granularity; // file granularity
  131. char * filePtr; // buffer holding the file data
  132. char * curPtr; // current read/write pointer
  133. };
  134. class idFile_BitMsg : public idFile {
  135. friend class idFileSystemLocal;
  136. public:
  137. idFile_BitMsg( idBitMsg &msg );
  138. idFile_BitMsg( const idBitMsg &msg );
  139. virtual ~idFile_BitMsg( void );
  140. virtual const char * GetName( void ) { return name.c_str(); }
  141. virtual const char * GetFullPath( void ) { return name.c_str(); }
  142. virtual int Read( void *buffer, int len );
  143. virtual int Write( const void *buffer, int len );
  144. virtual int Length( void );
  145. virtual ID_TIME_T Timestamp( void );
  146. virtual int Tell( void );
  147. virtual void ForceFlush( void );
  148. virtual void Flush( void );
  149. virtual int Seek( long offset, fsOrigin_t origin );
  150. private:
  151. idStr name; // name of the file
  152. int mode; // open mode
  153. idBitMsg * msg;
  154. };
  155. class idFile_Permanent : public idFile {
  156. friend class idFileSystemLocal;
  157. public:
  158. idFile_Permanent( void );
  159. virtual ~idFile_Permanent( void );
  160. virtual const char * GetName( void ) { return name.c_str(); }
  161. virtual const char * GetFullPath( void ) { return fullPath.c_str(); }
  162. virtual int Read( void *buffer, int len );
  163. virtual int Write( const void *buffer, int len );
  164. virtual int Length( void );
  165. virtual ID_TIME_T Timestamp( void );
  166. virtual int Tell( void );
  167. virtual void ForceFlush( void );
  168. virtual void Flush( void );
  169. virtual int Seek( long offset, fsOrigin_t origin );
  170. // returns file pointer
  171. FILE * GetFilePtr( void ) { return o; }
  172. private:
  173. idStr name; // relative path of the file - relative path
  174. idStr fullPath; // full file path - OS path
  175. int mode; // open mode
  176. int fileSize; // size of the file
  177. FILE * o; // file handle
  178. bool handleSync; // true if written data is immediately flushed
  179. };
  180. class idFile_InZip : public idFile {
  181. friend class idFileSystemLocal;
  182. public:
  183. idFile_InZip( void );
  184. virtual ~idFile_InZip( void );
  185. virtual const char * GetName( void ) { return name.c_str(); }
  186. virtual const char * GetFullPath( void ) { return fullPath.c_str(); }
  187. virtual int Read( void *buffer, int len );
  188. virtual int Write( const void *buffer, int len );
  189. virtual int Length( void );
  190. virtual ID_TIME_T Timestamp( void );
  191. virtual int Tell( void );
  192. virtual void ForceFlush( void );
  193. virtual void Flush( void );
  194. virtual int Seek( long offset, fsOrigin_t origin );
  195. private:
  196. idStr name; // name of the file in the pak
  197. idStr fullPath; // full file path including pak file name
  198. int zipFilePos; // zip file info position in pak
  199. int fileSize; // size of the file
  200. void * z; // unzip info
  201. };
  202. #endif /* !__FILE_H__ */