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.
- *}
-
- unit unMemEncode;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, XceedBinaryEncodingLib_TLB;
-
- const
- sTextToEncode = 'This is a little test to show you how the memory ' +
- 'encoding works and how easy it is to use.';
-
- // The different methods that will be used.
- type TEncodingMethod = ( emUUEncode, //0
- emXXEncode, //1
- emHexaDecimal, //2
- emBase64, //3
- emQuotedPrintable, //4
- emBinHex ); //5
-
- type
- TfrmMemEncode = class(TForm)
- Label1 : TLabel;
- Label2 : TLabel;
- Label3 : TLabel;
- Label4 : TLabel;
- Label5 : TLabel;
- Label6 : TLabel;
- Label7 : TLabel;
- lblDecodedSize : TLabel;
- lblEncodedSize : TLabel;
- cboEncodingMethod : TComboBox;
- cboEndOfLineType : TComboBox;
- edtMaxLineLength : TEdit;
- mmoDecodedText : TMemo;
- mmoEncodedText : TMemo;
- btEncode: TButton;
- btDecode: TButton;
- procedure FormCreate(Sender: TObject);
- procedure mmoEncodedTextChange(Sender: TObject);
- procedure mmoDecodedTextChange(Sender: TObject);
- procedure btEncodeClick(Sender: TObject);
- procedure btDecodeClick(Sender: TObject);
- private
- { Private declarations }
- function PrepareEncodingFormat( var xEncoder : TXceedBinaryEncoding ) : boolean;
- function PrepareDecodingFormat( var xEncoder : TXceedBinaryEncoding ) : boolean;
- public
- { Public declarations }
- end;
-
- var
- frmMemEncode: TfrmMemEncode;
-
- implementation
-
- {$R *.DFM}
-
- {------------------------------------------------------------}
- { Initialize the decoded size label and the combo boxes. For }
- { the purposes of this example, the combo box items were }
- { entered using Delphi's property box.
- {------------------------------------------------------------}
- procedure TfrmMemEncode.FormCreate(Sender: TObject);
- begin
- cboEncodingMethod.ItemIndex := 0;
- cboEndOfLineType.ItemIndex := 1;
-
- lblDecodedSize.Caption := IntToStr( Length( mmoDecodedText.Text ) );
- mmoDecodedText.Text := sTextToEncode;
- end;
-
- {------------------------------------------------------------}
- { Update the encoded size label when the user modifies the }
- { encoded text box content.
- {------------------------------------------------------------}
- procedure TfrmMemEncode.mmoEncodedTextChange(Sender: TObject);
- begin
- lblEncodedSize.Caption := IntToStr( Length( mmoEncodedText.Text ) );
- end;
-
- {------------------------------------------------------------}
- { Update the decoded size label when the user modifies the }
- { decoded text box content. }
- {------------------------------------------------------------}
- procedure TfrmMemEncode.mmoDecodedTextChange(Sender: TObject);
- begin
- lblDecodedSize.Caption := IntToStr( Length( mmoDecodedText.Text ) );
- end;
-
- {------------------------------------------------------------}
- { Prepare the encoding format according to the user }
- { return true if all succeeded }
- {------------------------------------------------------------}
- function TfrmMemEncode.PrepareEncodingFormat( var xEncoder : TXceedBinaryEncoding ) : boolean;
- var
- // We use one variable for each encoding format to simplify the
- // programming (code completion). Only one of these will be used at
- // a time (according to the encoding method chosen from the combo box
- xUUFormat : XceedUUEncodingFormat;
- xXXFormat : XceedXXEncodingFormat;
- xHexaFormat : XceedHexaEncodingFormat;
- xBase64Format : XceedBase64EncodingFormat;
- xQPFormat : XceedQuotedPrintableEncodingFormat;
- xBinHexFormat : XceedBinHexEncodingFormat;
- begin
- try
- case TEncodingMethod( cboEncodingMethod.ItemIndex ) of
- emUUEncode : begin
- xUUFormat := CoXceedUUEncodingFormat.Create();
- // We don't want any header/footer for this is only
- // a memory encoding process. The header/footer are
- // mainly used with files.
- xUUFormat.IncludeHeaderFooter := false;
- xUUFormat.EndOfLineType := EXBEndOfLineType( cboEndOfLineType.ItemIndex );
- // Set the maximum line length specifed by the user. This
- // is a mandatory value as the EndOfLineType can not be
- // None for UUEncoding.
- xUUFormat.MaxLineLength := StrToInt( edtMaxLineLength.Text );
- // Set the previously initialized Encoding format of the
- // Encoder object received as a parameter of this function
- xEncoder.EncodingFormat := xUUFormat;
- end;
- emXXEncode : begin
- xXXFormat := CoXceedXXEncodingFormat.Create();
- // We don't want any header/footer for this is only
- // memory encoding process. The header/footer are mainly
- // used with files.
- xXXFormat.IncludeHeaderFooter := false;
- xXXFormat.EndOfLineType := EXBEndOfLineType( cboEndOfLineType.ItemIndex );
- // Set the maximum line length specifed by the user. This
- // is a mandatory value as the EndOfLineType can not be
- // None for XXEncoding.
- xXXFormat.MaxLineLength := StrToInt( edtMaxLineLength.Text );
- xEncoder.EncodingFormat := xXXFormat;
- end;
- emHexaDecimal : begin
- xHexaFormat := CoXceedHexaEncodingFormat.Create();
- xHexaFormat.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.
- xHexaFormat.MaxLineLength := StrToInt( edtMaxLineLength.Text );
- xEncoder.EncodingFormat := xHexaFormat;
- end;
- emBase64 : begin
- xBase64Format := CoXceedBase64EncodingFormat.Create();
- xBase64Format.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.
- xBase64Format.MaxLineLength := StrToInt( edtMaxLineLength.Text );
- xEncoder.EncodingFormat := xBase64Format;
- end;
- emQuotedPrintable : begin
- xQPFormat := CoXceedQuotedPrintableEncodingFormat.Create();
- xQPFormat.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.
- xQPFormat.MaxLineLength := StrToInt( edtMaxLineLength.Text );
- xEncoder.EncodingFormat := xQPFormat;
- end;
- emBinHex : begin
- xBinHexFormat := CoXceedBinHexEncodingFormat.Create();
- xBinHexFormat.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.
- xBinHexFormat.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 DataForlLength is mandatory and must
- // be set to the size of the data that will be encoded.
- xBinHexFormat.HeaderDataForkLength := Length( mmoDecodedText.Text );
- // The ResourceForkLength, used by MAC systems, is not
- // relevant under a PC system. We set it to 0.
- xBinHexFormat.HeaderResourceForkLength := 0;
- // We don't want any formatting for this is only a memory
- // encoding process. The formatting is used mainly with files.
- xBinHexFormat.IncludeHeaderFooter := false;
- xEncoder.EncodingFormat := xBinHexFormat;
- end;
- end;
- PrepareEncodingFormat := true;
- except
- on xErr: Exception do
- begin
- ShowMessage( 'Error during encoding format initialization. ' + xErr.Message );
- PrepareEncodingFormat := false;
- end;
- end;
- end;
-
- {------------------------------------------------------------}
- { Prepare the decoding format according to the user }
- { selection. Return true is all succeeded. }
- {------------------------------------------------------------}
- function TfrmMemEncode.PrepareDecodingFormat( var xEncoder : TXceedBinaryEncoding ) : boolean;
- var
- // We use one variable for each dencoding format to simplify the
- // programming (code completion). Only one of these will be used at
- // a time (according to the encoding method chosen from the combo box)
- xUUFormat : XceedUUEncodingFormat;
- xXXFormat : XceedXXEncodingFormat;
- xHexaFormat : XceedHexaEncodingFormat;
- xBase64Format : XceedBase64EncodingFormat;
- xQPFormat : XceedQuotedPrintableEncodingFormat;
- xBinHexFormat : XceedBinHexEncodingFormat;
- begin
- try
- case TEncodingMethod( cboEncodingMethod.ItemIndex ) of
- emUUEncode : begin
- xUUFormat := CoXceedUUEncodingFormat.Create();
- // The encoded string does not contain any header/footer.
- // The header/footer are usually present only in files.
- xUUFormat.IncludeHeaderFooter := false;
- // We want to ignore an invalid encoded characters
- xUUFormat.ContinueOnInvalidData := true;
- xEncoder.EncodingFormat := xUUFormat;
- end;
- emXXEncode : begin
- xXXFormat := CoXceedXXEncodingFormat.Create();
- xXXFormat.IncludeHeaderFooter := false;
- xXXFormat.ContinueOnInvalidData := true;
- xEncoder.EncodingFormat := xXXFormat;
- end;
- emHexaDecimal : begin
- xHexaFormat := CoXceedHexaEncodingFormat.Create();
- xHexaFormat.ContinueOnInvalidData := true;
- xEncoder.EncodingFormat := xHexaFormat;
- end;
- emBase64 : begin
- xBase64Format := CoXceedBase64EncodingFormat.Create();
- xBase64Format.ContinueOnInvalidData := true;
- xEncoder.EncodingFormat := xBase64Format;
- end;
- emQuotedPrintable : begin
- xQPFormat := CoXceedQuotedPrintableEncodingFormat.Create();
- xQPFormat.ContinueOnInvalidData := true;
- xEncoder.EncodingFormat := xQPFormat;
- end;
- emBinHex : begin
- xBinHexFormat := CoXceedBinHexEncodingFormat.Create();
- // The encoded string does not contain any header/footer.
- // The header/footer are usually present only in files.
- xBinHexFormat.IncludeHeaderFooter := false;
- xBinHexFormat.ContinueOnInvalidData := true;
- xEncoder.EncodingFormat := xBinHexFormat;
- end;
- end;
- PrepareDecodingFormat := true;
- except
- on xErr: Exception do
- begin
- ShowMessage( 'Error during decoding format initialization. ' + xErr.Message );
- PrepareDecodingFormat := false;
- end;
- end;
- end;
-
- {------------------------------------------------------------}
- { Encode the text! }
- {------------------------------------------------------------}
- procedure TfrmMemEncode.btEncodeClick(Sender: TObject);
- var
- xEncoder : TXceedBinaryEncoding;
- vaDecodedText : OleVariant;
- vaEncoded : OleVariant;
-
- pcBuffer : PChar;
- nBufferLen : Cardinal;
- pcData : PChar;
- begin
- // Create our instance of the Xceed Binary Encoding object
- xEncoder := TXceedBinaryEncoding.Create( self );
-
- // Create and prepare the encoding format (UU, XX, BinHex, ...)
- if( PrepareEncodingFormat( xEncoder ) ) then
- begin
- vaDecodedText := mmoDecodedText.Text;
-
- try
- // Encode the data specify that (true parameter) this is the end
- // of the data (there will be no more calls to encode).
- vaEncoded := xEncoder.Encode( vaDecodedText, true );
-
- if VarIsEmpty( vaEncoded ) then
- begin
- // no output was produced.
- mmoEncodedText.Text := '';
- lblEncodedSize.Caption := '0';
- end
- else
- begin
- // Let's create a null-terminated string to display it.
- nBufferLen := VarArrayHighBound( vaEncoded, 1 ) -
- VarArrayLowBound( vaEncoded, 1 ) + 1;
-
- GetMem( pcBuffer, nBufferLen + 1 );
- pcData := VarArrayLock( vaEncoded );
- CopyMemory( pcBuffer, pcData, nBufferLen );
- VarArrayUnlock( vaEncoded );
-
- // Display the encoded result in the text box and show the encoded
- // string size.
- mmoEncodedText.SetTextBuf( pcBuffer );
- FreeMem( pcBuffer );
- lblEncodedSize.Caption := IntToStr( VarArrayHighBound( vaEncoded, 1 ) );
- end;
- except
- on xErr: Exception do
- ShowMessage( 'Error during the encoding process. ' + xErr.Message );
- end;
- end;
- // Release our instance of the Xceed Binary Encoding object
- xEncoder.Free();
- end;
-
- {------------------------------------------------------------}
- { Decode the encoded text! }
- {------------------------------------------------------------}
- procedure TfrmMemEncode.btDecodeClick(Sender: TObject);
- var
- xEncoder : TXceedBinaryEncoding;
- vaEncodedText : OleVariant;
- vaDecoded : OleVariant;
- begin
- // Create a new instance of our Xceed Binary Encoding object
- xEncoder := TXceedBinaryEncoding.Create( self );
-
- // Create and prepare the decoding format (UU, XX, BinHex, etc...)
- if( PrepareDecodingFormat( xEncoder ) ) then
- begin
- vaEncodedText := mmoEncodedText.Text;
-
- try
- // Decode the encoded data specifying that (True parameter) this is the
- // end of the data (there will be no more calls to the Decode method)
- vaDecoded := xEncoder.Decode( vaEncodedText, true );
-
- if VarIsEmpty( vaDecoded ) then
- begin
- // no output was produced.
- mmoDecodedText.Text := '';
- lblDecodedSize.Caption := '0';
- end
- else
- begin
- // Display the encoded result in the text box and show the encoded
- // string size.
- mmoDecodedText.Clear();
- mmoDecodedText.Text := vaDecoded;
- lblDecodedSize.Caption := IntToStr( VarArrayHighBound( vaDecoded, 1 ) );
- end;
- except
- on xErr: Exception do
- ShowMessage( 'Error during the decoding process. ' + xErr.Message );
- end;
- end;
- // Release our instance of the Xceed Binary object
- xEncoder.Free();
- end;
-
- end.
-