File.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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: WSYS Library
  33. //
  34. // Module: IO_
  35. //
  36. // File name: IO_File.cpp
  37. //
  38. // Created: 4/23/01
  39. //
  40. //----------------------------------------------------------------------------
  41. //----------------------------------------------------------------------------
  42. // Includes
  43. //----------------------------------------------------------------------------
  44. #include "PreRTS.h"
  45. #include <assert.h>
  46. #include <string.h>
  47. #include <stdarg.h>
  48. #include <stdio.h>
  49. #include "Common/File.h"
  50. //----------------------------------------------------------------------------
  51. // Externals
  52. //----------------------------------------------------------------------------
  53. //----------------------------------------------------------------------------
  54. // Defines
  55. //----------------------------------------------------------------------------
  56. //----------------------------------------------------------------------------
  57. // Private Types
  58. //----------------------------------------------------------------------------
  59. //----------------------------------------------------------------------------
  60. // Private Data
  61. //----------------------------------------------------------------------------
  62. //----------------------------------------------------------------------------
  63. // Public Data
  64. //----------------------------------------------------------------------------
  65. //----------------------------------------------------------------------------
  66. // Private Prototypes
  67. //----------------------------------------------------------------------------
  68. //----------------------------------------------------------------------------
  69. // Private Functions
  70. //----------------------------------------------------------------------------
  71. //=================================================================
  72. // File::File
  73. //=================================================================
  74. File::File()
  75. : m_open(FALSE),
  76. m_deleteOnClose(FALSE),
  77. m_access(NONE)
  78. {
  79. setName("<no file>");
  80. }
  81. //----------------------------------------------------------------------------
  82. // Public Functions
  83. //----------------------------------------------------------------------------
  84. //=================================================================
  85. // File::~File
  86. //=================================================================
  87. File::~File()
  88. {
  89. close();
  90. }
  91. //=================================================================
  92. // File::open
  93. //=================================================================
  94. /**
  95. * Any derived open() members must first call File::open. If File::open
  96. * succeeds but the derived class's open failes then make sure to call
  97. * File::close() before returning.
  98. */
  99. //=================================================================
  100. Bool File::open( const Char *filename, Int access )
  101. {
  102. if( m_open )
  103. {
  104. return FALSE;
  105. }
  106. setName( filename );
  107. if( (access & ( STREAMING | WRITE )) == ( STREAMING | WRITE ))
  108. {
  109. // illegal access
  110. return FALSE;
  111. }
  112. if( (access & ( TEXT | BINARY)) == ( TEXT | BINARY ))
  113. {
  114. // illegal access
  115. return FALSE;
  116. }
  117. if ( (access & (READ|WRITE)) == 0 )
  118. {
  119. access |= READ;
  120. }
  121. if ( !(access & (READ|APPEND)) )
  122. {
  123. access |= TRUNCATE;
  124. }
  125. if ( (access & (TEXT|BINARY)) == 0 )
  126. {
  127. access |= BINARY;
  128. }
  129. m_access = access;
  130. m_open = TRUE;
  131. return TRUE;
  132. }
  133. //=================================================================
  134. // File::close
  135. //=================================================================
  136. /**
  137. * Must call File::close() for each successful File::open() call.
  138. */
  139. //=================================================================
  140. void File::close( void )
  141. {
  142. if( m_open )
  143. {
  144. setName( "<no file>" );
  145. m_open = FALSE;
  146. if ( m_deleteOnClose )
  147. {
  148. this->deleteInstance(); // on special cases File object will delete itself when closing
  149. }
  150. }
  151. }
  152. //=================================================================
  153. // File::size
  154. //=================================================================
  155. /**
  156. * Default implementation of File::size. Derived classes can optimize
  157. * this member function.
  158. */
  159. //=================================================================
  160. Int File::size( void )
  161. {
  162. Int pos = seek( 0, CURRENT );
  163. Int size = seek( 0, END );
  164. seek( pos, START );
  165. return size < 0 ? 0 : size;
  166. }
  167. //============================================================================
  168. // File::position
  169. //============================================================================
  170. Int File::position( void )
  171. {
  172. return seek(0, CURRENT);
  173. }
  174. //============================================================================
  175. // File::print
  176. //============================================================================
  177. Bool File::print ( const Char *format, ...)
  178. {
  179. Char buffer[10*1024];
  180. Int len;
  181. if ( ! (m_access & TEXT ) )
  182. {
  183. return FALSE;
  184. }
  185. va_list args;
  186. va_start( args, format ); /* Initialize variable arguments. */
  187. len = vsprintf( buffer, format, args );
  188. va_end( args );
  189. if ( len >= sizeof(buffer) )
  190. {
  191. // Big Problem
  192. assert( FALSE );
  193. return FALSE;
  194. }
  195. return (write ( buffer, len ) == len);
  196. }
  197. Bool File::eof() {
  198. return (position() == size());
  199. }