WSYS_File.cpp 7.3 KB

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