home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipComp.exe / XceedEncoding.Cab / F112901_unMemEncode.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-05-11  |  16.5 KB  |  384 lines

  1. {*
  2.   Xceed Binary Encoding Library - Memory Encode Sample
  3.   Copyright (c) 2001 Xceed Software Inc.
  4.  
  5.   This sample demonstrates how to encode a chunk of memory data using
  6.   different encoding methods and how to decode encoded memory data.
  7.   It specifically uses:
  8.     - The Encode and Decode methods
  9.     - The ContinueOnInvalidData, IncludeHeaderFooter, EndOfLineType,
  10.       MaxLineLength, HeaderDataForkLength, HeaderResourceForkLength,
  11.       and the EncodingFormat properties.
  12.  
  13.   This file is part of the Xceed Binary Encoding Library sample applications.
  14.   The source code in this file is only intended as a supplement to the
  15.   Xceed Binary Encoding Library's documentation and is provided "as is",
  16.   without warranty of any kind, either expressed or implied.
  17. *}
  18.  
  19. unit unMemEncode;
  20.  
  21. interface
  22.  
  23. uses
  24.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  25.   StdCtrls, XceedBinaryEncodingLib_TLB;
  26.  
  27. const
  28.   sTextToEncode = 'This is a little test to show you how the memory ' +
  29.                   'encoding works and how easy it is to use.';
  30.  
  31. // The different methods that will be used.
  32. type TEncodingMethod = ( emUUEncode,          //0
  33.                          emXXEncode,          //1
  34.                          emHexaDecimal,       //2
  35.                          emBase64,            //3
  36.                          emQuotedPrintable,   //4
  37.                          emBinHex );          //5
  38.  
  39. type
  40.   TfrmMemEncode = class(TForm)
  41.     Label1            : TLabel;
  42.     Label2            : TLabel;
  43.     Label3            : TLabel;
  44.     Label4            : TLabel;
  45.     Label5            : TLabel;
  46.     Label6            : TLabel;
  47.     Label7            : TLabel;
  48.     lblDecodedSize    : TLabel;
  49.     lblEncodedSize    : TLabel;
  50.     cboEncodingMethod : TComboBox;
  51.     cboEndOfLineType  : TComboBox;
  52.     edtMaxLineLength  : TEdit;
  53.     mmoDecodedText    : TMemo;
  54.     mmoEncodedText    : TMemo;
  55.     btEncode: TButton;
  56.     btDecode: TButton;
  57.     procedure FormCreate(Sender: TObject);
  58.     procedure mmoEncodedTextChange(Sender: TObject);
  59.     procedure mmoDecodedTextChange(Sender: TObject);
  60.     procedure btEncodeClick(Sender: TObject);
  61.     procedure btDecodeClick(Sender: TObject);
  62.   private
  63.     { Private declarations }
  64.     function PrepareEncodingFormat( var xEncoder : TXceedBinaryEncoding ) : boolean;
  65.     function PrepareDecodingFormat( var xEncoder : TXceedBinaryEncoding ) : boolean;
  66.   public
  67.     { Public declarations }
  68.   end;
  69.  
  70. var
  71.   frmMemEncode: TfrmMemEncode;
  72.  
  73. implementation
  74.  
  75. {$R *.DFM}
  76.  
  77. {------------------------------------------------------------}
  78. { Initialize the decoded size label and the combo boxes. For }
  79. { the purposes of this example, the combo box items were     }
  80. { entered using Delphi's property box.
  81. {------------------------------------------------------------}
  82. procedure TfrmMemEncode.FormCreate(Sender: TObject);
  83. begin
  84.   cboEncodingMethod.ItemIndex := 0;
  85.   cboEndOfLineType.ItemIndex := 1;
  86.  
  87.   lblDecodedSize.Caption := IntToStr( Length( mmoDecodedText.Text ) );
  88.   mmoDecodedText.Text := sTextToEncode;
  89. end;
  90.  
  91. {------------------------------------------------------------}
  92. { Update the encoded size label when the user modifies the   }
  93. { encoded text box content.
  94. {------------------------------------------------------------}
  95. procedure TfrmMemEncode.mmoEncodedTextChange(Sender: TObject);
  96. begin
  97.   lblEncodedSize.Caption := IntToStr( Length( mmoEncodedText.Text ) );
  98. end;
  99.  
  100. {------------------------------------------------------------}
  101. { Update the decoded size label when the user modifies the   }
  102. { decoded text box content.                                  }
  103. {------------------------------------------------------------}
  104. procedure TfrmMemEncode.mmoDecodedTextChange(Sender: TObject);
  105. begin
  106.   lblDecodedSize.Caption := IntToStr( Length( mmoDecodedText.Text ) );
  107. end;
  108.  
  109. {------------------------------------------------------------}
  110. { Prepare the encoding format according to the user          }
  111. { return true if all succeeded                               }
  112. {------------------------------------------------------------}
  113. function TfrmMemEncode.PrepareEncodingFormat( var xEncoder : TXceedBinaryEncoding ) : boolean;
  114. var
  115.   // We use one variable for each encoding format to simplify the
  116.   // programming (code completion). Only one of these will be used at
  117.   // a time (according to the encoding method chosen from the combo box
  118.   xUUFormat     : XceedUUEncodingFormat;
  119.   xXXFormat     : XceedXXEncodingFormat;
  120.   xHexaFormat   : XceedHexaEncodingFormat;
  121.   xBase64Format : XceedBase64EncodingFormat;
  122.   xQPFormat     : XceedQuotedPrintableEncodingFormat;
  123.   xBinHexFormat : XceedBinHexEncodingFormat;
  124. begin
  125.   try
  126.   case TEncodingMethod( cboEncodingMethod.ItemIndex ) of
  127.     emUUEncode :  begin
  128.                     xUUFormat := CoXceedUUEncodingFormat.Create();
  129.                     // We don't want any header/footer for this is only
  130.                     // a memory encoding process. The header/footer are
  131.                     // mainly used with files.
  132.                     xUUFormat.IncludeHeaderFooter := false;
  133.                     xUUFormat.EndOfLineType := EXBEndOfLineType( cboEndOfLineType.ItemIndex );
  134.                     // Set the maximum line length specifed by the user. This
  135.                     // is a mandatory value as the EndOfLineType can not be
  136.                     // None for UUEncoding.
  137.                     xUUFormat.MaxLineLength := StrToInt( edtMaxLineLength.Text );
  138.                     // Set the previously initialized Encoding format of the
  139.                     // Encoder object received as a parameter of this function
  140.                     xEncoder.EncodingFormat := xUUFormat;
  141.                   end;
  142.     emXXEncode :  begin
  143.                     xXXFormat := CoXceedXXEncodingFormat.Create();
  144.                     // We don't want any header/footer for this is only
  145.                     // memory encoding process. The header/footer are mainly
  146.                     // used with files.
  147.                     xXXFormat.IncludeHeaderFooter := false;
  148.                     xXXFormat.EndOfLineType := EXBEndOfLineType( cboEndOfLineType.ItemIndex );
  149.                     // Set the maximum line length specifed by the user. This
  150.                     // is a mandatory value as the EndOfLineType can not be
  151.                     // None for XXEncoding.
  152.                     xXXFormat.MaxLineLength := StrToInt( edtMaxLineLength.Text );
  153.                     xEncoder.EncodingFormat := xXXFormat;
  154.                   end;
  155.     emHexaDecimal : begin
  156.                       xHexaFormat := CoXceedHexaEncodingFormat.Create();
  157.                       xHexaFormat.EndOfLineType := EXBEndOfLineType( cboEndOfLineType.ItemIndex );
  158.                       // Set the maximum line length specified by the user.
  159.                       // This value will be ignored by the Xceed Binary
  160.                       // Encoding Library if the EndOfLineType is set to None.
  161.                       xHexaFormat.MaxLineLength := StrToInt( edtMaxLineLength.Text );
  162.                       xEncoder.EncodingFormat := xHexaFormat;
  163.                     end;
  164.     emBase64      : begin
  165.                       xBase64Format := CoXceedBase64EncodingFormat.Create();
  166.                       xBase64Format.EndOfLineType := EXBEndOfLineType( cboEndOfLineType.ItemIndex );
  167.                       // Set the maximum line length specified by the user.
  168.                       // This value will be ignored by the Xceed Binary
  169.                       // Encoding Library if the EndOfLineType is set to None.
  170.                       xBase64Format.MaxLineLength := StrToInt( edtMaxLineLength.Text );
  171.                       xEncoder.EncodingFormat := xBase64Format;
  172.                     end;
  173.     emQuotedPrintable : begin
  174.                           xQPFormat := CoXceedQuotedPrintableEncodingFormat.Create();
  175.                           xQPFormat.EndOfLineType := EXBEndOfLineType( cboEndOfLineType.ItemIndex );
  176.                           // Set the maximum line length specified by the user.
  177.                           // This value will be ignored by the Xceed Binary
  178.                           // Encoding Library if the EndOfLineType is set to None.
  179.                           xQPFormat.MaxLineLength := StrToInt( edtMaxLineLength.Text );
  180.                           xEncoder.EncodingFormat := xQPFormat;
  181.                         end;
  182.     emBinHex :  begin
  183.                   xBinHexFormat := CoXceedBinHexEncodingFormat.Create();
  184.                   xBinHexFormat.EndOfLineType := EXBEndOfLineType( cboEndOfLineType.ItemIndex );
  185.                   // Set the maximum line length specified by the user.
  186.                   // This value will be ignored by the Xceed Binary
  187.                   // Encoding Library if the EndOfLineType is set to None.
  188.                   xBinHexFormat.MaxLineLength := StrToInt( edtMaxLineLength.Text );
  189.                   // For the BinHex format, we must specify the data fork
  190.                   // length and the resource fork length. The resource fork
  191.                   // is not mandatory. The DataForlLength is mandatory and must
  192.                   // be set to the size of the data that will be encoded.
  193.                   xBinHexFormat.HeaderDataForkLength := Length( mmoDecodedText.Text );
  194.                   // The ResourceForkLength, used by MAC systems, is not
  195.                   // relevant under a PC system. We set it to 0.
  196.                   xBinHexFormat.HeaderResourceForkLength := 0;
  197.                   // We don't want any formatting for this is only a memory
  198.                   // encoding process. The formatting is used mainly with files.
  199.                   xBinHexFormat.IncludeHeaderFooter := false;
  200.                   xEncoder.EncodingFormat := xBinHexFormat;
  201.                 end;
  202.   end;
  203.     PrepareEncodingFormat := true;
  204.   except
  205.     on xErr: Exception do
  206.     begin
  207.       ShowMessage( 'Error during encoding format initialization. ' + xErr.Message );
  208.       PrepareEncodingFormat := false;
  209.     end;
  210.   end;
  211. end;
  212.  
  213. {------------------------------------------------------------}
  214. { Prepare the decoding format according to the user          }
  215. { selection. Return true is all succeeded.                   }
  216. {------------------------------------------------------------}
  217. function TfrmMemEncode.PrepareDecodingFormat( var xEncoder : TXceedBinaryEncoding ) : boolean;
  218. var
  219.   // We use one variable for each dencoding format to simplify the
  220.   // programming (code completion). Only one of these will be used at
  221.   // a time (according to the encoding method chosen from the combo box)
  222.   xUUFormat     : XceedUUEncodingFormat;
  223.   xXXFormat     : XceedXXEncodingFormat;
  224.   xHexaFormat   : XceedHexaEncodingFormat;
  225.   xBase64Format : XceedBase64EncodingFormat;
  226.   xQPFormat     : XceedQuotedPrintableEncodingFormat;
  227.   xBinHexFormat : XceedBinHexEncodingFormat;
  228. begin
  229.   try
  230.   case TEncodingMethod( cboEncodingMethod.ItemIndex ) of
  231.     emUUEncode :  begin
  232.                     xUUFormat := CoXceedUUEncodingFormat.Create();
  233.                     // The encoded string does not contain any header/footer.
  234.                     // The header/footer are usually present only in files.
  235.                     xUUFormat.IncludeHeaderFooter := false;
  236.                     // We want to ignore an invalid encoded characters
  237.                     xUUFormat.ContinueOnInvalidData := true;
  238.                     xEncoder.EncodingFormat := xUUFormat;
  239.                   end;
  240.     emXXEncode :  begin
  241.                     xXXFormat := CoXceedXXEncodingFormat.Create();
  242.                     xXXFormat.IncludeHeaderFooter := false;
  243.                     xXXFormat.ContinueOnInvalidData := true;
  244.                     xEncoder.EncodingFormat := xXXFormat;
  245.                   end;
  246.     emHexaDecimal : begin
  247.                       xHexaFormat := CoXceedHexaEncodingFormat.Create();
  248.                       xHexaFormat.ContinueOnInvalidData := true;
  249.                       xEncoder.EncodingFormat := xHexaFormat;
  250.                     end;
  251.     emBase64 :  begin
  252.                   xBase64Format := CoXceedBase64EncodingFormat.Create();
  253.                   xBase64Format.ContinueOnInvalidData := true;
  254.                   xEncoder.EncodingFormat := xBase64Format;
  255.                 end;
  256.     emQuotedPrintable : begin
  257.                           xQPFormat := CoXceedQuotedPrintableEncodingFormat.Create();
  258.                           xQPFormat.ContinueOnInvalidData := true;
  259.                           xEncoder.EncodingFormat := xQPFormat;
  260.                         end;
  261.     emBinHex :  begin
  262.                   xBinHexFormat := CoXceedBinHexEncodingFormat.Create();
  263.                   // The encoded string does not contain any header/footer.
  264.                   // The header/footer are usually present only in files.
  265.                   xBinHexFormat.IncludeHeaderFooter := false;
  266.                   xBinHexFormat.ContinueOnInvalidData := true;
  267.                   xEncoder.EncodingFormat := xBinHexFormat;
  268.                 end;
  269.   end;
  270.     PrepareDecodingFormat := true;
  271.   except
  272.     on xErr: Exception do
  273.     begin
  274.       ShowMessage( 'Error during decoding format initialization. ' + xErr.Message );
  275.       PrepareDecodingFormat := false;
  276.     end;
  277.   end;
  278. end;
  279.  
  280. {------------------------------------------------------------}
  281. { Encode the text!                                           }
  282. {------------------------------------------------------------}
  283. procedure TfrmMemEncode.btEncodeClick(Sender: TObject);
  284. var
  285.   xEncoder      : TXceedBinaryEncoding;
  286.   vaDecodedText : OleVariant;
  287.   vaEncoded     : OleVariant;
  288.  
  289.   pcBuffer      : PChar;
  290.   nBufferLen    : Cardinal;
  291.   pcData        : PChar;
  292. begin
  293.   // Create our instance of the Xceed Binary Encoding object
  294.   xEncoder := TXceedBinaryEncoding.Create( self );
  295.  
  296.   // Create and prepare the encoding format (UU, XX, BinHex, ...)
  297.   if( PrepareEncodingFormat( xEncoder ) ) then
  298.   begin
  299.     vaDecodedText := mmoDecodedText.Text;
  300.  
  301.     try
  302.       // Encode the data specify that (true parameter) this is the end
  303.       // of the data (there will be no more calls to encode).
  304.       vaEncoded := xEncoder.Encode( vaDecodedText, true );
  305.  
  306.       if VarIsEmpty( vaEncoded ) then
  307.       begin
  308.         // no output was produced.
  309.         mmoEncodedText.Text := '';
  310.         lblEncodedSize.Caption := '0';
  311.       end
  312.       else
  313.       begin
  314.         // Let's create a null-terminated string to display it.
  315.         nBufferLen := VarArrayHighBound( vaEncoded, 1 ) -
  316.                       VarArrayLowBound( vaEncoded, 1 ) + 1;
  317.  
  318.         GetMem( pcBuffer, nBufferLen + 1 );
  319.         pcData := VarArrayLock( vaEncoded );
  320.         CopyMemory( pcBuffer, pcData, nBufferLen );
  321.         VarArrayUnlock( vaEncoded );
  322.  
  323.         // Display the encoded result in the text box and show the encoded
  324.         // string size.
  325.         mmoEncodedText.SetTextBuf( pcBuffer );
  326.         FreeMem( pcBuffer );
  327.         lblEncodedSize.Caption := IntToStr( VarArrayHighBound( vaEncoded, 1 ) );
  328.       end;
  329.     except
  330.       on xErr: Exception do
  331.         ShowMessage( 'Error during the encoding process. ' + xErr.Message );
  332.     end;
  333.   end;
  334.   // Release our instance of the Xceed Binary Encoding object
  335.   xEncoder.Free();
  336. end;
  337.  
  338. {------------------------------------------------------------}
  339. { Decode the encoded text!                                   }
  340. {------------------------------------------------------------}
  341. procedure TfrmMemEncode.btDecodeClick(Sender: TObject);
  342. var
  343.   xEncoder      : TXceedBinaryEncoding;
  344.   vaEncodedText : OleVariant;
  345.   vaDecoded     : OleVariant;
  346. begin
  347.   // Create a new instance of our Xceed Binary Encoding object
  348.   xEncoder := TXceedBinaryEncoding.Create( self );
  349.  
  350.   // Create and prepare the decoding format (UU, XX, BinHex, etc...)
  351.   if( PrepareDecodingFormat( xEncoder ) ) then
  352.   begin
  353.     vaEncodedText := mmoEncodedText.Text;
  354.  
  355.     try
  356.       // Decode the encoded data specifying that (True parameter) this is the
  357.       // end of the data (there will be no more calls to the Decode method)
  358.       vaDecoded := xEncoder.Decode( vaEncodedText, true );
  359.  
  360.       if VarIsEmpty( vaDecoded ) then
  361.       begin
  362.         // no output was produced.
  363.         mmoDecodedText.Text := '';
  364.         lblDecodedSize.Caption := '0';
  365.       end
  366.       else
  367.       begin
  368.         // Display the encoded result in the text box and show the encoded
  369.         // string size.
  370.         mmoDecodedText.Clear();
  371.         mmoDecodedText.Text := vaDecoded;
  372.         lblDecodedSize.Caption := IntToStr( VarArrayHighBound( vaDecoded, 1 ) );
  373.       end;
  374.     except
  375.       on xErr: Exception do
  376.         ShowMessage( 'Error during the decoding process. ' + xErr.Message );
  377.     end;
  378.   end;
  379.   // Release our instance of the Xceed Binary object
  380.   xEncoder.Free();
  381. end;
  382.  
  383. end.
  384.