Click here to Skip to main content
Click here to Skip to main content

The Ultimate Toolbox File Management

, 25 Aug 2007
Rate this:
Please Sign up or sign in to vote.
Ultimate Toolbox classes that deal with Binary Difference, Search and Replace, Compression, and more.

Visit the Ultimate Toolbox main page for an overview and configuration guide to the Ultimate Toolbox library.

Contents

Introduction

The Ultimate Toolbox contains some useful utilities for file encryption and compression, comparison, search and replace and more.

Binary Difference

The COXBinDiffCalculator, COXDiffFileHeader, and COXDiffProgress classes combine to encapsulate binary differences and are able to compute the differences between two versions of a file.

Imagine that you have an original file and an updated file. Using the COXBinDiffCalculator, you can see what has changed between the original and the updated version. This difference can then be written to a third file. Later on you can reproduce the updated version using the original and the difference file.

Difference = Update - Original and Update = Original + Difference. This can be useful if you want to send an updated file version to other people. You don't have to send them the entire new file, just the differences. These can then be applied to the original file which results in a fully updated version. Normally the difference file is much smaller then the entire updated file. You can supply path specifications directly to create the difference or updated file.

void SubtractFiles(LPCTSTR orgFilNam, LPCTSTR derivedFilNam, 
    LPCTSTR diffFilNam, COXDiffFileHeader* pHeader = NULL);
void AddFiles(LPCTSTR orgFilNam, LPCTSTR derivedFilNam, LPCTSTR diffFilNam, 
    COXDiffFileHeader* pHeader = NULL);

Another possibility would be to create three CFile derived objects, open them and let the COXBinDiffCalculator object use them. This creates the possibility of using other CFile based classes - CMemFile, COXCryptFile or your own.

virtual void SubtractFiles(CFile* pOrgFil, CFile* pDerivedFil, 
    CFile* pDiffFil, COXDiffFileHeader* pHeader = NULL);
virtual void AddFiles(CFile* pOrgFil, CFile* pDerivedFil, CFile* pDiffFil, 
    COXDiffFileHeader* pHeader = NULL);

While the differences are being computed or applied, a progress bar can help visualize how much has already been completed. This information is supplied to a COXDiffProgress object. By default this is just written to standard output (e.g. a console program). You can derive your own class from it and make another visual indicator:

void ReplaceProgressBar(COXDiffProgress* pProgressBar);

If you want to use the class only to AddFiles you can #define BDEXTR as 1, which will make the class more compact.

See the compiled HTML help for complete class references for these.

Compression and Encryption

COXCompressedFile, COXCompressor, COXCryptFile, COXMDCCryptor, and COXConvertedFile provide various forms of file compression and encryption.

COXCompressedFile

The COXCompressedFile class encapsulates compression over a CFile object. Simply put, COXCompressedFile enhances the CFile class to allow the data to be compressed. You treat a COXCompressedFile in the same way as a CFile object except that some operations are not available on the compressed file. These operations are:

  1. Setting the file size via SetLength.

  2. Locking and unlocking a byte range via LockRange and UnlockRange.

  3. Flushing a file via Flush.

In each of these cases, the COXCompressedFile class will throw a CNotSupportedException MFC exception object.

COXCompressor

The COXCompressor class encapsulates an enhanced version of the Lempel Ziv 77 compression algorithm. It is based on the code supplied by Mark Nelson in his book The Data Compression Book, (M&T Publishing 1992). This algorithm is based on a sliding window dictionary-based compression scheme.

To compress and uncompress (or expand) data use the following functions:

int Compress(const LPBYTE pInBuffer, int nInLength, LPBYTE pOutBuffer, int 
    nMaxOutLength);
int Expand  (const LPBYTE pInBuffer, int nInLength, LPBYTE pOutBuffer, 
    int nMaxOutLength);

Each of these functions takes a pointer to input data identified by pInBuffer and writes the compressed (if calling Compress) or uncompressed (if calling Expand) data to the output buffer pOutBuffer.

COXConvertedFile, COXCryptFile and COXMDCCryptor

COXCryptFile is derived from the COXConvertedFile which in turn extends the MFCs CFile class to facilitate block encryption and decryption operations on a file.

COXCryptFile layers a COXMDCCryptor to provide MD5 block encryption for files.

The CProtectedFile class in the samples\Advenced\DIBManager sample uses these calsses to apply protection to files:

BOOL CProtectedFile::SetProtection(CFile* pFile)
{
    CDIBManagerApp* pApp=(CDIBManagerApp*)AfxGetApp();

    pFile->SeekToBegin();

    // we use either compression and encryption

    // or just compression to protect the file

    // by means of delegating rights to read and write operations

#ifdef _UT_DIBMANAGER_ENCRYPTION
    COXCryptFile EncryptFile(40);
    COXCompressedFile CompressFile;
    EncryptFile.DelegateOpen(&m_file,CFile::modeReadWrite);
    CompressFile.DelegateOpen(&EncryptFile,CFile::modeWrite);
#else
    COXCompressedFile CompressFile;
    CompressFile.DelegateOpen(&m_file,CFile::modeWrite);
#endif
        
    if(!CopyFile(&CompressFile,pFile,TRUE))
    {
        pApp->ErrorNotify(_T("Cannot protect image!"));
        return FALSE;
    }
    return TRUE;
}

