OpenBarnyard
 
Loading...
Searching...
No Matches
TNativeFile Class Reference

#include <TNativeFile_Win.h>

Inheritance diagram for TNativeFile:
TFile

Public Member Functions

 TNativeFile (TNativeFileSystem *a_pFileSystem)
 
 TNativeFile (const TNativeFile &other)
 
 ~TNativeFile ()
 
virtual TSIZE Read (void *a_pDst, TSIZE a_uiSize) OVERRIDE
 Reads specified number of bytes from the file into the buffer.
 
virtual TSIZE Write (const void *a_pSrc, TSIZE a_uiSize) OVERRIDE
 Writes specified number of bytes to the file from the buffer.
 
virtual TBOOL Seek (TINT a_iOffset, TSEEK a_eOrigin=TSEEK_CUR) OVERRIDE
 Shifts current file cursor based on the specified offset and origin.
 
virtual TSIZE Tell () OVERRIDE
 
virtual void FlushBuffers () OVERRIDE
 Flushes write buffers.
 
virtual TSIZE GetSize () OVERRIDE
 
virtual TUINT64 GetDate () OVERRIDE
 
virtual TCHAR GetCChar () OVERRIDE
 Reads one character of type TCHAR from the file.
 
virtual TWCHAR GetWChar () OVERRIDE
 Reads one character of type wchar from the file.
 
virtual TINT PutCChar (TCHAR a_cCharacter) OVERRIDE
 Writes one character of type TCHAR to the file.
 
virtual TINT PutWChar (TWCHAR a_wcCharacter) OVERRIDE
 Writes one character of type wchar to the file.
 
virtual TINT CPrintf (const TCHAR *a_szFormat,...) OVERRIDE
 Analogue of printf but writes result to the file.
 
virtual TINT WPrintf (const TWCHAR *a_wszFormat,...) OVERRIDE
 Analogue of wprintf but writes result to the file.
 
TBOOL Open (const TString8 &a_FileName, TFILEMODE a_Mode)
 
void Close ()
 
- Public Member Functions inherited from TFile
 TFile (TFileSystem *a_pFS)
 
 TFile (const TFile &a_rOther)
 
virtual ~TFile ()
 
TFileSystemGetFileSystem () const
 
void Destroy ()
 

Static Public Attributes

static constexpr TUINT32 BUFFER_SIZE = 0x800
 

Protected Attributes

void * m_Handle
 
TUINT32 m_Position
 
TUINT32 m_RBufferPosition
 
TUINT32 m_PrevBufferPos
 
TUINT32 m_LastBufferSize
 
TCHARm_RBuffer
 
TCHARm_WBuffer
 
TUINT32 m_WriteBufferUsed
 
TBOOL m_WriteBuffered
 

Additional Inherited Members

- Static Public Member Functions inherited from TFile
static TString8 ConcatPath (const TString8 &a_rcPath1, const TString8 &a_rcPath2)
 
static TString8 SimplifyPath (const TString8 &a_rcPath)
 
static TFileCreate (const TString8 &a_rcFilename, TFILEMODE a_eMode=TFILEMODE_READ)
 

Detailed Description

Definition at line 22 of file TNativeFile_Win.h.

Constructor & Destructor Documentation

◆ TNativeFile() [1/2]

TNativeFile::TNativeFile ( TNativeFileSystem * a_pFileSystem)

Definition at line 162 of file TNativeFile_Win.cpp.

163 : TFile( pFS )
164{
165 m_Handle = INVALID_HANDLE_VALUE;
166 m_Position = -1;
168 m_PrevBufferPos = -1;
174}
#define TNULL
Definition Typedefs.h:23
#define TTRUE
Definition Typedefs.h:25
TFile(TFileSystem *a_pFS)
Definition TFile.cpp:13
TUINT32 m_WriteBufferUsed
TCHAR * m_RBuffer
TCHAR * m_WBuffer
TBOOL m_WriteBuffered
TUINT32 m_Position
TUINT32 m_LastBufferSize
TUINT32 m_PrevBufferPos
TUINT32 m_RBufferPosition

◆ TNativeFile() [2/2]

TNativeFile::TNativeFile ( const TNativeFile & other)

Definition at line 176 of file TNativeFile_Win.cpp.

177 : TFile( other )
178{
179 m_Handle = other.m_Handle;
180 m_Position = other.m_Position;
184 m_RBuffer = other.m_RBuffer;
185 m_WBuffer = other.m_WBuffer;
188}

