StreamingArchiveFile.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. ** Command & Conquer Generals(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. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. //----------------------------------------------------------------------------
  24. //
  25. // Westwood Studios Pacific.
  26. //
  27. // Confidential Information
  28. // Copyright(C) 2001 - All Rights Reserved
  29. //
  30. //----------------------------------------------------------------------------
  31. //
  32. // Project: RTS
  33. //
  34. // Module: IO
  35. //
  36. // File name: StreamingArchiveFile.cpp
  37. //
  38. // Created: 12/06/02
  39. //
  40. //----------------------------------------------------------------------------
  41. //----------------------------------------------------------------------------
  42. // Includes
  43. //----------------------------------------------------------------------------
  44. #include "PreRTS.h"
  45. #include <stdio.h>
  46. #include <fcntl.h>
  47. #include <io.h>
  48. #include <string.h>
  49. #include <sys/stat.h>
  50. #include "Common/AsciiString.h"
  51. #include "Common/FileSystem.h"
  52. #include "Common/StreamingArchiveFile.h"
  53. #include "Common/PerfTimer.h"
  54. //----------------------------------------------------------------------------
  55. // Externals
  56. //----------------------------------------------------------------------------
  57. //----------------------------------------------------------------------------
  58. // Defines
  59. //----------------------------------------------------------------------------
  60. //----------------------------------------------------------------------------
  61. // Private Types
  62. //----------------------------------------------------------------------------
  63. //----------------------------------------------------------------------------
  64. // Private Data
  65. //----------------------------------------------------------------------------
  66. //----------------------------------------------------------------------------
  67. // Public Data
  68. //----------------------------------------------------------------------------
  69. //----------------------------------------------------------------------------
  70. // Private Prototypes
  71. //----------------------------------------------------------------------------
  72. //----------------------------------------------------------------------------
  73. // Private Functions
  74. //----------------------------------------------------------------------------
  75. //=================================================================
  76. // StreamingArchiveFile::StreamingArchiveFile
  77. //=================================================================
  78. StreamingArchiveFile::StreamingArchiveFile()
  79. : m_file(NULL),
  80. m_startingPos(0),
  81. m_size(0),
  82. m_curPos(0)
  83. {
  84. }
  85. //----------------------------------------------------------------------------
  86. // Public Functions
  87. //----------------------------------------------------------------------------
  88. //=================================================================
  89. // StreamingArchiveFile::~StreamingArchiveFile
  90. //=================================================================
  91. StreamingArchiveFile::~StreamingArchiveFile()
  92. {
  93. File::close();
  94. }
  95. //=================================================================
  96. // StreamingArchiveFile::open
  97. //=================================================================
  98. /**
  99. * This function opens a file using the standard C open() call. Access flags
  100. * are mapped to the appropriate open flags. Returns true if file was opened
  101. * successfully.
  102. */
  103. //=================================================================
  104. //DECLARE_PERF_TIMER(StreamingArchiveFile)
  105. Bool StreamingArchiveFile::open( const Char *filename, Int access )
  106. {
  107. //USE_PERF_TIMER(StreamingArchiveFile)
  108. File *file = TheFileSystem->openFile( filename, access );
  109. if ( file == NULL )
  110. {
  111. return FALSE;
  112. }
  113. return (open( file ) != NULL);
  114. }
  115. //============================================================================
  116. // StreamingArchiveFile::open
  117. //============================================================================
  118. Bool StreamingArchiveFile::open( File *file )
  119. {
  120. return TRUE;
  121. }
  122. //============================================================================
  123. // StreamingArchiveFile::openFromArchive
  124. //============================================================================
  125. Bool StreamingArchiveFile::openFromArchive(File *archiveFile, const AsciiString& filename, Int offset, Int size)
  126. {
  127. //USE_PERF_TIMER(StreamingArchiveFile)
  128. if (archiveFile == NULL) {
  129. return FALSE;
  130. }
  131. if (File::open(filename.str(), File::READ | File::BINARY | File::STREAMING) == FALSE) {
  132. return FALSE;
  133. }
  134. m_file = archiveFile;
  135. m_startingPos = offset;
  136. m_size = size;
  137. m_curPos = 0;
  138. if (m_file->seek(offset, File::START) != offset) {
  139. return FALSE;
  140. }
  141. if (m_file->seek(size) != m_startingPos + size) {
  142. return FALSE;
  143. }
  144. // We know this will succeed.
  145. m_file->seek(offset, File::START);
  146. m_nameStr = filename;
  147. return TRUE;
  148. }
  149. //=================================================================
  150. // StreamingArchiveFile::close
  151. //=================================================================
  152. /**
  153. * Closes the current file if it is open.
  154. * Must call StreamingArchiveFile::close() for each successful StreamingArchiveFile::open() call.
  155. */
  156. //=================================================================
  157. void StreamingArchiveFile::close( void )
  158. {
  159. File::close();
  160. }
  161. //=================================================================
  162. // StreamingArchiveFile::read
  163. //=================================================================
  164. // if buffer is null, just advance the current position by 'bytes'
  165. Int StreamingArchiveFile::read( void *buffer, Int bytes )
  166. {
  167. if (!m_file) {
  168. return 0;
  169. }
  170. // There shouldn't be a way that this can fail, because we've already verified that the file
  171. // contains at least this many bits.
  172. m_file->seek(m_startingPos + m_curPos, File::START);
  173. if (bytes + m_curPos > m_size)
  174. bytes = m_size - m_curPos;
  175. Int bytesRead = m_file->read(buffer, bytes);
  176. m_curPos += bytesRead;
  177. return bytesRead;
  178. }
  179. //=================================================================
  180. // StreamingArchiveFile::write
  181. //=================================================================
  182. Int StreamingArchiveFile::write( const void *buffer, Int bytes )
  183. {
  184. DEBUG_CRASH(("Cannot write to streaming files.\n"));
  185. return -1;
  186. }
  187. //=================================================================
  188. // StreamingArchiveFile::seek
  189. //=================================================================
  190. Int StreamingArchiveFile::seek( Int pos, seekMode mode)
  191. {
  192. Int newPos;
  193. switch( mode )
  194. {
  195. case START:
  196. newPos = pos;
  197. break;
  198. case CURRENT:
  199. newPos = m_curPos + pos;
  200. break;
  201. case END:
  202. DEBUG_ASSERTCRASH(pos <= 0, ("StreamingArchiveFile::seek - position should be <= 0 for a seek starting from the end."));
  203. newPos = m_size + pos;
  204. break;
  205. default:
  206. // bad seek mode
  207. return -1;
  208. }
  209. if ( newPos < 0 )
  210. {
  211. newPos = 0;
  212. }
  213. else if ( newPos > m_size )
  214. {
  215. newPos = m_size;
  216. }
  217. m_curPos = newPos;
  218. return m_curPos;
  219. }