filesys.cpp

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

CPP
2,090
字号
		//@parm const CString& | FileName | The name of the file.
		//@parm  BYTE& | nAttribute | If successful, the attribute byte specifying 
		// file info. (See <md FileSys::Attribute>).
		//@comm To return the attribute byte for the specified file.  See the 
		// <md FileSys::Attribute> enum in filesys.h for specific attributes.
		//@xref <c FileSys> <mf FileSys::GetFileStatus>
		// <mf FileSys::GetFileModifyTime> <mf FileSys::GetFileAccessTime>
		// <mf FileSys::GetFileSize> <md FileSys::Attribute>
		//@ex |
		//BOOL bRetVal = fs.GetFileAttribute("c:\\test.txt", bAttr);
		BOOL FileSys::GetFileAttribute(const CString& FileName, BYTE& nAttribute)
		{
		  CFileStatus FileStatus;
		  if (CFile::GetStatus(FileName, FileStatus))
		  {
			nAttribute = FileStatus.m_attribute;
			return TRUE;
		  } // if

		  return FALSE;
		} // GetFileAttribute


		#ifdef WIN32
		//***************************************************************************
		//
		// Name:    GetADirectoryEntry
		//
		// Purpose: Function used in Win32 to get directory entries with the
		//          correct attributes set.
		//
		// Ret Val: TRUE: If no error.
		//          FALSE: Error occurred.
		//
		// Example:
		//
		// Notes:   None
		//
		//***************************************************************************
		BOOL FileSys::GetADirectoryEntry(CString &fileName, const CString& Wildcard /* = "" */, const unsigned long eFileAttrib /* = normal */)
		{
			BOOL      bRetVal = TRUE;
			DWORD		tmp;
			unsigned long mask = eFileAttrib;
			
			// If they passed in a Wildcard, we want to lookup the first entry.
			if (Wildcard != "")
			{
				m_hFind = FindFirstFile((const TCHAR *)Wildcard, &m_FileInfo);
				if (m_hFind == INVALID_HANDLE_VALUE)
				{
					bRetVal = FALSE;
				} // if
			} // if
			else
			{
				bRetVal = FindNextFile(m_hFind, &m_FileInfo);
			} // else
			
			
			if (bRetVal == FALSE)
			{
				// Error occurred.
				fileName = "";
				return FALSE;
			} // if
			
			tmp = m_FileInfo.dwFileAttributes;
		#ifndef UNDER_CE
			if (mask==_A_NORMAL || mask==FILE_ATTRIBUTE_NORMAL)
		#else
			if ( mask==FILE_ATTRIBUTE_NORMAL)
		#endif //UNDER_CE (WindowsCE)
			{  
				if (!(tmp & 0x5e)) tmp = 0x80, mask = 0x80;
			}
			if (mask==allfiles || (tmp & mask))
			{
				fileName=m_FileInfo.cFileName;
			} // if
			else
			{
				fileName = "";
			} // else
				
			return TRUE;
		} // GetADirectoryEntry
		#endif


		//***************************************************************************
		//
		// Name:     GetDirectoryEntry
		//
		// Purpose:  To return the next directory entry based on a wildcard, etc...
		//
		// Example:  CString *pFileName = fs.GetDirectoryEntry("*.txt", hidden);
		//
		// Notes:    None.
		//
		//***************************************************************************
		//@doc FileSys
		//@mfunc Returns a single directory entry.
		//@rdesc The name of a file in the current directory or a directory specified 
		// in Wildcard.
		//@parmopt const CString& | Wildcard | "" | The wildcard to use.
		//@parmopt  const unsigned long | eFileAttrib | normal | The file attributes
		// filter. (See <md FileSys::Attribute>).
		//@comm To return the next directory entry based on a wildcard, etc.  
		//@devnote The CString returned by GetDirectoryEntry() must be deleted by the user.
		//@xref <c FileSys> <mf FileSys::GetDirectory> <md FileSys::Attribute>
		//@ex |
		//// Get the first filename matching *.txt
		//CString *pFileName = fs.GetDirectoryEntry("*.txt", CFileSystem::normal);
		//// Get the second filename matching *.txt
		//CString *pFileName = fs.GetDirectoryEntry();
		//@end
		CString * FileSys::GetDirectoryEntry(const CString& Wildcard /* = "" */, const unsigned long eFileAttrib /* = normal */)
		{
		#ifdef WIN32

		  // Turn off critical error handler.
		#ifndef UNDER_CE
		  UINT nPrevErrorMode = ::SetErrorMode(SEM_FAILCRITICALERRORS);
		#endif //UNDER_CE (WindowsCE)
		  BOOL      bRetVal = TRUE;

		  CString fileName;
		  CString l_Wildcard = Wildcard;
		  while (bRetVal  &&  fileName == "")
		  {
			bRetVal = GetADirectoryEntry(fileName, l_Wildcard, eFileAttrib);
			l_Wildcard = "";
		  } // while
		#ifndef UNDER_CE
		  // Restore critical error handler.
		  ::SetErrorMode(nPrevErrorMode);
		#endif //UNDER_CE (WindowsCE)
		  if (bRetVal == FALSE  &&  fileName == "")
		  {
			return NULL;
		  } // if

		  // Create a string and copy the name to it.
		  CString *pString = new CString(fileName);
		  return pString;

		#else

			int nRetVal;

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

			// If they passed in a Wildcard, we want to lookup the first entry.
			if (Wildcard != "")
			{
				nRetVal = _dos_findfirst((const char *) Wildcard, (int) eFileAttrib, &m_FileInfo);
			} // if
			else
			{
				nRetVal = _dos_findnext(&m_FileInfo);
			} // else

			if (nRetVal != 0)
			{
				// Turn on DOS critical error handler.
				::SetErrorMode(0);

				// Error occurred, return NULL.
				return NULL;
			} // if

			// Create a string and copy the name to it.
			CString *pString = new CString(m_FileInfo.name);
			pString->Mid(0,12);

			// Make the filename lower case.
			pString->MakeLower();

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

			return pString;

		#endif
		} // GetDirectoryEntry


		//***************************************************************************
		//
		// Name:     GetCurrentDirectory
		//
		// Purpose:  To get the current working directory.
		//
		// Example:  CString CurrentDir = fs.GetCurrentDirecory();
		//
		// Notes:    None
		//
		//***************************************************************************
		//@doc FileSys
		//@mfunc Returns the current directory name.
		//@rdesc CString 
		//@parmopt const CString& | FileSystem | "" | The name of the filesystem that 
		// the current directory is retrieved for.  If no filesystem or an empty 
		// string is given, the current filesystem is used.
		//@comm To get the current working directory of the current filesystem 
		// (i.e., drive).  Also, the current directory of another filesystem can be 
		// retrieved.
		//
		//To get the current directory of a filesystem that is not the current filesystem, 
		// the current filesystem is changed to the requested filesystem and then the 
		// filesystem is changed back after the current working directory is retrieved.
		//@xref <c FileSys>  <mf FileSys::GetCurrentFileSystem>
		// <mf FileSys::ChangeDirectory>
		//@ex |
		//CString CurrentDir = fs.GetCurrentDirecory();
		//ASSERT(CurrentDir == "c:\\tmp");
		//CString CurrentDir = fs.GetCurrentDirecory("a:\\");
		//ASSERT(CurrentDir == "a:\\foo");
		//@end
		CString FileSys::GetCurrentDirectory(const CString& FileSystem /* = "" */)
		{
		  // Turn off critical error handler.
		#ifndef UNDER_CE
		  ::SetErrorMode(SEM_FAILCRITICALERRORS);
		#endif //UNDER_CE (WindowsCE)

		  CString CurrentFileSystem = GetCurrentFileSystem();
		  if (FileSystem != "")
		  {
			if (!ChangeFileSystem(FileSystem))
			{
			  return "";
			} // if
		  } // if

		  CString String;
		#ifdef UNDER_CE
			String = "";
		#else
		#ifdef WIN32

		  DWORD dwRetVal = ::GetCurrentDirectory(m_nMaxFileNameLength, String.GetBufferSetLength(m_nMaxFileNameLength));
		  String.ReleaseBuffer(-1);

		  if (dwRetVal == 0)
		  {
			String = "";
		  } // if

		#else
			char *pRetVal = _tgetcwd(String.GetBufferSetLength(m_nMaxFileNameLength), m_nMaxFileNameLength);
			String.ReleaseBuffer(-1);

			// Make the string lower case.
			String.MakeLower();

			// If an error occured, clean up and return NULL.
			if (pRetVal == 0)
			{
				String = "";
			} // if

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

		  if (FileSystem != "")
		  {
			VERIFY(ChangeFileSystem(CurrentFileSystem));
		  } // if
		#ifndef UNDER_CE
		  // Turn on critical error handler.
		  ::SetErrorMode(0);
		#endif //UNDER_CE (WindowsCE)
		  return String;
		} // GetCurrentDirectory


		//***************************************************************************
		//
		// Name:    ChangeDirectory
		//
		// Purpose: To change the current working directory.
		//
		// Example: BOOL bRetVal = fs.ChangeDirectory("c:\\foo\\bar");
		//
		// Notes:   None.
		//
		//***************************************************************************
		//@doc FileSys
		//@mfunc Changes the current working directory.
		//@rdesc Nonzero if successful; 0 if an error occurred.
		//@parm const CString& | NewDirectory | Name of the directory to change to.
		//@comm To change the current working directory.
		//@xref <c FileSys> <mf FileSys::GetCurrentDirectory>
		//@ex |
		//BOOL bRetVal = fs.ChangeDirectory("c:\\foo\\bar");
		BOOL FileSys::ChangeDirectory(const CString& NewDirectory)
		{
		  CString DirName = NewDirectory;
		#ifdef UNDER_CE
		  return TRUE;
		#else
		#ifdef WIN32

		  return SetCurrentDirectory((const TCHAR *) DirName);

		#else

			int nRetVal = _tchdir((const TCHAR *) DirName);
			if (nRetVal == -1)
			{
				return FALSE;
			} // if

			return TRUE;

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


		//***************************************************************************
		//
		// Name:    RenameDirectory
		//
		// Purpose: To rename a directory.
		//
		// Ret Val: TRUE : No error, or OldName == NewName.
		//          FALSE : Error.
		//
		// Example: BOOL bRetVal = fs.RenameDirectory("c:\\foo", "c:\\bar");
		//
		// Notes:   Using WIN16, if the new directory name is longer than 8.3,
		//          the parts that are too long will be truncated.
		//
		//***************************************************************************
		//@doc FileSys
		//@mfunc Renames a directory.
		//@rdesc Nonzero if successful; 0 if an error occurred.
		//@parm const CString& | OldName | The name of the directory to rename.
		//@parm  const CString& | NewName | The new name of the directory.
		//@comm To rename a directory.
		//
		//It is not an error to rename a directory to the same name.
		//@xref <c FileSys> <mf FileSys::RenameFile>
		//@ex |
		// BOOL bRetVal = fs.RenameDirectory("c:\\foo", "c:\\bar");
		BOOL FileSys::RenameDirectory(const CString& OldName, const CString& NewName)
		{
		  if (OldName == NewName)
		  {
			return TRUE;
		  } // if

		  if (!DirectoryExists(OldName)  ||  DirectoryExists(NewName))
		  {
			return FALSE;
		  } // if

		  TRY
		  {
			CFile::Rename((const TCHAR *) OldName, (const TCHAR *) NewName);
		  } // TRY
		  CATCH(CFileException, Exception)
		  {
			return FALSE;
		  } // CATCH
		  END_CATCH

		  return TRUE;
		} // RenameDirectory


		//***************************************************************************
		//
		// Name:    MakeDirectory
		//
		// Purpose: To make a directory.
		//
		// Example: BOOL bRetVal = fs.MakeDirectory("c:\\foo\\bar");
		//
		// Notes:   None
		//
		//***************************************************************************
		//@doc FileSys
		//@mfunc Creates a subdirectory.
		//@rdesc Nonzero if successful; 0 if an error occurred.
		//@parm const CString& | NewDirectory | The name of the new directory.
		//@xref <c FileSys> <mf FileSys::MakePath>
		// <mf FileSys::DeleteDirectory>
		//@ex | 
		// BOOL bRetVal = fs.MakeDirectory("c:\\foo\\bar");
		BOOL FileSys::MakeDirectory(const CString& NewDirectory)
		{
		#ifdef WIN32

		  SECURITY_ATTRIBUTES security_attrib;
		  security_attrib.nLength = sizeof(SECURITY_ATTRIBUTES);
		  security_attrib.lpSecurityDescriptor = NULL;
		  security_attrib.bInheritHandle = TRUE;

		  BOOL bRetVal = CreateDirectory((const TCHAR *) NewDirectory, &security_attrib);
		  return bRetVal;

		#else

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

			return TRUE;

		#endif
		} // MakeDirectory

⌨️ 快捷键说明

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