◆ ~TNativeFile()

TNativeFile::~TNativeFile ( )

Definition at line 191 of file TNativeFile_Win.cpp.

192{
193}

Member Function Documentation

◆ Close()

void TNativeFile::Close ( )

Definition at line 638 of file TNativeFile_Win.cpp.

639{
640 FlushWriteBuffer();
641 CloseHandle( m_Handle );
642
643 m_Handle = INVALID_HANDLE_VALUE;
644 m_Position = -1;
646 m_PrevBufferPos = -1;
647 m_LastBufferSize = -1;
648
649 if ( m_RBuffer != TNULL )
650 {
651 TFree( m_RBuffer );
653 }
654
655 if ( m_WBuffer != TNULL )
656 {
657 TFree( m_WBuffer );
659 }
660}
void TFree(void *a_pMem)
Frees previously allocated memory.
Definition TMemory.cpp:1054

◆ CPrintf()

TINT TNativeFile::CPrintf ( const TCHAR * a_szFormat,
... )
virtual

Analogue of printf but writes result to the file.

Returns
number of characters written

Implements TFile.

Definition at line 524 of file TNativeFile_Win.cpp.

525{
526 va_list args;
527 va_start( args, a_szFormat );
528
529 TCHAR str[ 512 ];
530
531 TINT iResult = T2String8::FormatV( str, TARRAYSIZE( str ), a_szFormat, args );
532
533 va_end( args );
534
535 Write( str, iResult );
536
537 return iResult;
538}
#define TARRAYSIZE(ARRAY)
Definition Defines.h:70
char TCHAR
Definition Typedefs.h:20
int TINT
Definition Typedefs.h:7
virtual TSIZE Write(const void *a_pSrc, TSIZE a_uiSize) OVERRIDE
Writes specified number of bytes to the file from the buffer.
static TINT FormatV(TCHAR *a_pcString, TINT size, const TCHAR *a_pcFormat, va_list args)
Definition T2String8.cpp:22

◆ FlushBuffers()

void TNativeFile::FlushBuffers ( )
virtual

Flushes write buffers.

Reimplemented from TFile.

Definition at line 662 of file TNativeFile_Win.cpp.

663{
664 FlushWriteBuffer();
665}

◆ GetCChar()

TCHAR TNativeFile::GetCChar ( )
virtual

Reads one character of type TCHAR from the file.

Returns
the read character

Implements TFile.

Definition at line 456 of file TNativeFile_Win.cpp.

457{
458 FlushWriteBuffer();
459
460 if ( m_RBuffer != TNULL )
461 {
462 TUINT32 curBufferPos = m_Position / BUFFER_SIZE * BUFFER_SIZE;
463 if ( ( curBufferPos == m_PrevBufferPos ) && ( m_Position - curBufferPos <= m_LastBufferSize - 1 ) )
464 {
465 TCHAR c = m_RBuffer[ m_Position - curBufferPos ];
466 m_Position += sizeof( c );
467 return c;
468 }
469 }
470
471 TCHAR result;
472 if ( Read( &result, sizeof( result ) ) != sizeof( result ) )
473 return -1;
474
475 return result;
476}
uint32_t TUINT32
Definition Typedefs.h:13
static constexpr TUINT32 BUFFER_SIZE
virtual TSIZE Read(void *a_pDst, TSIZE a_uiSize) OVERRIDE
Reads specified number of bytes from the file into the buffer.

◆ GetDate()

TUINT64 TNativeFile::GetDate ( )
virtual
Returns
last write time

Reimplemented from TFile.

Definition at line 571 of file TNativeFile_Win.cpp.

572{
573 TUINT64 uiLastWriteTime = 0;
574
575 if ( m_Handle != TNULL )
576 {
577 GetFileTime( m_Handle, NULL, NULL, (LPFILETIME)&uiLastWriteTime );
578 }
579
580 return uiLastWriteTime;
581}
uint64_t TUINT64
Definition Typedefs.h:11

◆ GetSize()

TSIZE TNativeFile::GetSize ( )
virtual
Returns
size of the file

Implements TFile.

Definition at line 558 of file TNativeFile_Win.cpp.

559{
560 m_RBufferPosition = SetFilePointer( m_Handle, 0, TNULL, TSEEK_END );
561
562 if ( m_RBufferPosition == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR )
563 {
564 return 0;
565 }
566
567 return m_RBufferPosition;
568}
@ TSEEK_END
Definition TFile.h:32

◆ GetWChar()

