home *** CD-ROM | disk | FTP | other *** search
- /*
- Xceed Binary Encoding Library - Memory Encode Sample
- Copyright (c) 2001 Xceed Software Inc.
-
- This sample demonstrates how to encode a chunk of memory data using
- different encoding methods and how to decode encoded memory data.
- It specifically uses:
- - The Encode and Decode methods
- - The ContinueOnInvalidData, IncludeHeaderFooter, EndOfLineType,
- MaxLineLength, HeaderDataForkLength, HeaderResourceForkLength,
- and the EncodingFormat properties.
-
- This file is part of the Xceed Binary Encoding Library sample applications.
- The source code in this file is only intended as a supplement to the
- Xceed Binary Encoding Library's documentation and is provided "as is",
- without warranty of any kind, either expressed or implied.
- */
-
- #include <vcl.h>
- #pragma hdrstop
-
- #include "unMemEncoding.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma link "XceedBinaryEncodingLib_OCX"
- #pragma link "XceedBinaryEncodingLib_TLB"
- #pragma resource "*.dfm"
- TfrmMemEncoding *frmMemEncoding;
- //---------------------------------------------------------------------------
- __fastcall TfrmMemEncoding::TfrmMemEncoding(TComponent* Owner)
- : TForm(Owner)
- {
- }
-
- //---------------------------------------------------------------------------
- // Initialize the decoded file size label and the combo boxes. For the
- // purposes of this example, the combo box items were entered using
- // C++Builder's property box.
- //---------------------------------------------------------------------------
- void __fastcall TfrmMemEncoding::FormCreate(TObject *Sender)
- {
- cboEncodingMethod->ItemIndex = 0;
- cboEndOfLineType->ItemIndex = 1;
-
- lblDecodedSize->Caption = IntToStr( mmoDecodedText->Text.Length() );
- }
-
- //---------------------------------------------------------------------------
- // Update the encoded size label when the user modifies the encoded text
- //---------------------------------------------------------------------------
- void __fastcall TfrmMemEncoding::mmoEncodedTextChange(TObject *Sender)
- {
- lblEncodedSize->Caption = IntToStr( mmoEncodedText->Text.Length() );
- }
-
- //---------------------------------------------------------------------------
- // Update the decoded size label when the user modifies the decoded text
- //---------------------------------------------------------------------------
- void __fastcall TfrmMemEncoding::mmoDecodedTextChange(TObject *Sender)
- {
- lblDecodedSize->Caption = IntToStr( mmoDecodedText->Text.Length() );
- }
-
- //---------------------------------------------------------------------------
- // Prepare the encoding format accoring to the user. Return true is successful
- //---------------------------------------------------------------------------
- bool TfrmMemEncoding::PrepareEncodingFormat( IXceedBinaryEncodingPtr& piEncoder )
- {
- try
- {
- switch( cboEncodingMethod->ItemIndex )
- {
- case 0 : //UUEncode
- {
- IXceedUUEncodingFormatPtr piUUFormat;
- piUUFormat.CreateInstance( CLSID_XceedUUEncodingFormat );
-
- // We don't want any header/footer since this is only a memory
- // encoding process. The header/footer are mainly used with files
- piUUFormat->IncludeHeaderFooter = false;
- piUUFormat->EndOfLineType = EXBEndOfLineType( cboEndOfLineType->ItemIndex );
-
- // Set the maximum line length specified by the user. This is a mandatory
- // value as the EndOfLineType cannot be None for UUEncoding.
- piUUFormat->MaxLineLength = StrToInt( edtMaxLineLength->Text );
-
- // Set the previously initialized Encoding format of the Encoder object
- // received as a parameter.
- piEncoder->EncodingFormat = piUUFormat;
-
- break;
- }
- case 1 : //XXEncode
- {
- IXceedXXEncodingFormatPtr piXXFormat;
- piXXFormat.CreateInstance( CLSID_XceedXXEncodingFormat );
-
- piXXFormat->IncludeHeaderFooter = false;
- piXXFormat->EndOfLineType = EXBEndOfLineType( cboEndOfLineType->ItemIndex );
-
- // Set the maximum line length specified by the user. This is a mandatory
- // value as the EndOfLineType cannot be None for XXEncoding.
- piXXFormat->MaxLineLength = StrToInt( edtMaxLineLength->Text );
- piEncoder->EncodingFormat = piXXFormat;
-
- break;
- }
- case 2 : //HexaDecimal
- {
- IXceedHexaEncodingFormatPtr piHexaFormat;
- piHexaFormat.CreateInstance( CLSID_XceedHexaEncodingFormat );
-
- piHexaFormat->EndOfLineType = EXBEndOfLineType( cboEndOfLineType->ItemIndex );
-
- // Set the maximum line length specified by the user. This value will
- // be ignored by the Xceed Binary Encoding Library if the EndOfLineType
- // is set to None.
- piHexaFormat->MaxLineLength = StrToInt( edtMaxLineLength->Text );
- piEncoder->EncodingFormat = piHexaFormat;
-
- break;
- }
- case 3 : //Base 64
- {
- IXceedBase64EncodingFormatPtr piBase64Format;
- piBase64Format.CreateInstance( CLSID_XceedBase64EncodingFormat );
-
- piBase64Format->EndOfLineType = EXBEndOfLineType( cboEndOfLineType->ItemIndex );
-
- // Set the maximum line length specified by the user. This value will
- // be ignored by the Xceed Binary Encoding Library if the EndOfLineType
- // is set to None.
- piBase64Format->MaxLineLength = StrToInt( edtMaxLineLength->Text );
- piEncoder->EncodingFormat = piBase64Format;
-
- break;
- }
- case 4 : //Quoted Printable
- {
- IXceedQuotedPrintableEncodingFormatPtr piQPFormat;
- piQPFormat.CreateInstance( CLSID_XceedQuotedPrintableEncodingFormat );
-
- piQPFormat->EndOfLineType = EXBEndOfLineType( cboEndOfLineType->ItemIndex );
-
- // Set the maximum line length specified by the user. This value will
- // be ignored by the Xceed Binary Encoding Library if the EndOfLineType
- // is set to None.
- piQPFormat->MaxLineLength = StrToInt( edtMaxLineLength->Text );
- piEncoder->EncodingFormat = piQPFormat;
-
- break;
- }
- case 5 : //BinHex
- {
- IXceedBinHexEncodingFormatPtr piBinHexFormat;
- piBinHexFormat.CreateInstance( CLSID_XceedBinHexEncodingFormat );
-
- piBinHexFormat->EndOfLineType = EXBEndOfLineType( cboEndOfLineType->ItemIndex );
-
- // Set the maximum line length specified by the user. This value will
- // be ignored by the Xceed Binary Encoding Library if the EndOfLineType
- // is set to None.
- piBinHexFormat->MaxLineLength = StrToInt( edtMaxLineLength->Text );
-
- // For the BinHex format, we must specify the data fork length and
- // the resource fork length. The resource fork is not mandatory.
- // The DataForkLength is mandatory and must be set to the size
- // of the data that will be encoded.
- piBinHexFormat->HeaderDataForkLength = mmoDecodedText->Text.Length();
-
- // The ResouceForkLength, used by MAC systems, is not relevant under
- // a PC system so we set it to 0
- piBinHexFormat->HeaderResourceForkLength = 0;
- piEncoder->EncodingFormat = piBinHexFormat;
-
- break;
- }
- }
- return true;
- }
- catch( Exception& xErr )
- {
- ShowMessage( xErr.Message );
- return false;
- }
- catch( ... )
- {
- ShowMessage( "An unexpected error occured while preparing "
- "the encoding format" );
- return false;
- }
- }
-
- //---------------------------------------------------------------------------
- // Prepare the decoding format accoring to the user. Return true is successful
- //---------------------------------------------------------------------------
- bool TfrmMemEncoding::PrepareDecodingFormat( IXceedBinaryEncodingPtr& piEncoder )
- {
- try
- {
- switch( cboEncodingMethod->ItemIndex )
- {
- case 0 : //UUEncode
- {
- IXceedUUEncodingFormatPtr piUUFormat;
- piUUFormat.CreateInstance( CLSID_XceedUUEncodingFormat );
-
- // The encoded string does not contain and header/footer.
- // The header/footer are usually present only in files.
- piUUFormat->IncludeHeaderFooter = false;
-
- // We want to ignore invalid encoded characters
- piUUFormat->ContinueOnInvalidData = true;
- piEncoder->EncodingFormat = piUUFormat;
-
- break;
- }
- case 1 : //XXEncode
- {
- IXceedXXEncodingFormatPtr piXXFormat;
- piXXFormat.CreateInstance( CLSID_XceedXXEncodingFormat );
-
- piXXFormat->IncludeHeaderFooter = false;
- piXXFormat->ContinueOnInvalidData = true;
- piEncoder->EncodingFormat = piXXFormat;
-
- break;
- }
- case 2 : //HexaDecimal
- {
- IXceedHexaEncodingFormatPtr piHexaFormat;
- piHexaFormat.CreateInstance( CLSID_XceedHexaEncodingFormat );
-
- piHexaFormat->ContinueOnInvalidData = true;
- piEncoder->EncodingFormat = piHexaFormat;
-
- break;
- }
- case 3 : //Base64
- {
- IXceedBase64EncodingFormatPtr piBase64Format;
- piBase64Format.CreateInstance( CLSID_XceedBase64EncodingFormat );
-
- piBase64Format->ContinueOnInvalidData = true;
- piEncoder->EncodingFormat = piBase64Format;
-
- break;
- }
- case 4 : //QuotedPrintable
- {
- IXceedQuotedPrintableEncodingFormatPtr piQPFormat;
- piQPFormat.CreateInstance( CLSID_XceedQuotedPrintableEncodingFormat );
-
- piQPFormat->ContinueOnInvalidData = true;
- piEncoder->EncodingFormat = piQPFormat;
-
- break;
- }
- case 6 : //BinHex
- {
- IXceedBinHexEncodingFormatPtr piBinHexFormat;
- piBinHexFormat.CreateInstance( CLSID_XceedBinHexEncodingFormat );
-
- // The encoded string does not contain any header/footer.
- // The header/footer are usually present only in files.
- piBinHexFormat->IncludeHeaderFooter = false;
- piBinHexFormat->ContinueOnInvalidData = true;
- piEncoder->EncodingFormat = piBinHexFormat;
-
- break;
- }
- }
- return true;
- }
- catch( Exception& xErr )
- {
- ShowMessage( xErr.Message );
- return false;
- }
- catch( ... )
- {
- ShowMessage( "An unexpected error occured while preparing "
- "the decoding format" );
- return false;
- }
- }
-
- //---------------------------------------------------------------------------
- // Encode the text!
- //---------------------------------------------------------------------------
- void __fastcall TfrmMemEncoding::btEncodeClick(TObject *Sender)
- {
- // Create our instance of the Xceed Binary Encoding object
- IXceedBinaryEncodingPtr piEncoder;
- piEncoder.CreateInstance( CLSID_XceedBinaryEncoding );
-
- // Create and prepare the encoding format (UU, XX, BinHex, ... )
- if( PrepareEncodingFormat( piEncoder ) )
- {
- try
- {
- char* szSource = mmoDecodedText->Text.c_str();
- // We add 1 because we want to compress the null-char too
- DWORD dwSourceSize = mmoDecodedText->Text.Length() + 1;
- BYTE* pcEncoded;
- DWORD dwEncodedSize;
-
- // Encode the data in a single call (bEndOfData is True).
- piEncoder->Encode( ( BYTE* )szSource, dwSourceSize, TRUE, &pcEncoded, &dwEncodedSize );
-
- if( !pcEncoded )
- {
- // no output was produced
- mmoEncodedText->Text = "";
- lblEncodedSize->Caption = "";
- }
- else
- {
- // Display the encoded result in the memo field and show the
- // encoded string size
- mmoEncodedText->Clear();
- mmoEncodedText->SetTextBuf( ( CHAR*) pcEncoded );
- lblEncodedSize->Caption = IntToStr( dwEncodedSize );
- }
-
- }
- catch( Exception& xErr )
- {
- ShowMessage( xErr.Message );
- }
- catch( ... )
- {
- ShowMessage( "An unexpected error occured while encoding the data." );
- }
- }
- }
-
- //---------------------------------------------------------------------------
- // Decode the encoded text!
- //---------------------------------------------------------------------------
- void __fastcall TfrmMemEncoding::btDecodeClick(TObject *Sender)
- {
- // Create our instance of the Xceed Binary Encoding object
- IXceedBinaryEncodingPtr piEncoder;
- piEncoder.CreateInstance( CLSID_XceedBinaryEncoding );
-
- // Create and prepare the decoding format (UU, XX, BinHex, ... )
- if( PrepareDecodingFormat( piEncoder ) )
- {
- try
- {
- char* szSource = mmoEncodedText->Text.c_str();
- DWORD dwSourceSize = mmoEncodedText->Text.Length();
- BYTE* pcDecoded;
- DWORD dwDecodedSize;
-
- // Decode the data in a single call (bEndOfData is True).
- piEncoder->Decode( ( BYTE* )szSource, dwSourceSize, TRUE, &pcDecoded, &dwDecodedSize );
-
- if( !pcDecoded )
- {
- // no output was produced
- mmoDecodedText->Text = "";
- lblDecodedSize->Caption = "";
- }
- else
- {
- // Display the decoded result in the memo field and show the
- // decoded string size
- mmoDecodedText->Clear();
- mmoDecodedText->SetTextBuf( ( CHAR* )pcDecoded );
- lblDecodedSize->Caption = IntToStr( dwDecodedSize );
- }
- }
- catch( Exception& xErr )
- {
- ShowMessage( xErr.Message );
- }
- catch( ... )
- {
- ShowMessage( "An unexpected error occured while decoding the data." );
- }
- }
- }
-
-
-