📄 pcn_flat.cpp
字号:
/*
** CPQDIF_PC_FlatFile class. Implements a persistence controller
** for a flat file on disk.
** --------------------------------------------------------------------------
**
** File name: $Workfile: pcn_flat.cpp $
** Last modified: $Modtime: 7/27/98 11:14a $
** Last modified by: $Author: Rob $
**
** VCS archive path: $Archive: /Hank/DMM/FirmWare/Level3/ObDatMgr/pcn_flat.cpp $
** VCS revision: $Revision: 4 $
*/
#include "PQDIF_classes.h"
// Construction
// ============
CPQDIF_PC_FlatFile::CPQDIF_PC_FlatFile()
{
m_chunk = NULL;
m_sizeChunk = 0;
m_fname = "";
m_pstream = NULL;
m_pprocHeader = NULL;
m_pprocBody = NULL;
}
CPQDIF_PC_FlatFile::~CPQDIF_PC_FlatFile()
{
if( m_pstream )
delete m_pstream;
if( m_pprocHeader )
delete m_pprocHeader;
if( m_pprocBody )
delete m_pprocBody;
}
bool CPQDIF_PC_FlatFile::ReadHeaders( void )
{
bool status = FALSE;
LINKABS4 posNext = 0;
// Get rid of any old stream hanging around
if( m_pstream )
{
delete m_pstream;
m_pstream = NULL;
}
// Are we dealing with a chunk or a file?
if( m_chunk )
m_whichStream = PSIO_Chunk;
else
m_whichStream = PSIO_FlatFile;
// Create streams and default processors
m_pstream = theFactory.NewStreamIO( m_whichStream );
m_pprocHeader = theFactory.NewStreamProcessor( ID_COMP_ALG_NONE );
m_pprocBody = theFactory.NewStreamProcessor( ID_COMP_ALG_NONE );
// Open the stream
if( m_whichStream == PSIO_Chunk )
{
((CPQDIF_S_Chunk *) m_pstream)->SetInput( m_chunk, m_sizeChunk );
}
else
{
((CPQDIF_S_FlatFile *) m_pstream)->Open( m_fname.c_str(), true );
}
// Read first header w/NOTHING processors
m_pstream->ConnectProcessor( m_pprocHeader );
CPQDIFRecord * precord = theFactory.NewRecord( PFR_Record );
if( precord )
{
status = precord->ReadHeader( m_pstream );
if( status )
{
// Go ahead and read the first record body
status = precord->ReadBody( m_pstream );
if( status )
{
bool foundCompInfo = FALSE;
UINT4 styleComp;
UINT4 algComp;
CPQDIF_R_Container * pcont;
// Assume it's a container and try to get compression info
pcont = (CPQDIF_R_Container *) precord;
foundCompInfo = pcont->GetCompressionInfo( styleComp, algComp );
// Do we need to change processors?
if( foundCompInfo )
{
switch( styleComp )
{
case ID_COMP_STYLE_TOTALFILE:
// Unsupported
status = FALSE;
break;
case ID_COMP_STYLE_RECORDLEVEL:
// Yup! Check what algorithm we should use...
delete m_pprocBody;
m_pprocBody = theFactory.NewStreamProcessor( algComp );
// Can we support it?
if( !m_pprocBody )
status = FALSE;
break;
case ID_COMP_STYLE_NONE:
default:
// Do nothing
break;
}
}
if( status )
{
// Done -- add the first record to the array
m_arrayRecords.Add( precord );
}
} // Read body OK
} // Read header OK
} // Record object created OK
// Read through record headers and build list
while( status )
{
status = precord->HeaderGetPosNextRecord( posNext );
// Are we at the end?
if( status && posNext == 0 )
break;
if( status )
{
m_pstream->SeekPos( posNext );
precord = theFactory.NewRecord( PFR_Record );
if( precord )
{
status = precord->ReadHeader( m_pstream );
if( status )
{
m_arrayRecords.Add( precord );
}
}
}
} // while()
// If this is a chunk, we need to force it to read in ALL records
// immediately.
if( status && m_whichStream == PSIO_Chunk )
{
for( long idxRec = 0; idxRec < GetRecordCount(); idxRec++ )
{
GetRecordFull( idxRec );
}
}
m_state = pqpc_Empty;
return status;
}
CPQDIFRecord * CPQDIF_PC_FlatFile::GetRecordFull( long index )
{
bool status = TRUE;
CPQDIFRecord * prec = NULL;
prec = GetRecord( index );
ASSERT_VALID( prec );
if( prec && m_pstream && m_pprocBody )
{
// Connect the appropriate processor
m_pstream->ConnectProcessor( m_pprocBody );
// Read the body
status = prec->ReadBody( m_pstream );
}
// If it didn't work, don't return the record pointer
if( !status )
prec = NULL;
return prec;
}
bool CPQDIF_PC_FlatFile::WriteNew( void )
{
bool status = FALSE;
CPQDIF_StreamIO * pstrmWrite;
CPQDIF_StreamProcessor * pprocWriteHeader;
CPQDIF_StreamProcessor * pprocWriteBody;
CPQDIF_StreamProcessor * pprocWriteBodyFirstRecord;
// We should have a new file name, but
// the original streams and processors should still be around.
// We will use these if we need to read the full records
// into memory.
// Are we dealing with a chunk or a file?
if( m_chunk )
m_whichStream = PSIO_Chunk;
else
m_whichStream = PSIO_FlatFile;
// Init the basic stuff
pstrmWrite = theFactory.NewStreamIO( m_whichStream );
pprocWriteHeader = theFactory.NewStreamProcessor( ID_COMP_ALG_NONE );
pprocWriteBodyFirstRecord = theFactory.NewStreamProcessor( ID_COMP_ALG_NONE );
// Init the streams
if( m_whichStream == PSIO_Chunk )
{
// need to fix
//((CPQDIF_S_Chunk *) pstrmWrite)->SetInput( m_chunk, m_sizeChunk );
}
else
{
((CPQDIF_S_FlatFile *) pstrmWrite)->New( m_fname.c_str() );
}
// Should we use a different body processor?
switch( GetCompressionStyle() )
{
case ID_COMP_STYLE_TOTALFILE:
// We don't support this yet
pprocWriteBody = NULL;
break;
case ID_COMP_STYLE_RECORDLEVEL:
pprocWriteBody = theFactory.NewStreamProcessor( GetCompressionAlgorithm() );
break;
case ID_COMP_STYLE_NONE:
default:
pprocWriteBody = theFactory.NewStreamProcessor( ID_COMP_ALG_NONE );
break;
}
// Check all the objects
if( pstrmWrite && pprocWriteHeader && pprocWriteBody && pprocWriteBodyFirstRecord )
{
status = TRUE;
}
else
{
status = FALSE;
}
// Write the file out
if( status )
{
long countRecords;
long idxRecord;
LINKABS4 posCurrentRecord;
LINKABS4 posCurrentRecordBody;
LINKABS4 posNextRecord;
SIZE4 sizeHeader;
SIZE4 sizeBody;
CPQDIFRecord * precord;
// Init
countRecords = m_arrayRecords.GetSize();
posCurrentRecord = 0;
posNextRecord = 0;
for( idxRecord = 0; idxRecord < countRecords; idxRecord++ )
{
precord = (CPQDIFRecord *) m_arrayRecords[ idxRecord ];
if( !precord )
break;
// MAKE SURE THE BODY IS UP-TO-DATE
// -- This must be done before we start making any changes
// to the record; otherwise, it will read using information
// from the new file rather than the old one.
if( m_pstream && m_pprocBody )
{
m_pstream->ConnectProcessor( m_pprocBody );
precord->ReadBody( m_pstream );
}
// GET HEADER INFO (& set initial header info)
posCurrentRecord = posNextRecord;
precord->HeaderSetPos( posCurrentRecord );
precord->HeaderGetSize( sizeHeader, sizeBody );
posCurrentRecordBody = posCurrentRecord + sizeHeader;
// WRITE THE BODY
// Connect the appropriate body processor
// (The first record is a little different)
if( idxRecord == 0 )
pstrmWrite->ConnectProcessor( pprocWriteBodyFirstRecord );
else
pstrmWrite->ConnectProcessor( pprocWriteBody );
status = precord->WriteBody( pstrmWrite );
// This determines where the next record will go
// (a new posNextRecord).
pstrmWrite->GetPos( posNextRecord );
// UPDATE THE HEADER INFO (w/next record pos and body size)
sizeBody = (SIZE4) ( posNextRecord - posCurrentRecordBody );
precord->HeaderSetSize( sizeHeader, sizeBody );
if( idxRecord == (countRecords - 1) )
{
// Last record!
posNextRecord = 0;
}
precord->HeaderSetPosNextRecord( posNextRecord );
// WRITE THE HEADER
pstrmWrite->ConnectProcessor( pprocWriteHeader );
status = precord->WriteHeader( pstrmWrite );
}
pstrmWrite->Flush();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -