📄 str_flat.cpp
字号:
/*
** CPQDIF_S_FlatFile class. A stream I/O implementing which supports the
** standard flat file. This is the normal persistence mechanism for PQDIF.
** --------------------------------------------------------------------------
**
** File name: $Workfile: str_flat.cpp $
** Last modified: $Modtime: 3/24/98 11:16a $
** Last modified by: $Author: Rob $
**
** VCS archive path: $Archive: /Hank/DMM/FirmWare/Level3/ObDatMgr/str_flat.cpp $
** VCS revision: $Revision: 5 $
*/
#include "PQDIF_classes.h"
// Local constants
// ===============
const int defaultIncrementSize = 64;
// Construction
// ============
CPQDIF_S_FlatFile::CPQDIF_S_FlatFile()
{
m_pf = NULL;
}
CPQDIF_S_FlatFile::~CPQDIF_S_FlatFile()
{
if( m_pf )
{
fclose( m_pf );
}
}
bool CPQDIF_S_FlatFile::Open
(
const char * fname,
bool readOnly
)
{
bool status = FALSE;
char * flagsOpen = "";
if( readOnly )
{
// Read-only
flagsOpen = "rb";
m_canWriteFull = FALSE;
m_canWriteInc = FALSE;
}
else
{
// Read/write
flagsOpen = "r+b";
m_canWriteFull = TRUE;
m_canWriteInc = TRUE;
}
if( !m_pf )
{
m_pf = fopen( fname, flagsOpen );
if( m_pf )
status = TRUE;
}
return status;
}
bool CPQDIF_S_FlatFile::New( const char * fname )
{
bool status = FALSE;
// Init
m_canWriteFull = FALSE;
m_canWriteInc = FALSE;
if( !m_pf )
{
m_pf = fopen( fname, "w+b" );
if( m_pf )
{
m_canWriteFull = TRUE;
m_canWriteInc = TRUE;
status = TRUE;
}
}
return status;
}
bool CPQDIF_S_FlatFile::SeekPos( long pos )
{
bool status = FALSE;
if( m_pf )
{
int rc = fseek( m_pf, pos, SEEK_SET );
if( rc == 0 )
status = TRUE;
}
return status;
}
bool CPQDIF_S_FlatFile::SeekEnd( void )
{
bool status = FALSE;
if( m_pf )
{
int rc = fseek( m_pf, 0, SEEK_END );
if( rc == 0 )
status = TRUE;
}
return status;
}
bool CPQDIF_S_FlatFile::GetPos( long& pos )
{
bool status = FALSE;
if( m_pf )
{
pos = ftell( m_pf );
status = TRUE;
}
return status;
}
BYTE * CPQDIF_S_FlatFile::ReadBlock( long size, long& actualSize )
{
BYTE * buffRet = NULL;
long sizeRead;
bool status = FALSE;
// Init
sizeRead = 0;
// Clear buffers
m_buffRead.SetSize( 0, defaultIncrementSize );
m_posRead = 0;
m_buffWrite.SetSize( 0, defaultIncrementSize );
if( m_pf )
{
// End of file yet?
if( ! feof( m_pf ) )
{
// Nope, read a buffer full...
// Size the buffer first
m_buffRead.SetSize( size, defaultIncrementSize );
// Attempt to read the block
sizeRead = (long) fread( (BYTE*) m_buffRead.GetData(), 1, size, m_pf );
if( sizeRead > 0 )
{
// We got the block -- now decode it.
m_buffRead.SetSize( sizeRead, defaultIncrementSize );
status = ExecuteProcessorDecode();
if( status )
{
// Pass back the decoded buffer
buffRet = m_buffWrite.GetData();
actualSize = (long) m_buffWrite.GetSize();
}
}
}
}
return buffRet;
}
bool CPQDIF_S_FlatFile::WriteBlock( long &sizeActual )
{
bool status = FALSE;
long sizeWritten;
m_buffWrite.SetSize( 0, defaultIncrementSize );
sizeActual = 0;
if( m_pf )
{
// End of file yet?
if( ! feof( m_pf ) )
{
// Encode the block
status = ExecuteProcessorEncode();
if( status )
{
// The write buffer should contain the output
sizeActual = m_buffWrite.GetSize();
// Attempt to read the block
sizeWritten = (long) fwrite( (BYTE*) m_buffWrite.GetData(), 1, sizeActual, m_pf );
if( sizeWritten == sizeActual )
{
status = TRUE;
}
}
}
}
return status;
}
void CPQDIF_S_FlatFile::Flush( void )
{
if( m_pf )
{
fflush( m_pf );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -