|
Broken links? Email us! |
|
I've been envolved in some projects which required file gathering through directories and
this class allows just that: gather file information recursively by directory and, as a
bonus track, it also calculates 32bit file-checksum (note this is not NT's executables checksum
calculated with MapFileAndChecksum
) and 32bit file-CRC
(with a borrowed code, I didn't feel like re-inventing the wheel and the other option was to
review my Codification Theory notes and I'm a bit alergic to dust).
The second part of this article presents FCompare
, a sample application of
CFileInfo
and CFileInfoArray
usage. This application does a:
CFileInfo
, CFileInfoArray
and
FCompare can be safely recompiled to unicode.
/** * @class Stores information about a file in a way like <c CFindFile> does */ class CFileInfo { public: /** @access Public members */ CFileInfo(); /** * @cmember Copy constructor * @parm CFileInfo to copy member variables from. */ CFileInfo(const CFileInfo& finf); /** * @cmember Destructor */ ~CFileInfo(); /** * @cmember Initializes CFileInfo member variables. * @parm Values to init member variables. * @parm Path of the file the CFileInfo refers to. * @parmopt User defined parameter. */ void Create(const WIN32_FIND_DATA* pwfd, const CString strPath, LPARAM lParam=NULL); /** * @cmember Initializes CFileInfo member variables. * @parm Absolute path for file or directory * @parmopt User defined parameter. */ void Create(const CString strFilePath, LPARAM lParam = NULL); /** * @cmember Calcs 32bit checksum of file (i.e. sum of all the DWORDS of the file, * truncated to 32bit). * @parmopt Number of maximum bytes read for checksum calculation. This number is * up-rounded to a multiple of 4 bytes (DWORD). If 0 or bigger than uhFileSize, checksum * for all the file is calculated. * @parmopt Force recalculation of checksum (otherwise if checksum has already * been calculated, it isn't calculated again and previous calculated value is returned). * @parmopt Flag to allow calling application to abort the calculation of * checksum (for multithreaded applications). * @parmopt Pointer to counter of bytes whose checksum has been calculated. * This value is updated while checksum is being calculated, so calling application * can view the progress of checksum calc (for multithreaded applications). * Maximum value for pulCount is uhFileSize. */ DWORD GetChecksum(const ULONGLONG uhUpto=0, const BOOL bRecalc = FALSE, const volatile BOOL* pbAbort=NULL, volatile ULONG* pulCount = NULL); /** * @cmember Calcs 32bit CRC of file contents (i.e. CRC of all the DWORDS of the file). * @parmopt Number of maximum bytes read for CRC calculation. This number is * up-rounded to a multiple of 4 bytes (DWORD). If 0 or bigger than uhFileSize, CRC * for all the file is calculated. * @parmopt Force recalculation of CRC (otherwise if CRC has already * been calculated, it isn't calculated again and previous calculated value is returned). * @parmopt pbAbort Flag to allow calling application to abort the calculation of * CRC (for multithreaded applications). * @parmopt Pointer to counter of bytes whose CRC has been calculated. * This value is updated while CRC is being calculated, so calling application * can view the progress of CRC calc (for multithreaded applications). * Maximum value for pulCount is uhFileSize. */ DWORD GetCRC(const ULONGLONG dhUpto=0, const BOOL bRecalc = FALSE, const volatile BOOL* pbAbort=NULL, volatile ULONG* pulCount = NULL); /** @cmember File size in bytes as a DWORD value. */ DWORD GetLength(void) const { return (DWORD) m_uhFileSize; }; /** @cmember File size in bytes as an ULONGLONG value. */ ULONGLONG GetLength64(void) const { return m_uhFileSize; }; /** Get File split info (equivalent to CFindFile members) */ /** * @cmember Gets the file drive * @rdesc Returns C: for C:\WINDOWS\WIN.INI */ CString GetFileDrive(void) const; /** * @cmember Gets the file dir * @rdesc Returns \WINDOWS\ for C:\WINDOWS\WIN.INI */ CString GetFileDir(void) const; /** @cmember returns WIN for C:\WINDOWS\WIN.INI */ CString GetFileTitle(void) const; /** @cmember returns INI for C:\WINDOWS\WIN.INI */ CString GetFileExt(void) const; /** @cmember returns C:\WINDOWS\ for C:\WINDOWS\WIN.INI */ CString GetFileRoot(void) const { return GetFileDrive() + GetFileDir(); }; /** @cmember returns WIN.INI for C:\WINDOWS\WIN.INI */ CString GetFileName(void) const { return GetFileTitle() + GetFileExt(); }; /** @cmember returns C:\WINDOWS\WIN.INI for C:\WINDOWS\WIN.INI */ const CString& GetFilePath(void) const { return m_strFilePath; } /* Get File times info (equivalent to CFindFile members) */ /** @cmember returns creation time */ const CTime& GetCreationTime(void) const { return m_timCreation; }; /** @cmember returns last access time */ const CTime& GetLastAccessTime(void) const { return m_timLastAccess; }; /** @cmember returns las write time */ const CTime& GetLastWriteTime(void) const { return m_timLastWrite; }; /* Get File attributes info (equivalent to CFindFile members) */ /** @cmember returns file attributes */ DWORD GetAttributes(void) const { return m_dwAttributes; }; /** @cmember returns TRUE if the file is a directory */ BOOL IsDirectory(void) const { return m_dwAttributes & FILE_ATTRIBUTE_DIRECTORY; }; /** @cmember Returns TRUE if the file has archive bit set */ BOOL IsArchived(void) const { return m_dwAttributes & FILE_ATTRIBUTE_ARCHIVE; }; /** @cmember Returns TRUE if the file is read-only */ BOOL IsReadOnly(void) const { return m_dwAttributes & FILE_ATTRIBUTE_READONLY; }; /** @cmember Returns TRUE if the file is compressed */ BOOL IsCompressed(void) const { return m_dwAttributes & FILE_ATTRIBUTE_COMPRESSED; }; /** @cmember Returns TRUE if the file is a system file */ BOOL IsSystem(void) const { return m_dwAttributes & FILE_ATTRIBUTE_SYSTEM; }; /** @cmember Returns TRUE if the file is hidden */ BOOL IsHidden(void) const { return m_dwAttributes & FILE_ATTRIBUTE_HIDDEN; }; /** @cmember Returns TRUE if the file is temporary */ BOOL IsTemporary(void) const { return m_dwAttributes & FILE_ATTRIBUTE_TEMPORARY; }; /** @cmember Returns TRUE if the file is a normal file */ BOOL IsNormal(void) const { return m_dwAttributes == 0; }; LPARAM m_lParam; /** User-defined parameter */ private: /** @access Private members */ CString m_strFilePath; /** @cmember Full filepath of file (directory+filename) */ DWORD m_dwAttributes; /** @cmember File attributes of file (as returned by FindFile() */ ULONGLONG m_uhFileSize; /** @cmember File of size. (COM states LONGLONG as hyper, so "uh" means unsigned hyper) */ CTime m_timCreation; /** @cmember Creation time */ CTime m_timLastAccess; /** @cmember Last Access time */ CTime m_timLastWrite; /** @cmember Last write time */ DWORD m_dwChecksum; /** @cmember Checksum calculated for the first m_uhChecksumBytes bytes */ DWORD m_dwCRC; /** @cmember CRC calculated for the first m_uhCRCBytes bytes */ DWORD m_uhCRCBytes; /** @cmember Number of file bytes with CRC calc'ed (4 multiple or filesize ) */ DWORD m_uhChecksumBytes;/** @cmember Number of file bytes with Checksum calc'ed (4 multiple or filesize) */ }; /** * @class Allows to retrieve <c CFileInfo>s from files/directories in a directory */ class CFileInfoArray : public CArray<CFILEINFO, CFileInfo&> { public: /** @access Public members */ /** * @cmember Default constructor */ CFileInfoArray(); /** * @cmember,menum Default values for <md CFileInfoArray.lAddParam> */ enum { AP_NOSORT=0, /** @@emem Insert <c CFileInfo>s in a unordered manner */ AP_SORTASCENDING=0, /** @@emem Insert <c CFileInfo>s in a ascending order */ AP_SORTDESCENDING=1, /** @@emem Insert <c CFileInfo>s in a descending number */ AP_SORTBYSIZE=2, /** @@emem AP_SORTBYSIZE | Insert <c CFileInfo>s ordered by uhFileSize (presumes array is previously ordered by uhFileSize). */ AP_SORTBYNAME=4 /** @@emem AP_SORTBYNAME | Insert <c CFileInfo>s ordered by strFilePath (presumes array is previously ordered by strFilePath) */ }; /** * @cmember Adds a file or all contained in a directory to the CFileInfoArray * Only "static" data for CFileInfo is filled (by default CRC and checksum are NOT * calculated when inserting CFileInfos). Returns the number of <c CFileInfo>s added to the array * @parm Name of the directory, ended in backslash. * @parm Mask of files to add in case that strDirName is a directory * @parm Wether to recurse or not subdirectories * @parmopt Parameter to pass to protected member function AddFileInfo * @parmopt Wether to add or not CFileInfos for directories * @parmopt Pointer to a variable to signal abort of directory retrieval * (multithreaded apps). * @parmopt pulCount Pointer to a variable incremented each time a CFileInfo is added to the * array (multithreaded apps). * @xref <mf CFileInfoArray.AddFile> <mf CFileInfoArray.AddFileInfo> <md CFileInfoArray.AP_NOSORT> */ int AddDir(const CString strDirName, const CString strMask, const BOOL bRecurse, LPARAM lAddParam=AP_NOSORT, const BOOL bIncludeDirs=FALSE, const volatile BOOL* pbAbort = NULL, volatile ULONG* pulCount = NULL); /** * @cmember Adds a single file or directory to the CFileInfoArray. In case of directory, files * contained in the directory are NOT added to the array. * Returns the position in the array where the <c CFileInfo> was added (-1 if <c CFileInfo> * wasn't added) * @parm Name of the file or directory to add. NOT ended with backslash. * @parm Parameter to pass to protected member function AddFileInfo. * @xref <mf CFileInfoArray.AddDir> <mf CFileInfoArray.AddFileInfo> */ int AddFile(const CString strFilePath, LPARAM lAddParam); protected: /** @access Protected Members */ /** * @cmember Called by AddXXXX to add a CFileInfo to the array. Can be overriden to: * 1. Add only desired CFileInfos (filter) * 2. Fill user param lParam * 3. Change sort order/criteria * Returns the position in the array where the CFileInfo was added or -1 if the CFileInfo * wasn't added to the array. * Default implementation sorts by lAddParam values and adds all CFileInfos * (no filtering) * @parm CFileInfo to insert in the array. * @parm Parameter passed from AddDir function. * @xref <mf CFileInfoArray.AddDir> */ virtual int AddFileInfo(CFileInfo& finf, LPARAM lAddParam); };
I recommend you to read thoroughly the above class header to get an overall view of the classes and their methods. For further refference, you can inspect FCompare's source code (see second half of article).
Anyway, there it goes some sample code:
This code adds all files in root directory and its subdirectories (but not directories themselves)
to the array and TRACE
s them:
CFileInfoArray fia; fia.AddDir( "C:\\", // Directory "*.*", // Filemask (all files) TRUE, // Recurse subdirs fia::AP_SORTBYNAME | fia::AP_SORTASCENDING, // Sort by name and ascending FALSE // Do not add array entries for directories (only for files) ); TRACE("Dumping directory contents\n"); for (int i=0;i<fia.GetSize();i++) TRACE(fia[i].GetFilePath()+"\n");You can also call
AddDir
multiple times. The example shows files in root directories
(but not subdirectories) of C:\\ and D:\\:
CFileInfoArray fia; // Note both AddDir use the same sorting order and direction fia.AddDir("C:\\", "*.*", FALSE, fia::AP_SORTBYNAME | fia::AP_SORTASCENDING, FALSE ); fia.AddDir("D:\\", "*.*", FALSE, fia::AP_SORTBYNAME | fia::AP_SORTASCENDING, FALSE ); TRACE("Dumping directory contents for C:\\ and D:\\ \n"); for (int i=0;i<fia.GetSize();i++) TRACE(fia[i].GetFilePath()+"\n");
Or you can add individual files:
CFileInfoArray fin; // Note both AddDir and AddFile must use the same sorting order and direction fia.AddDir("C:\\WINDOWS\\", "*.*", FALSE, fia::AP_SORTBYNAME | fia::AP_SORTDESCENDING, FALSE ); fia.AddFile("C:\\AUTOEXEC.BAT", fia::AP_SORTBYNAME | fia::SORTDESCENDING); TRACE("Dumping directory contents for C:\\WINDOWS\\ and file C:\\AUTOEXEC.BAT\n"); for (int i=0;i<fia.GetSize();i++) TRACE(fia[i].GetFilePath()+"\n");
And mix directories with individual files:
CFileInfoArray fin; // Note both AddDir and AddFile must use the same sorting order and direction // Note also the list of filemasks *.EXE and *.COM fia.AddDir("C:\\WINDOWS\\", "*.EXE;*.COM", FALSE, fia::AP_SORTBYNAME | fia::AP_SORTDESCENDING, FALSE ); fia.AddFile("C:\\AUTOEXEC.BAT", fia::AP_SORTBYNAME | fia::SORTDESCENDING); // Note no trailing bar for next AddFile (we want to insert an entry for the directory // itself, not for the files inside the directory) fia.AddFile("C:\\PROGRAM FILES", fia::AP_SORTBYNAME | fia::SORTDESCENDING); TRACE("Dumping directory contents for C:\\WINDOWS\\, file C:\\AUTOEXEC.BAT and " " directory \"C:\\PROGRAM FILES\" \n"); for (int i=0;i<fia.GetSize();i++) TRACE(fia[i].GetFilePath+"\n");
CFileInfo
as a descendant of CFindFile
, but I
don't like its FindFile
, FindNextFile
and Close
methods
at all (I don't need them) and CFindFile
stores information as pointers, which I
also didn't like (see To pointer or not to pointer discussion below about wether to
use pointers to elements or elements themselves for CArray
's contents).
fread
does) so I wrote a few more lines in file reading loops in order to make
a little of buffered access.
nFileSizeHigh
& nFileSizeLow
scheme, I used the type ULONGLONG
, a MS-propietary unsigned long long
(64bit).
BTW, Visual C++ 6.0 doesn't support unsigned long long
type (although it
defines this ULONGLONG
for this purpose).
AddDir
is aborted, some CFileInfos
will be missing, but all
the CFileInfos
contained in the array are OK.
GetCRC
or GetChecksum
are aborted, CRC or checksum will not
be entirely calculated, and will return the corect value calculated up to
the abort moment.volatile
qualifiers in AddDir
definition.
It's because those parameters are to be set in multithreaded applications, where they are read
by AddDir
loop and are set by another thread, so they must not be cached on a
register.
InterlockedIncrement
and the like): just don't
rely too much in a temporary weird pulCount
value, but just for the
sake of rightness, I use InterlockedExchange
and InterlockedIncrement
to increment pulCount
in CFileInfoArray::AddDir
, in CFileInfo::GetCRC
and CFileInfo::GetChecksum
.
volatile
qualifier, main application doesn't need to modify
multithreaded-vars with thread-safe functions (InterlockedIncrement
...): the
only variable of this kind the application needs to modify is pbAbort
and due
to its boolean nature, it is not prone to errors because a non-atomic modification of it.
CFileInfo
structures. CArray
regrowing increment, it also reduces the number of calls to memory
allocation routines (at least is far from the one-allocation-call-per-element ratio that
would otherwise be necessary).lParam
of a listview item, as it happens in FCompare app)
and you add new elements to the array, those refferences aren't up to date anymore and you
have to update them somehow (in FCompare I do it by rebuilding the listview).
CArray
's element storing in front of pointer storing is the
fact that elements are automagically deallocated when the array is deallocated.
When storing pointers, a template function DeleteItems
ought to be written to
deallocate individual elements as they are removed from the array.Download source - 35 KB
FCompare or Binary File Compare is an application to binary compare a group
of files, selectable recursively from a given directory and filemask.
Binary comparison can be done by comparing files' size, CRC,
checksum or contents. When comparing by CRC, checksum and contents you can limit
the number of bytes the comparison will take into account.
WM_TIMER
message, isolating worker threads
from UI tasks as much as possible and avoiding to overload them (graphical information is
displayed and updated at a constant time rate, not at worker thread's looping rate).
CTabCtrl
.
*.htm;*.html;*.shtml;*.asp
to find all HTML-related files.
CPropertySheet
) embedded in a bigger dialog, is quite a
pain:
CProperty
stuff.
OnIdle
nor
WM_ENTER_IDLE
messages. Also, PumpMessage
doesn't work properly.
Due to that, the only way I've found to make a progress-report loop (even if work is being done in a
worker thread) is to set a timer.OnIdle
or a similar hack based on PeekMessage
/
PumpMessage
pair, but as I stated before, they don´t work for Dialogbox apps (or
at least I haven't been able to make them work).
fread
so it can't address 64bit-sized files.
<OL>
<LI>Let iSource and iTarget be indexes to source[] and target[] arrays.
<LI>iSource = 0, iTarget=0
<LI>while target[] and source[] have elements do
<LI>if target[iTarget] = source[iSource] there is a probable match, do further comparing
by checksum or whatever (if you inspect FCompares's source code, you'll see some tricky code here to
ensure every needed comparison is made). If further match is positive, add to match array.
<LI>if target[iTarget]>=source[iSource] then iSource++ else iTarget++
<LI>end while</LI>
</OL>
GetCRC
and
GetChecksum
as suggested (again!) by Róbert Szucs:4-(dwRead & 0x3)
instead of dwRead & 0x3
when calc'ing the padding mask. GetCRC
and GetChecksum
as
suggested by Róbert Szucs:buffer[dwRead
+3...]
it ought to access buffer[dwRead...]
(shame on
me! :'(). Create(CString, LPARAM)
as suggested by Nhycoh:CFileInfo::Create(strFilePath, lparam)
stating
strFilePath.GetLength()-nBarPos
instead of
nBarPos+1
(I'm quite sure I left my head on my pillow the day I
did that %-#).
GetCRC
& GetChecksum
to avoid some
bug cases SetTimer
fails. CFileInfoArray::AddDir
as suggested by Zhuang Yuyao: bIncludeDirs
wasn't used if bRecurse
was false. CDirDialog
, the directory browsing not-so-common-dialog-box wrapper initially by Girish Bharadwaj and Lars Klose and later enhanced by Vladimir Kvash. BTW, I slightly modified
DirDialog.h
in order MS VC++ not to complain about not using csStatusText
and lpcsSelection
in SelChanged
declaration.CHyperLinkEx
by Giancarlo Iovino,
where I also commented unused parameter nFlag
at CHyperLink::OnMouseMove
.
|
|