WSYS_StdFile.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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: IO_StdFile.cpp
  32. //
  33. // Created: 4/23/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_StdFile.h"
  45. //----------------------------------------------------------------------------
  46. // Externals
  47. //----------------------------------------------------------------------------
  48. //----------------------------------------------------------------------------
  49. // Defines
  50. //----------------------------------------------------------------------------
  51. //----------------------------------------------------------------------------
  52. // Private Types
  53. //----------------------------------------------------------------------------
  54. //----------------------------------------------------------------------------
  55. // Private Data
  56. //----------------------------------------------------------------------------
  57. //----------------------------------------------------------------------------
  58. // Public Data
  59. //----------------------------------------------------------------------------
  60. //----------------------------------------------------------------------------
  61. // Private Prototypes
  62. //----------------------------------------------------------------------------
  63. //----------------------------------------------------------------------------
  64. // Private Functions
  65. //----------------------------------------------------------------------------
  66. //=================================================================
  67. // StdFile::StdFile
  68. //=================================================================
  69. StdFile::StdFile()
  70. : m_handle(-1)
  71. {
  72. }
  73. //----------------------------------------------------------------------------
  74. // Public Functions
  75. //----------------------------------------------------------------------------
  76. //=================================================================
  77. // StdFile::~StdFile
  78. //=================================================================
  79. StdFile::~StdFile()
  80. {
  81. if( m_handle != -1 )
  82. {
  83. _close( m_handle );
  84. m_handle = -1;
  85. }
  86. File::close();
  87. }
  88. //=================================================================
  89. // StdFile::open
  90. //=================================================================
  91. /**
  92. * This function opens a file using the standard C open() call. Access flags
  93. * are mapped to the appropriate open flags. Returns true if file was opened
  94. * successfully.
  95. */
  96. //=================================================================
  97. Bool StdFile::open( const Char *filename, Int access )
  98. {
  99. if( !File::open( filename, access) )
  100. {
  101. return FALSE;
  102. }
  103. /* here we translate WSYS file access to the std C equivalent */
  104. int flags = 0;
  105. if(m_access & CREATE) flags |= _O_CREAT;
  106. if(m_access & TRUNCATE) flags |= _O_TRUNC;
  107. if(m_access & APPEND) flags |= _O_APPEND;
  108. if(m_access & TEXT) flags |= _O_TEXT;
  109. if(m_access & BINARY) flags |= _O_BINARY;
  110. if((m_access & READWRITE )== READWRITE )
  111. {
  112. flags |= _O_RDWR;
  113. }
  114. else if(m_access & WRITE)
  115. {
  116. flags |= _O_WRONLY;
  117. }
  118. else
  119. flags |= _O_RDONLY;
  120. m_handle = _open( filename, flags , _S_IREAD | _S_IWRITE);
  121. if( m_handle == -1 )
  122. {
  123. goto error;
  124. }
  125. if ( m_access & APPEND )
  126. {
  127. if ( seek ( 0, END ) < 0 )
  128. {
  129. goto error;
  130. }
  131. }
  132. return TRUE;
  133. error:
  134. close();
  135. return FALSE;
  136. }
  137. //=================================================================
  138. // StdFile::close
  139. //=================================================================
  140. /**
  141. * Closes the current file if it is open.
  142. * Must call StdFile::close() for each successful StdFile::open() call.
  143. */
  144. //=================================================================
  145. void StdFile::close( void )
  146. {
  147. File::close();
  148. }
  149. //=================================================================
  150. // StdFile::read
  151. //=================================================================
  152. Int StdFile::read( void *buffer, Int bytes )
  153. {
  154. if( !m_open || !buffer )
  155. {
  156. return -1;
  157. }
  158. return _read( m_handle, buffer, bytes );
  159. }
  160. //=================================================================
  161. // StdFile::write
  162. //=================================================================
  163. Int StdFile::write( void *buffer, Int bytes )
  164. {
  165. if( !m_open || !buffer )
  166. {
  167. return -1;
  168. }
  169. return _write( m_handle, buffer, bytes );
  170. }
  171. //=================================================================
  172. // StdFile::seek
  173. //=================================================================
  174. Int StdFile::seek( Int pos, seekMode mode)
  175. {
  176. int lmode;
  177. switch( mode )
  178. {
  179. case START:
  180. lmode = SEEK_SET;
  181. break;
  182. case CURRENT:
  183. lmode = SEEK_CUR;
  184. break;
  185. case END:
  186. lmode = SEEK_END;
  187. break;
  188. default:
  189. // bad seek mode
  190. return -1;
  191. }
  192. return _lseek( m_handle, pos, lmode );
  193. }