filesys.cpp

来自「A Model-View-Controller Framework that i」· C++ 代码 · 共 2,090 行 · 第 1/5 页

CPP
2,090 行
字号


		//***************************************************************************
		//
		// Name:    MakePath
		//
		// Purpose: To make all the directories in a given path.  If any of the
		//          directories exist, the creation continues with lower level
		//          directories.
		//
		// Ret Val: TRUE : No error.
		//          FALSE : Unable to create path.
		//                  Path already exists.
		//                  No path given.
		//
		// Example: BOOL bRetVal = fs.MakePath("c:\\foo\\bar\\what");
		//
		// Notes:    None
		//
		//***************************************************************************
		//@doc FileSys
		//@mfunc Makes a subdirectory (including all intervening dirs necessary).
		//@rdesc Nonzero if successful; otherwise 0 (Unable to create path. Error 
		// occurred, path already exists, or no path given).
		//@parm const CString& | NewDirectory | Name of path to create.
		//@comm To make all the directories in a given path.  If any of the 
		// directories exist, the creation continues with lower level directories.
		//@xref <c FileSys> <mf FileSys::MakeDirectory>
		// <mf FileSys::DeleteDirectory>
		//@ex | BOOL bRetVal = fs.MakePath("c:\\foo\\bar\\what");
		BOOL FileSys::MakePath(const CString& NewDirectory)
		{
		  CString NewDir = NewDirectory;  // Copy string for manipulation
		  CString  DirName;
		  BOOL    bRetVal = TRUE;

		  // Error if no directory specified.
		  if (NewDir.GetLength() == 0)
		  {
			return FALSE;
		  } // if

		  // Make sure the directory name ends in a slash
		  if (NewDir[NewDir.GetLength() - 1] != '\\' && NewDir[NewDir.GetLength() - 1] != '/')
		  {
			NewDir = NewDir + '\\';
		  } // if

		  // Create each directory in the path
		  int	nIndex = 0;
		  int	nFSIndex = 0;
		  BOOL  bDone = FALSE;
		  while (!bDone)
		  {
			// Extract one directory
			nIndex = NewDir.Find(_T('\\'));
			nFSIndex = NewDir.Find(_T('/'));
			if((nFSIndex >= 0) && (nFSIndex < nIndex))	nIndex = nFSIndex;
			if (nIndex != -1)
			{
			  DirName = DirName + NewDir.Left(nIndex);
			  NewDir = NewDir.Right(NewDir.GetLength() - nIndex - 1);

			  // The first time through, we might have a drive name
			  if (DirName.GetLength() >= 1  &&  DirName[DirName.GetLength() - 1] != ':')
			  {
				bRetVal = MakeDirectory(DirName);
			  } // if
			  DirName = DirName + '\\';
			} // if
			else
			{
			  // We're finished
			  bDone = TRUE;
			} // else
		  } // while

		  // Return the last MakeDirectory() return value.
		  return bRetVal;
		} // MakePath


		//***************************************************************************
		//
		// Name:    DeleteDirectory
		//
		// Purpose: To delete a directory.  Optionally, all lower level files
		//          and directories can be deleted.
		//
		// Ret Val: TRUE : Successfully deleted directory.
		//          FALSE : Failed to delete directory.
		//
		// Example: BOOL bRetVal = fs.DeleteDirectory("c:\\foo\\bar", TRUE);
		//
		// Notes:   If bDeleteFilesAndDirs == FALSE, directory must be empty or
		//          an error will occur and FALSE will be returned.
		//
		//***************************************************************************
		//@doc FileSys
		//@mfunc Deletes a directory and all its files and subdirectories.
		//@rdesc Nonzero if successful; 0 if an error occurred.
		//@parm const CString& | Directory | Name of the directory to delete.
		//@parmopt  const BOOL | bDeleteFilesAndDirs | FALSE | Whether to delete all 
		//the files and directories in the specified directory before the specified 
		// directory is deleted.
		//@comm To delete a directory.  Optionally, all lower level files and 
		// directories can be deleted.
		//
		//If bDeleteFilesAndDirs is FALSE, directory must be empty or an error 
		// will occur and FALSE will be returned.
		//@xref <c FileSys> <mf FileSys::MakeDirectory>
		// <mf FileSys::MakePath> <mf FileSys::DeleteFiles>
		//@end
		BOOL FileSys::DeleteDirectory(const CString& Directory, const BOOL bDeleteFilesAndDirs /* = FALSE */)
		{
		  if (bDeleteFilesAndDirs)
		  {
			CStringList *pSubDirs = GetSubdirList(Directory);
			for (POSITION pos = pSubDirs->GetHeadPosition(); pos != NULL; )
			{
			  CString DirName = pSubDirs->GetNext(pos);
			  DeleteDirectory(DirName, bDeleteFilesAndDirs);
			} // for

			delete pSubDirs ;
			pSubDirs = NULL;

			DeleteFiles(AppendWildcard(Directory, "*.*"), (const unsigned long)allfiles);
		  } // if

		#ifdef WIN32

		  BOOL bRetVal = RemoveDirectory((const TCHAR *) Directory);
		  return bRetVal;

		#else

			int nRetVal = _trmdir((const char *) Directory);
			if (nRetVal == -1)
			{
				return FALSE;
			} // if

			return TRUE;
		#endif
		} // DeleteDirectory


		//***************************************************************************
		//
		// Name:    GetDirectorySize
		//
		// Purpose: To return the size (in bytes) of the files in the specified
		//          directory.
		//
		// Example: LONG lSize = fs.GetDirectorySize("c:\\foo\\bar", "*.*", TRUE);
		//
		// Notes:   If an error occurs in reading the status of a file, that
		//          file is skipped and not counted as part of the size.
		//          There is currently not way to find out if an error of this
		//          kind occured.
		//
		//***************************************************************************
		//@doc FileSys
		//@mfunc Returns the number of bytes in all the files in a directory.
		//@rdesc The sum of all the sizes of the files in the current directory.
		//@parmopt const CString& | Directory | "" | Name of directroy to get the 
		// size of.  If not specified, the current directory is assumed.
		//@parmopt const CString& | WildCard | "*.*" | Wildcard to use when 
		// looking for files.
		//@parmopt const BOOL | bRecurseSubdirs | FALSE | Whether to traverse 
		// subdirectories.
		//@comm To return the size (in bytes) of the files in the specified directory.
		//
		//If an error occurs in reading the status of a file, that file is skipped and 
		// not counted as part of the size.  There is currently no way to find out if 
		// an error of this kind occurred.
		//@xref <c FileSys> <mf FileSys::GetFileSize>
		//@ex | LONG lSize = fs.GetDirectorySize("c:\\foo\\bar", "*.*", TRUE);
		//@end
		LONG FileSys::GetDirectorySize(const CString& Directory /* = "" */, const CString& WildCard /* = "*.*" */, const BOOL bRecurseSubdirs /* = FALSE */)
		{
		  LONG lSize = 0L;

		  // Do all the subdirectories first...
		  if (bRecurseSubdirs)
		  {
			CStringList *pSubDirs = GetSubdirList(Directory);
			for (POSITION pos = pSubDirs->GetHeadPosition(); pos != NULL; )
			{
			  CString DirName = pSubDirs->GetNext(pos);
			  lSize += GetDirectorySize(DirName, WildCard, bRecurseSubdirs);
			} // for

			delete pSubDirs;
			pSubDirs = NULL;
		  } // if

		  // Find the sizes of all the files in the specified directory.
		  CStringList *pFileList = GetFileList(AppendWildcard(Directory, WildCard), 
  											  (const unsigned long)allfiles);
		  for (POSITION pos = pFileList->GetHeadPosition(); pos != NULL; )
		  {
			CFileStatus status;
			CString FileName = pFileList->GetNext(pos);
			if (CFile::GetStatus(FileName, status))
			{
			  lSize += status.m_size;
			} // if
		  } // for

		  delete pFileList;
		  pFileList = NULL;
		  
		  return lSize;
		} // GetDirectorySize


		//***************************************************************************
		//
		// Name:    GetCurrentFileSystem
		//
		// Purpose: To return a string containing the current file system name.
		//
		// Example: CString FileSystem = fs.GetCurrentFileSystem();
		//
		// Notes:   None
		//
		//***************************************************************************
		//@doc FileSys
		//@mfunc Returns the current filesystem.
		//@rdesc The name of the current filesystem.
		//@comm To return a string containing the current filesystem name.
		//@xref <c FileSys> <mf FileSys::GetCurrentDirectory>
		// <mf FileSys::ChangeFileSystem>
		//@ex | CString FileSystem = fs.GetCurrentFileSystem();
		CString FileSys::GetCurrentFileSystem()
		{
		#ifdef UNDER_CE
		  return CString(_T(""));
		#else
		  unsigned int nDrive = 0;
		  char cDrive = 'a';

		#ifdef WIN32
		  nDrive = _getdrive();
		#else
			_dos_getdrive(&nDrive);
		#endif //Win32
		  cDrive = (char) ('a' + nDrive - 1);
		  CString String = cDrive;
		  String += ":\\";

		  return String;
		#endif //UNDER_CE (WindowsCE)
		} // GetCurrentFileSystem


		//***************************************************************************
		//
		// Name:    ChangeFileSystem
		//
		// Purpose: To change the current file system.
		//
		// Example: BOOL bRetVal = fs.ChangeFileSystem('c');
		//
		// Notes:   Obsolete.  The version that takes a CString parameter
		//          should be used instead.  This change was made in preparation
		//          for the addition of Windows NT support.
		//
		//***************************************************************************
		//@doc FileSys
		//@mfunc Changes the current filesystem.
		//@syntax ChangeFileSystem(const char cFileSystem);
		//@syntax ChangeFileSystem(const CString& FileSystem)
		//@rdesc Nonzero if successful; 0 if an error occurred.
		//@parm const char | cFileSystem | The file system drive letter.
		//@parm const CString& | FileSystem | The file system name.
		//@devnote The version that takes a char parameter is obsolete.  The version 
		// that takes a CString parameter should be used instead.  This change was 
		// made for Windows NT support.
		//@xref <c FileSys> <mf FileSys::GetCurrentFileSystem>
		//@ex | BOOL bRetVal = fs.ChangeFileSystem("c:\\");
		BOOL FileSys::ChangeFileSystem(const char cFileSystem)
		{
		#ifdef UNDER_CE
			return FALSE;
		#else
		  unsigned int nNewDrive;
		  BOOL bRetVal = FALSE;

		  // Turn off critical error handler.
		  ::SetErrorMode(SEM_FAILCRITICALERRORS);

		  nNewDrive = _totupper(cFileSystem) - 'A' + 1;

		  if (nNewDrive >= 0  &&  nNewDrive <= 26)
		  {
		#ifdef WIN32

			if (_chdrive(nNewDrive) == 0)
			{
			  bRetVal = TRUE;
			} // if

		#else
		    
				// Attempt change filesystems.
			unsigned int nNumDrives;		// ignored return value
				_dos_setdrive(nNewDrive, &nNumDrives);

				// Make sure the change actually happened (it's the only way).
				unsigned int nCurrentDrive;
				_dos_getdrive(&nCurrentDrive);
				if (nCurrentDrive == nNewDrive)
				{
					bRetVal = TRUE;
				} // if

		#endif //Win32
		  } // if

		  // Turn on critical error handler.
		  ::SetErrorMode(0);

		  return bRetVal;
		#endif //UNDER_CE (WindowsCE)
		} // ChangeFileSystem


		//***************************************************************************
		//
		// Name:    ChangeFileSystem
		//
		// Purpose: To change the current file system.
		//
		// Example: BOOL bRetVal = fs.ChangeFileSystem("c:\\");
		//
		// Notes:   Expects a filesystem name of the form "X:\".
		//
		//***************************************************************************
		BOOL FileSys::ChangeFileSystem(const CString& FileSystem)
		{
		#ifdef UNDER_CE
			return FALSE;
		#else
		#ifdef WIN32

		  return SetCurrentDirectory((const TCHAR *) FileSystem);

		#else

			BOOL bRetVal = FALSE;

			if (FileSystem.GetLength() > 0)
			{
				char cFileSystem = FileSystem[0];
				bRetVal = ChangeFileSystem(cFileSystem);
			} // if

			return bRetVal;

		#endif //Win32
		#endif //UNDER_CE (WindowsCE)
		} // ChangeFileSystem


		//***************************************************************************
		//
		// Name:    GetFileSystemList
		//
		// Purpose: To return a list of available file systems (ie. drives).
		//
		// Example: CStringList *pFSList = fs.GetFileSystemList();
		//
		// Notes:   The user must free the returned list.
		//
		//***************************************************************************
		//@doc FileSys
		//@mfunc Returns a list of available filesystems.
		//@rdesc A list of filesystem names.  Items from this list can be directly 
		// passed to <mf FileSys::ChangeFileSystem>.
		//@comm To return a list of available filesystems (i.e., drives).
		//@devnote The user will have to delete the CStringList returned.
		//@xref <c FileSys> <mf FileSys::ChangeFileSystem>
		//@ex | CStringList *pFSList = fs.GetFileSystemList();
		CStringList  * FileSys::GetFileSystemList()
		{
		#ifdef UNDER_CE
			return NULL;
		#else
		  CStringList *pStringList = new CStringList();

		  // Turn off critical error handler.
		  ::SetErrorMode(SEM_FAILCRITICALERRORS);

		  for (int nDriveNum = 0; nDriveNum < 26; nDriveNum++)
		  {

		#ifdef WIN32
		  
			CString DriveName = (char)('a' + nDriveNum);
			DriveName = DriveName + ":\\";
			if (GetDriveType((const TCHAR *)DriveName) > 1)
			{
			  CString DriveName = (char)('a' + nDriveNum);
			  DriveName = DriveName + ":\\";
			  pStringList->AddTail((const TCHAR *)DriveName);
			} // if

		#else

				if (GetDriveType(nDriveNum) != 0)
				{
					CString DriveName = (char)('a' + nDriveNum);
					DriveName = DriveName + ":\\";
					pStringList->AddTail((const char *)DriveName);
				} // if

⌨️ 快捷键说明

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