⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 path.cpp

📁 cab文件压缩、解压程序源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
                
    if(Name.LoadString(nNameResourceID))
        LocalProfile(Name,Extension);
}
#endif

#ifdef __MFC__
//-------------------------------------------------------------
// Pre     :
// Post    : 
// Globals :
// I/O     :
// Task    : Set path 2 file with same name as the current module
//           and .INI extension in the Windows directory
//-------------------------------------------------------------
void CPath::PrivateProfile()
{
    CPath WindowsDirectory(WINDOWS_DIRECTORY);
    
	Module();
	SetDriveDirectory(WindowsDirectory);
	SetExtension(INI_EXTENSION);
}
#endif

//-------------------------------------------------------------
// Pre     :
// Post    : 
// Globals :
// I/O     :
// Task    : Set path 2 file WIN.INI in the Windows directory
//-------------------------------------------------------------
void CPath::WindowsProfile()
{
    WindowsDirectory();
	SetNameExtension(_T("Win.INI"));
}

//-------------------------------------------------------------
// Pre     :
// Post    : 
// Globals :
// I/O     :
// Task    : Set path 2 file WIN.INI in the Windows directory
//-------------------------------------------------------------
void CPath::SystemProfile()
{
    WindowsDirectory();
	SetNameExtension(_T("System.INI"));
}

//-------------------------------------------------------------
// Pre     :
// Post    : 
// Globals :
// I/O     :
// Task    : Turn this path from "x:\directory\subdirectory\name.ext"
//           to just "x:\"
//-------------------------------------------------------------
void CPath::MakeRoot()
{   
	SetDirectory(_T(""));
	SetNameExtension(_T(""));
}

//-------------------------------------------------------------
// Pre     : Only the first 3 character from lpcszPrefix will be used
// Post    : Returns TRUE on success
// Globals :
// I/O     :
// Task    : Creates a temporary name
//-------------------------------------------------------------
BOOL CPath::CreateTempName(LPCTSTR lpcszPrefix)
{
    // Check that we've got a prefix
    if(!lpcszPrefix)
        return FALSE;

	string Dir;
    TCHAR  temp_file[MAX_PATH];

    GetDriveDirectory(Dir);

    if(::GetTempFileName(Dir.c_str(),lpcszPrefix,0,temp_file) != 0)
    {
        // Got a temp file name
        *this =temp_file;
        SetExtension(_T("tmp"));

        // GetTempFileName actually created the file, remove it now,
        // we only needed a name
        Delete(TRUE);

        return TRUE;
    }

	return FALSE;
}

//---------------------------------------------------------------------------
// Pre     : Only the first 3 character from lpcszPrefix will be used
// Post    : Returns TRUE on success
// Globals : 
// I/O     : 
// Task    : Creates a temporary folder name
//---------------------------------------------------------------------------
BOOL CPath::CreateTempDir(LPCTSTR lpcszPrefix, UINT nRetries)
{
    // Check that we've got a prefix
    if(!lpcszPrefix)
        return FALSE;

    UINT  retries =0;
    BOOL  bSuccess =FALSE;
	TCHAR temp_prefix[ 5];
    TCHAR temp_name  [15];

	ZeroMemory(temp_prefix, sizeof(temp_prefix));
	STRNCPY(temp_prefix,lpcszPrefix,4);
    temp_prefix[3] ='\0';

    while(!bSuccess && (retries < nRetries))
    {
        STRCPY(temp_name, temp_prefix);
        string temp =RandomDigits(5);
        STRCAT(temp_name,temp.c_str());
        STRCAT(temp_name,_T(".tmp"));

        CPath test(*this);

        test.AppendDirectory(temp_name);
        if(!test.DirectoryExists() && test.CreateDirectory())
        {
            // CreateTempDir actually created the folder, remove it now,
            // we only needed a name
            test.RemoveDirectory();
            bSuccess =TRUE;
        }

        retries++;
    }

    if(bSuccess)
        AppendDirectory(temp_name);

	return bSuccess;
}

