Skip to content

Commit 02fdf4f

Browse files
authored
feat(file): Implement character read and write functions for LocalFile (#1431)
1 parent f3ba422 commit 02fdf4f

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

Core/GameEngine/Include/Common/LocalFile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,13 @@ class LocalFile : public File
9999
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
102+
virtual Int readChar(); ///< Read a character from the file
103+
virtual Int readWideChar(); ///< Read a wide character from the file
102104
virtual Int write( const void *buffer, Int bytes ); ///< Write the specified number of bytes from the buffer: See File::write
103105
virtual Int writeFormat( const Char* format, ... ); ///< Write an unterminated formatted string to the file
104106
virtual Int writeFormat( const WideChar* format, ... ); ///< Write an unterminated formatted string to the file
107+
virtual Int writeChar( const Char* character ); ///< Write a character to the file
108+
virtual Int writeChar( const WideChar* character ); ///< Write a wide character to the file
105109
virtual Int seek( Int new_pos, seekMode mode = CURRENT ); ///< Set file position: See File::seek
106110
virtual Bool flush(); ///< flush data to disk
107111
virtual void nextLine(Char *buf = NULL, Int bufSize = 0); ///< moves file position to after the next new-line

Core/GameEngine/Include/Common/RAMFile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,13 @@ class RAMFile : public File
9090
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
93+
virtual Int readChar(); ///< Read a character from the file
94+
virtual Int readWideChar(); ///< Read a wide character from the file
9395
virtual Int write( const void *buffer, Int bytes ); ///< Write the specified number of bytes from the buffer: See File::write
9496
virtual Int writeFormat( const Char* format, ... ); ///< Write the formatted string to the file
9597
virtual Int writeFormat( const WideChar* format, ... ); ///< Write the formatted string to the file
98+
virtual Int writeChar( const Char* character ); ///< Write a character to the file
99+
virtual Int writeChar( const WideChar* character ); ///< Write a wide character to the file
96100
virtual Int seek( Int new_pos, seekMode mode = CURRENT ); ///< Set file position: See File::seek
97101
virtual Bool flush(); ///< flush data to disk
98102
virtual void nextLine(Char *buf = NULL, Int bufSize = 0); ///< moves current position to after the next new-line

Core/GameEngine/Include/Common/file.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ class File : public MemoryPoolObject
149149
* memory pointed at by buffer. Returns the number of bytes read.
150150
* Returns -1 if an error occurred.
151151
*/
152+
virtual Int readChar() = 0 ; /**< Read a character from the file
153+
* Returns the character converted to an integer.
154+
* Returns EOF if an error occurred.
155+
*/
156+
virtual Int readWideChar() = 0 ; /**< Read a wide character from the file
157+
* Returns the wide character converted to an integer.
158+
* Returns wide EOF if an error occurred.
159+
*/
152160
virtual Int write( const void *buffer, Int bytes ) = 0 ; /**< Write the specified number of bytes from the
153161
* memory pointed at by buffer to the file. Returns the number of bytes written.
154162
* Returns -1 if an error occurred.
@@ -161,6 +169,14 @@ class File : public MemoryPoolObject
161169
* Returns the number of bytes written.
162170
* Returns -1 if an error occurred.
163171
*/
172+
virtual Int writeChar( const Char* character ) = 0 ; /**< Write a character to the file
173+
* Returns a copy of the character written.
174+
* Returns EOF if an error occurred.
175+
*/
176+
virtual Int writeChar( const WideChar* character ) = 0 ; /**< Write a wide character to the file
177+
* Returns a copy of the wide character written.
178+
* Returns wide EOF if an error occurred.
179+
*/
164180
virtual Int seek( Int bytes, seekMode mode = CURRENT ) = 0; /**< Sets the file position of the next read/write operation. Returns the new file
165181
* position as the number of bytes from the start of the file.
166182
* Returns -1 if an error occurred.

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,38 @@ Int LocalFile::read( void *buffer, Int bytes )
360360
return ret;
361361
}
362362

363+
//=================================================================
364+
// LocalFile::readChar
365+
//=================================================================
366+
367+
Int LocalFile::readChar( )
368+
{
369+
Char character = '\0';
370+
371+
Int ret = read( &character, sizeof(character) );
372+
373+
if (ret == sizeof(character))
374+
return (Int)character;
375+
376+
return EOF;
377+
}
378+
379+
//=================================================================
380+
// LocalFile::readWideChar
381+
//=================================================================
382+
383+
Int LocalFile::readWideChar( )
384+
{
385+
WideChar character = L'\0';
386+
387+
Int ret = read( &character, sizeof(character) );
388+
389+
if (ret == sizeof(character))
390+
return (Int)character;
391+
392+
return WEOF;
393+
}
394+
363395
//=================================================================
364396
// LocalFile::write
365397
//=================================================================
@@ -412,6 +444,32 @@ Int LocalFile::writeFormat( const WideChar* format, ... )
412444
return write( buffer, length * sizeof(WideChar) );
413445
}
414446

447+
//=================================================================
448+
// LocalFile::writeChar - Ascii
449+
//=================================================================
450+
451+
Int LocalFile::writeChar( const Char* character )
452+
{
453+
if ( write( character, sizeof(Char) ) == sizeof(Char) ) {
454+
return (Int)character;
455+
}
456+
457+
return EOF;
458+
}
459+
460+
//=================================================================
461+
// LocalFile::writeChar - Wide character
462+
//=================================================================
463+
464+
Int LocalFile::writeChar( const WideChar* character )
465+
{
466+
if ( write( character, sizeof(WideChar) ) == sizeof(WideChar) ) {
467+
return (Int)character;
468+
}
469+
470+
return WEOF;
471+
}
472+
415473
//=================================================================
416474
// LocalFile::seek
417475
//=================================================================

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,24 @@ Int RAMFile::read( void *buffer, Int bytes )
285285
return bytes;
286286
}
287287

288+
//=================================================================
289+
// RAMFile::readChar
290+
//=================================================================
291+
292+
Int RAMFile::readChar( )
293+
{
294+
return EOF;
295+
}
296+
297+
//=================================================================
298+
// RAMFile::readWideChar
299+
//=================================================================
300+
301+
Int RAMFile::readWideChar( )
302+
{
303+
return WEOF;
304+
}
305+
288306
//=================================================================
289307
// RAMFile::write
290308
//=================================================================
@@ -312,6 +330,24 @@ Int RAMFile::writeFormat( const WideChar* format, ... )
312330
return -1;
313331
}
314332

333+
//=================================================================
334+
// RAMFile::writeChar - Ascii
335+
//=================================================================
336+
337+
Int RAMFile::writeChar( const Char* character )
338+
{
339+
return EOF;
340+
}
341+
342+
//=================================================================
343+
// RAMFile::writeChar - Wide character
344+
//=================================================================
345+
346+
Int RAMFile::writeChar( const WideChar* character )
347+
{
348+
return WEOF;
349+
}
350+
315351
//=================================================================
316352
// RAMFile::seek
317353
//=================================================================

0 commit comments

Comments
 (0)