📄 vmcomparedirs.cpp
字号:
/*****************************************************************************/
/* SOURCE FILE */
/*****************************************************************************/
/*
$Archive: $
$Revision: $
$Date: $
$Author: $
Description: Implementation of the VMCompareDirs class. Class counts
number of bytes in two different DirTrees and can also
compare the two counts
TOOL And XML FORMS License
==========================
Except where otherwise noted, all of the documentation
and software included in the TOOL package is
copyrighted by Michael Swartzendruber.
Copyright (C) 2005 Michael John Swartzendruber.
All rights reserved.
Access to this code, whether intentional or accidental,
does NOT IMPLY any transfer of rights.
This software is provided "as-is," without any express
or implied warranty. In no event shall the author be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for
any purpose, including commercial applications, and to
alter and redistribute it, provided that the following
conditions are met:
1. All redistributions of source code files must retain
all copyright notices that are currently in place,
and this list of conditions without modification.
2. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
3. If you use this software in another product, an acknowledgment
in the product documentation would be appreciated but is
not required.
4. Modified versions in source or binary form must be plainly
marked as such, and must not be misrepresented as being
the original software.
*/
static char OBJECT_ID[] = "$Revision: $ : $Date: $";
/*****************************************************************************/
#include "../../../stdafx.h"
#include <stdio.h>
#include <direct.h>
#include <string.h>
#include "VMCompareDirs.h"
/*****************************************************************************/
/*
FUNCTION NAME: VMVMCompareDirs::GetDirSize
DESCRIPTION: returns one of the counts of bytes maintained in this
INPUT: eTarget - selects which of the two counts maintained in
this will be returned
OUTPUT: none
RETURNS: see INPUT above
*/
DWORD VMCompareDirs::GetDirSize( whichtarget eTarget )
{
switch ( eTarget )
{
case VMCompareDirs::target1:
return( dwSizeTargetOne );
break;
case VMCompareDirs::target2:
return( dwSizeTargetTwo );
break;
default:
return( 0L );
break;
}
}
/* End of function "VMCompareDirs::GetDirSize"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMCompareDirs::GetDirSizeAsFormattedString
DESCRIPTION: returns a formatted string representing the number of bytes
in one of the subdirs measured by this
INPUT: eTarget - selects which count will be formatted for the
caller
pchOut - the output buffer for this
OUTPUT: pchOut
RETURNS: void
*/
void VMCompareDirs::GetDirSizeAsFormattedString( whichtarget eTarget, char* pchOut )
{
switch ( eTarget )
{
case VMCompareDirs::target1:
FormatNumber( pchOut, dwSizeTargetOne );
break;
case VMCompareDirs::target2:
FormatNumber( pchOut, dwSizeTargetTwo );
break;
default:
strcpy( pchOut, "unknown" );
break;
}
}
/* End of function "VMCompareDirs::GetDirSizeAsFormattedString"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMCompareDirs::Compare
DESCRIPTION: given two subdirs, will see if the subdirs have the same
number of bytes
INPUT: pchTargetOne - subdir #1
pchTargetTwo - subdir #2
OUTPUT:
RETURNS: true if the subdirs have the same # of bytes, false
otherwise
*/
bool VMCompareDirs::Compare( char* pchTargetOne, char* pchTargetTwo )
{
if ( IsDirectory( pchTargetOne ) && IsDirectory( pchTargetTwo ) )
{
dwSizeTargetOne = GetDirectorySize( pchTargetOne );
dwSizeTargetTwo = GetDirectorySize( pchTargetTwo );
return( dwSizeTargetOne == dwSizeTargetTwo );
}
else
{
return( false );
}
}
/* End of function "VMCompareDirs::Compare"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMCompareDirs::GetDirectorySize
DESCRIPTION: returns the total number of bytes in the directory passed
to this
INPUT: pchDir - the directory to "count"
OUTPUT: none
RETURNS: the number of bytes
*/
DWORD VMCompareDirs::GetDirectorySize( char* pchDir )
{
DWORD dwSize = 0;
HANDLE hFile;
WIN32_FIND_DATA xFileData;
char pchNextDir[MAX_PATH]; // sub-directory path
// search for all files/types
//
strcat( pchDir, "\\*.*" );
hFile = ::FindFirstFile( pchDir, &xFileData );
if ( hFile == INVALID_HANDLE_VALUE )
{
::FindClose( hFile );
return( dwSize );
}
if ( '.' != xFileData.cFileName[0] )
{
// check if it is a sub-directory
//
if ( xFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
strcpy( pchNextDir, pchDir );
pchNextDir[strlen(pchDir) - 3] = '\0';
strcat( pchNextDir, xFileData.cFileName );
dwSize += GetDirectorySize( pchNextDir );
}
else
{
dwSize += xFileData.nFileSizeLow;
}
}
while ( NULL != FindNextFile( hFile, &xFileData ) )
{
if ( '.' != xFileData.cFileName[0] )
{
if ( xFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
strcpy( pchNextDir, pchDir );
pchNextDir[strlen(pchDir) - 3] = '\0';
strcat( pchNextDir, xFileData.cFileName );
dwSize += GetDirectorySize( pchNextDir );
}
else
{
dwSize += xFileData.nFileSizeLow;
}
}
}
return( dwSize );
}
/* End of function "VMCompareDirs::GetDirectorySize"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMCompareDirs::FormatNumber
DESCRIPTION: creates a comma-included numeric string from dwInput to
make it easier to read
INPUT: pchOut - the output buffer for this
dwInput - the value to format
OUTPUT:
RETURNS: void
*/
void VMCompareDirs::FormatNumber( char* pchOut, DWORD dwInput )
{
char chTemp[32];
int iLen;
int iNumCommas;
int iCurPos = 0;
// get unformatted string
//
sprintf( chTemp, "%1lu", dwInput );
iLen = strlen( chTemp );
iNumCommas = ( iLen - 1 ) / 3;
// terminate target string
//
pchOut[ iNumCommas + iLen ] = '\0';
while( iLen-- )
{
if ( 3 == iCurPos )
{
// 3 digits transferred, add a comma
//
pchOut[ iLen + iNumCommas] = ',';
iCurPos = 0; // reset digit counter
iNumCommas--; // one less extra char
}
// copy one digit
//
pchOut[ iLen+iNumCommas ] = chTemp[ iLen ];
iCurPos++;
}
}
/* End of function "VMCompareDirs::FormatNumber"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMCompareDirs::IsDirectory
DESCRIPTION: Check if the path passed to this is a directory
INPUT: pchDir - the "directory" to check
OUTPUT: none
RETURNS: true if the string is a valid directory. false if not
*/
bool VMCompareDirs::IsDirectory( char* pchDir )
{
DWORD dwRet = GetFileAttributes( pchDir );
return( ( dwRet != 0xFFFFFFFF ) && ( dwRet & FILE_ATTRIBUTE_DIRECTORY ) );
}
/* End of function "VMCompareDirs::IsDirectory"
/*****************************************************************************/
/*****************************************************************************/
/* Check-in history */
/*
*$Log: $
*/
/*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -