filesys.cpp
来自「A Model-View-Controller Framework that i」· C++ 代码 · 共 2,090 行 · 第 1/5 页
CPP
2,090 行
//@rdesc The number of free bytes on the specified filesystem. For use with
//filesystems up to 9 ExaBytes (9E18 Bytes) Note: This API not available under VC++
//version 5 or lower.
//@parm const CString& | FileSystem | The name of the filesystem.
//@comm To get the number of free bytes on a filesystem.
//@xref <c FileSys> <mf FileSys::GetTotalSpaceEx>
//@ex | LONG lSize = fs.GetFreeSpaceEx("c:\\");
#ifdef UNDER_CE
ULONG FileSys::GetFreeSpaceEx(const CString& sPath)
{
CString FileSystem = GetFileSystem(sPath);
LONG liFBAC,liTNB,liTNBFree;
GetDiskFreeSpaceEx(FileSystem,(PULARGE_INTEGER)&liFBAC,
(PULARGE_INTEGER)&liTNB,
(PULARGE_INTEGER)&liTNBFree);
return liFBAC;
}
#else
LONG64 FileSys::GetFreeSpaceEx(const CString& sPath)
{
CString FileSystem = GetFileSystem(sPath);
#ifdef WIN32
::SetErrorMode(SEM_FAILCRITICALERRORS);
__int64 FreeSpace = 0;
bool Result = false;
// GetFreeSpaceEx() was introduced for Win95 OSR2, check if it is available
typedef BOOL (CALLBACK *LPGETDISKFREESPACEEX)( LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER );
HINSTANCE hInstance = LoadLibrary( _T( "Kernel32.dll" ) );
LPGETDISKFREESPACEEX lpGetDiskFreeSpaceEx;
#if defined (UNICODE)
lpGetDiskFreeSpaceEx =
(LPGETDISKFREESPACEEX)GetProcAddress( (HMODULE)hInstance, "GetDiskFreeSpaceExW" );
#else
lpGetDiskFreeSpaceEx =
(LPGETDISKFREESPACEEX)GetProcAddress( (HMODULE)hInstance, "GetDiskFreeSpaceExA" );
#endif
if ( lpGetDiskFreeSpaceEx != NULL )
{
ULARGE_INTEGER FBATC, TNOB, TNOFB;
if ( lpGetDiskFreeSpaceEx( FileSystem, &FBATC, &TNOB, &TNOFB ) )
{
FreeSpace = (__int64)FBATC.QuadPart;
Result = true;
}
}
FreeLibrary( (HMODULE)hInstance );
if ( !Result )
{ // GetDiskFreeSpaceEx() is not available.
// Use GetDiskFreeSpace(), which may not work correctly for drives > 2GB
DWORD SPC, BPC, NOFC, TNOC;
if ( GetDiskFreeSpace( FileSystem, &SPC, &BPC, &NOFC, &TNOC ) )
{
FreeSpace = (__int64)SPC * (__int64)BPC * (__int64)NOFC;
Result = true;
}
}
::SetErrorMode(0);
ASSERT( (BOOL)Result ); // something should have worked
return( FreeSpace );
#else
// not Win32. Use _dos_getdiskfree()
unsigned long lFreeSpace = -1;
struct _diskfree_t diskspace;
if (FileSystem.GetLength() > 0)
{
char cFileSystem = (char) _totupper(FileSystem[0]);
int nDriveNum = _totupper(cFileSystem) - 'A';
if (nDriveNum >= 0 && nDriveNum <= 26)
{
::SetErrorMode(1);
if (_dos_getdiskfree( nDriveNum + 1, &diskspace ) == 0)
{
lFreeSpace =(unsigned long) diskspace.avail_clusters *
(unsigned long) diskspace.sectors_per_cluster *
(unsigned long) diskspace.bytes_per_sector;
} // if
::SetErrorMode(0);
} // if
} // if
return lFreeSpace;
#endif // else -- not Win32
} // GetFreeSpaceEx
#endif // else -- not UNDER_CE
#endif // _MSC_VER >= 1200
//***************************************************************************
//
// Name: IsReadOnlyFileSystem
//
//***************************************************************************
//@doc FileSys
//@mfunc Determines if the specified filesystem is writeable.
//@rdesc Nonzero if the filesystem is read-only or an error occurred; 0 if
// the filesystem is not read-only.
//@parm const CString& | FileSystem | The name of the filesystem to interrogate.
//@comm To determine if a filesystem is read-only, an attempt to open a
// new file is made. If the file creation is successful, the file is deleted.
//@ex | BOOL bRetVal = IsReadOnlyFileSystem("c:\\");
//@xref <c FileSys>
BOOL FileSys::IsReadOnlyFileSystem(const CString& FileSystem)
{
BOOL bRetVal = TRUE;
#if defined(WIN32) && !defined(UNDER_CE)
// Turn off critical error handler.
::SetErrorMode(SEM_FAILCRITICALERRORS);
TCHAR lpTempFileName[MAX_PATH];
// Try to create a temp file using GetTempFileName
// Strings must ANSI for Win95/98
UINT uErr = ::GetTempFileName(FileSystem, _T("SEC"), 0, lpTempFileName);
if(uErr)
{
if( DeleteFile(lpTempFileName) ) bRetVal = FALSE;
}
// Turn on DOS critical error handler.
::SetErrorMode(0);
#else //UNDER_CE (WindowsCE)
CFile TestFile;
CFileException fileException;
CString TempFileName;
// Get the temp file name
TempFileName = "xxxxtest.999";
// Try to create the temporary file.
if (TestFile.Open(AppendWildcard(FileSystem, TempFileName), CFile::modeCreate, &fileException))
{
TestFile.Close();
if (DeleteFile(AppendWildcard(FileSystem, TempFileName)))
{
bRetVal = FALSE;
} // else
} // if
#endif //UNDER_CE (WindowsCE)
return bRetVal;
} // IsReadOnlyFileSystem
//***************************************************************************
//
// Name: RenameFile
//
// Purpose: To rename a file.
//
// Ret Val: TRUE : No error, or OldFileName == NewFileName.
// FALSE : Error.
//
// Example: BOOL bRetVal = fs.RenameFile("c:\\foo.txt", "c:\\bar.doc");
// BOOL bRetVal = fs.RenameFile("c:\\foo.txt", "c:\\what\\bar.doc");
//
// Notes: A file can also be moved using this function by specifying a
// different directory in the NewFileName.
//
//***************************************************************************
//@doc FileSys
//@mfunc Renames a file.
//@rdesc Nonzero if successful; 0 if an error occurred.
//@parm const CString& | OldFileName | The name of the file to rename.
//@parm const CString& | NewFileName | The new name of the file.
//@comm To rename a file. A file can also be moved from one directory to
// another in the same filesystem by specifying a new directory (in addition
// to the filename) in the second parameter.
//
//It is not an error to rename a file to the same name.
//@xref <c FileSys> <mf FileSys::RenameDirectory>
//@ex | BOOL bRetVal = fs.RenameFile("c:\\foo.txt", "c:\\bar.doc");
//ASSERT(fs.RenameFile("c:\\foo.txt", "c:\\foo.txt") == TRUE);
//ASSERT(fs.RenameFile("c:\\foo.txt", "c:\\bar\\foo.txt") == TRUE);
BOOL FileSys::RenameFile(const CString& OldFileName, const CString& NewFileName)
{
if (OldFileName == NewFileName)
{
return TRUE;
} // if
if (!FileExists(OldFileName) || FileExists(NewFileName))
{
return FALSE;
} // if
TRY
{
CFile::Rename((const TCHAR *) OldFileName, (const TCHAR *) NewFileName);
} // TRY
CATCH(CFileException, Exception)
{
return FALSE;
} // CATCH
END_CATCH
return TRUE;
} // RenameFile
//***************************************************************************
//
// Name: DeleteFiles
//
// Purpose: To delete a set of files based on a wildcard file specification.
//
// Example: BOOL bRetVal = fs.DeleteFiles("c:\\foo\\bar\\*.txt");
//
//***************************************************************************
//@doc FileSys
//@mfunc Deletes specified files (i.e., *.txt).
//@rdesc Nonzero if successful; 0 if an error occurred.
//@parm const CString& | FileSpec | The file specifier.
//@parmopt const unsigned long | eFileAttrib | normal | The file attributes
// filter (See <md FileSys::Attribute>.
//@comm To delete a set of files based on a wildcard file specification.
//@xref <c FileSys> <mf FileSys::DeleteFile> <md FileSys::Attribute>
//@ex | BOOL bRetVal = fs.DeleteFiles("c:\\foo\\bar\\*.txt");
//@end
BOOL FileSys::DeleteFiles(const CString& FileSpec, const unsigned long eFileAttrib /* = normal */)
{
BOOL bRetVal = TRUE;
CStringList *pDir = GetDirectory(FileSpec, eFileAttrib);
for (POSITION pos = pDir->GetHeadPosition(); pos != NULL; )
{
CString FileName = pDir->GetNext(pos);
if (DeleteFile(FileName) == FALSE)
{
bRetVal = FALSE;
} // if
} // for
// Clean up.
delete pDir;
pDir = NULL;
return bRetVal;
} // DeleteFiles
//***************************************************************************
//
// Name: DeleteFile
//
// Purpose: To delete a file.
//
// Example:
// BOOL bRetVal = fs.DeleteFile("c:\foo.txt");
//
// Notes: None
//
//***************************************************************************
//@doc FileSys
//@mfunc Deletes a file.
//@rdesc Nonzero if successful; 0 if an error occurred.
//@parm const CString& | FileName | Name of the file to delete.
//@xref <c FileSys> <mf FileSys::DeleteFiles>
//@ex | BOOL bRetVal = fs.DeleteFile("c:\\foo.txt");
BOOL FileSys::DeleteFile(const CString& FileName)
{
TRY
{
CFile::Remove((const TCHAR *) FileName);
} // TRY
CATCH(CFileException, Exception)
{
return FALSE;
} // CATCH
END_CATCH
return TRUE;
} // DeleteFile
//***************************************************************************
//
// Name: CloseFile
//
// Purpose: To close a file.
//
// Ret Val: TRUE : File was closed successfully.
// FALSE : Error occured.
//
// Notes: None
//
//***************************************************************************
BOOL FileSys::CloseFile(CFile *pFile) const
{
BOOL bRetVal = TRUE;
TRY
{
pFile->Close();
} // TRY
CATCH(CFileException, e)
{
bRetVal = FALSE;
} // CATCH
END_CATCH
return bRetVal;
} // CloseFile
//***************************************************************************
//
// Name: CopyFile
//
// Purpose: To copy a file.
//
// Ret Val: TRUE : File was copied successfully.
// FALSE : Error occured.
//
// Example: BOOL bRetVal = fs.CopyFile("foo.txt", "a:\\bar.txt", 20480);
//
// Notes: If the destination file exists, an error occurs.
//
//***************************************************************************
//@doc FileSys
//@mfunc Copies the contents of one file to another.
//@rdesc Nonzero if successful; 0 if an error occurred.
//@devnote If the destination file exists, an error occurs.
//@parm const CString& | SourceFileName | Name of the file to copy.
//@parm const CString& | DestFileName | Name of the new file.
//@parmopt const unsigned long | lBuffSize | 10240 | Size of buffer to use
// when copying file data.
//@xref <c FileSys> <mf FileSys::CopyFiles>
//@ex | BOOL bRetVal = fs.CopyFile("foo.txt", "a:\\bar.txt", 20480);
//@end
BOOL FileSys::CopyFile(const CString& SourceFileName, const CString& DestFileName, const unsigned long lBuffSize /* = 10240 */)
{
BOOL bRetVal = TRUE;
char * pBuff = new char[lBuffSize];
CFile Source;
CFile Dest;
// Check for exitance of the destination file.
CFileStatus destStatus;
if ((CFile::GetStatus(DestFileName, destStatus)))
{
delete [] pBuff;
pBuff = NULL;
return FALSE;
} // if
// Open the files, creating the destination.
if (Source.Open((const TCHAR *) SourceFileName, CFile::modeRead))
{
if (Dest.Open((const TCHAR *) DestFileName, CFile::modeCreate | CFile::modeWrite))
{
DWORD dwLength = Source.GetLength();
// Copy the data in the file.
while (dwLength > 0)
{
UINT nRead = Source.Read(pBuff, (UINT) lBuffSize);
if (nRead)
{
Dest.Write(pBuff, nRead);
} // if
dwLength -= nRead;
} // while
CloseFile(&Source);
CloseFile(&Dest);
} // if
else
{
CloseFile(&Source);
bRetVal = FALSE;
} // else
} // if
else
{
bRetVal = FALSE;
} // else
delete [] pBuff;
pBuff = NULL;
return bRetVal;
} // CopyFile
//***************************************************************************
//
// Name: CopyFiles
//
// Purpose: To copy a set of files based on a wildcard file specification.
//
// Example: BOOL bRetVal = fs.CopyFiles("c:\\foo\\bar\\*.txt", "c:\\foo2");
//
// Notes: If an error occurs, the copy is aborted. There is no way to
// which if any of the files were copied.
//
// Even if you are ignoring errors, the return value indicates
// whether an error occured.
//
//***************************************************************************
//@doc FileSys
//@mfunc Copies more than one file to another subdirectory.
//@rdesc Nonzero if successful; 0 if an error occurred.
//@parm const CString& | FileSpec | Name of the files to copy (with wildcards).
//@parm const CString& | DestPath | Name of the directory to copy the files to.
//@parmopt const BOOL | bIgnoreErrors | FALSE | Ignore errors when copying files.
//@parmopt const unsigned long | eFileAttrib | normal | The file attributes
// filter of files to copy (See <md FileSys::Attribute>).
//@comm To copy a set of fi
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?