win32filepeer.cpp

来自「这是VCF框架的代码」· C++ 代码 · 共 1,123 行 · 第 1/3 页

CPP
1,123
字号
			FileSearchFilter* filter = finder->getSearchFilterFileObject();			if ( NULL != filter ) {				file = filter->passSearchFilter( file, dynamic_cast<const Directory*>(file_), finder );			}			// recursion management			if ( NULL != file ) {				// ok we found a subdirectory matching the filter				// now recursion into this subdirectory				if ( isDir ) {					if ( finder->getRecurse() ) {						if ( finder->canRecurseDown() )	{							this->goDirDown( finder, file );						} 						else {							// continue the search in the next subdirectory							continueSearch( finder, file );						}					}				}				if ( NULL != file ) {					// file found					break;				}			}		} 		else {			DWORD searchErr = ::GetLastError();						// end of search or error			if ( ERROR_NO_MORE_FILES == searchErr )  {				if ( INVALID_HANDLE_VALUE != searchHandle_ ) {					this->endFileSearch( finder );					// what do we need to do next ?					continueSearch( finder, file );				}				break;			}						else if ( (ERROR_ACCESS_DENIED == searchErr) && (INVALID_HANDLE_VALUE == searchHandle_) )  {				endFileSearch( finder );				continueSearch( finder, file );				break;			}			else {				String dirInfo = "\ndirectory: " + file_->getName();				String error = "findNextFileInSearch: " + VCFWin32::Win32Utils::getErrorString( GetLastError() ) + dirInfo ;				throw BasicException( error );			}		}	} // while ( true )	finder->setCurrentElement( file );	return file;}void Win32FilePeer::endFileSearch( Directory::Finder* finder ){	if ( NULL != searchHandle_ && INVALID_HANDLE_VALUE != searchHandle_ ) {		if ( !::FindClose( searchHandle_ )) {			String error = "endFileSearch: " + VCFWin32::Win32Utils::getErrorString( GetLastError() );			throw BasicException( error );		}	}	searchHandle_ = NULL;}void Win32FilePeer::continueSearch( Directory::Finder* finder, File* file ){	finder->continueSearch( file );}void Win32FilePeer::goDirDown( Directory::Finder* finder, File* file ){		finder->goDownDir( file );}void Win32FilePeer::goDirUp( Directory::Finder* finder, File* file ){	finder->goUpDir( file );}/*static*/ void Win32FilePeer::copyFromAttributeData( File* file, WIN32_FILE_ATTRIBUTE_DATA& fileAttribData, File::StatMask statMask/*=File::smMaskAll*/ ){	// throw if errors	file->internal_removeFromStatMask( statMask );		if ( statMask & File::smAttributes ) {		file->internal_setFileAttributes( Win32FilePeer::convertAttributesFromSystemSpecific( fileAttribData.dwFileAttributes ) );	}		// we are ok if here	file->internal_addToStatMask( statMask );}bool Win32FilePeer::isExecutable(){	String ext = FilePath::getExtension( getName() );	return ( ext == ".exe" || ext == ".com" || ext == ".dat" ) ? true : false;}/*static*/ File* Win32FilePeer::updateFindDataInfos( File* file, Win32FindData* findData, File::StatMask statMask/*=File::smMaskAll*/ ){	// throw in copyFromAttributeData if errors	file->internal_removeFromStatMask( statMask );	WIN32_FILE_ATTRIBUTE_DATA fileAttribData;	if ( System::isUnicodeEnabled() ) {			Win32FindDataW* w = ((Win32FindDataW*)findData);			WIN32_FIND_DATAW& dataW2 = w->findData_ ;			WIN32_FIND_DATAW& dataW = ((Win32FindDataW*)findData)->findData_ ;			file->internal_setFileName( dataW.cFileName );      fileAttribData.dwFileAttributes = dataW.dwFileAttributes ;      fileAttribData.ftCreationTime   = dataW.ftCreationTime   ;      fileAttribData.ftLastAccessTime = dataW.ftLastAccessTime ;      fileAttribData.ftLastWriteTime  = dataW.ftLastWriteTime  ;      fileAttribData.nFileSizeHigh    = dataW.nFileSizeHigh    ;      fileAttribData.nFileSizeLow     = dataW.nFileSizeLow     ;			copyFromAttributeData( file, fileAttribData, statMask );	} else {			WIN32_FIND_DATAA& dataA = ((Win32FindDataA*)findData)->findData_ ;			file->internal_setFileName( dataA.cFileName );      fileAttribData.dwFileAttributes = dataA.dwFileAttributes ;      fileAttribData.ftCreationTime   = dataA.ftCreationTime   ;      fileAttribData.ftLastAccessTime = dataA.ftLastAccessTime ;      fileAttribData.ftLastWriteTime  = dataA.ftLastWriteTime  ;      fileAttribData.nFileSizeHigh    = dataA.nFileSizeHigh    ;      fileAttribData.nFileSizeLow     = dataA.nFileSizeLow     ;			copyFromAttributeData( file, fileAttribData, statMask );	}	//file->fileType_          = 0;	//file->creatorType_       = 0;	//file->finderFlags_       = 0;	file->internal_addToStatMask( statMask );	return file;}void Win32FilePeer::updateStat( File::StatMask statMask/*=File::smMaskAll*/ ){	// this is very similar to updateFindDataInfos but using a different data structure	String fileName = getName();	//VCF_ASSERT( !FilePath::getPathName( fileName, true ).empty() );		VCF_ASSERT( !fileName.empty() );		WIN32_FILE_ATTRIBUTE_DATA fileAttribData;	file_->internal_removeFromStatMask( statMask );	int res;	if ( System::isUnicodeEnabled() ) {		if ( res = ::GetFileAttributesExW( getName().c_str(), ::GetFileExInfoStandard, (void*)&fileAttribData ) ) {			copyFromAttributeData( file_, fileAttribData, statMask );		}	} 	else {		if ( res = ::GetFileAttributesExA( getName().ansi_c_str(), ::GetFileExInfoStandard, (void*)&fileAttribData ) ) {			copyFromAttributeData( file_, fileAttribData, statMask );		}	}	if( !res ) {		String error = "updateStat: " + VCFWin32::Win32Utils::getErrorString( GetLastError() );		throw BasicException( error );	}}/*virtual*/ void Win32FilePeer::setFileAttributes( const File::FileAttributes fileAttributes ){	file_->internal_removeFromStatMask( File::smAttributes );	try {		bool unicodeEnabled = System::isUnicodeEnabled();		if ( unicodeEnabled ) {			ulong32 dwFileAttributes = Win32FilePeer::convertAttributesToSystemSpecific( fileAttributes );			if ( ! ::SetFileAttributesW( file_->getName().c_str(), dwFileAttributes ) ) {				String error = VCFWin32::Win32Utils::getErrorString( GetLastError() );				throw BasicException( error );			}		} else {			ulong32 dwFileAttributes = Win32FilePeer::convertAttributesToSystemSpecific( fileAttributes );			if ( ! ::SetFileAttributesA( file_->getName().ansi_c_str(), dwFileAttributes ) ) {				String error = VCFWin32::Win32Utils::getErrorString( GetLastError() );				throw BasicException( error );			}		}	}	catch ( BasicException& /*be*/ ) {		throw; // re-throw	}	catch( ... ) {		String error = VCFWin32::Win32Utils::getErrorString( GetLastError() );		throw RuntimeException( MAKE_ERROR_MSG_2( error ) );	}	file_->internal_setFileAttributes( fileAttributes );	file_->internal_addToStatMask( File::smAttributes );	return;}void Win32FilePeer::setDateModified( const DateTime& dateModified ){	// see CFile::SetStatus(LPCTSTR lpszFileName, const CFileStatus& status)	// see AfxTimeToFileTime;	file_->internal_removeFromStatMask( File::smDateModified );	try {		bool unicodeEnabled = System::isUnicodeEnabled();		// DateTime --> systemTime		SYSTEMTIME st;		FILETIME   ftUTC;		HANDLE hFile = NULL;		unsigned long y, m, d, h, min, s, ms;		dateModified.get( &y, &m, &d, &h, &min, &s, &ms );		if ( ( y < 1601 ) || ( 30827 < y ) ) {			throw BasicException( "The SYSTEMTIME structure doesn't allow dates outside the range [1601,30827]" );		}		st.wYear   = y;		st.wMonth  = m;		st.wDayOfWeek = dateModified.getWeekDay();		st.wDay    = d;		st.wHour   = h;		st.wMinute = min;		st.wSecond = s;		st.wMilliseconds = ms;		// convert system time to filetime		if ( !::SystemTimeToFileTime( &st, &ftUTC ) ) { // stUTC --> ftUTC			String error = VCFWin32::Win32Utils::getErrorString( GetLastError() );			throw BasicException( error );		}		if ( unicodeEnabled ) {			hFile = ::CreateFileW( file_->getName().c_str(), GENERIC_READ|GENERIC_WRITE,				FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);			if (hFile == INVALID_HANDLE_VALUE) {				String error = VCFWin32::Win32Utils::getErrorString( GetLastError() );				throw BasicException( error );			}		} 		else {						hFile = ::CreateFileA( file_->getName().ansi_c_str(), GENERIC_READ|GENERIC_WRITE,				FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);			if (hFile == INVALID_HANDLE_VALUE) {				String error = VCFWin32::Win32Utils::getErrorString( GetLastError() );				throw BasicException( error );			}		}		if (! ::SetFileTime( hFile, NULL, NULL, &ftUTC ) ) {			String error = VCFWin32::Win32Utils::getErrorString( GetLastError() );			throw BasicException( error );		}		if (! ::CloseHandle(hFile)) {			String error = VCFWin32::Win32Utils::getErrorString( GetLastError() );			throw BasicException( error );		}	}	catch ( BasicException& /*be*/ ) {		throw; // re-throw	}	catch( ... ) {		String error = VCFWin32::Win32Utils::getErrorString( GetLastError() );		throw RuntimeException( MAKE_ERROR_MSG_2( error ) );	}	file_->internal_addToStatMask( File::smDateModified );}/*static*/ ulong32 Win32FilePeer::convertAttributesFromSystemSpecific( const ulong32& dwAttributes ){	ulong32 fileAttributes = File::faNone;	if ( dwAttributes & FILE_ATTRIBUTE_READONLY ) {		fileAttributes += File::faReadOnly;	}	if ( dwAttributes & FILE_ATTRIBUTE_HIDDEN ) {		fileAttributes += File::faHidden;	}	if ( dwAttributes & FILE_ATTRIBUTE_SYSTEM ) {		fileAttributes += File::faSystem;	}	if ( dwAttributes & FILE_ATTRIBUTE_DIRECTORY ) {		fileAttributes += File::faDirectory;	}	if ( dwAttributes & FILE_ATTRIBUTE_ARCHIVE ) {		fileAttributes += File::faArchive;	}#if (_MSC_VER >= 1300)	// vc7 and above	if ( dwAttributes & FILE_ATTRIBUTE_DEVICE ) {		fileAttributes += File::faDevice;	}#endif	if ( dwAttributes & FILE_ATTRIBUTE_NORMAL ) {		fileAttributes += File::faNormal;	}	return fileAttributes;}/*static*/ ulong32 Win32FilePeer::convertAttributesToSystemSpecific( File::FileAttributes fileAttributes ){	ulong32 dwAttributes = 0;	if ( fileAttributes & File::faReadOnly ) {		dwAttributes += FILE_ATTRIBUTE_READONLY;	}	if ( fileAttributes & File::faHidden ) {		dwAttributes += FILE_ATTRIBUTE_HIDDEN;	}	if ( fileAttributes & File::faSystem ) {		dwAttributes += FILE_ATTRIBUTE_SYSTEM;	}	if ( fileAttributes & File::faDirectory ) {		dwAttributes += FILE_ATTRIBUTE_DIRECTORY;	}	if ( fileAttributes & File::faArchive ) {		dwAttributes += FILE_ATTRIBUTE_ARCHIVE;	}#if (_MSC_VER >= 1300)	// vc7 and above	if ( fileAttributes & File::faDevice ) {		dwAttributes += FILE_ATTRIBUTE_DEVICE;	}#endif	if ( fileAttributes & File::faNormal ) {		dwAttributes += FILE_ATTRIBUTE_NORMAL;	}	return dwAttributes;}void Win32FilePeer::open( const String& fileName, ulong32 openFlags, File::ShareFlags shareFlags/*=File::shMaskAny*/ ){	//check initial arguments	ulong32 f1 =  File::ofNone;	VCF_ASSERT( openFlags != f1 );	String winFileName = fileName;	//attach to the file	FilePath fp = winFileName;	String fileDir = fp.getPathName(true);

⌨️ 快捷键说明

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