Skip to content

Commit 3ac1832

Browse files
authored
feat(file): Implement functions in LocalFile for writing formatted strings to a file (#1414)
1 parent 5efeb0e commit 3ac1832

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

Core/GameEngine/Include/Common/LocalFile.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
#include "Common/file.h"
5656

5757
#if USE_BUFFERED_IO
58-
#include <stdio.h>
58+
#include "Utility/stdio_adapter.h"
5959
#endif
6060

6161
//----------------------------------------------------------------------------
@@ -100,6 +100,8 @@ class LocalFile : public File
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
103+
virtual Int writeFormat( const Char* format, ... ); ///< Write an unterminated formatted string to the file
104+
virtual Int writeFormat( const WideChar* format, ... ); ///< Write an unterminated formatted string to the file
103105
virtual Int seek( Int new_pos, seekMode mode = CURRENT ); ///< Set file position: See File::seek
104106
virtual Bool flush(); ///< flush data to disk
105107
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class RAMFile : public File
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
94+
virtual Int writeFormat( const Char* format, ... ); ///< Write the formatted string to the file
95+
virtual Int writeFormat( const WideChar* format, ... ); ///< Write the formatted string to the file
9496
virtual Int seek( Int new_pos, seekMode mode = CURRENT ); ///< Set file position: See File::seek
9597
virtual Bool flush(); ///< flush data to disk
9698
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ class File : public MemoryPoolObject
153153
* memory pointed at by buffer to the file. Returns the number of bytes written.
154154
* Returns -1 if an error occurred.
155155
*/
156+
virtual Int writeFormat( const Char* format, ... ) = 0 ; /**< Write an unterminated formatted string to the file
157+
* Returns the number of bytes written.
158+
* Returns -1 if an error occurred.
159+
*/
160+
virtual Int writeFormat( const WideChar* format, ... ) = 0 ; /**< Write an unterminated formatted wide character string to the file
161+
* Returns the number of bytes written.
162+
* Returns -1 if an error occurred.
163+
*/
156164
virtual Int seek( Int bytes, seekMode mode = CURRENT ) = 0; /**< Sets the file position of the next read/write operation. Returns the new file
157165
* position as the number of bytes from the start of the file.
158166
* Returns -1 if an error occurred.

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747

4848
#include "PreRTS.h"
4949

50-
#include <stdio.h>
5150
#include <fcntl.h>
5251
#include <io.h>
5352
#include <string.h>
@@ -381,6 +380,38 @@ Int LocalFile::write( const void *buffer, Int bytes )
381380
return ret;
382381
}
383382

383+
//=================================================================
384+
// LocalFile::writeFormat - Ascii
385+
//=================================================================
386+
387+
Int LocalFile::writeFormat( const Char* format, ... )
388+
{
389+
char buffer[1024];
390+
391+
va_list args;
392+
va_start(args, format);
393+
Int length = vsnprintf(buffer, sizeof(buffer), format, args);
394+
va_end(args);
395+
396+
return write( buffer, length );
397+
}
398+
399+
//=================================================================
400+
// LocalFile::writeFormat - Wide character
401+
//=================================================================
402+
403+
Int LocalFile::writeFormat( const WideChar* format, ... )
404+
{
405+
WideChar buffer[1024];
406+
407+
va_list args;
408+
va_start(args, format);
409+
Int length = vswprintf(buffer, sizeof(buffer) / sizeof(WideChar), format, args);
410+
va_end(args);
411+
412+
return write( buffer, length * sizeof(WideChar) );
413+
}
414+
384415
//=================================================================
385416
// LocalFile::seek
386417
//=================================================================

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,24 @@ Int RAMFile::write( const void *buffer, Int bytes )
294294
return -1;
295295
}
296296

297+
//=================================================================
298+
// RAMFile::writeFormat - Ascii
299+
//=================================================================
300+
301+
Int RAMFile::writeFormat( const Char* format, ... )
302+
{
303+
return -1;
304+
}
305+
306+
//=================================================================
307+
// RAMFile::writeFormat - Wide character
308+
//=================================================================
309+
310+
Int RAMFile::writeFormat( const WideChar* format, ... )
311+
{
312+
return -1;
313+
}
314+
297315
//=================================================================
298316
// RAMFile::seek
299317
//=================================================================

0 commit comments

Comments
 (0)