Skip to content

Commit 179975b

Browse files
committed
[CORE] Implement custom buffer options for LocalFile::open()
1 parent 77e76df commit 179975b

File tree

17 files changed

+77
-43
lines changed

17 files changed

+77
-43
lines changed

Core/GameEngine/Include/Common/FileSystem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@
5050
// Includes
5151
//----------------------------------------------------------------------------
5252

53+
#include "Common/file.h"
5354
#include "Common/STLTypedefs.h"
5455
#include "Common/SubsystemInterface.h"
5556

5657
//----------------------------------------------------------------------------
5758
// Forward References
5859
//----------------------------------------------------------------------------
59-
class File;
6060

6161
//----------------------------------------------------------------------------
6262
// Type Defines
@@ -130,7 +130,7 @@ class FileSystem : public SubsystemInterface
130130
void reset();
131131
void update();
132132

133-
File* openFile( const Char *filename, Int access = 0 ); ///< opens a File interface to the specified file
133+
File* openFile( const Char *filename, Int access = File::NONE, size_t bufferSize = File::BUFFERSIZE ); ///< opens a File interface to the specified file
134134
Bool doesFileExist(const Char *filename) const; ///< returns TRUE if the file exists. filename should have no directory.
135135
void getFileListInDirectory(const AsciiString& directory, const AsciiString& searchName, FilenameList &filenameList, Bool searchSubdirectories) const; ///< search the given directory for files matching the searchName (egs. *.ini, *.rep). Possibly search subdirectories.
136136
Bool getFileInfo(const AsciiString& filename, FileInfo *fileInfo) const; ///< fills in the FileInfo struct for the file given. returns TRUE if successful.

Core/GameEngine/Include/Common/LocalFile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class LocalFile : public File
9696
//virtual ~LocalFile();
9797

9898

99-
virtual Bool open( const Char *filename, Int access = 0 ); ///< Open a file for access
99+
virtual Bool open( const Char *filename, Int access = NONE, size_t bufferSize = BUFFERSIZE ); ///< Open a file for access
100100
virtual void close( void ); ///< Close the file
101101
virtual Int read( void *buffer, Int bytes ); ///< Read the specified number of bytes in to buffer: See File::read
102102
virtual Int write( const void *buffer, Int bytes ); ///< Write the specified number of bytes from the buffer: See File::write

Core/GameEngine/Include/Common/LocalFileSystem.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
#include "Common/SubsystemInterface.h"
3535
#include "FileSystem.h" // for typedefs, etc.
3636

