⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcn_flat.cpp

📁 电能质量交换格式转换库
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*
**  CPQDIF_PC_FlatFile class. Implements a persistence controller
**  for a flat file on disk.
**  --------------------------------------------------------------------------
**
**  File name:          $Workfile: pcn_flat.cpp $
**  Last modified:      $Modtime: 4/20/99 4:07p $
**  Last modified by:   $Author: Jamie $
**
**  VCS archive path:   $Archive: /Hank/DMM/FirmWare/Level3/ObDatMgr/pcn_flat.cpp $
**  VCS revision:       $Revision: 6 $ 
*/
#include "PQDIF_classes.h"


//  Construction
//  ============

CPQDIF_PC_FlatFile::CPQDIF_PC_FlatFile()
    {
    m_chunk = NULL;
    m_sizeChunk = 0;

    m_fname = _T( "" );

    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::ImportContainerAndRecordsFromChunk( BYTE * chunk, long size, bool &bCompressed )
    {
    bool        status = false;
    //
    //
    //  Initialize compression flag
    //
    bCompressed = false;

    //  Get rid of any old stream and processors hanging around
    if( m_pstream )
        {
        delete m_pstream;
        m_pstream = NULL;
        }
    if( m_pprocHeader )
        {
        delete m_pprocHeader;
        m_pprocHeader = NULL;
        }
    if( m_pprocBody )
        {
        delete m_pprocBody;
        m_pprocBody = NULL;
        }

    if( chunk == NULL || size <= 0)
        return false;

    //  Create streams and default processors
    m_pstream     = theFactory.NewStreamIO( PSIO_Chunk );
    m_pprocHeader = theFactory.NewStreamProcessor( ID_COMP_ALG_NONE );
    m_pprocBody   = theFactory.NewStreamProcessor( ID_COMP_ALG_NONE );

    //  Open the stream
    ((CPQDIF_S_Chunk *) m_pstream)->SetInput( chunk, size );

    //  Read first header w/NOTHING processors
    m_pstream->ConnectProcessor( m_pprocHeader );

    LINKABS4    posNext = 0;

    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 );
                            if (m_pprocBody)
                                {
                                bCompressed = true;
                                }
                            else
                                {
                                //  Can we support it?
                                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
        if (!status)
            delete precord;
        }	//	Record object created OK

    //  Read through record headers and build list
    while( status )
        {
        //
        //
        //  This function always returns true so we
        //  must check the position.  If the link recod
        //  is zero, then we are done.  If we get a chunk
        //  that doesn't set this to zero, then we will
        //  stop when the position is checked against the
        //  size in the statement that follows.
        //
        precord->HeaderGetPosNextRecord( posNext );
        //
        //
        //  Are we at the end?
        //
        if( posNext > 0 && posNext < size)
            {
            m_pstream->SeekPos( posNext );

            precord = theFactory.NewRecord( PFR_Record );
            if( precord )
                {
                status = precord->ReadHeader( m_pstream );
                if( status )
                    {
                    m_arrayRecords.Add( precord );
                    }
                }
            }
        else
            {
            //
            //
            // yep, we are at the end
            //  use a break so the status flag is true indicating
            //  no failure so far.
            //
            break;
            }

        }   //  while()

    if( status )
        {
        for( long idxRec = 0; idxRec < GetRecordCount(); idxRec++ )
            {
            GetRecordFull( idxRec );
            }
        }

    m_state = pqpc_Modified;

    return status;
    }

//
//
//  This function imports record from a chunk and appends them
//  to the internal array of records.
//
bool CPQDIF_PC_FlatFile::ImportRecordsFromChunk( BYTE * chunk, long size, bool bCompressed )
    {
    bool        status = false;

    //  Get rid of any old stream and processors hanging around
    if( m_pstream )
        {
        delete m_pstream;
        m_pstream = NULL;
        }
    if( m_pprocHeader )
        {
        delete m_pprocHeader;
        m_pprocHeader = NULL;
        }
    if( m_pprocBody )
        {
        delete m_pprocBody;
        m_pprocBody = NULL;
        }

    if( chunk == NULL || size <= 0)
        return status;
    //
    //
    //  Create streams and default processors
    //
    m_pstream     = theFactory.NewStreamIO( PSIO_Chunk );
    m_pprocHeader = theFactory.NewStreamProcessor( ID_COMP_ALG_NONE );

    if (bCompressed)
        m_pprocBody   = theFactory.NewStreamProcessor( ID_COMP_ALG_ZLIB );
    else
        m_pprocBody   = theFactory.NewStreamProcessor( ID_COMP_ALG_NONE );

    //  Open the stream
    ((CPQDIF_S_Chunk *) m_pstream)->SetInput( chunk, size );

    LINKABS4        posNext = 0;
    CPQDIFRecord *  precord;

    status = true;
    //  Read through record headers and build list
    while( status && posNext < size)
        {
        //
        //
        //  Cue up to next record in stream
        //
        m_pstream->SeekPos( posNext );
        //
        //
        //  Make a new record object
        //
        precord = theFactory.NewRecord( PFR_Record );
        if( precord )
            {
            //
            //
            //  Read the record header with the appropriate
            //  stream processor (no compression)
            //
            m_pstream->ConnectProcessor( m_pprocHeader );
            status = precord->ReadHeader( m_pstream );
            if( status )
                {
                //
                //
                //  Read the body with the appropriate stream
                //  processor (may or may not do compression)
                //
                m_pstream->ConnectProcessor( m_pprocBody );
                status = precord->ReadBody( m_pstream );
                if (status)
                    {
                    //
                    //
                    //  Add the record to our array
                    //
                    m_arrayRecords.Add( precord );
                    //
                    //
                    //  This function always returns true so we
                    //  must check the position.  If the link recod
                    //  is zero, then we are done.  If we get a chunk
                    //  that doesn't set this to zero, then we will
                    //  die when the position is checked against the
                    //  size in the while statement
                    //
                    precord->HeaderGetPosNextRecord( posNext );
                    if (posNext == 0)
                        break;
                    }
                }
            else
                {
                //
                //
                //  if we got here, we probably tried to read past the
                //  end of the stream so we clean up.
                //
                delete precord;
                }
            }
        }   //  while()

//    m_state = pqpc_Empty;
    m_state = pqpc_Modified;

    return status;
    }

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 )
        {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -