file.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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: wsys/File.h
  37. //
  38. // Created: 4/23/01
  39. //
  40. //----------------------------------------------------------------------------
  41. #pragma once
  42. #ifndef __FILE_H
  43. #define __FILE_H
  44. //----------------------------------------------------------------------------
  45. // Includes
  46. //----------------------------------------------------------------------------
  47. #include "lib/basetype.h"
  48. #include "Common/AsciiString.h"
  49. #include "Common/GameMemory.h"
  50. // include FileSystem.h as it will be used alot with File.h
  51. //#include "Common/FileSystem.h"
  52. //----------------------------------------------------------------------------
  53. // Forward References
  54. //----------------------------------------------------------------------------
  55. //----------------------------------------------------------------------------
  56. // Type Defines
  57. //----------------------------------------------------------------------------
  58. //===============================
  59. // File
  60. //===============================
  61. /**
  62. * File is an interface class for basic file operations.
  63. *
  64. * All code should use the File class and not its derivatives, unless
  65. * absolutely necessary. Also FS::Open should be used to create File objects and open files.
  66. */
  67. //===============================
  68. class File : public MemoryPoolObject
  69. {
  70. MEMORY_POOL_GLUE_ABC(File)
  71. // friend doesn't play well with MPO (srj)
  72. // friend class FileSystem;
  73. public:
  74. enum access
  75. {
  76. NONE = 0x00000000,
  77. READ = 0x00000001, ///< Access file for reading
  78. WRITE = 0x00000002, ///< Access file for writing
  79. APPEND = 0x00000004, ///< Seek to end of file on open
  80. CREATE = 0x00000008, ///< Create file if it does not exist
  81. TRUNCATE = 0x00000010, ///< Delete all data in file when opened
  82. TEXT = 0x00000020, ///< Access file as text data
  83. BINARY = 0x00000040, ///< Access file as binary data
  84. READWRITE = (READ | WRITE),
  85. ONLYNEW = 0x00000080, ///< Only create file if it does not exist
  86. // NOTE: STREAMING is Mutually exclusive with WRITE
  87. STREAMING = 0x00000100 ///< Do not read this file into a ram file, read it as requested.
  88. };
  89. enum seekMode
  90. {
  91. START, ///< Seek position is relative to start of file
  92. CURRENT, ///< Seek position is relative to current file position
  93. END ///< Seek position is relative from the end of the file
  94. };
  95. protected:
  96. AsciiString m_nameStr; ///< Stores file name
  97. Int m_access; ///< How the file was opened
  98. Bool m_open; ///< Has the file been opened
  99. Bool m_deleteOnClose; ///< delete File object on close()
  100. File(); ///< This class can only used as a base class
  101. //virtual ~File();
  102. public:
  103. Bool eof();
  104. virtual Bool open( const Char *filename, Int access = 0 ); ///< Open a file for access
  105. virtual void close( void ); ///< Close the file !!! File object no longer valid after this call !!!
  106. virtual Int read( void *buffer, Int bytes ) = NULL ; /**< Read the specified number of bytes from the file in to the
  107. * memory pointed at by buffer. Returns the number of bytes read.
  108. * Returns -1 if an error occured.
  109. */
  110. virtual Int write( const void *buffer, Int bytes ) = NULL ; /**< Write the specified number of bytes from the
  111. * memory pointed at by buffer to the file. Returns the number of bytes written.
  112. * Returns -1 if an error occured.
  113. */
  114. virtual Int seek( Int bytes, seekMode mode = CURRENT ) = NULL; /**< Sets the file position of the next read/write operation. Returns the new file
  115. * position as the number of bytes from the start of the file.
  116. * Returns -1 if an error occured.
  117. *
  118. * seekMode determines how the seek is done:
  119. *
  120. * START : means seek to the specified number of bytes from the start of the file
  121. * CURRENT: means seek the specified the number of bytes from the current file position
  122. * END: means seek the specified number of bytes back from the end of the file
  123. */
  124. virtual void nextLine(Char *buf = NULL, Int bufSize = 0) = 0; ///< reads until it reaches a new-line character
  125. virtual Bool scanInt(Int &newInt) = 0; ///< read an integer from the current file position.
  126. virtual Bool scanReal(Real &newReal) = 0; ///< read a real number from the current file position.
  127. virtual Bool scanString(AsciiString &newString) = 0; ///< read a string from the current file position.
  128. virtual Bool print ( const Char *format, ...); ///< Prints formated string to text file
  129. virtual Int size( void ); ///< Returns the size of the file
  130. virtual Int position( void ); ///< Returns the current read/write position
  131. void setName( const char *name ); ///< Set the name of the file
  132. const char* getName( void ) const; ///< Returns a pointer to the name of the file
  133. Int getAccess( void ) const; ///< Returns file's access flags
  134. void deleteOnClose ( void ); ///< Causes the File object to delete itself when it closes
  135. /**
  136. Allocate a buffer large enough to hold entire file, read
  137. the entire file into the buffer, then close the file.
  138. the buffer is owned by the caller, who is responsible
  139. for freeing is (via delete[]). This is a Good Thing to
  140. use because it minimizes memory copies for BIG files.
  141. */
  142. virtual char* readEntireAndClose() = 0;
  143. virtual File* convertToRAMFile() = 0;
  144. };
  145. //----------------------------------------------------------------------------
  146. // Inlining
  147. //----------------------------------------------------------------------------
  148. inline const char* File::getName( void ) const { return m_nameStr.str(); }
  149. inline void File::setName( const char *name ) { m_nameStr.set(name); }
  150. inline Int File::getAccess( void ) const { return m_access; }
  151. inline void File::deleteOnClose( void ) { m_deleteOnClose = TRUE; }
  152. #endif // __FILE_H