TWCHAR TNativeFile::GetWChar ( )
virtual

Reads one character of type wchar from the file.

Returns
the read character

Implements TFile.

Definition at line 479 of file TNativeFile_Win.cpp.

480{
481 FlushWriteBuffer();
482
483 if ( m_RBuffer != TNULL )
484 {
485 TUINT32 curBufferPos = m_Position / BUFFER_SIZE * BUFFER_SIZE;
486 if ( ( curBufferPos == m_PrevBufferPos ) && ( m_Position - curBufferPos <= m_LastBufferSize - 1 ) )
487 {
488 TWCHAR c = *TREINTERPRETCAST( TWCHAR*, &m_RBuffer[ m_Position - curBufferPos ] );
489 m_Position += sizeof( c );
490 return c;
491 }
492 }
493
494 TWCHAR result;
495 if ( Read( &result, sizeof( result ) ) != sizeof( result ) )
496 return L'\xFFFF';
497
498 return result;
499}
#define TREINTERPRETCAST(TYPE, VALUE)
Definition Defines.h:68
wchar_t TWCHAR
Definition Typedefs.h:21

◆ Open()

TBOOL TNativeFile::Open ( const TString8 & a_FileName,
TFILEMODE a_Mode )

Definition at line 584 of file TNativeFile_Win.cpp.

585{
586 TASSERT( a_FileName.IsIndexValid( 0 ), "TNativeFile::Open - wrong filename" );
587
588 DWORD dwCreationDisposition = OPEN_EXISTING;
589 DWORD dwDesiredAccess = 0;
590 DWORD dwShareMode = 0;
591
592 dwDesiredAccess |= ( a_Mode & TFILEMODE_READ ) ? GENERIC_READ : dwDesiredAccess;
593 dwDesiredAccess |= ( a_Mode & TFILEMODE_WRITE ) ? GENERIC_WRITE : dwDesiredAccess;
594 dwDesiredAccess |= ( a_Mode & TFILEMODE_READWRITE ) ? ( GENERIC_READ | GENERIC_WRITE ) : dwDesiredAccess;
595
596 if ( a_Mode & TFILEMODE_CREATENEW )
597 {
598 dwShareMode = FILE_SHARE_READ;
599 dwCreationDisposition = CREATE_ALWAYS;
600 dwDesiredAccess |= GENERIC_WRITE;
601 }
602
603 if ( dwDesiredAccess == 0 )
604 {
605 TWARN( "WARNING: File created with no access mode, assuming WRITEONLY\n" );
606 dwDesiredAccess = GENERIC_WRITE;
607 }
608
609 m_Handle = CreateFileA( a_FileName.GetString(), dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, NULL, NULL );
610
611 if ( m_Handle != INVALID_HANDLE_VALUE )
612 {
613 m_Position = 0;
615 m_PrevBufferPos = -1;
617
618 if ( a_Mode & TFILEMODE_NOBUFFER )
619 {
621 }
622 else
623 {
627 }
628 }
629 else
630 {
631 return TFALSE;
632 }
633
634 return TTRUE;
635}
void * TMalloc(TSIZE a_uiSize, Toshi::TMemory::MemBlock *a_pMemBlock, const TCHAR *a_szFileName, TINT a_iLineNum)
Allocates memory from a specific memory block.
Definition TMemory.cpp:973
@ TFILEMODE_READWRITE
Definition TFile.h:40
@ TFILEMODE_READ
Definition TFile.h:38
@ TFILEMODE_NOBUFFER
Definition TFile.h:42
@ TFILEMODE_WRITE
Definition TFile.h:39
@ TFILEMODE_CREATENEW
Definition TFile.h:41
#define TASSERT(X,...)
Definition Defines.h:138
#define TWARN(...)
Definition Defines.h:152
#define TFALSE
Definition Typedefs.h:24
TBOOL IsIndexValid(TINT a_iIndex) const
Definition TString8.h:103
const TCHAR * GetString(TINT a_iIndex=0) const
Definition TString8.cpp:286

◆ PutCChar()

TINT TNativeFile::PutCChar ( TCHAR a_cCharacter)
virtual

Writes one character of type TCHAR to the file.

Returns
code of the written character

Implements TFile.

Definition at line 502 of file TNativeFile_Win.cpp.

503{
504 if ( Write( &a_cCharacter, 1 ) != 1 )
505 {
506 return -1;
507 }
508
509 return a_cCharacter & 0xFF;
510}

◆ PutWChar()

