WSYS_RAMFile.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. // Westwood Studios Pacific.
  21. //
  22. // Confidential Information
  23. // Copyright(C) 2001 - All Rights Reserved
  24. //
  25. //----------------------------------------------------------------------------
  26. //
  27. // Project: WSYS Library
  28. //
  29. // Module: IO
  30. //
  31. // File name: WSYS_RAMFile.cpp
  32. //
  33. // Created: 11/08/01
  34. //
  35. //----------------------------------------------------------------------------
  36. //----------------------------------------------------------------------------
  37. // Includes
  38. //----------------------------------------------------------------------------
  39. #include <stdio.h>
  40. #include <fcntl.h>
  41. #include <io.h>
  42. #include <string.h>
  43. #include <sys/stat.h>
  44. #include "WSYS_FileSystem.h"
  45. #include "WSYS_RAMFile.h"
  46. //----------------------------------------------------------------------------
  47. // Externals
  48. //----------------------------------------------------------------------------
  49. //----------------------------------------------------------------------------
  50. // Defines
  51. //----------------------------------------------------------------------------
  52. //----------------------------------------------------------------------------
  53. // Private Types
  54. //----------------------------------------------------------------------------
  55. //----------------------------------------------------------------------------
  56. // Private Data
  57. //----------------------------------------------------------------------------
  58. //----------------------------------------------------------------------------
  59. // Public Data
  60. //----------------------------------------------------------------------------
  61. //----------------------------------------------------------------------------
  62. // Private Prototypes
  63. //----------------------------------------------------------------------------
  64. //----------------------------------------------------------------------------
  65. // Private Functions
  66. //----------------------------------------------------------------------------
  67. //=================================================================
  68. // RAMFile::RAMFile
  69. //=================================================================
  70. RAMFile::RAMFile()
  71. : m_size(0),
  72. m_data(NULL)
  73. {
  74. }
  75. //----------------------------------------------------------------------------
  76. // Public Functions
  77. //----------------------------------------------------------------------------
  78. //=================================================================
  79. // RAMFile::~RAMFile
  80. //=================================================================
  81. RAMFile::~RAMFile()
  82. {
  83. delete [] m_data;
  84. File::close();
  85. }
  86. //=================================================================
  87. // RAMFile::open
  88. //=================================================================
  89. /**
  90. * This function opens a file using the standard C open() call. Access flags
  91. * are mapped to the appropriate open flags. Returns true if file was opened
  92. * successfully.
  93. */
  94. //=================================================================
  95. Bool RAMFile::open( const Char *filename, Int access )
  96. {
  97. File *file = TheFileSystem->open( filename, access );
  98. if ( file == NULL )
  99. {
  100. return FALSE;
  101. }
  102. Bool result = open( file );
  103. file->close();
  104. return result;
  105. return TRUE;
  106. }
  107. //============================================================================
  108. // RAMFile::open
  109. //============================================================================
  110. Bool RAMFile::open( File *file )
  111. {
  112. if ( file == NULL )
  113. {
  114. return NULL;
  115. }
  116. Int access = file->getAccess();
  117. if ( !File::open( file->getName(), access ))
  118. {
  119. return FALSE;
  120. }
  121. // read whole file in to memory
  122. m_size = file->size();
  123. m_data = new char [ m_size ];
  124. if ( m_data == NULL )
  125. {
  126. return FALSE;
  127. }
  128. m_size = file->read( m_data, m_size );
  129. if ( m_size < 0 )
  130. {
  131. delete [] m_data;
  132. m_data = NULL;
  133. return FALSE;
  134. }
  135. m_pos = 0;
  136. return TRUE;
  137. }
  138. //=================================================================
  139. // RAMFile::close
  140. //=================================================================
  141. /**
  142. * Closes the current file if it is open.
  143. * Must call RAMFile::close() for each successful RAMFile::open() call.
  144. */
  145. //=================================================================
  146. void RAMFile::close( void )
  147. {
  148. if ( m_data )
  149. {
  150. delete [] m_data;
  151. m_data = NULL;
  152. }
  153. File::close();
  154. }
  155. //=================================================================
  156. // RAMFile::read
  157. //=================================================================
  158. Int RAMFile::read( void *buffer, Int bytes )
  159. {
  160. if( m_data == NULL )
  161. {
  162. return -1;
  163. }
  164. Int bytesLeft = m_size - m_pos ;
  165. if ( bytes > bytesLeft )
  166. {
  167. bytes = bytesLeft;
  168. }
  169. if ( bytes > 0 )
  170. {
  171. memcpy ( buffer, &m_data[m_pos], bytes );
  172. }
  173. else
  174. {
  175. bytes = 0;
  176. }
  177. m_pos += bytes;
  178. return bytes;
  179. }
  180. //=================================================================
  181. // RAMFile::write
  182. //=================================================================
  183. Int RAMFile::write( void *buffer, Int bytes )
  184. {
  185. return -1;
  186. }
  187. //=================================================================
  188. // RAMFile::seek
  189. //=================================================================
  190. Int RAMFile::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_pos + pos;
  200. break;
  201. case END:
  202. newPos = m_size - pos - 1;
  203. break;
  204. default:
  205. // bad seek mode
  206. return -1;
  207. }
  208. if ( newPos < 0 )
  209. {
  210. newPos = 0;
  211. }
  212. else if ( newPos > m_size - 1 )
  213. {
  214. newPos = m_size - 1;
  215. }
  216. m_pos = newPos;
  217. return m_pos;
  218. }