home *** CD-ROM | disk | FTP | other *** search
- {* Xceed Binary Encoding Library - Encoding Manager sample
- Copyright (c) 2001 Xceed Software Inc.
-
- [unManager.pas]
-
- This sample demonstrates how to encode a file using different kinds
- of encoding methods, and decode an encoded file. It specifically uses:
- - The ProcessFile method.
- - The EndOfLineType, MaxLineLength, ContinueOnInvalidData,
- HeaderDataForkLen, HeaderResourceForkLen, HeaderFilename, EncodingFormat
- and DataFormating propeties.
-
- 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 Xceed
- Binary Encoding Library's documentation, and is provided "as is", without
- warranty of any kind, either expressed or implied. *}
-
- unit unManager;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, ComCtrls, ExtCtrls, OleCtrls, FileCtrl, ShlObj, ActiveX,
- XCEEDBINARYENCODINGLib_TLB;
-
-
- {* The different encoding methods corresponding to the option buttons (the same
- for the Decode and Encode tabs) *}
- type TEncodingMethod = ( emUUEncode, //0
- emXXEncode, //1
- emBase64, //2
- emHexaDecimal, //3
- emQuotedPrintable, //4
- emBinHex ); //5
-
- type
- TfrmManager = class(TForm)
- xBinEncode : TXceedBinaryEncoding;
- pgMain : TPageControl;
- tabEncode : TTabSheet;
- tabDecode : TTabSheet;
- Label1 : TLabel;
- Label2 : TLabel;
- Label3 : TLabel;
- Label4 : TLabel;
- Label5 : TLabel;
- Label6 : TLabel;
- Label7 : TLabel;
- edtEncSourceFile : TEdit;
- edtEncDestFile : TEdit;
- edtDecDestination : TEdit;
- edtDecFilename : TEdit;
- edtMaxLen : TEdit;
- btBrowseForSource : TButton;
- btBrowseForDest : TButton;
- btEncode : TButton;
- btAddFile : TButton;
- btRemoveFile : TButton;
- btClear : TButton;
- btBrowseForDecDest : TButton;
- btDecode : TButton;
- rgMethod : TRadioGroup;
- rgLineTypeOptions : TRadioGroup;
- grpDecode : TGroupBox;
- grpEncOptions : TGroupBox;
- lstEncodedFile : TListBox;
- lstMessages : TListBox;
- chkInvalidData : TCheckBox;
- xOpenDialog : TOpenDialog;
-
- procedure FormCreate (Sender: TObject);
- procedure btBrowseForSourceClick (Sender: TObject);
- procedure btBrowseForDestClick (Sender: TObject);
- procedure rgMethodClick (Sender: TObject);
- procedure btEncodeClick (Sender: TObject);
- procedure rgLineTypeOptionsClick (Sender: TObject);
- procedure edtMaxLenChange (Sender: TObject);
- procedure chkInvalidDataClick (Sender: TObject);
- procedure btClearClick (Sender: TObject);
- procedure btBrowseForDecDestClick (Sender: TObject);
- procedure btRemoveFileClick (Sender: TObject);
- procedure btDecodeClick (Sender: TObject);
- procedure btAddFileClick (Sender: TObject);
- procedure edtEncSourceFileExit (Sender: TObject);
- private
- //The values chosen by the user that will be used throughout this sample
- m_lMaxLineLength : longint;
- m_eEndOfLineType : EXBEndOfLineType;
- m_bContinueOnInvalidData : boolean;
-
- //The Encoding and Decoding method that will be used
- m_eEncodingMethod : TEncodingMethod;
- m_eDecodingMethod : TEncodingMethod;
-
- procedure SetDestinationFileExtension( eOldEncodingMethod : TEncodingMethod;
- eNewEncodingMethod : TEncodingMethod );
- procedure SetDestinationFilename();
- procedure AddEncodedFileToList( sFilename : string );
-
- function ExtractFilename ( sFilename : string ) : string;
- function ExtractFolder ( sFilename : string ) : string;
- function ExtractFileExtension( sFilename : string ) : string;
- function RemoveFileExtension ( sFilename : string ) : string;
- function StdFileExtension ( eMethod : TEncodingMethod ) : string;
-
- function EncodeFile ( sSourceFilename : string;
- eMethod : TEncodingMethod;
- eEndOfLineType : EXBEndOfLineType;
- lMaxLineLength : longint;
- sEncodedFilename : string ) : boolean;
-
- function DecodeFile ( var slEncodedFile : TStringList;
- nNbEncodedFile : integer;
- eMethod : TEncodingMethod;
- bContinueOnInvalidData : boolean;
- sDecodeFolder : string;
- sDecodedFilename : string ) : boolean;
-
- function BrowseFolder ( hWnd : integer; var sFolder : string;
- const sDesc : string ) : boolean;
-
-
- public
- { Public declarations }
- end;
-
- var
- frmManager: TfrmManager;
-
- implementation
-
- {$R *.DFM}
- {***************************************************************}
- { ***** XCEED PRIVATE PROCEDURES ***** }
- {***************************************************************}
-
- {---------------------------------------------------------------}
- { If a destination file name already exists for encoding, }
- { change it's extension for a new one appropriate to the new }
- { encoding method. }
- {---------------------------------------------------------------}
- procedure TFrmManager.SetDestinationFileExtension( eOldEncodingMethod : TEncodingMethod;
- eNewEncodingMethod : TEncodingMethod );
- var
- sDestinationFile : string;
- sDestinationFileExtension : string;
- bChangedExtension : boolean;
- begin
- bChangedExtension := false;
- sDestinationFile := edtEncDestFile.Text;
- sDestinationFileExtension := UpperCase( ExtractFileExtension( sDestinationFile ) );
-
- // If there is a destination file, we verify if its extension corresponds to the
- // old encoding method. If it corresponds or if there is no extension, we set
- // the flag that will do the change of extension below
- if ( Length( sDestinationFile ) <> 0 ) then
- begin
- case eOldEncodingMethod of
- emUUEncode : if ( sDestinationFileExtension = 'UUE' ) or
- ( Length( sDestinationFileExtension ) = 0 ) then
- bChangedExtension := true;
-
- emXXEncode : if ( sDestinationFileExtension = 'XXE' ) or
- ( Length( sDestinationFileExtension ) = 0 ) then
- bChangedExtension := true;
-
- emBase64 : if ( sDestinationFileExtension = 'B64' ) or
- ( Length( sDestinationFileExtension ) = 0 ) then
- bChangedExtension := true;
-
- emHexadecimal : if ( sDestinationFileExtension = 'HEX' ) or
- ( Length( sDestinationFileExtension ) = 0 ) then
- bChangedExtension := true;
-
- emQuotedPrintable : if ( sDestinationFileExtension = 'QPR' ) or
- ( Length( sDestinationFileExtension ) = 0 ) then
- bChangedExtension := true;
-
- emBinHex : if ( sDestinationFileExtension = 'HQX' ) or
- ( Length( sDestinationFileExtension ) = 0 ) then
- bChangedExtension := true;
- end;
-
- // If we determined above that a change of extension is in order. Do it
- if bChangedExtension then
- begin
- sDestinationFile := RemoveFileExtension( sDestinationFile ) +
- StdFileExtension( eNewEncodingMethod );
- edtEncDestFile.Text := sDestinationFile;
- end;
- end;
- end;
-
- {---------------------------------------------------------------}
- { Set a default destination file name used for the encoding }
- { process. This file name is derived from the source file name }
- { If a destination file name is already specified, do nothing. }
- {---------------------------------------------------------------}
- procedure TFrmManager.SetDestinationFilename();
- var
- sEncodedFilename : string;
- begin
- sEncodedFilename := edtEncDestFile.Text;
- if ( Length( sEncodedFilename ) = 0 ) then
- begin
- sEncodedFilename := RemoveFileExtension( edtEncSourceFile.Text );
-
- if ( Length( sEncodedFilename ) <> 0 ) then
- begin
- edtEncDestFile.Text := sEncodedFilename;
- SetDestinationFileExtension( m_eEncodingMethod, m_eEncodingMethod );
- end;
- end;
- end;
-
- {---------------------------------------------------------------}
- { Add the specified file name to the list box of the file names }
- { to decode }
- {---------------------------------------------------------------}
- procedure TFrmManager.AddEncodedFileToList( sFilename : string );
- var
- i : integer;
- bFound : boolean;
- nNbItem : integer;
- sDecodeFolder : string;
- sExtension : string;
- begin
- nNbItem := lstEncodedFile.Items.Count;
- bFound := false;
-
- // Do not allow more than 1000 files to be decoded at one time
- if ( nNbItem < 1000 ) then
- begin
- for i := 0 to ( nNbItem - 1 ) do
- begin
- // Check if the file is already in the list of files
- if ( lstEncodedFile.Items.Strings[ i ] = sFilename ) then
- begin
- bFound := true;
- break;
- end;
- end;
-
- if not bFound then
- begin
- // add the file name to the list
- lstEncodedFile.Items.Add( sFilename );
-
- // if no decode folder is specified, set one to the folder name of the added file name.
- sDecodeFolder := edtDecDestination.Text;
-
- if ( Length( sDecodeFolder ) = 0 ) then
- begin
- sDecodeFolder := ExtractFolder( sFilename );
-
- if ( Length( sDecodeFolder ) <> 0 ) then
- edtDecDestination.Text := sDecodeFolder;
- end;
-
- // If it's the first file added to the list, select it in the list
- if ( nNbItem = 0 ) then
- lstEncodedFile.ItemIndex := 0;
-
- / /Set the decoding method according to the file name extension
- sExtension := UpperCase( Copy( sFilename, Length( sFilename ) - 3, 4 ) );
-
- if ( sExtension = '.UUE' ) then
- begin
- m_eDecodingMethod := emUUEncode;
- rgMethod.ItemIndex := 0;
- end;
-
- if ( sExtension = '.XXE' ) then
- begin
- m_eDecodingMethod := emXXEncode;
- rgMethod.ItemIndex := 1;
- end;
-
- if ( sExtension = '.B64' ) then
- begin
- m_eDecodingMethod := emBase64;
- rgMethod.ItemIndex := 2;
- end;
-
- if ( sExtension = '.HEX' ) then
- begin
- m_eDecodingMethod := emHexaDecimal;
- rgMethod.ItemIndex := 3;
- end;
-
- if ( sExtension = '.QPR' ) then
- begin
- m_eDecodingMethod := emQuotedPrintable;
- rgMethod.ItemIndex := 4;
- end;
-
- if ( sExtension = '.HQX' ) then
- begin
- m_eDecodingMethod := emBinHex;
- rgMethod.ItemIndex := 5;
- end;
- end;
- end;
- end;
-
- {***************************************************************}
- { ***** XCEED PRIVATE FUNCTIONS ***** }
- {***************************************************************}
-
- {---------------------------------------------------------------}
- { Return the file name part from a "path and file name" string }
- {---------------------------------------------------------------}
- function TFrmManager.ExtractFilename( sFilename : string ) : string;
- var
- i : integer;
- nFilenameLen : integer;
- nLenToRemove : integer;
- begin
- nFilenameLen := Length( sFilename );
- i := nFilenameLen;
- nLenToRemove := -1;
-
- // Starting from the end of the string, we check each character and stop
- // at the first occurence of \ or :
- while ( i > 0 ) and ( nLenToRemove = -1 ) do
- begin
- // The length of the file name part is the length of the string
- // minus the position of the \ or :
- if ( copy( sFilename, i, 1 ) = '\' ) or ( copy( sFilename, i, 1 ) = ':' ) then
- nLenToRemove := nFilenameLen - i;
- i := i - 1
- end;
-
- // No \ or : were present. We assume that the string was a file name and
- // we return it
- if ( nLenToRemove = -1 ) then
- ExtractFilename := sFilename
- else
- // We return the right part of the string corresponding to the file name
- ExtractFilename := copy( sFilename, Length( sFilename ) - nLenToRemove + 1,
- nLenToRemove );
- end;
-
- {---------------------------------------------------------------}
- { Return the folder part from a "path and file name" string }
- { excluding any terminating \ }
- {---------------------------------------------------------------}
- function TFrmManager.ExtractFolder( sFilename : string ) : string;
- var
- i : integer;
- nFilenameLen : integer;
- nLenToRemove : integer;
- begin
- nFilenameLen := Length( sFilename );
- i := nFilenameLen;
- nlenToRemove := -1;
-
- // Starting from the end of the string, we check each character and stop
- // at the first occurence of \ or :
- while ( i > 0 ) and ( nLenToRemove = -1 ) do
- begin
- // The length of the folder name part is the position of the \ minus 1
- // (to exclude the \)
- if ( copy( sFilename, i, 1 ) = '\' ) then
- nLenToRemove := i - 1;
- // The length of the folder name part is the same as the position of the :
- if ( copy( sFilename, i, 1 ) = ':' ) then
- nLenToRemove := i;
-
- i := i - 1;
- end;
-
- if ( nLenToRemove = -1 ) then
- // The string contains no folder. We return an empty string
- ExtractFolder := ''
- else
- // Return the left part of the string corresponding to the folder name
- ExtractFolder := copy( sFilename, 1, nLenToRemove )
- end;
-
- {---------------------------------------------------------------}
- { Return the extension part of a specified file name }
- {---------------------------------------------------------------}
- function TFrmManager.ExtractFileExtension( sFilename : string ) : string;
- var
- i : integer;
- nFilenameLen : integer;
- nLenToRemove : integer;
- begin
- nFilenameLen := Length( sFilename );
- i := nFilenameLen;
- nLenToRemove := -1;
-
- // Starting from the end of the string, we check each character and stop
- // at the first occurence of . or \
- while ( i > 0 ) and ( nLenToRemove = -1 ) do
- begin
- // The length of the extension part is the length of the string
- // minus the position of the .
- if ( copy( sFilename, i, 1 ) = '.' ) then
- nLenToRemove := nFilenameLen - i;
-
- // No extension was found. We will return an empty string
- if ( copy( sFilename, i, 1 ) = '\' ) then
- nLenToRemove := 0;
-
- i := i - 1;
- end;
-
- // No extension was found. We return an empty string
- if ( nLenToRemove = -1 ) then
- ExtractFileExtension := ''
- else
- ExtractFileExtension := copy( sFilename, Length( sFilename ) - nLenToRemove + 1,
- nLenToRemove )
- end;
-
- {---------------------------------------------------------------}
- { Return the specified file name WITHOUT its extension, if any }
- {---------------------------------------------------------------}
- function TFrmManager.RemoveFileExtension( sFilename : string ) : string;
- var
- i : integer;
- nFilenameLen : integer;
- nLenToRemove : integer;
- begin
- nFilenameLen := Length( sFilename );
- i := nFilenameLen;
- nLenToRemove := -1;
-
- // Starting from the end of the string, we check each character and stop
- // at the first occurence of . or \
- while ( i > 0 ) and ( nLenToRemove = -1 ) do
- begin
- // The length of the filename name part is the position of the . minus 1
- // (to exclude the .)
- if ( copy( sFilename, i, 1 ) = '.' ) then
- nLenToRemove := i - 1;
- // The file name contains a path. Returns all the string
- if ( copy( sFilename, i, 1 ) = '\' ) then
- nLenToRemove := nFilenameLen;
-
- i := i - 1;
- end;
-
- // No extension was found return an empty string
- if ( nLenToRemove = -1 ) then
- RemoveFileExtension := ''
- else
- RemoveFileExtension := copy( sFilename, 1, nLenToRemove );
- end;
-
- {---------------------------------------------------------------}
- { Return the extension associated with the specified encoding }
- { method }
- {---------------------------------------------------------------}
- function TFrmManager.StdFileExtension( eMethod : TEncodingMethod ) : string;
- begin
- StdFileExtension := '';
-
- case eMethod of
- emUUEncode : StdFileExtension := '.uue';
- emXXEncode : StdFileExtension := '.xxe';
- emBase64 : StdFileExtension := '.b64';
- emHexadecimal : StdFileExtension := '.hex';
- emQuotedPrintable : StdFileExtension := '.qpr';
- emBinHex : StdFileExtension := '.hqx';
- end;
- end;
-
- {---------------------------------------------------------------}
- { Encode the File! }
- {---------------------------------------------------------------}
- function TFrmManager.EncodeFile( sSourceFilename : string;
- eMethod : TEncodingMethod;
- eEndOfLineType : EXBEndOfLineType;
- lMaxLineLength : longint;
- sEncodedFilename : string ) : boolean;
- var
- vaBytesRead : OleVariant;
- hFileHandle : hFile;
-
- xUUEncoding : XceedUUEncodingFormat;
- xXXEncoding : XceedXXEncodingFormat;
- xBase64Encoding : XceedBase64EncodingFormat;
- xHexaEncoding : XceedHexaEncodingFormat;
- xQuotedPrintableEncoding : XceedQuotedPrintableEncodingFormat;
- xBinHexEncoding : XceedBinHexEncodingFormat;
- begin
- EncodeFile := false;
- Screen.Cursor := crHourGlass;
-
- // Create and prepare the encoding format (XX, UU, BinHex, ...) we also
- // Set the End of line type and the Maximum line length of the chosen
- // encoding format
- //
- case eMethod of
- emUUEncode : begin
- xUUEncoding := CoXceedUUEncodingFormat.Create();
- xUUEncoding.IncludeHeaderFooter := true;
- xUUEncoding.EndOfLineType := eEndOfLineType;
- xUUEncoding.MaxLineLength := lMaxLineLength;
-
- xBinEncode.EncodingFormat := xUUEncoding;
- end;
- emXXEncode : begin
- xXXEncoding := CoXceedXXEncodingFormat.Create();
- xXXEncoding.IncludeHeaderFooter := true;
- xXXEncoding.EndOfLineType := eEndOfLineType;
- xXXEncoding.MaxLineLength := lMaxLineLength;
-
- xBinEncode.EncodingFormat := xXXEncoding;
- end;
- emBase64 : begin
- xBase64Encoding := CoXceedBase64EncodingFormat.Create();
- xBase64Encoding.EndOfLineType := eEndOfLineType;
- xBase64Encoding.MaxLineLength := lMaxLineLength;
-
- xBinEncode.EncodingFormat := xBase64Encoding;
- end;
- emHexaDecimal : begin
- xHexaEncoding := CoXceedHexaEncodingFormat.Create();
- xHexaEncoding.EndOfLineType := eEndOfLineType;
- xHexaEncoding.MaxLineLength := lMaxLineLength;
-
- xBinEncode.EncodingFormat := xHexaEncoding;
- end;
- emQuotedPrintable : begin
- xQuotedPrintableEncoding := CoXceedQuotedPrintableEncodingFormat.Create();
- xQuotedPrintableEncoding.EndOfLineType := eEndOfLineType;
- xQuotedPrintableEncoding.MaxLineLength := lMaxLineLength;
-
- xBinEncode.EncodingFormat := xQuotedPrintableEncoding;
- end;
- emBinHex : begin
- xBinHexEncoding := CoXceedBinHexEncodingFormat.Create();
- xBinHexEncoding.IncludeHeaderFooter := true;
- xBinHexEncoding.EndOfLineType := eEndOfLineType;
- xBinHexEncoding.MaxLineLength := lMaxLineLength;
-
- // For the BinHex format, we must specify the data fork length and the
- // resource fork length
- hFileHandle := CreateFile( PChar( sSourceFilename ), GENERIC_READ,
- FILE_SHARE_READ, nil, OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL, 0 );
-
- xBinHexEncoding.HeaderDataForkLength := GetFileSize( hFileHandle, nil );
- CloseHandle( hFileHandle );
- xBinHexEncoding.HeaderResourceForkLength := 0;
-
- xBinEncode.EncodingFormat := xBinHexEncoding;
- end;
- end;
-
- // If no extension for the destination file name was provided by the user,
- // we set a default one (according the the encoding method)
- if ( Length( ExtractFileExtension( sEncodedFilename ) ) = 0 ) then
- sEncodedFilename := sEncodedFilename + StdFileExtension( eMethod );
-
- // encode the file
- lstMessages.Clear();
-
- try
- // Encode the file, specifying that :
- // We want to encode all the source file name (0,0 parameters)
- // This is the end of data, no more file to encode will follow (True parameter)
- // We want to overwrite a possibly existing destination file (False parameter)
- xBinEncode.ProcessFile( sSourceFilename, 0, 0, bfpEncode,
- true, sEncodedFilename, false, vaBytesRead );
-
- // Display a message of success
- lstMessages.Items.Add( sSourceFilename + ' successfully encoded in ' +
- sEncodedFilename );
- EncodeFile := true;
- except
- on xErr : Exception do
- // Display the error that occured
- lstMessages.Items.Add( xErr.Message );
- end;
-
- Screen.Cursor := crDefault;
- end;
-
- {---------------------------------------------------------------}
- { Decode file(s)! }
- { If more than one source file was specified by the user; they }
- { will be decoded in the same destination file (showing an }
- { example of the bAppend parameter). }
- {---------------------------------------------------------------}
- function TFrmManager.DecodeFile ( var slEncodedFile : TStringList;
- nNbEncodedFile : integer;
- eMethod : TEncodingMethod;
- bContinueOnInvalidData : boolean;
- sDecodeFolder : string;
- sDecodedFilename : string ) : boolean;
- var
- i : integer;
- vaBytesRead : OleVariant;
-
- xUUEncoding : XceedUUEncodingFormat;
- xXXEncoding : XceedXXEncodingFormat;
- xBase64Encoding : XceedBase64EncodingFormat;
- xHexaEncoding : XceedHexaEncodingFormat;
- xQuotedPrintableEncoding : XceedQuotedPrintableEncodingFormat;
- xBinHexEncoding : XceedBinHexEncodingFormat;
- begin
- DecodeFile := false;
- Self.Cursor := crHourGlass;
-
- if ( copy( sDecodeFolder, Length( sDecodeFolder ) - 1, 1 ) <> '\' ) then
- // The decode folder must end with a \
- sDecodeFolder := sDecodeFolder + '\';
-
- // Create and prepare the encoding format (XX, UU, BinHex, ...) and
- // tell whether or not we want to ignore invalid character in the file(s)
- // to decode. If no file name was specified by the user. Read the one used by default
- // by the encoding library (set at the first call).
- case eMethod of
- emUUEncode : begin
- xUUEncoding := CoXceedUUEncodingFormat.Create();
- xUUEncoding.IncludeHeaderFooter := true;
- xUUEncoding.ContinueOnInvalidData := bContinueOnInvalidData;
-
- if ( Length( sDecodedFilename ) = 0 ) then
- sDecodedFilename := xUUEncoding.HeaderFilename;
-
- xBinEncode.EncodingFormat := xUUEncoding;
- end;
- emXXEncode : begin
- xXXEncoding := CoXceedXXEncodingFormat.Create();
- xXXEncoding.IncludeHeaderFooter := true;
- xXXEncoding.ContinueOnInvalidData := bContinueOnInvalidData;
-
- if ( Length( sDecodedFilename ) = 0 ) then
- sDecodedFilename := xXXEncoding.HeaderFilename;
-
- xBinEncode.EncodingFormat := xXXEncoding;
- end;
- emBase64 : begin
- xBase64Encoding := CoXceedBase64EncodingFormat.Create();
- xBase64Encoding.ContinueOnInvalidData := bContinueOnInvalidData;
-
- xBinEncode.EncodingFormat := xBase64Encoding;
- end;
- emHexaDecimal : begin
- xHexaEncoding := CoXceedHexaEncodingFormat.Create();
- xHexaEncoding.ContinueOnInvalidData := bContinueOnInvalidData;
-
- xBinEncode.EncodingFormat := xHexaEncoding;
- end;
- emQuotedPrintable : begin
- xQuotedPrintableEncoding := CoXceedQuotedPrintableEncodingFormat.Create();
- xQuotedPrintableEncoding.ContinueOnInvalidData := bContinueOnInvalidData;
-
- xBinEncode.EncodingFormat := xQuotedPrintableEncoding;
- end;
- emBinHex : begin
- xBinHexEncoding := CoXceedBinHexEncodingFormat.Create();
- xBinHexEncoding.IncludeHeaderFooter := true;
- xBinHexEncoding.ContinueOnInvalidData := bContinueOnInvalidData;
-
- if ( Length( sDecodedFilename ) = 0 ) then
- sDecodedFilename := xBinHexEncoding.HeaderFilename;
-
- xBinEncode.EncodingFormat := xBinHexEncoding;
- end;
- end;
-
- lstMessages.Clear();
-
- try
- for i := 0 to nNbEncodedFile-1 do
- begin
- // Decode a source file, providing:
- // - The source filename, without an offset or size (since we want to
- // decode all the file).
- // - True in the bEndOfdata for the last file only.
- // - The processing we want to perform, in this case bfpDecode.
- // - The destination filename we want to decode to. For the BinHex, UUEncode,
- // and XXEncode formats, the first call can contain only a folder. The
- // filename stored in the format will be used (the FileName property of the
- // format object).
- // - True in the bAppend parameter for all but the first file.
- // - A Variant that will receive the number of bytes read on return.
- xBinEncode.ProcessFile( slEncodedFile.Strings[ i ], 0, 0,
- bfpDecode, ( i = nNbEncodedFile - 1 ),
- sDecodeFolder + sDecodedFilename,
- ( i > 0 ), vaBytesRead );
-
- //Display a message of success
- lstMessages.Items.Add( slEncodedFile.Strings[ i ] + ' successfully decoded in ' +
- sDecodeFolder + sDecodedFilename );
- DecodeFile := True;
-
- end;
-
- except
- on xErr: Exception do
- //Display the error that occured
- lstMessages.Items.Add( xErr.Message );
- end;
-
- Self.Cursor := crDefault;
- end;
-
- {---------------------------------------------------------------}
- { Initialize folder browsing window }
- {---------------------------------------------------------------}
- function BrowseCallbackProc ( hWnd : HWND; nMsg : Cardinal; nParam : integer;
- pData : integer ) : integer; stdcall;
- var
- szDir : array[0..300] of char;
- begin
- if nMsg = BFFM_INITIALIZED then
- SendMessage (hWnd, BFFM_SETSELECTION, 1, pData)
- else if nMsg = BFFM_SELCHANGED then
- begin
- { Change the current folder label }
- if (SHGetPathFromIDList (PItemIDList (nParam), szDir)) then
- SendMessage (hWnd, BFFM_SETSTATUSTEXT, 0, integer (@(szDir[0])));
- end;
-
- result := 0;
- end;
-
- {---------------------------------------------------------------}
- { Browse for a destination folder }
- {---------------------------------------------------------------}
- function TFrmManager.BrowseFolder ( hWnd : integer; var sFolder : string;
- const sDesc : string ) : boolean;
- var
- bi : TBrowseInfoA;
- pIDList : PItemIDList;
- pszBuffer : PChar;
- begin
- pszBuffer := StrAlloc (300);
-
- bi.hwndOwner := hWnd;
- bi.pidlRoot := nil;
- bi.pszDisplayName := pszBuffer;
- bi.lpszTitle := PChar(sDesc);
- bi.ulFlags := BIF_RETURNONLYFSDIRS;
- bi.lpfn := BrowseCallbackProc;
- bi.lParam := LongInt(PChar(sFolder));
- bi.iImage := 0;
-
- result := false;
- pIDList := SHBrowseForFolder (bi);
-
- if Assigned (pIDList) then
- begin
- if SHGetPathFromIDList (pIDList, pszBuffer) then
- begin
- sFolder := pszBuffer;
- result := true;
- end;
-
- CoTaskMemFree (pIDList);
- end;
-
- StrDispose (pszBuffer);
- end;
-
- {***************************************************************}
- { PROCEDURES AND EVENTS TRIGGERED BY THE FORM AND ITS CONTROLS }
- {***************************************************************}
-
- {---------------------------------------------------------------}
- { Select the source folder and file name that will be encoded }
- { by the encode action }
- {---------------------------------------------------------------}
- procedure TfrmManager.btBrowseForSourceClick(Sender: TObject);
- begin
- xOpenDialog.Files.Clear();
- xOpenDialog.Title := 'Source file';
- xOpenDialog.Filter := 'All type (*.*)|*.*';
- xOpenDialog.FilterIndex := 0;
-
- // Show an Open common dialog to let the user select a file
- if xOpenDialog.Execute then
- begin
- edtEncSourceFile.Text := trim( xOpenDialog.Files.Text );
- SetDestinationFileName();
- end;
- end;
-
- {---------------------------------------------------------------}
- { Select the destination and name of the file that will be }
- { created by the encode action }
- {---------------------------------------------------------------}
- procedure TfrmManager.btBrowseForDestClick(Sender: TObject);
- begin
- xOpenDialog.Files.Clear();
- xOpenDialog.Title := 'Destination file';
- xOpenDialog.Filter := 'Encoded (*.uue;*.xxe;*.b64;*.hex)|' +
- '*.uue;*.xxe;*.b64;*.hex|UU encoded (*.uue)|' +
- '*.uue|XX Encoded (*.xxe)|*.xxe|Base 64 (*.b64)|' +
- '*.b64|Hexadecimal (*.hex)|*.hex|BinHex (*.hqz)|' +
- '*.hqx|Quoted Printable (*.qpr)|All Type (*.*)|*.*';
- xOpenDialog.FilterIndex := 0;
-
- // Show an Open common dialog to let the user select the destination file name
- if xOpenDialog.Execute then
- edtEncDestFile.Text := xOpenDialog.Files.Text;
- end;
-
- {---------------------------------------------------------------}
- { The user changed the selected encoding or decoding method }
- {---------------------------------------------------------------}
- procedure TfrmManager.rgMethodClick(Sender: TObject);
- begin
- if ( pgMain.ActivePage = tabEncode ) then
- begin
- // Change the extension of the destination (encoded) file name to be
- // consistent with the newly selected encoding method
- SetDestinationFileExtension( m_eEncodingMethod, TEncodingMethod( rgMethod.ItemIndex ) );
- m_eEncodingMethod := TEncodingMethod( rgMethod.ItemIndex );
- end;
-
- if ( pgMain.ActivePage = tabDecode ) then
- // The user changed the selected Decoding method
- m_eDecodingMethod := TEncodingMethod( rgMethod.ItemIndex );
- end;
-
- {---------------------------------------------------------------}
- { Do the encoding of the selected source file name to the }
- { destination file }
- {---------------------------------------------------------------}
- procedure TfrmManager.btEncodeClick(Sender: TObject);
- begin
- if ( Length( Trim( edtEncSourceFile.Text ) ) <> 0 ) then
- begin
- // There is something to Encode! Do the encoding
- if ( EncodeFile( edtEncSourceFile.Text, m_eEncodingMethod, m_eEndOfLineType,
- m_lMaxLineLength, edtEncDestFile.Text ) ) then
- begin
- // The encoding was successful, we clear the source and destination text boxes.
- edtEncSourceFile.Text := '';
- edtEncDestFile.Text := '';
- end;
- end;
- end;
-
- {---------------------------------------------------------------}
- { Prepare the main window by assigning default values }
- {---------------------------------------------------------------}
- procedure TfrmManager.FormCreate(Sender: TObject);
- begin
- m_eEncodingMethod := emUUEncode;
- m_eDecodingMethod := emUUEncode;
- m_lMaxLineLength := StrToInt( edtMaxLen.Text );
- m_eEndOfLineType := bltCrLf;
-
- // decoding option
- m_bContinueOnInvalidData := chkInvalidData.Checked;
- rgMethod.ItemIndex := 0;
- lstEncodedFile.Clear();
- end;
-
- {---------------------------------------------------------------}
- procedure TfrmManager.rgLineTypeOptionsClick(Sender: TObject);
- begin
- case rgLineTypeOptions.ItemIndex of
- 0 : m_eEndOfLineType := bltCrLf;
- 1 : m_eEndOfLineType := bltLf;
- end;
- end;
-
- {---------------------------------------------------------------}
- procedure TfrmManager.edtMaxLenChange(Sender: TObject);
- begin
- m_lMaxLineLength := StrToInt( edtMaxLen.Text );
- end;
-
- {---------------------------------------------------------------}
- procedure TfrmManager.chkInvalidDataClick(Sender: TObject);
- begin
- m_bContinueOnInvalidData := chkInvalidData.Checked;
- end;
-
- {---------------------------------------------------------------}
- { Remove all items from the Encoded file(s) list box }
- {---------------------------------------------------------------}
- procedure TfrmManager.btClearClick(Sender: TObject);
- begin
- lstEncodedFile.Clear();
- end;
-
- {---------------------------------------------------------------}
- { Select a folder that will contain the decoded files }
- {---------------------------------------------------------------}
- procedure TfrmManager.btBrowseForDecDestClick(Sender: TObject);
- var
- sFolder : string;
- begin
- // By default the browse folder window will be positionned in the currently
- // selected Decode folder
- sFolder := edtDecDestination.Text;
-
- if BrowseFolder( self.Handle, sFolder, 'DecodeFolder' ) then
- edtDecDestination.Text := sFolder;
- end;
-
- {---------------------------------------------------------------}
- { Remove the selected file from the list of files to decode }
- {---------------------------------------------------------------}
- procedure TfrmManager.btRemoveFileClick(Sender: TObject);
- var
- nFirstItem : integer;
- nNbItemRemoved : integer;
- nNbItemsToRemove : integer;
- nNbItem : integer;
- i : integer;
- begin
- nNbItemsToRemove := lstEncodedFile.SelCount;
- nNbItemRemoved := 0;
- nFirstItem := 0;
-
- if ( nNbItemsToRemove <> 0 ) then
- begin
- // Check each file in the list of files to decode and remove it if it is selected
- nNbItem := lstEncodedFile.Items.Count;
-
- for i := nNbItem - 1 downto 0 do
- begin
- if ( lstEncodedFile.Selected[ i ] ) then
- begin
- lstEncodedFile.Items.Delete( i );
- nNbItemRemoved := nNbItemRemoved + 1;
- nNbItem := nNbItem - 1;
-
- if ( nNbItemRemoved = nNbItemsToRemove ) then
- begin
- // We removed the original number of files selected. We set the new item
- // to select in the file list (the file that follows the last selected
- // item) amd exit the loop
- nFirstItem := i;
- break;
- end;
- end;
- end;
-
- if ( nNbItem <> 0 ) then
- begin
- // There is at least 1 file left in the list
- if ( nFirstItem >= nNbItem ) then
- // There was no file after the last one that was removed. We select the
- // last file of the list
- lstEncodedFile.ItemIndex := nNbItem - 1
- else
- lstEncodedFile.ItemIndex := nFirstItem;
-
- end;
- end;
- end;
-
- {---------------------------------------------------------------}
- { Do the decoding of the selected source file(s) to the }
- { selected destination folder }
- {---------------------------------------------------------------}
- procedure TfrmManager.btDecodeClick(Sender: TObject);
- var
- sDecodedFilename : string;
- slEncodedFile : TStringList;
- nNbEncodedFile : integer;
- i : integer;
- begin
- sDecodedFilename := edtDecFilename.Text;
-
- if ( ( Length( sDecodedFilename ) = 0 ) and ( m_eDecodingMethod <> emUUEncode )
- and ( m_eDecodingMethod <> emXXEncode ) ) then
- begin
- // No decode file namw was entered by the user. Use the file name of the
- // first item in the Encoded File(s) list box.
- sDecodedFilename := RemoveFileExtension( ExtractFilename
- ( lstEncodedFile.Items.Strings[ 0 ] ) ) +
- '.bin';
- if ( Length( sDecodedFilename ) <> 0 ) then
- edtDecFilename.Text := sDecodedFilename;
- end;
-
- nNbEncodedFile := lstEncodedFile.Items.Count;
-
- if ( nNbEncodedFile <> 0 ) then
- begin
- // Fill a TStringList with all the encoded file names from the listbox
- slEncodedFile := TStringList.Create();
-
- nNbEncodedFile := nNbEncodedFile - 1;
-
- for i := 0 to nNbEncodedFile do
- slEncodedFile.Add( lstEncodedFile.Items.Strings[ i ] );
-
- // Do the decoding
- if ( DecodeFile( slEncodedFile, nNbEncodedFile, m_eDecodingMethod,
- m_bContinueOnInvalidData, edtDecDestination.Text,
- sDecodedFilename ) ) then
- begin
- // The decoding was successfully. We clear the file source list box
- // and the destination text box
- edtDecDestination.Text := '';
- edtDecFilename.Text := '';
- lstEncodedFile.Clear();
- end;
- slEncodedFile.Free();
- end;
- end;
-
- {---------------------------------------------------------------}
- { Add file(s) to the list of files to decode
- {---------------------------------------------------------------}
- procedure TfrmManager.btAddFileClick(Sender: TObject);
- var
- slFilenames : TStringList;
- i : integer;
- begin
- xOpenDialog.Files.Clear();
- xOpenDialog.Title := 'Encoded file';
- xOpenDialog.Filter := 'Encoded (*.uue;*.xxe;*.b64;*.hex)|' +
- '*.uue;*.xxe;*.b64;*.hex|UU encoded (*.uue)|' +
- '*.uue|XX Encoded (*.xxe)|*.xxe|Base 64 (*.b64)|' +
- '*.b64|Hexadecimal (*.hex)|*.hex|BinHex (*.hqz)|' +
- '*.hqx|Quoted Printable (*.qpr)|All Type (*.*)|*.*';
- xOpenDialog.FilterIndex := 0;
- xOpenDialog.Options := [ ofAllowMultiSelect ];
-
- // Show an Open common dialog to let the user select files
- if xOpenDialog.Execute then
- begin
- slFilenames := TStringList.Create();
- slFilenames.AddStrings( xOpenDialog.Files );
-
- for i := 0 to ( slFilenames.Count - 1 ) do
- // Add the extracted file name to the list box of file names
- AddEncodedFileToList( slFilenames.Strings[ i ] );
-
- slFilenames.Free();
- end;
- end;
-
- {---------------------------------------------------------------}
- { Fill the destination file name to the default value }
- {---------------------------------------------------------------}
- procedure TfrmManager.edtEncSourceFileExit(Sender: TObject);
- begin
- SetDestinationFilename();
- end;
-
- end.
-