win32filepeer.cpp

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

CPP
1,123
字号
//Win32FilePeer.cpp/*Copyright 2000-2004 The VCF Project.Please see License.txt in the top level directorywhere you installed the VCF.*/#include "vcf/FoundationKit/FoundationKit.h"#include "vcf/FoundationKit/FoundationKitPrivate.h"using namespace VCF;Win32FilePeer::Win32FilePeer( File* file ){	findData_ = NULL;	searchHandle_ = NULL;	file_ = file;	// will have a value when opening the file	fileHandle_ = NULL;}Win32FilePeer::~Win32FilePeer(){	if ( NULL != findData_ ) {		delete findData_;	}}void Win32FilePeer::setFile( File* file ){	// close any open handles	close(); // %%%		findData_ = NULL;	searchHandle_ = NULL;		file_ = file;		// will have a value when opening the file	fileHandle_ = NULL;}void Win32FilePeer::create(  ulong32 openFlags  ){	FilePath fp = getName();	String filename = fp.transformToOSSpecific();		if ( false == filename.empty() ){		if ( NULL != fileHandle_ ){			::CloseHandle( fileHandle_ );		}		if ( FilePath::isDirectoryName( filename ) ) {			fileHandle_ = NULL;			std::vector<String> dirPaths;			String tmp = filename;			int pos = tmp.find( "\\" );			while ( pos != String::npos ) {				dirPaths.push_back( tmp.substr( 0, pos ) );				tmp.erase( 0, pos+1 );				pos = tmp.find( "\\" );			}			if ( dirPaths.size() == 1 ) {				String dir = dirPaths[0];				if ( dir[dir.size()-1] != ':' ) {					throw BasicFileError( "Unable to create directory \"" + filename + "\", path too short or incorrect." );				}			}			else if ( dirPaths.size() < 2 ) {				throw BasicFileError( "Unable to create directory \"" + filename + "\", path too short or incorrect." );			}			std::vector<String>::iterator it = dirPaths.begin();			String dirPath = *it;			it++;			while ( it != dirPaths.end() ) {				dirPath += "\\" + *it;				BOOL res = FALSE;				if ( System::isUnicodeEnabled() ) {					res = ::CreateDirectoryW( dirPath.c_str(), NULL );				}				else {					res = ::CreateDirectoryA( dirPath.ansi_c_str(), NULL );				}				if ( !res ) {					int err = GetLastError();					if ( ERROR_ALREADY_EXISTS != err ) {						throw BasicFileError( "Unable to create directory \"" + filename + "\"" );					}				}				it++;			}		}		else {			//attach to the file			FilePath fp = filename;			String fileDir = fp.getPathName(true);			if ( true == fileDir.empty() ){				TCHAR currentDir[MAX_PATH];				memset( currentDir, 0 , sizeof(currentDir) );				GetCurrentDirectory( MAX_PATH, currentDir );				filename = "\\" + filename;				filename = currentDir +  filename;			}			DWORD rwFlags = 0;			DWORD shFlags = 0;			if ( openFlags & File::ofRead ) {				rwFlags |= GENERIC_READ;				shFlags |= FILE_SHARE_READ;			}			if ( openFlags & File::ofWrite ) {				rwFlags |= GENERIC_WRITE;				shFlags |= FILE_SHARE_WRITE;			}			if ( System::isUnicodeEnabled() ) {				fileHandle_ = ::CreateFileW( filename.c_str(),											rwFlags,											shFlags,											NULL,											CREATE_ALWAYS,											FILE_ATTRIBUTE_NORMAL,											NULL );			}			else {				fileHandle_ = ::CreateFileA( filename.ansi_c_str(),											rwFlags,											shFlags,											NULL,											CREATE_ALWAYS,											FILE_ATTRIBUTE_NORMAL,											NULL );			}			if ( (NULL == fileHandle_) || (INVALID_HANDLE_VALUE == fileHandle_) ){				fileHandle_ = NULL;				//throw exception				throw BasicFileError("Unable to create the specified file");			}		}	}}ulong64 Win32FilePeer::getSize(){	ulong64 result = 0;	if ( NULL != fileHandle_ ) {		uint32 resultLow = 0;		uint32 resultHigh = 0;		if ( NULL != fileHandle_ ){			resultLow = ::GetFileSize( fileHandle_, (DWORD*)&resultHigh );			result.lo( resultLow );			result.hi( resultHigh );		}	}	else {		WIN32_FILE_ATTRIBUTE_DATA fileAttribData;					int res;		if ( System::isUnicodeEnabled() ) {			res = ::GetFileAttributesExW( getName().c_str(), ::GetFileExInfoStandard, (void*)&fileAttribData );		} 		else {			res = ::GetFileAttributesExA( getName().ansi_c_str(), ::GetFileExInfoStandard, (void*)&fileAttribData );		}		result.lo( fileAttribData.nFileSizeLow );		result.hi( fileAttribData.nFileSizeHigh );	}	return result;}DateTime Win32FilePeer::getDateModified(){	DateTime result ;	WIN32_FILE_ATTRIBUTE_DATA fileAttribData;			String fileName = getName();	VCF_ASSERT( !fileName.empty() );	int res;	if ( System::isUnicodeEnabled() ) {		res = ::GetFileAttributesExW( fileName.c_str(), ::GetFileExInfoStandard, (void*)&fileAttribData );	} 	else {		res = ::GetFileAttributesExA( fileName.ansi_c_str(), ::GetFileExInfoStandard, (void*)&fileAttribData );	}	if ( res ) {		result = Win32FilePeer::convertFileTimeToDateTime( fileAttribData.ftLastWriteTime );			}	else {		throw BasicFileError( MAKE_ERROR_MSG_2("Unable to retreive file attributes for file " + fileName) );	}		return result;}DateTime Win32FilePeer::getDateCreated(){	DateTime result ;	WIN32_FILE_ATTRIBUTE_DATA fileAttribData;			String fileName = getName();	VCF_ASSERT( !fileName.empty() );	int res;	if ( System::isUnicodeEnabled() ) {		res = ::GetFileAttributesExW( fileName.c_str(), ::GetFileExInfoStandard, (void*)&fileAttribData );	} 	else {		res = ::GetFileAttributesExA( fileName.ansi_c_str(), ::GetFileExInfoStandard, (void*)&fileAttribData );	}	if ( res ) {		result = Win32FilePeer::convertFileTimeToDateTime( fileAttribData.ftCreationTime );			}	else {		throw BasicFileError( MAKE_ERROR_MSG_2("Unable to retreive file attributes for file " + fileName) );	}		return result;}DateTime Win32FilePeer::getDateAccessed(){	DateTime result ;	WIN32_FILE_ATTRIBUTE_DATA fileAttribData;			String fileName = getName();	VCF_ASSERT( !fileName.empty() );	int res;	if ( System::isUnicodeEnabled() ) {		res = ::GetFileAttributesExW( fileName.c_str(), ::GetFileExInfoStandard, (void*)&fileAttribData );	} 	else {		res = ::GetFileAttributesExA( fileName.ansi_c_str(), ::GetFileExInfoStandard, (void*)&fileAttribData );	}	if ( res ) {		result = Win32FilePeer::convertFileTimeToDateTime( fileAttribData.ftLastAccessTime );			}	else {		throw BasicFileError( MAKE_ERROR_MSG_2("Unable to retreive file attributes for file " + fileName) );	}		return result;}// directory search supportvoid Win32FilePeer::initDataSearch(){	if ( NULL == findData_ ) {		if ( System::isUnicodeEnabled() ) {			findData_ = new Win32FindDataW;		} else {			findData_ = new Win32FindDataA;		}		if ( NULL == findData_ ) {			throw NoFreeMemException(); 		}	}}void Win32FilePeer::initFileSearch( Directory::Finder* finder ){	initDataSearch();	searchHandle_ = NULL;	// other initialize of the finder before starting the search in it	finder->internal_updateToActiveFinder();}File* Win32FilePeer::findNextFileInSearch( Directory::Finder* finder ){	// ::FindFirstFileEx is better suited to look for subdirectories but it is unsupported by win98	File* file = NULL;	bool unicodeEnabled = System::isUnicodeEnabled();	bool isDir = false;	bool ok;	ulong32 fAttribs;	// looping until we find the directory/file matching the filter	while ( true )	{		file = NULL;		finder->getFileInfo().internal_setStatMask( File::smStatNone );		isDir = false;		// are we starting a new search ?		if ( NULL == searchHandle_ ) {			// Remarks: 1 - do not use searchPostfix_ != L"*", like: L"*.cpp", because there is 			//				      there is a bug that gives error when the directory is empty! :(			//				      Is this happening only with Unicode ? or with the searchPrefix_ ?			//			//				  2 - Do not use '\\' and '/' at the same time, beside the prefix L"\\\\?\\",			//              otherwise ::FindFirstFileW() could fail.			String searchDirFilter = String( L"\\\\?\\" )+ getName() + String( L"*" );			if ( unicodeEnabled ) {				searchHandle_ = ::FindFirstFileW( searchDirFilter.c_str(), &((Win32FindDataW*)findData_)->findData_ );			} else {				searchHandle_ = ::FindFirstFileA( searchDirFilter.ansi_c_str(), &((Win32FindDataA*)findData_)->findData_ );			}			ok = ( INVALID_HANDLE_VALUE != searchHandle_ );		} 		else {			if ( unicodeEnabled ) {				ok = ( 0 != ::FindNextFileW( searchHandle_, &((Win32FindDataW*)findData_)->findData_ ) );			} 			else {				ok = ( 0 != ::FindNextFileA( searchHandle_, &((Win32FindDataA*)findData_)->findData_ ) );			}		}		if  ( ok ) {			if ( unicodeEnabled ) {				fAttribs = Win32FilePeer::convertAttributesFromSystemSpecific( ((Win32FindDataW*)findData_)->findData_.dwFileAttributes );			} 			else {				fAttribs = Win32FilePeer::convertAttributesFromSystemSpecific( ((Win32FindDataA*)findData_)->findData_.dwFileAttributes );			}						// first filtering for attributes			isDir = ( 0 != ( fAttribs & File::faDirectory ) );			if ( ( isDir && finder->getCurrentDisplayOrder() == Directory::Finder::dmFiles ) ||			     ( !isDir && ( ( finder->getCurrentDisplayOrder() == Directory::Finder::dmDirs ) ||			                   ( ( File::faNone != finder->getMaskFilterFileAttribs() ) && 			                     ( 0 == ( fAttribs & finder->getMaskFilterFileAttribs() ) ) ) ) ) ) {				continue;			}			// to test the time: with and without			file = updateFindDataInfos( &finder->getFileInfo(), findData_, (File::StatMask)( finder->getStatMask() & ~File::smAttributes ) );			file->internal_setFileAttributes( fAttribs );			file->internal_addToStatMask( File::smAttributes );			if ( isDir && !finder->getAllowSpecialDirs() ) {				if ( file->getName() == L"." || file->getName() == L".." ) {					continue;				}			}			// finally the full filename			file->internal_setFileName( file_->getName() + file->getName() );		}		if ( NULL != file ) {			// filtering

⌨️ 快捷键说明

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