TINT TNativeFile::PutWChar ( TWCHAR a_wcCharacter)
virtual

Writes one character of type wchar to the file.

Returns
number of written bytes

Implements TFile.

Definition at line 513 of file TNativeFile_Win.cpp.

514{
515 if ( Write( &a_wcCharacter, 2 ) != 2 )
516 {
517 return -1;
518 }
519
520 return a_wcCharacter & 0xFFFF;
521}

◆ Read()

TSIZE TNativeFile::Read ( void * a_pDst,
TSIZE a_uiSize )
virtual

Reads specified number of bytes from the file into the buffer.

Parameters
a_pDstdestination buffer
a_uiSizenumber of bytes to read
Returns
the number of read bytes

Implements TFile.

Definition at line 289 of file TNativeFile_Win.cpp.

290{
291 FlushWriteBuffer();
292
293 if ( a_uiSize < 1 ) return 0;
294
295 if ( m_RBuffer != TNULL )
296 {
297 TSIZE readedCount = 0;
298 TSIZE startPos = m_Position;
299 TSIZE curBufferPos = startPos / BUFFER_SIZE * BUFFER_SIZE;
300 TSIZE newBufferPos = ( startPos + a_uiSize ) / BUFFER_SIZE * BUFFER_SIZE;
301 LPVOID curPosBuffer = a_pDst;
302
303 if ( curBufferPos != newBufferPos )
304 {
305 if ( curBufferPos == m_PrevBufferPos )
306 {
307 DWORD readCount = m_LastBufferSize - ( startPos - curBufferPos );
308
309 if ( readCount > 0 )
310 {
311 TUtil::MemCopy( a_pDst, m_RBuffer + startPos - curBufferPos, readCount );
312
313 curPosBuffer = (TCHAR*)a_pDst + readCount;
314 m_Position += readCount;
315 readedCount = readCount;
316 }
317 }
318
319 DWORD toReadCount = newBufferPos - m_Position;
320 curBufferPos = newBufferPos;
321
322 if ( toReadCount > 0 )
323 {
324 TUINT32 readed = ReadUnbuffered( curPosBuffer, toReadCount );
325 curPosBuffer = (TCHAR*)curPosBuffer + readed;
326 readedCount += readed;
327
328 if ( readed != toReadCount )
329 {
330 // end of file?
331 return readedCount;
332 }
333 }
334 }
335
336 if ( readedCount != a_uiSize && LoadBuffer( curBufferPos ) )
337 {
338 a_uiSize -= readedCount;
339 DWORD bufferLeftSize = m_Position - curBufferPos;
340 DWORD readCount = m_LastBufferSize - bufferLeftSize;
341 readCount = TMath::Min<DWORD>( readCount, a_uiSize );
342
343 if ( readCount > 0 )
344 {
345 TUtil::MemCopy( curPosBuffer, m_RBuffer + bufferLeftSize, readCount );
346 m_Position += readCount;
347 readedCount += readCount;
348 }
349 }
350
351 return readedCount;
352 }
353
354 return ReadUnbuffered( a_pDst, a_uiSize );
355}
size_t TSIZE
Definition Typedefs.h:9
TFORCEINLINE const T & Min(const T &a, const T &b)
static void * MemCopy(void *dst, const void *src, TSIZE size)
Definition TUtil.h:90

◆ Seek()

TBOOL TNativeFile::Seek ( TINT a_iOffset,
TSEEK a_eOrigin = TSEEK_CUR )
virtual

Shifts current file cursor based on the specified offset and origin.

Parameters
a_iOffsetnumber of characters to shift the position
a_eOriginposition to which a_iOffset is added
Returns
TTRUE if succeeded

Implements TFile.

Definition at line 426 of file TNativeFile_Win.cpp.

427{
428 FlushWriteBuffer();
429
430 if ( a_eOrigin == TSEEK_SET )
431 {
432 m_Position = a_iOffset;
433 }
434 else if ( a_eOrigin == TSEEK_CUR )
435 {
436 m_Position += a_iOffset;
437 }
438 else if ( a_eOrigin == TSEEK_END )
439 {
440 m_RBufferPosition = SetFilePointer( m_Handle, a_iOffset, TNULL, FILE_END );
441
442 if ( m_RBufferPosition == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR )
443 {
444 return TFALSE;
445 }
446
448 }
449
450 if ( m_Position < 0 ) m_Position = 0;
451
452 return TTRUE;
453}
@ TSEEK_SET
Definition TFile.h:30
@ TSEEK_CUR
Definition TFile.h:31