//-------------------------------------------------------------
// Pre     :
// Post    : Returns TRUE on success
// Globals :
// I/O     :
// Task    : Sets path 2 a random name, and optionally ensures
//           uniqueness of that path
//-------------------------------------------------------------
BOOL CPath::CreateRandomName(BOOL bMustNotExist /*= TRUE*/, UINT nRetries /*= 1000*/)
{
	string Name;

	for(UINT nRetry=0; nRetry < nRetries; nRetry++)
	{
		Name =RandomDigits(8);
		SetName(Name.c_str());
        if(!bMustNotExist)
            return TRUE;
		if(!Exists())
			return TRUE;
	}

	return FALSE;
}

//-------------------------------------------------------------
// Pre     :
// Post    : Returns TRUE on success
// Globals :
// I/O     :
// Task    : Create a new name, based on the existing name, for the same
//           drive and directory. If bMustNotExist, test the path up to 
//           nRetries till we get an unused path
//           See also: CreateRandomName
//-------------------------------------------------------------
BOOL CPath::CreateSimilarName(BOOL bMustNotExist /*= TRUE*/, UINT nRetries /*= 1000*/)
{
	string NewName;
    string OriginalName;

	GetName(OriginalName);
		
	for(UINT nRetry=0; nRetry < nRetries; nRetry++)
	{
		NewName =OriginalName + RandomDigits(_MAX_FNAME - OriginalName.length());
		SetName(NewName.c_str());
		if(!Exists() || !bMustNotExist)
			return TRUE;
	}

	return FALSE;
}

//-------------------------------------------------------------
// Pre     :
// Post    : Returns one of the EX_DRIVE_ constants
// Globals :
// I/O     :
// Task    : Return the type of the drive this path points to
//           See DrvType.H for more details
//-------------------------------------------------------------
UINT CPath::GetDriveType() const
{
	CPath  RootPath = *this;
    string Root;

	RootPath.MakeRoot();
    Root =(LPCTSTR)RootPath;

	return GetDriveTypeEx(TOLOWER(Root[0]) - 'a');
}

//-------------------------------------------------------------
// Pre     :
// Post    : Return -1 on error
// Globals :
// I/O     :
// Task    : Find out the amount of free space on drive (in bytes)
//-------------------------------------------------------------
DWORD CPath::DriveFreeSpaceBytes() const
{
    CPath  RootPath = *this;
    string Root;

	RootPath.MakeRoot();

	DWORD nSectorsPerCluster;
	DWORD nBytesPerSector;
	DWORD nFreeClusters;
	DWORD nClusters;

	if(!GetDiskFreeSpace((LPCTSTR)RootPath,&nSectorsPerCluster,&nBytesPerSector,&nFreeClusters,&nClusters))
		return 0;
	else		
		return nFreeClusters * nSectorsPerCluster * nBytesPerSector;
}

//-------------------------------------------------------------
// Pre     :
// Post    : Return -1 on error
// Globals :
// I/O     :
// Task    : Find out the size of the drive (in bytes)
//-------------------------------------------------------------
DWORD CPath::DriveTotalSpaceBytes() const
{
    CPath  RootPath = *this;
    string Root;

	RootPath.MakeRoot();

	DWORD nSectorsPerCluster;
	DWORD nBytesPerSector;
	DWORD nFreeClusters;
	DWORD nClusters;

	if(!GetDiskFreeSpace((LPCTSTR)RootPath,&nSectorsPerCluster,&nBytesPerSector,&nFreeClusters,&nClusters))
		return 0;
	else		
		return nClusters * nSectorsPerCluster * nBytesPerSector;
}

//-------------------------------------------------------------
// Pre     :
// Post    : Return -1 on error
// Globals :
// I/O     :
// Task    : Find out the cluster size on this drive (in bytes)
//-------------------------------------------------------------
DWORD CPath::GetDriveClusterSize() const
{
    CPath  RootPath = *this;
    string Root;

	RootPath.MakeRoot();

	DWORD nSectorsPerCluster;
	DWORD nBytesPerSector;
	DWORD nFreeClusters;
	DWORD nClusters;

	if(!GetDiskFreeSpace((LPCTSTR)RootPath,&nSectorsPerCluster,&nBytesPerSector,&nFreeClusters,&nClusters))
		return 0;
	else		
		return nSectorsPerCluster * nBytesPerSector;
}

