home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipComp.exe / XceedZip.Cab / F112511_MsgStack.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-17  |  3.3 KB  |  120 lines

  1. /*----------------------------------------------------------------------------*/
  2. /* Memory Compression sample using The Xceed Zip Compression Library 4        */
  3. /* For C++Builder 3                                                           */
  4. /* Copyright (c) 1999 Xceed Software Inc.                                     */
  5. /*----------------------------------------------------------------------------*/
  6.  
  7. #include <vcl.h>
  8. #pragma hdrstop
  9.  
  10. #include "MsgStack.h"
  11.  
  12. #pragma package(smart_init)
  13. #pragma link "XceedZipLib_TLB"
  14. #pragma resource "*.dfm"
  15.  
  16. TfrmMsgStack *frmMsgStack;
  17.  
  18. //
  19. // Constructor
  20. //
  21. __fastcall TfrmMsgStack::TfrmMsgStack(TComponent* Owner)
  22.   : TForm(Owner)
  23. {
  24.   m_pHead   = NULL;
  25.   m_nCount  = 0;
  26. }
  27.  
  28. //
  29. // Destructor
  30. //
  31. __fastcall TfrmMsgStack::~TfrmMsgStack( void )
  32. {
  33.   while( m_pHead )
  34.   {
  35.     TCompressedMessage* pCurrent  = m_pHead;
  36.     m_pHead = m_pHead->pNext;
  37.  
  38.     delete pCurrent;
  39.   }
  40.  
  41.   m_nCount  = 0;
  42. }
  43.  
  44. //
  45. // Compressing a string
  46. //
  47. void __fastcall TfrmMsgStack::btAddClick(TObject *Sender)
  48. {
  49.   // If you assign a string to a Variant object, it will convert the
  50.   // string to a WIDE string. The best thing to use is a variant array.
  51.   long    lOrigSize = mmoAdd->GetTextLen() + 1; // for the null char
  52.   Variant vaSource  = VarArrayCreate( OPENARRAY(int, (0, lOrigSize-1)), varByte );
  53.  
  54.   mmoAdd->GetTextBuf( ( char * )VarArrayLock( vaSource ), lOrigSize );
  55.   VarArrayUnlock( vaSource );
  56.  
  57.   // Allocate a stack item
  58.   TCompressedMessage* pNew  = new TCompressedMessage;
  59.  
  60.   // To compress, we need to feed the compressor a variant array of bytes or
  61.   // a variant string (BSTR). It will always yield a variant array of bytes.
  62.   xcdCompressionError xResult = xComp->Compress( vaSource, pNew->vaCompressed, true );
  63.  
  64.   if( xResult == xceSuccess )
  65.   {
  66.     pNew->pNext = m_pHead;
  67.     m_pHead = pNew;
  68.     m_nCount++;
  69.     edtCount->Text  = IntToStr( m_nCount );
  70.     btRemove->Enabled = true;
  71.  
  72.     long lCompSize  = VarArrayHighBound( pNew->vaCompressed, 1 )
  73.                     - VarArrayLowBound( pNew->vaCompressed, 1 ) + 1;
  74.  
  75.     ShowMessage( AnsiString( "Compressed a " ) + IntToStr( lOrigSize ) +
  76.                  " byte(s) message to a " + IntToStr( lCompSize ) + " bytes blob." );
  77.   }
  78.   else
  79.   {
  80.     delete pNew;
  81.     ShowMessage( xComp->GetErrorDescription( xResult ) );
  82.   }
  83. }
  84.  
  85. //
  86. // Uncompressing a compressed buffer
  87. //
  88. void __fastcall TfrmMsgStack::btRemoveClick(TObject *Sender)
  89. {
  90.   if( m_pHead )
  91.   {
  92.     TCompressedMessage* pCurrent  = m_pHead;
  93.     m_pHead = m_pHead->pNext;
  94.  
  95.     // To uncompress, we need to feed the compressor the original data as a
  96.     // variant array of bytes. It will always yield in vaDest a variant array
  97.     // of bytes.
  98.     Variant vaDest;
  99.     xcdCompressionError xResult = xComp->Uncompress( pCurrent->vaCompressed, vaDest, true );
  100.  
  101.     // Let's dispose of item before analysing result
  102.     m_nCount--;
  103.     delete pCurrent;
  104.     edtCount->Text  = IntToStr( m_nCount );
  105.     btRemove->Enabled = ( m_nCount > 0 );
  106.  
  107.     if( xResult == xceSuccess )
  108.     {
  109.       mmoRemoved->SetTextBuf( ( char * )VarArrayLock( vaDest ) );
  110.       VarArrayUnlock( vaDest );
  111.     }
  112.     else
  113.     {
  114.       mmoRemoved->Text  = "";
  115.       ShowMessage( xComp->GetErrorDescription( xResult ) );
  116.     }
  117.   }
  118. }
  119.  
  120.