See the File Management | Compression and Encryption section of compiled HTML help for more on these classes and their usage.

Note: In versions previous to 9.0 of the Ultimate Toolbox library COXMDCCryptor and COXCryptFile did not ship with the main installation files of Ultimate Toolbox. They were available to users within North America only, in compliance with certain U.S. export restrictions concerning encryption algorithms.

File Watch

The File Watcher sample in action.

COXFileWatcher and COXFileWatchNotifier organize file change notifications in a number of directories supplied by the user.

See the File Watcher article for more on this class and its usage.

Search and Replace

The COXFileChanger class facilitates search and replace of a files contents.

The samples\utility\FileChanger example project in action.

The COXFileChanger can be set to send notifications (WM_OX_FILE_MATCH_FOUND, WM_OX_FILE_PRE_REPLACE, WM_OX_FILE_POST_REPLACE) to a window to allow for confirmation of replacement actions resulting from calls to the Search and Replace methods. This bit of code asks the user whether to add this instance to the count of items found - the search can also be cancelled by setting the data m_bContinue flag to false:

LRESULT CFileChangerDemoDlg::OnFileMatchFound(WPARAM wParam, 
    LPARAM /* lParam */)
    {
    COXFileChanger::COXFileChangerData* pFileChangerData = (
        COXFileChanger::COXFileChangerData*)wParam;
    ASSERT(AfxIsValidAddress(pFileChangerData, sizeof(
        COXFileChanger::COXFileChangerData)));

    if (m_bMessage)
        {
        CString sMsg;
        sMsg.Format(_T("%s [%d]\r\n\r\nCount?"), 
            pFileChangerData->m_pFile->GetFileName(), 
            pFileChangerData->m_nPos);

        switch(AfxMessageBox(sMsg, MB_YESNOCANCEL | MB_ICONQUESTION))
            {
            case IDCANCEL: pFileChangerData->m_bContinue = FALSE;
            case IDNO:  return FALSE;
            }
        }
    return (LRESULT)TRUE;
    }

URL - UNC

COXURL, COXUNC, COXUNCStandardActor, and COXIteratorUNC.

Visit the URL article for more on this class and its usage.

Unix to Unix Decoding

The COXUUDecoder class performs decoding of Unix to Unix encoded files.

Unix to Unix encoding arose from the need to transfer binary data through 7 bit ASCII text protocols, and survives as one encoding scheme available for MIME encoded email and news messages.

Usage

CStringArray DecodedArray;

HRESULT hr; 

COXUUDecoder UUDecoder;

if (UUDecoder.IsUUEncodedFile(
    "c:\temp\msg.uue"))
{
     UUDecoder.SetSkipIfExists(TRUE);
     hr = UUDecoder.UUDecodeFile(
         "c:\temp\msg.uue", "c:\uudecoded", DecodedArray);

     if (FAILED(hr))
          AfxMessageBox("UUDecoding Failed");
     }
}

The code above examines whether the file c:\temp\msg.uue is a uuencoded file or not. If it is a uuencoded file, it tries to decode it and save the file(s) with the filename(s) it finds in the uuencoded file and place the saved file(s) in the directory c:\uudecoded as specified by the user. If the uuencoded file is a multiple part file, it will decode and save all parts with their stored names in the directory c:\uudecoded. After the UUDecode function is complete, you can examine the CStringArray to find out which files were saved and where they were saved.

To give the part(s) of a (multiple part) uuencoded file, other filenames than the ones stored in the uuencoded file, you must set the strings(paths) in the CStringArray before you pass it to the uudecode function.

See the compiled HTML help documentation for more on the COXUUDecoder class.

History

Initial CodeProject release August 2007.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

The Ultimate Toolbox
Web Developer
Canada Canada
In January 2005, David Cunningham and Chris Maunder created TheUltimateToolbox.com, a new group dedicated to the continued development, support and growth of Dundas Software’s award winning line of MFC, C++ and ActiveX control products.
 
Ultimate Grid for MFC, Ultimate Toolbox for MFC, and Ultimate TCP/IP have been stalwarts of C++/MFC development for a decade. Thousands of developers have used these products to speed their time to market, improve the quality of their finished products, and enhance the reliability and flexibility of their software.
Group type: Organisation

381 members


Comments and Discussions

 
QuestionWhere does one get COXCryptFile? PinmemberCoreyCooper28-Sep-11 20:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 25 Aug 2007
Article Copyright 2007 by The Ultimate Toolbox
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid