WSYS_file.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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: wsys/File.h
  32. //
  33. // Created: 4/23/01
  34. //
  35. //----------------------------------------------------------------------------
  36. #pragma once
  37. #ifndef __WSYS_FILE_H
  38. #define __WSYS_FILE_H
  39. //----------------------------------------------------------------------------
  40. // Includes
  41. //----------------------------------------------------------------------------
  42. #include "lib/basetype.h"
  43. //----------------------------------------------------------------------------
  44. // Forward References
  45. //----------------------------------------------------------------------------
  46. //----------------------------------------------------------------------------
  47. // Type Defines
  48. //----------------------------------------------------------------------------
  49. #define IO_MAX_PATH (2*1024) ///< Maximum allowable path legnth
  50. //===============================
  51. // File
  52. //===============================
  53. /**
  54. * File is an interface class for basic file operations.
  55. *
  56. * All code should use the File class and not its derivatives, unless
  57. * absolutely necessary. Also FS::Open should be used to create File objects and open files.
  58. */
  59. //===============================
  60. class File
  61. {
  62. friend class FileSystem;
  63. public:
  64. enum access
  65. {
  66. NONE = 0x00000000,
  67. READ = 0x00000001, ///< Access file for reading
  68. WRITE = 0x00000002, ///< Access file for writing
  69. APPEND = 0x00000004, ///< Seek to end of file on open
  70. CREATE = 0x00000008, ///< Create file if it does not exist
  71. TRUNCATE = 0x00000010, ///< Delete all data in file when opened
  72. TEXT = 0x00000020, ///< Access file as text data
  73. BINARY = 0x00000040, ///< Access file as binary data
  74. READWRITE = (READ | WRITE),
  75. NEW = 0x00000080 ///< Only create file if it does not exist
  76. };
  77. enum seekMode
  78. {
  79. START, ///< Seek position is relative to start of file
  80. CURRENT, ///< Seek position is relative to current file position
  81. END ///< Seek position is relative from the end of the file
  82. };
  83. protected:
  84. Char m_name[IO_MAX_PATH+1]; ///< Stores file name
  85. Bool m_open; ///< Has the file been opened
  86. Bool m_deleteOnClose; ///< delete File object on close()
  87. Int m_access; ///< How the file was opened
  88. File(); ///< This class can only used as a base class
  89. virtual ~File();
  90. public:
  91. virtual Bool open( const Char *filename, Int access = 0 ); ///< Open a file for access
  92. virtual void close( void ); ///< Close the file !!! File object no longer valid after this call !!!
  93. virtual Int read( void *buffer, Int bytes ) = NULL ; /**< Read the specified number of bytes from the file in to the
  94. * memory pointed at by buffer. Returns the number of bytes read.
  95. * Returns -1 if an error occured.
  96. */
  97. virtual Int write( void *buffer, Int bytes ) = NULL ; /**< Write the specified number of bytes from the
  98. * memory pointed at by buffer to the file. Returns the number of bytes written.
  99. * Returns -1 if an error occured.
  100. */
  101. virtual Int seek( Int bytes, seekMode mode = CURRENT ) = NULL; /**< Sets the file position of the next read/write operation. Returns the new file
  102. * position as the number of bytes from the start of the file.
  103. * Returns -1 if an error occured.
  104. *
  105. * seekMode determines how the seek is done:
  106. *
  107. * START : means seek to the specified number of bytes from the start of the file
  108. * CURRENT: means seek the specified the number of bytes from the current file position
  109. * END: means seek the specified number of bytes back from the end of the file
  110. */
  111. virtual Bool printf ( const Char *format, ...); ///< Prints formated string to text file
  112. virtual Int size( void ); ///< Returns the size of the file
  113. virtual Int position( void ); ///< Returns the current read/write position
  114. void setName( const Char *name ); ///< Set the name of the file
  115. Char* getName( void ); ///< Returns a pointer to the name of the file
  116. Bool getName( Char *buffer, Int max ); ///< Copies the name of the file to the buffer
  117. Int getAccess( void ); ///< Returns file's access flags
  118. void deleteOnClose ( void ); ///< Causes the File object to delete itself when it closes
  119. };
  120. //----------------------------------------------------------------------------
  121. // Inlining
  122. //----------------------------------------------------------------------------
  123. inline Char* File::getName( void ) { return m_name;};
  124. inline Int File::getAccess( void ) { return m_access;};
  125. inline void File::deleteOnClose( void ) { m_deleteOnClose = TRUE;};
  126. // include FileSystem.h as it will be used alot with File.h
  127. //#include "wsys/FileSystem.h"
  128. #endif // __WSYS_FILE_H