◆ Tell()

TSIZE TNativeFile::Tell ( )
virtual
Returns
current position of the file cursor

Implements TFile.

Definition at line 419 of file TNativeFile_Win.cpp.

420{
421 FlushWriteBuffer();
422 return m_Position;
423}

◆ WPrintf()

TINT TNativeFile::WPrintf ( const TWCHAR * a_wszFormat,
... )
virtual

Analogue of wprintf but writes result to the file.

Returns
number of characters written

Implements TFile.

Definition at line 541 of file TNativeFile_Win.cpp.

542{
543 va_list args;
544 va_start( args, a_wszFormat );
545
546 TWCHAR str[ 512 ];
547
548 TINT iResult = T2String16::FormatV( str, TARRAYSIZE( str ), a_wszFormat, args );
549
550 Write( str, iResult * sizeof( TWCHAR ) );
551
552 va_end( args );
553
554 return iResult;
555}
static TINT FormatV(TWCHAR *a_pcString, TINT size, const TWCHAR *a_pcFormat, va_list args)

◆ Write()

TSIZE TNativeFile::Write ( const void * a_pSrc,
TSIZE a_uiSize )
virtual

Writes specified number of bytes to the file from the buffer.

Parameters
a_pSrcsourde buffer
a_uiSizenumber of bytes to write
Returns
the number of written bytes

Implements TFile.

Definition at line 358 of file TNativeFile_Win.cpp.

359{
361 {
363 m_RBufferPosition = SetFilePointer( m_Handle, m_Position, NULL, FILE_BEGIN );
364
365 if ( m_RBufferPosition == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR )
366 {
367 return 0;
368 }
369
371 }
372
373 if ( m_WriteBuffered == TFALSE )
374 {
375 DWORD written;
376 BOOL bRes = WriteFile( m_Handle, buffer, size, &written, NULL );
377
378 if ( bRes == FALSE )
379 {
380 return 0;
381 }
382
383 m_RBufferPosition += written;
385 return written;
386 }
387
388 // Flush write buffer if data doesn't fit it
389 if ( m_WriteBufferUsed + size >= BUFFER_SIZE )
390 {
391 FlushWriteBuffer();
392 }
393
394 if ( size < BUFFER_SIZE )
395 {
396 // Data fits the write buffer so append it to it
397 TUtil::MemCopy( m_WBuffer + m_WriteBufferUsed, buffer, size );
398 m_WriteBufferUsed += size;
399 return size;
400 }
401 else
402 {
403 // Data doesn't fit the write buffer at all so write it right now
404 DWORD written;
405 BOOL bRes = WriteFile( m_Handle, buffer, size, &written, NULL );
406
407 if ( bRes != FALSE )
408 {
409 m_RBufferPosition += size;
411 return written;
412 }
413 }
414
415 return 0;
416}

Member Data Documentation

◆ BUFFER_SIZE

TUINT32 TNativeFile::BUFFER_SIZE = 0x800
staticconstexpr

Definition at line 25 of file TNativeFile_Win.h.

◆ m_Handle

void* TNativeFile::m_Handle
protected

Definition at line 59 of file TNativeFile_Win.h.

◆ m_LastBufferSize

TUINT32 TNativeFile::m_LastBufferSize
protected

Definition at line 63 of file TNativeFile_Win.h.

◆ m_Position

TUINT32 TNativeFile::m_Position
protected

Definition at line 60 of file TNativeFile_Win.h.

◆ m_PrevBufferPos

TUINT32 TNativeFile::m_PrevBufferPos
protected

Definition at line 62 of file TNativeFile_Win.h.

◆ m_RBuffer

TCHAR* TNativeFile::m_RBuffer
protected

Definition at line 64 of file TNativeFile_Win.h.

◆ m_RBufferPosition

TUINT32 TNativeFile::m_RBufferPosition
protected

Definition at line 61 of file TNativeFile_Win.h.

◆ m_WBuffer

TCHAR* TNativeFile::m_WBuffer
protected

Definition at line 65 of file TNativeFile_Win.h.

◆ m_WriteBuffered

TBOOL TNativeFile::m_WriteBuffered
protected

Definition at line 67 of file TNativeFile_Win.h.

◆ m_WriteBufferUsed

TUINT32 TNativeFile::m_WriteBufferUsed
protected

Definition at line 66 of file TNativeFile_Win.h.


The documentation for this class was generated from the following files: