home *** CD-ROM | disk | FTP | other *** search
- /*
- Xceed Binary Encoding Library - Encoding/Decoding Manager sample
- Copyright (c) 2001 Xceed Software Inc.
-
- [unMain.cpp]
- s
- This sample demonstrate how to encode a file using different kinds of
- encoding methods, and decode and encoded file. It specifically uses:
- - The ProcessFile method
- - The EndOfLineType, MaxLineLength, ContinueOnInvalidData,
- HeaderDataForkLength, HeaderResourceForkLength, HeaderFilename,
- EncodingFormat and DataFormating 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 "unMain.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma link "XceedBinaryEncodingLib_OCX"
- #pragma resource "*.dfm"
- TfrmManager *frmManager;
-
- //---------------------------------------------------------------------------
- // If a destination file name already exists for the encoding, change
- // its extension for a new one appropriate to the new encoding method
- //---------------------------------------------------------------------------
- void TfrmManager::SetDestinationFileExtension( TEncodingMethod eOldEncodingMethod,
- TEncodingMethod eNewEncodingMethod )
- {
- AnsiString sDestinationFilename;
- AnsiString sDestinationFilenameExtension;
- bool bChangedExtension;
-
- bChangedExtension = false;
- sDestinationFilename = edtDestinationFile->Text;
- sDestinationFilenameExtension = ExtractFileExtension( sDestinationFilename.UpperCase() );
-
- // 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( sDestinationFilename.Length() != 0 )
- {
- switch( eOldEncodingMethod )
- {
- case emUUEncode :
- {
- if( ( sDestinationFilenameExtension == "UUE" ) || ( sDestinationFilenameExtension.Length() == 0 ) )
- {
- bChangedExtension = true;
- break;
- }
- }
- case emXXEncode :
- {
- if( ( sDestinationFilenameExtension == "XXE" ) || ( sDestinationFilenameExtension.Length() == 0 ) )
- {
- bChangedExtension = true;
- break;
- }
- }
- case emBase64 :
- {
- if( ( sDestinationFilenameExtension == "B64" ) || ( sDestinationFilenameExtension.Length() == 0 ) )
- {
- bChangedExtension = true;
- break;
- }
- }
- case emHexaDecimal :
- {
- if( ( sDestinationFilenameExtension == "HEX" ) || ( sDestinationFilenameExtension.Length() == 0 ) )
- {
- bChangedExtension = true;
- break;
- }
- }
- case emQuotedPrintable :
- {
- if( ( sDestinationFilenameExtension == "QPR" ) || ( sDestinationFilenameExtension.Length() == 0 ) )
- {
- bChangedExtension = true;
- break;
- }
- }
- case emBinHex :
- {
- if( ( sDestinationFilenameExtension == "HQX" ) || ( sDestinationFilenameExtension.Length() == 0 ) )
- {
- bChangedExtension = true;
- break;
- }
- }
- }
-
- // If we have determined that a change of extension is in order, Do it!
- if( bChangedExtension )
- {
- sDestinationFilename = RemoveFileExtension( sDestinationFilename ) +
- StdFileExtension( eNewEncodingMethod );
- edtDestinationFile->Text = sDestinationFilename;
- }
- }
- }
-
- //---------------------------------------------------------------------------
- // 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.
- //---------------------------------------------------------------------------
- void TfrmManager::SetDestinationFilename()
- {
- AnsiString sEncodedFilename;
-
- sEncodedFilename = edtDestinationFile->Text;
-
- if( sEncodedFilename.Length() == 0 )
- {
- sEncodedFilename = RemoveFileExtension( edtSourceFileToEncode->Text );
-
- if( sEncodedFilename.Length() != 0 )
- {
- edtDestinationFile->Text = sEncodedFilename;
- SetDestinationFileExtension( m_eEncodingMethod, m_eEncodingMethod );
- }
- }
- }
-
- //---------------------------------------------------------------------------
- // Add the specified file name to the list box of file names to decode
- //---------------------------------------------------------------------------
- void TfrmManager::AddEncodedFileToList( AnsiString sFilename )
- {
- int i;
- bool bFound;
- int nNbItem;
- AnsiString sDecodeFolder;
- AnsiString sExtension;
-
- nNbItem = lstFilesToDecode->Items->Count;
- bFound = false;
-
- // Do not allow more than 1000 files to be decoded at one time
- if( nNbItem < 1000 )
- {
- for( i = 0; i <= nNbItem - 1; i++ )
- {
- // Check if the file is already in the list of files
- if( lstFilesToDecode->Items->Strings[ i ] == sFilename )
- {
- bFound = true;
- break;
- }
- }
-
- if( !bFound )
- {
- // add the file name to the list
- lstFilesToDecode->Items->Add( sFilename );
-
- // If no decode folder is specified, set one to the folder name of
- // the added file name.
- sDecodeFolder = edtDecodeFolder->Text;
-
- if( sDecodeFolder.Length() == 0 )
- {
- sDecodeFolder = ExtractFilePath( sFilename );
-
- if( sDecodeFolder.Length() != 0 )
- edtDecodeFolder->Text = sDecodeFolder;
- }
-
- // If it's the first file added to the list, select it in the list
- if( nNbItem == 0 )
- lstFilesToDecode->ItemIndex = 0;
-
- // Set the decoding method according to the file name extension
- sExtension = sFilename.SubString( ( sFilename.Length() - 3 ), 4 );
-
- if( sExtension.UpperCase() == ".UUE" )
- {
- m_eDecodingMethod = emUUEncode;
- rgMethod->ItemIndex = 0;
- }
-
- if( sExtension.UpperCase() == ".XXE" )
- {
- m_eDecodingMethod = emXXEncode;
- rgMethod->ItemIndex = 1;
- }
-
- if( sExtension.UpperCase() == ".B64" )
- {
- m_eDecodingMethod = emBase64;
- rgMethod->ItemIndex = 2;
- }
-
- if( sExtension.UpperCase() == ".HEX" )
- {
- m_eDecodingMethod = emHexaDecimal;
- rgMethod->ItemIndex = 3;
- }
-
- if( sExtension.UpperCase() == ".QPR" )
- {
- m_eDecodingMethod = emQuotedPrintable;
- rgMethod->ItemIndex = 4;
- }
-
- if( sExtension.UpperCase() == ".HQX" )
- {
- m_eDecodingMethod = emBinHex;
- rgMethod->ItemIndex = 5;
- }
- }
- }
- }
-
- //---------------------------------------------------------------------------
- // Return the extension part of a specified file name
- //---------------------------------------------------------------------------
- AnsiString TfrmManager::ExtractFileExtension( AnsiString sFilename )
- {
- int i;
- int nFilenameLen;
- int nLenToRemove;
-
- nFilenameLen = sFilename.Length();
- i = nFilenameLen;
- nLenToRemove = -1;
-
- // Start from the end of the string, we check each character and stop
- // at the first occurrence of . or "\"
- while( ( i > 0 ) && ( nLenToRemove == -1 ) )
- {
- // The length of the extension part is the length of the string minus
- // the position of the "."
- if( sFilename.SubString( i, 1 ) == "." )
- nLenToRemove = nFilenameLen - i;
-
- if( sFilename.SubString( i, 1 ) == "\\" )
- nLenToRemove = 0;
-
- i = i - 1;
- }
-
- // If no extension was found we return an empty string
- if( nLenToRemove == -1 )
- return "";
- else
- return sFilename.SubString( ( sFilename.Length() - nLenToRemove + 1 ),
- nLenToRemove );
- }
-
- //---------------------------------------------------------------------------
- // Return the specified file name WITHOUT its extension, if any
- //---------------------------------------------------------------------------
- AnsiString TfrmManager::RemoveFileExtension( AnsiString sFilename )
- {
- int i;
- int nFilenameLen;
- int nLenToRemove;
-
- nFilenameLen = sFilename.Length();
- i = nFilenameLen;
- nLenToRemove = -1;
-
- // Starting from the end of the string, we check each character and stop
- // at the first occurrence of "." or "\\"
- while( ( i > 0 ) && ( nLenToRemove == -1 ) )
- {
- // The length of the file name part is the position of the "." minus
- // 1 (to exclude the ".")
- if( sFilename.SubString( i, 1 ) == "." )
- nLenToRemove = i - 1;
-
- // The filename contains a path. Returns all the string
- if( sFilename.SubString( i, 1 ) == "\\" )
- nLenToRemove = nFilenameLen;
-
- i = i -1;
- }
-
- // No extension was found, return an empty strin
- if( nLenToRemove == -1 )
- return "";
- else
- return sFilename.SubString( 1, nLenToRemove );
- }
-
- //---------------------------------------------------------------------------
- // Return a default extension based on an encoding method.
- //---------------------------------------------------------------------------
- AnsiString TfrmManager::StdFileExtension( TEncodingMethod eMethod )
- {
- switch( eMethod )
- {
- case emUUEncode : return ".uue";
- case emXXEncode : return ".xxe";
- case emBase64 : return ".b64";
- case emHexaDecimal : return ".hex";
- case emQuotedPrintable : return ".qpr";
- case emBinHex : return ".hqx";
- }
- // although this situation should noto occur. IF it does, we will return a
- // default UUEncode extention.
- return ".uue";
- }
-
- //---------------------------------------------------------------------------
- // Encode the file!
- //---------------------------------------------------------------------------
- bool TfrmManager::EncodeFile( AnsiString sSourceFilename,
- TEncodingMethod eMethod,
- EXBEndOfLineType eEndOfLineType,
- long lMaxLineLength,
- AnsiString sEncodedFilename )
- {
- IXceedBinaryEncodingPtr piEncoder;
-
- // Create an instance of our Xceed Binary Encoding Object
- piEncoder.CreateInstance( CLSID_XceedBinaryEncoding );
-
- // Create and prepare the encoding format (XX, UU, BinHex...). we also set
- // the end of line type and the maximum line length for the chosen
- // encoding format.
-
- switch( eMethod )
- {
- case emUUEncode :
- {
- IXceedUUEncodingFormatPtr piUUEncode;
- piUUEncode.CreateInstance( CLSID_XceedUUEncodingFormat );
-
- piUUEncode->IncludeHeaderFooter = true;
- piUUEncode->EndOfLineType = eEndOfLineType;
- piUUEncode->MaxLineLength = lMaxLineLength;
-
- piEncoder->EncodingFormat = piUUEncode;
- break;
- }
- case emXXEncode :
- {
- IXceedXXEncodingFormatPtr piXXEncode;
- piXXEncode.CreateInstance( CLSID_XceedXXEncodingFormat );
-
- piXXEncode->IncludeHeaderFooter = true;
- piXXEncode->EndOfLineType = eEndOfLineType;
- piXXEncode->MaxLineLength = lMaxLineLength;
-
- piEncoder->EncodingFormat = piXXEncode;
- break;
- }
- case emBase64 :
- {
- IXceedBase64EncodingFormatPtr piBase64;
- piBase64.CreateInstance( CLSID_XceedBase64EncodingFormat );
-
- piBase64->EndOfLineType = eEndOfLineType;
- piBase64->MaxLineLength = lMaxLineLength;
-
- piEncoder->EncodingFormat = piBase64;
- break;
- }
- case emHexaDecimal :
- {
- IXceedHexaEncodingFormatPtr piHexaDecimal;
- piHexaDecimal.CreateInstance( CLSID_XceedHexaEncodingFormat );
-
- piHexaDecimal->EndOfLineType = eEndOfLineType;
- piHexaDecimal->MaxLineLength = lMaxLineLength;
-
- piEncoder->EncodingFormat = piHexaDecimal;
- break;
- }
- case emQuotedPrintable :
- {
- IXceedQuotedPrintableEncodingFormatPtr piQuotedPrintable;
- piQuotedPrintable.CreateInstance( CLSID_XceedQuotedPrintableEncodingFormat );
-
- piQuotedPrintable->EndOfLineType = eEndOfLineType;
- piQuotedPrintable->MaxLineLength = lMaxLineLength;
-
- piEncoder->EncodingFormat = piQuotedPrintable;
- break;
- }
- case emBinHex :
- {
- IXceedBinHexEncodingFormatPtr piBinHex;
- piBinHex.CreateInstance( CLSID_XceedBinHexEncodingFormat );
-
- piBinHex->IncludeHeaderFooter = true;
- piBinHex->EndOfLineType = eEndOfLineType;
- piBinHex->MaxLineLength = lMaxLineLength;
-
- // For the BinHex format, we must specify the data fork length
- // and the resource fork length
- HANDLE hFileHandle;
- hFileHandle = CreateFile( sSourceFilename.c_str(), GENERIC_READ,
- FILE_SHARE_READ, NULL, OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL, NULL );
-
-
- piBinHex->HeaderDataForkLength = GetFileSize( hFileHandle, NULL );
- piBinHex->HeaderResourceForkLength = 0;
-
- CloseHandle( hFileHandle );
- piEncoder->EncodingFormat = piBinHex;
- break;
- }
- }
-
- // if no extension for the destination file name was provided by the user
- // we set a default one (according to the encoding method)
- if( AnsiString( ExtractFileExtension( sEncodedFilename ) ).Length() == 0 )
- sEncodedFilename = sEncodedFilename + StdFileExtension( eMethod );
-
- lstMessages->Clear();
-
- try
- {
- wchar_t wszSourceFilename[ MAX_PATH ];
- wchar_t wszEncodedFilename[ MAX_PATH ];
-
- sSourceFilename.WideChar( wszSourceFilename, MAX_PATH );
- sEncodedFilename.WideChar( wszEncodedFilename, MAX_PATH );
-
- DWORD dwBytesRead;
- DWORD dwBytesWritten;
-
- // Encode the file, providing:
- // - A source filename, without any offset or size, since we want
- // encode it all.
- // - The processing we want to perform, in this case cfpEncode.
- // - True in the bEndOfData parameter, since we encode it in a single
- // call.
- // - A destination filename we wish to overwrite (bAppend parameter
- // is FALSE).
- // - The address of two DWORD which will receive the number of bytes
- // read and written.
- piEncoder->ProcessFile( wszSourceFilename, 0, 0, bfpEncode, true,
- wszEncodedFilename, false, &dwBytesRead,
- &dwBytesWritten );
-
- // Display a message of success!
- lstMessages->Items->Add( sSourceFilename + " successfully encoded in " +
- sEncodedFilename );
- }
- catch( Exception& xErr )
- {
- lstMessages->Items->Add( xErr.Message );
- return false;
- }
- catch( ... )
- {
- lstMessages->Items->Add( "An unexpected error occured while encoding the file" );
- return false;
- }
- return true;
- }
-
- //---------------------------------------------------------------------------
- // Decode the files!
- //---------------------------------------------------------------------------
- bool TfrmManager::DecodeFile( TStringList* pslEncodedFile,
- int nNbEncodedFile,
- TEncodingMethod eMethod,
- bool bContinueOnInvalidData,
- AnsiString sDecodeFolder,
- AnsiString sDecodedFilename )
- {
- IXceedBinaryEncodingPtr piDecoder;
-
- // Create our instance of the Xceed Binary Encoding Library
- piDecoder.CreateInstance( CLSID_XceedBinaryEncoding );
-
- if( sDecodeFolder.SubString( sDecodeFolder.Length() - 1, 1 ) == "\\" )
- // The decode folder must end with a "\\"
- sDecodeFolder = sDecodeFolder + "\\";
-
- // Create and prepare the decoding format (XX, UU, BinHex...) and tell
- // wether 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).
- switch( eMethod )
- {
- case emUUEncode :
- {
- IXceedUUEncodingFormatPtr piUUEncode;
- piUUEncode.CreateInstance( CLSID_XceedUUEncodingFormat );
-
- piUUEncode->IncludeHeaderFooter = true;
- piUUEncode->ContinueOnInvalidData = bContinueOnInvalidData;
-
- if( sDecodedFilename.Length() == 0 )
- sDecodedFilename = piUUEncode->HeaderFilename;
-
- piDecoder->EncodingFormat = piUUEncode;
- break;
- }
- case emXXEncode :
- {
- IXceedXXEncodingFormatPtr piXXEncode;
- piXXEncode.CreateInstance( CLSID_XceedXXEncodingFormat );
-
- piXXEncode->IncludeHeaderFooter = true;
- piXXEncode->ContinueOnInvalidData = bContinueOnInvalidData;
-
- if( sDecodedFilename.Length() == 0 )
- sDecodedFilename = piXXEncode->HeaderFilename;
-
- piDecoder->EncodingFormat = piXXEncode;
- break;
- }
- case emBase64 :
- {
- IXceedBase64EncodingFormatPtr piBase64;
- piBase64.CreateInstance( CLSID_XceedBase64EncodingFormat );
-
- piBase64->ContinueOnInvalidData = bContinueOnInvalidData;
-
- piDecoder->EncodingFormat = piBase64;
- break;
- }
- case emHexaDecimal :
- {
- IXceedHexaEncodingFormatPtr piHexa;
- piHexa.CreateInstance( CLSID_XceedHexaEncodingFormat );
-
- piHexa->ContinueOnInvalidData = bContinueOnInvalidData;
-
- piDecoder->EncodingFormat = piHexa;
- break;
- }
- case emQuotedPrintable :
- {
- IXceedQuotedPrintableEncodingFormatPtr piQuotedPrintable;
- piQuotedPrintable.CreateInstance( CLSID_XceedQuotedPrintableEncodingFormat );
-
- piQuotedPrintable->ContinueOnInvalidData = bContinueOnInvalidData;
-
- piDecoder->EncodingFormat = piQuotedPrintable;
- break;
- }
- case emBinHex :
- {
- IXceedBinHexEncodingFormatPtr piBinHex;
- piBinHex.CreateInstance( CLSID_XceedBinHexEncodingFormat );
-
- piBinHex->IncludeHeaderFooter = true;
- piBinHex->ContinueOnInvalidData = bContinueOnInvalidData;
-
- if( sDecodedFilename.Length() == 0 )
- sDecodedFilename = piBinHex->HeaderFilename;
-
- piDecoder->EncodingFormat = piBinHex;
- break;
- }
- }
-
- lstMessages->Clear();
-
- try
- {
- wchar_t wsEncodedFilename[ MAX_PATH ];
- wchar_t wsDestinationFilename[ MAX_PATH ];
- AnsiString sFullDestinationFilename;
- DWORD dwBytesRead;
- DWORD dwBytesWritten;
-
- sFullDestinationFilename = sDecodeFolder + sDecodedFilename;
- sFullDestinationFilename.WideChar( wsDestinationFilename, MAX_PATH );
-
- for( int i = 0; i < nNbEncodedFile; i++ )
- {
- pslEncodedFile->Strings[ i ].WideChar( wsEncodedFilename, MAX_PATH );
-
- // Decode a source file, providing:
- // - The source filename, without any offset or size, since we
- // want to decode it all.
- // - The type of processing, in this case cfpDecode.
- // - bEndOfData set to true only for the last file.
- // - The destination filename we are appending to
- // (bAppend set to True).
- // - The addres of two DWORd that will contain the number of bytes
- // read and written on return.
- piDecoder->ProcessFile( wsEncodedFilename, 0, 0, bfpDecode,
- ( i == nNbEncodedFile-1 ), wsDestinationFilename,
- true, &dwBytesRead, &dwBytesWritten );
-
- // Display a message of success
- lstMessages->Items->Add( AnsiString( wsEncodedFilename ) + " successfully decoded in " +
- AnsiString( wsDestinationFilename ) );
- }
- }
- catch( Exception& xErr )
- {
- lstMessages->Items->Add( xErr.Message );
- return false;
- }
- catch( ... )
- {
- lstMessages->Items->Add( "An expected error occurred while decoding" );
- return false;
- }
- return true;
- }
-
- //---------------------------------------------------------------------------
- // Initalize folder browsing window
- //---------------------------------------------------------------------------
- /* static */
- int __stdcall TfrmManager::BrowseCallbackProc( HWND hWnd, UINT uMsg,
- LPARAM lParam, LPARAM lpData )
- {
- if( uMsg == BFFM_INITIALIZED )
- SendMessageA( hWnd, BFFM_SETSELECTION, 1, lpData );
- else if( uMsg == BFFM_SELCHANGED )
- {
- // Change the text of the current directory
- char szDir[ MAX_PATH ];
- if( SHGetPathFromIDList( ( _ITEMIDLIST* )lParam, szDir ) )
- SendMessageA( hWnd, BFFM_SETSTATUSTEXT, 0, ( LPARAM )szDir );
- }
- return 0;
- }
-
- //---------------------------------------------------------------------------
- // Browse for a destination folder
- //---------------------------------------------------------------------------
- bool TfrmManager::BrowseFolder( HWND hWnd, AnsiString& sFolder,
- AnsiString sDesc )
- {
- char szBuffer[ 300 ];
-
- BROWSEINFO bi;
-
- bi.hwndOwner = hWnd;
- bi.pidlRoot = NULL;
- bi.pszDisplayName = szBuffer;
- bi.lpszTitle = sDesc.c_str();
- bi.ulFlags = BIF_RETURNONLYFSDIRS;
- bi.lpfn = BrowseCallbackProc;
- bi.lParam = ( DWORD )sFolder.c_str();
- bi.iImage = 0;
-
- bool bResult = false;
- _ITEMIDLIST* pItem = SHBrowseForFolder( &bi );
-
- if( pItem )
- {
- if( SHGetPathFromIDList( pItem, szBuffer ) )
- {
- sFolder = szBuffer;
- bResult = true;
- }
-
- CoTaskMemFree( pItem );
- }
-
- return bResult;
- }
-
- //---------------------------------------------------------------------------
- // Prepare the main window by assigning the default values
- //---------------------------------------------------------------------------
- __fastcall TfrmManager::TfrmManager(TComponent* Owner)
- : TForm(Owner)
- {
- m_eEncodingMethod = emUUEncode;
- m_eDecodingMethod = emUUEncode;
- m_lMaxLineLength = StrToInt( edtMaxLineLen->Text );
- m_eEndOfLineType = bltCrLf;
-
- // Decoding option
- m_bContinueOnInvalidData = chkContinueOnInvalidData->Checked;
-
- rgMethod->ItemIndex = 0;
- lstFilesToDecode->Clear();
- }
-
- //---------------------------------------------------------------------------
- // Do the encoding of the selected file name to the destination file
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::btEncodeClick(TObject *Sender)
- {
- if( edtSourceFileToEncode->Text.Length() != 0 )
- {
- // There is something to encode! Do the encoding
-
- if( EncodeFile( edtSourceFileToEncode->Text, m_eEncodingMethod,
- m_eEndOfLineType, m_lMaxLineLength,
- edtDestinationFile->Text ) )
- {
- // The encoding was successful sp we clear the source and destination
- // edit boxes
- edtSourceFileToEncode->Text = "";
- edtDestinationFile->Text = "";
- }
- }
- }
-
- //---------------------------------------------------------------------------
- // Remove all items from the files to decode list box
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::btClearListClick(TObject *Sender)
- {
- lstFilesToDecode->Clear();
- }
-
- //---------------------------------------------------------------------------
- // Select wether the deconding process should continue if invalid data is
- // encountered
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::chkContinueOnInvalidDataClick(TObject *Sender)
- {
- m_bContinueOnInvalidData = chkContinueOnInvalidData->Checked;
- }
-
- //---------------------------------------------------------------------------
- // Update the MaxLineLength to use
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::edtMaxLineLenChange(TObject *Sender)
- {
- m_lMaxLineLength = StrToInt( edtMaxLineLen->Text );
- }
-
- //---------------------------------------------------------------------------
- // Select the end of line type to use
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::rgEndOfLineOptionsClick(TObject *Sender)
- {
- switch( rgEndOfLineOptions->ItemIndex )
- {
- case 0 :
- {
- m_eEndOfLineType = bltCrLf;
- break;
- }
- case 1 :
- {
- m_eEndOfLineType = bltLf;
- break;
- }
- }
- }
-
- //---------------------------------------------------------------------------
- // Remove the selected file from the list of files to decode
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::btRemoveFilesClick(TObject *Sender)
- {
- int nFirstItem = 0;
- int nNbItemRemoved = 0;
- int nNbItemsToRemove;
- int nNbItem;
- int i;
-
- nNbItemsToRemove = lstFilesToDecode->SelCount;
-
- if( nNbItemsToRemove != 0 )
- {
- // Check each file in the list of files to decode and remove if selected
- nNbItem = lstFilesToDecode->Items->Count;
-
- for( i = nNbItem - 1; i >= 0; i-- )
- {
- if( lstFilesToDecode->Selected[ i ] )
- {
- lstFilesToDecode->Items->Delete( i );
- nNbItemRemoved = nNbItemRemoved + 1;
- nNbItem = nNbItem - 1;
-
- if( nNbItem == nNbItemsToRemove )
- {
- // We removed the original number of files selected. We set then
- // new item to select in the file list (the file that follows the
- // last selected item) and exit the loop.
- nFirstItem = i;
- break;
- }
- }
- }
-
- if( nNbItem != 0 )
- {
- // There is at least one file left in the list
- if( nFirstItem >= nNbItem )
- // There was no file after the last one that was removed. We select
- // the last file of the list
- lstFilesToDecode->ItemIndex = nNbItem - 1;
- else
- lstFilesToDecode->ItemIndex = nFirstItem;
- }
- }
- }
-
-
- //---------------------------------------------------------------------------
- // File the destination file name to the default value
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::edtSourceFileToEncodeExit(TObject *Sender)
- {
- SetDestinationFilename();
- }
-
- //---------------------------------------------------------------------------
- // Select the source folder and file name that will be encoded
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::btBrowseForSourceClick(TObject *Sender)
- {
- 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() )
- {
- edtSourceFileToEncode->Text = xOpenDialog->Files->Text.Trim();
- SetDestinationFilename();
- }
- }
-
- //---------------------------------------------------------------------------
- // Select the destination and name of the encoded file that will be created
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::btBrowseForDestinationClick(TObject *Sender)
- {
- xOpenDialog->Files->Clear();
- xOpenDialog->Title = "Destination file";
- xOpenDialog->FilterIndex = 0;
- xOpenDialog->Filter = "Encoded (*.uue;*.xxe;*.b64;*.hex;*.hqz;*.qpr)|"
- "*.uue;*.xxe;*.b64;*.hex;*.hqz;*.qpr|"
- "UU encode (*.uue)|*.uue|XX encode (*.xxe)|*.xxe|"
- "Base 64 (*.b64)|*.b64|HexaDecimal (*.hex)|*.hex|"
- "Quoted Printable (*.qpr)|*.qpr|BinHex (*.hqz)|*.hqz";
-
- // Show an open dialog to let the user select the destination file name
- if( xOpenDialog->Execute() )
- edtDestinationFile->Text = xOpenDialog->Files->Text.Trim();
- }
-
- //---------------------------------------------------------------------------
- // The user changed the selected encoding or decoding method
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::rgMethodClick(TObject *Sender)
- {
- if( pgMain->ActivePage == tabEncode )
- {
- // 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 );
- }
-
- if( pgMain->ActivePage == tabDecode )
- // The user changed the selected decoding method
- m_eDecodingMethod = TEncodingMethod( rgMethod->ItemIndex );
- }
-
- //---------------------------------------------------------------------------
- // Add file(s) to the list of files to decode
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::btAddFilesToDecodeClick(TObject *Sender)
- {
- TStringList* pslFilenames = new TStringList();
- int i;
-
- xOpenDialog->Files->Clear();
- xOpenDialog->Title = "Encoded file(s)";
- xOpenDialog->FilterIndex = 0;
- xOpenDialog->Options << ofAllowMultiSelect;
-
- xOpenDialog->Filter = "Encoded (*.uue;*.xxe;*.b64;*.hex;*.hqz;*.qpr)|"
- "*.uue;*.xxe;*.b64;*.hex;*.hqz;*.qpr|"
- "UU encode (*.uue)|*.uue|XX encode (*.xxe)|*.xxe|"
- "Base 64 (*.b64)|*.b64|HexaDecimal (*.hex)|*.hex|"
- "Quoted Printable (*.qpr)|*.qpr|BinHex (*.hqz)|*.hqz|"
- "All types (*.*)|*.*";
-
- // Show an Open common dialog to let the user select the files to be decoded
- if( xOpenDialog->Execute() )
- {
- pslFilenames->AddStrings( xOpenDialog->Files );
-
- for( i = 0; i <= pslFilenames->Count - 1; i++ )
- // Add the extracted file name to the list of files to decode
- AddEncodedFileToList( pslFilenames->Strings[ i ] );
-
- delete pslFilenames;
- }
- }
-
- //---------------------------------------------------------------------------
- // Do the decoding of the selected source file(s) to the selected
- // destination folder
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::btDecodeClick(TObject *Sender)
- {
- AnsiString sDecodedFilename;
-
- sDecodedFilename = edtDecodedFilename->Text;
-
- if( ( sDecodedFilename.Length() == 0 ) &&
- ( m_eDecodingMethod != emUUEncode ) &&
- ( m_eDecodingMethod != emXXEncode ) )
- {
- // No decode file name was entered by the user. Use the filename of the
- // first item in the list of encoded files.
- sDecodedFilename = RemoveFileExtension( ExtractFileName
- ( lstFilesToDecode->Items->Strings[ 0 ] ) ) +
- ".bin";
- if( sDecodedFilename.Length() != 0 )
- edtDecodedFilename->Text = sDecodedFilename;
- }
-
- int nNbEncodedFile = lstFilesToDecode->Items->Count;
-
- if( nNbEncodedFile != 0 )
- {
- // Fill the TStringList with all the encoded file names from the list box
- TStringList* slEncodedFiles = new TStringList();
-
- for( int i = 0; i < nNbEncodedFile; i++ )
- slEncodedFiles->Add( lstFilesToDecode->Items->Strings[ i ] );
-
- // Do the decoding
- if( DecodeFile( slEncodedFiles, nNbEncodedFile, m_eDecodingMethod,
- m_bContinueOnInvalidData, edtDecodeFolder->Text,
- sDecodedFilename ) );
- {
- // The decoding was successful. We clear the file source list box
- // and the destination text box
- edtDecodedFilename->Text = "";
- edtDecodeFolder->Text = "";
- lstFilesToDecode->Clear();
-
- delete slEncodedFiles;
- }
- }
- }
-
- //---------------------------------------------------------------------------
- // Select a folder that will contain the decoded file
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::btBrowseForDecodeFolderClick(TObject *Sender)
- {
- AnsiString sFolder = edtDecodeFolder->Text;
-
- // By default the browse folder window will be positionned in the currently
- // selected Decode folder.
- if( BrowseFolder( Handle, sFolder, "Decode folder:" ) )
- {
- edtDecodeFolder->Text = sFolder;
- }
- }
- //---------------------------------------------------------------------------
-
-