37-
class File;
38-
3937
class LocalFileSystem : public SubsystemInterface
4038
{
4139
public:
@@ -45,7 +43,7 @@ class LocalFileSystem : public SubsystemInterface
4543
virtual void reset() = 0;
4644
virtual void update() = 0;
4745

48-
virtual File * openFile(const Char *filename, Int access = 0) = 0;
46+
virtual File * openFile(const Char *filename, Int access = File::NONE, size_t bufferSize = File::BUFFERSIZE) = 0;
4947
virtual Bool doesFileExist(const Char *filename) const = 0;
5048
virtual void getFileListInDirectory(const AsciiString& currentDirectory, const AsciiString& originalDirectory, const AsciiString& searchName, FilenameList &filenameList, Bool searchSubdirectories) const = 0; ///< search the given directory for files matching the searchName (egs. *.ini, *.rep). Possibly search subdirectories.
5149
virtual Bool getFileInfo(const AsciiString& filename, FileInfo *fileInfo) const = 0; ///< see FileSystem.h

Core/GameEngine/Include/Common/RAMFile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class RAMFile : public File
8787
//virtual ~RAMFile();
8888

8989

90-
virtual Bool open( const Char *filename, Int access = 0 ); ///< Open a file for access
90+
virtual Bool open( const Char *filename, Int access = NONE, size_t bufferSize = 0 ); ///< Open a file for access
9191
virtual void close( void ); ///< Close the file
9292
virtual Int read( void *buffer, Int bytes ); ///< Read the specified number of bytes in to buffer: See File::read
9393
virtual Int write( const void *buffer, Int bytes ); ///< Write the specified number of bytes from the buffer: See File::write

Core/GameEngine/Include/Common/StreamingArchiveFile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class StreamingArchiveFile : public RAMFile
8888
//virtual ~StreamingArchiveFile();
8989

9090

91-
virtual Bool open( const Char *filename, Int access = 0 ); ///< Open a file for access
91+
virtual Bool open( const Char *filename, Int access = NONE, size_t bufferSize = BUFFERSIZE ); ///< Open a file for access
9292
virtual void close( void ); ///< Close the file
9393
virtual Int read( void *buffer, Int bytes ); ///< Read the specified number of bytes in to buffer: See File::read
9494
virtual Int write( const void *buffer, Int bytes ); ///< Write the specified number of bytes from the buffer: See File::write

Core/GameEngine/Include/Common/file.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,20 @@ class File : public MemoryPoolObject
8989

9090
enum access
9191
{
92-
NONE = 0x00000000,
92+
NONE = 0x00000000, ///< Access file. Reading by default
93+
9394
READ = 0x00000001, ///< Access file for reading
9495
WRITE = 0x00000002, ///< Access file for writing
96+
READWRITE = (READ | WRITE),
97+
9598
APPEND = 0x00000004, ///< Seek to end of file on open
9699
CREATE = 0x00000008, ///< Create file if it does not exist
97100
TRUNCATE = 0x00000010, ///< Delete all data in file when opened
98-
TEXT = 0x00000020, ///< Access file as text data
99-
BINARY = 0x00000040, ///< Access file as binary data
100-
READWRITE = (READ | WRITE),
101+
102+
// NOTE: accesses file as text data (with full buffering) if neither TEXT and BINARY are set
103+
TEXT = 0x00000020, ///< Access file as text data (with line buffering)
104+
BINARY = 0x00000040, ///< Access file as binary data (with full buffering)
105+
101106
ONLYNEW = 0x00000080, ///< Only create file if it does not exist
102107

103108
// NOTE: STREAMING is Mutually exclusive with WRITE
@@ -111,6 +116,11 @@ class File : public MemoryPoolObject
111116
END ///< Seek position is relative from the end of the file
112117
};
113118

119+
enum
120+
{
121+
BUFFERSIZE = BUFSIZ,
122+
};
123+
114124
protected:
115125

116126
AsciiString m_nameStr; ///< Stores file name
@@ -126,20 +136,20 @@ class File : public MemoryPoolObject
126136

127137

128138
Bool eof();
129-
virtual Bool open( const Char *filename, Int access = 0 ); ///< Open a file for access
139+
virtual Bool open( const Char *filename, Int access = NONE, size_t bufferSize = BUFFERSIZE ); ///< Open a file for access
130140
virtual void close( void ); ///< Close the file !!! File object no longer valid after this call !!!
131141

132142
virtual Int read( void *buffer, Int bytes ) = NULL ; /**< Read the specified number of bytes from the file in to the
133143
* memory pointed at by buffer. Returns the number of bytes read.
134-
* Returns -1 if an error occured.
144+
* Returns -1 if an error occurred.
135145
*/
136146
virtual Int write( const void *buffer, Int bytes ) = NULL ; /**< Write the specified number of bytes from the
137147
* memory pointed at by buffer to the file. Returns the number of bytes written.
138-
* Returns -1 if an error occured.
148+
* Returns -1 if an error occurred.
139149
*/
140150
virtual Int seek( Int bytes, seekMode mode = CURRENT ) = NULL; /**< Sets the file position of the next read/write operation. Returns the new file
141151
* position as the number of bytes from the start of the file.
142-
* Returns -1 if an error occured.
152+
* Returns -1 if an error occurred.
143153
*
144154
* seekMode determines how the seek is done:
145155
*

Core/GameEngine/Source/Common/System/File.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ File::~File()
135135
*/
136136
//=================================================================
137137

138-
Bool File::open( const Char *filename, Int access )
138+
Bool File::open( const Char *filename, Int access, size_t bufferSize )
139139
{
140140
if( m_open )
141141
{
@@ -161,7 +161,7 @@ Bool File::open( const Char *filename, Int access )
161161
access |= READ;
162162
}
163163

164-
if ( !(access & (READ|APPEND)) )
164+
if ( (access & (READ|APPEND)) == 0 )
165165
{
166166
access |= TRUNCATE;
167167
}

Core/GameEngine/Source/Common/System/FileSystem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,14 @@ void FileSystem::reset( void )
171171
// FileSystem::open
172172
//============================================================================
173173

174-
File* FileSystem::openFile( const Char *filename, Int access )
174+
File* FileSystem::openFile( const Char *filename, Int access, size_t bufferSize )
175175
{
176176
USE_PERF_TIMER(FileSystem)
177177
File *file = NULL;
178178

179179
if ( TheLocalFileSystem != NULL )
180180
{
181-
file = TheLocalFileSystem->openFile( filename, access );
181+
file = TheLocalFileSystem->openFile( filename, access, bufferSize );
182182
if (file != NULL && (file->getAccess() & File::CREATE))
183183
{
184184
unsigned key = TheNameKeyGenerator->nameToLowercaseKey(filename);

Core/GameEngine/Source/Common/System/LocalFile.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,14 @@ LocalFile::~LocalFile()
151151
// LocalFile::open
152152
//=================================================================
153153
/**
154-
* This function opens a file using the standard C open() call. Access flags
155-
* are mapped to the appropriate open flags. Returns true if file was opened
156-
* successfully.
154+
* This function opens a file using the standard C open() or
155+
* fopen() call. Access flags are mapped to the appropriate flags.
156+
* Returns true if file was opened successfully.
157157
*/
158158
//=================================================================
159159

160160
//DECLARE_PERF_TIMER(LocalFile)
161-
Bool LocalFile::open( const Char *filename, Int access )
161+
Bool LocalFile::open( const Char *filename, Int access, size_t bufferSize )
162162
{
163163
//USE_PERF_TIMER(LocalFile)
164164
if( !File::open( filename, access) )
@@ -182,6 +182,7 @@ Bool LocalFile::open( const Char *filename, Int access )
182182
const Bool append = (m_access & APPEND) != 0;
183183
const Bool create = (m_access & CREATE) != 0;
184184
const Bool truncate = (m_access & TRUNCATE) != 0;
185+
const Bool text = (m_access & TEXT) != 0;
185186
const Bool binary = (m_access & BINARY) != 0;
186187

187188
const Char *mode = NULL;
@@ -216,6 +217,26 @@ Bool LocalFile::open( const Char *filename, Int access )
216217
goto error;
217218
}
218219

220+
{
221+
Int result = 0;
222+
223+
if (bufferSize == 0)
224+
{
225+
result = setvbuf(m_file, NULL, _IONBF, 0); // Uses no buffering.
226+
}
227+
else
228+
{
229+
const Int bufferMode = text
230+
? _IOLBF // Uses line buffering
231+
: _IOFBF; // Uses full buffering
232+
233+
// Buffer is expected to lazy allocate on first read or write later.
234+
result = setvbuf(m_file, NULL, bufferMode, bufferSize);
235+
}
236+
237+
DEBUG_ASSERTCRASH(result == 0, ("LocalFile::open - setvbuf failed"));
238+
}
239+
219240
#else
220241

221242
int flags = 0;

Core/GameEngine/Source/Common/System/RAMFile.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,20 @@ RAMFile::~RAMFile()
138138
// RAMFile::open
139139
//=================================================================
140140
/**
141-
* This function opens a file using the standard C open() call. Access flags
142-
* are mapped to the appropriate open flags. Returns true if file was opened
143-
* successfully.
141+
* This function opens a file using the standard C open() or
142+
* fopen() call. Access flags are mapped to the appropriate flags.
143+
* Returns true if file was opened successfully.
144144
*/
145145
//=================================================================
146146

147147
//DECLARE_PERF_TIMER(RAMFile)
148-
Bool RAMFile::open( const Char *filename, Int access )
148+
Bool RAMFile::open( const Char *filename, Int access, size_t bufferSize )
149149
{
150150
//USE_PERF_TIMER(RAMFile)
151-
File *file = TheFileSystem->openFile( filename, access );
151+
152+
bufferSize = 0; // RAM File needs no file buffer because it is read in one go.
153+
154+
File *file = TheFileSystem->openFile( filename, access, bufferSize );
152155

153156
if ( file == NULL )
154157
{
@@ -174,7 +177,7 @@ Bool RAMFile::open( File *file )
174177
return NULL;
175178
}
176179

177-
Int access = file->getAccess();
180+
const Int access = file->getAccess();
178181

179182
if ( !File::open( file->getName(), access ))
180183
{

0 commit comments

Comments
 (0)