//-------------------------------------------------------------
// Pre     :
// Post    : Return TRUE on success
// Globals :
// I/O     :
// Task    : Find out info about drive
//-------------------------------------------------------------
BOOL CPath::GetDiskInfo(LPDWORD lpSectorsPerCluster,
						LPDWORD lpBytesPerSector,
						LPDWORD lpFreeClusters,
						LPDWORD lpClusters) const
{
    // Create root path
	CPath RootPath = *this;
	RootPath.MakeRoot();

	return GetDiskFreeSpace((LPCTSTR)RootPath,
                            lpSectorsPerCluster,
                            lpBytesPerSector,
                            lpFreeClusters,
                            lpClusters);
}

//---------------------------------------------------------------------------
// Pre     : 
// Post    : Return TRUE if a directory
// Globals : 
// I/O     : 
// Task    : Check if this path represents a directory
//---------------------------------------------------------------------------
BOOL CPath::IsDirectory() const
{
    // Check if this path has a filename
    string file_name;
    GetNameExtension(file_name);
    
    return file_name.empty();
}

//-------------------------------------------------------------
// Pre     :
// Post    : Return TRUE if directory exists
// Globals :
// I/O     :
// Task    : To determine if the directory exists, we need to
//           create a test path with a wildcard (*.*) extension
//           and see if FindFirstFile returns anything.  We don't
//           use CPath::FindFirst() because that routine parses out
//           '.' and '..', which fails for empty directories
//-------------------------------------------------------------
BOOL CPath::DirectoryExists() const
{
    // Create test path	
	CPath TestPath(m_strPath.c_str());
	TestPath.SetNameExtension(WILD_NAME_EXTENSION);

	WIN32_FIND_DATA	FindData;
	HANDLE          hFindFile =FindFirstFile((LPCTSTR)TestPath,&FindData); // Find anything
	BOOL            bGotFile  =(hFindFile != INVALID_HANDLE_VALUE);

	if(hFindFile != NULL)	// Make sure we close the search
	    FindClose(hFindFile);

	return bGotFile;
}                                                     

//-------------------------------------------------------------
// Pre     :
// Post    : Return TRUE if there are no files(s)/folder(s) in
//           directory. All objects (with hidden, system, etc. attributes)
//           are looked up
// Globals :
// I/O     :
// Task    : Check if directory contains any file(s)
//-------------------------------------------------------------	
BOOL CPath::IsDirectoryEmpty() const
{
	CPath FileSpec = *this;
	
	FileSpec.SetNameExtension(WILD_NAME_EXTENSION);
	return !FileSpec.FindFirst(_A_NORMAL | _A_ARCH | _A_HIDDEN | _A_SYSTEM | _A_RDONLY) &&
           !FileSpec.FindFirst(_A_HIDDEN | _A_SUBDIR);
}

//-------------------------------------------------------------
// Pre     :
// Post    : Return TRUE if these is such a file
// Globals :
// I/O     :
// Task    : Check if file exists
//-------------------------------------------------------------
BOOL CPath::Exists() const
{
	WIN32_FIND_DATA FindData;
	HANDLE          hFindFile =FindFirstFile(m_strPath.c_str(),&FindData);
	BOOL            bSuccess  =(hFindFile != INVALID_HANDLE_VALUE);

	if(hFindFile != NULL)	// Make sure we close the search
	    FindClose(hFindFile);

	return bSuccess;
}

//-------------------------------------------------------------
// Pre     :
// Post    : Return file size, -1 on error
// Globals :
// I/O     :
// Task    : Get file size (in bytes)
//-------------------------------------------------------------
DWORD CPath::GetSize() const
{
    WIN32_FIND_DATA FindData;
	HANDLE          hFindFile =FindFirstFile(m_strPath.c_str(),&FindData);
	BOOL            bSuccess  =(hFindFile != INVALID_HANDLE_VALUE);

	if(hFindFile != NULL)	// Make sure we close the search
	    FindClose(hFindFile);

    return bSuccess ? FindData.nFileSizeLow : (DWORD)-1;
}

//-------------------------------------------------------------
// Pre     :
// Post    : Return file attributes
// Globals :
// I/O     :
// Task    : Get attributes of the file
//---------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -