home *** CD-ROM | disk | FTP | other *** search
- /*----------------------------------------------------------------------------*/
- /* Memory Compression sample using The Xceed Zip Compression Library 4 */
- /* For C++Builder 4 */
- /* Copyright (c) 1999 Xceed Software Inc. */
- /*----------------------------------------------------------------------------*/
-
- #include <vcl.h>
- #pragma hdrstop
-
- #include "MsgStack.h"
-
- #pragma package(smart_init)
- #pragma link "XceedZipLib_OCX"
- #pragma resource "*.dfm"
-
- TfrmMsgStack *frmMsgStack;
-
- //
- // Constructor
- //
- __fastcall TfrmMsgStack::TfrmMsgStack(TComponent* Owner)
- : TForm(Owner)
- {
- m_pHead = NULL;
- m_nCount = 0;
- }
-
- //
- // Destructor
- //
- __fastcall TfrmMsgStack::~TfrmMsgStack( void )
- {
- while( m_pHead )
- {
- TCompressedMessage* pCurrent = m_pHead;
- m_pHead = m_pHead->pNext;
-
- delete pCurrent;
- }
-
- m_nCount = 0;
- }
-
- //
- // Compressing a string
- //
- void __fastcall TfrmMsgStack::btAddClick(TObject *Sender)
- {
- // If you assign a string to a Variant object, it will convert the
- // string to a WIDE string. The best thing to use is a variant array.
- long lOrigSize = mmoAdd->GetTextLen() + 1; // for the null char
- Variant vaSource = VarArrayCreate( OPENARRAY(int, (0, lOrigSize-1)), varByte );
-
- mmoAdd->GetTextBuf( ( char * )VarArrayLock( vaSource ), lOrigSize );
- VarArrayUnlock( vaSource );
-
- // Allocate a stack item
- TCompressedMessage* pNew = new TCompressedMessage;
-
- // To compress, we need to feed the compressor a variant array of bytes or
- // a variant string (BSTR). It will always yield a variant array of bytes.
- xcdCompressionError xResult = xComp->Compress( vaSource, pNew->vaCompressed, true );
-
- if( xResult == xceSuccess )
- {
- pNew->pNext = m_pHead;
- m_pHead = pNew;
- m_nCount++;
- edtCount->Text = IntToStr( ( int )m_nCount );
- btRemove->Enabled = true;
-
- long lCompSize = VarArrayHighBound( pNew->vaCompressed, 1 )
- - VarArrayLowBound( pNew->vaCompressed, 1 ) + 1;
-
- ShowMessage( AnsiString( "Compressed a " ) + IntToStr( ( int )lOrigSize ) +
- " byte(s) message to a " + IntToStr( ( int )lCompSize ) + " bytes blob." );
- }
- else
- {
- delete pNew;
- ShowMessage( xComp->GetErrorDescription( xResult ) );
- }
- }
-
- //
- // Uncompressing a compressed buffer
- //
- void __fastcall TfrmMsgStack::btRemoveClick(TObject *Sender)
- {
- if( m_pHead )
- {
- TCompressedMessage* pCurrent = m_pHead;
- m_pHead = m_pHead->pNext;
-
- // To uncompress, we need to feed the compressor the original data as a
- // variant array of bytes. It will always yield in vaDest a variant array
- // of bytes.
- Variant vaDest;
- xcdCompressionError xResult = xComp->Uncompress( pCurrent->vaCompressed, vaDest, true );
-
- // Let's dispose of item before analysing result
- m_nCount--;
- delete pCurrent;
- edtCount->Text = IntToStr( ( int )m_nCount );
- btRemove->Enabled = ( m_nCount > 0 );
-
- if( xResult == xceSuccess )
- {
- mmoRemoved->SetTextBuf( ( char * )VarArrayLock( vaDest ) );
- VarArrayUnlock( vaDest );
- }
- else
- {
- mmoRemoved->Text = "";
- ShowMessage( xComp->GetErrorDescription( xResult ) );
- }
- }
- }
-
-