📄 win32file.cpp
字号:
////////////////////////////////////////////////////////////////////////////////void Win32File::finalize( ){ if( m_hWin32WriteHandle != INVALID_HANDLE_VALUE || m_hWin32ReadHandle != INVALID_HANDLE_VALUE ) { close( ); }}////////////////////////////////////////////////////////////////////////////////bool Win32File::exists( ) throw( IOException ){ bool bResult = false; HANDLE hFindHandle; WIN32_FIND_DATA findData; hFindHandle = FindFirstFile( m_sFullName, &findData ); if( hFindHandle != INVALID_HANDLE_VALUE ) { FindClose( hFindHandle ); bResult = true; } return bResult;}////////////////////////////////////////////////////////////////////////////////p<Directory::Entry> Win32File::getDescriptor( ){ if( m_pDescriptor == 0 ) { m_pDescriptor = CREATE Win32Directory::Win32Entry( m_sFullName ); } return m_pDescriptor;}////////////////////////////////////////////////////////////////////////////////p<Directory> Win32File::getParent( ) throw( IOException ){ p<Directory> pResult; p<Directory::Entry> pDescriptor = getDescriptor( ); p<Directory::Entry> pParentDescriptor = pDescriptor -> getParent( ); if( pParentDescriptor != 0 ) { pResult = CREATE Win32Directory( pParentDescriptor ); } return pResult;}////////////////////////////////////////////////////////////////////////////////void Win32File::open( File::AccessMode accessMode) throw( IOException ){ throwIfAlreadyOpen( m_hWin32WriteHandle ); throwIfAlreadyOpen( m_hWin32ReadHandle ); if( accessMode == READ_ONLY || accessMode == READ_WRITE ) { DWORD dwShareMode = FILE_SHARE_READ; if( accessMode == READ_WRITE ) { dwShareMode |= FILE_SHARE_WRITE; } m_hWin32ReadHandle = CreateFile( m_sFullName, GENERIC_READ, dwShareMode, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if( m_hWin32ReadHandle == INVALID_HANDLE_VALUE ) { throw IOException( -1, "Could not open file '" + m_sFullName + "' for reading" ); } } if( accessMode == WRITE_CREATE || accessMode == WRITE_APPEND || accessMode == READ_WRITE ) { DWORD dwCreationDisposition; if( accessMode == WRITE_CREATE ) { dwCreationDisposition = CREATE_ALWAYS; } else if( accessMode == WRITE_APPEND && !exists( ) ) { dwCreationDisposition = CREATE_NEW; } else { dwCreationDisposition = OPEN_EXISTING; } m_hWin32WriteHandle = CreateFile( m_sFullName, GENERIC_WRITE, FILE_SHARE_READ, NULL, dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL ); if( m_hWin32WriteHandle == INVALID_HANDLE_VALUE ) { if( m_hWin32ReadHandle != INVALID_HANDLE_VALUE ) { CloseHandle( m_hWin32ReadHandle ); m_hWin32ReadHandle = INVALID_HANDLE_VALUE; } throw IOException( -1, "Could not open file '" + m_sFullName + "' for writing" ); } if( accessMode == WRITE_APPEND ) { SetFilePointer( m_hWin32WriteHandle, 0, NULL, FILE_END ); } }}////////////////////////////////////////////////////////////////////////////////p<InputStream> Win32File::getInputStream( ) throw( IOException ){ throwIfNotOpen( m_hWin32ReadHandle ); return CREATE Win32FileInputStream( this );}////////////////////////////////////////////////////////////////////////////////p<OutputStream> Win32File::getOutputStream( ) throw( IOException ){ throwIfNotOpen( m_hWin32WriteHandle ); return CREATE Win32FileOutputStream( this );}////////////////////////////////////////////////////////////////////////////////void Win32File::close( ) throw( IOException ){ if( m_hWin32WriteHandle == INVALID_HANDLE_VALUE && m_hWin32ReadHandle == INVALID_HANDLE_VALUE ) { throw IOException( -1, "File has not been opened" ); } if( m_hWin32WriteHandle != INVALID_HANDLE_VALUE ) { CloseHandle( m_hWin32WriteHandle ); m_hWin32WriteHandle = INVALID_HANDLE_VALUE; } if( m_hWin32ReadHandle != INVALID_HANDLE_VALUE ) { CloseHandle( m_hWin32ReadHandle ); m_hWin32ReadHandle = INVALID_HANDLE_VALUE; }}////////////////////////////////////////////////////////////////////////////////void Win32File::throwIfNotOpen( HANDLE hFileHandle ) const throw( IOException ){ if( hFileHandle == INVALID_HANDLE_VALUE ) { throw IOException( -1, "File has not been opened" ); }}////////////////////////////////////////////////////////////////////////////////void Win32File::throwIfAlreadyOpen( HANDLE hFileHandle ) const throw( IOException ){ if( hFileHandle != INVALID_HANDLE_VALUE ) { throw IOException( -1, "File has already been opened" ); }}////////////////////////////////////////////////////////////////////////////////size_t Win32File::available( ) throw( IOException ){ throwIfNotOpen( m_hWin32ReadHandle ); DWORD nSizeOfFile = 0; DWORD nCurrPosition = 0; nSizeOfFile = GetFileSize( m_hWin32ReadHandle, NULL ); nCurrPosition = SetFilePointer( m_hWin32ReadHandle, 0, NULL, FILE_CURRENT ); return ( size_t ) nSizeOfFile - nCurrPosition;}////////////////////////////////////////////////////////////////////////////////bool Win32File::hasBeenClosed( ) throw( IOException ){ return m_hWin32WriteHandle == INVALID_HANDLE_VALUE && m_hWin32ReadHandle == INVALID_HANDLE_VALUE;}////////////////////////////////////////////////////////////////////////////////size_t Win32File::skip( size_t nNoOfBytes) throw( IOException ){ throwIfNotOpen( m_hWin32ReadHandle ); DWORD nCurrPosition = SetFilePointer( m_hWin32ReadHandle, 0, NULL, FILE_CURRENT ); DWORD nNewPosition = SetFilePointer( m_hWin32ReadHandle, ( LONG ) nNoOfBytes, NULL, FILE_CURRENT ); if( nNewPosition == 0xFFFFFFFF ) { throw IOException( GetLastError( ), "Error while setting file pointer" ); } return ( size_t ) nNewPosition;}////////////////////////////////////////////////////////////////////////////////size_t Win32File::read( void* pBuffer, size_t nMaxNoOfBytes) throw( IOException ){ throwIfNotOpen( m_hWin32ReadHandle ); DWORD nResult = 0; char* lpBuffer = ( ( char* ) pBuffer ); if( !ReadFile( m_hWin32ReadHandle, lpBuffer, nMaxNoOfBytes, &nResult, NULL ) ) { throw IOException( -1, "Error while reading bytes from file" ); } return ( size_t ) nResult;}////////////////////////////////////////////////////////////////////////////////void Win32File::write( const void* pBuffer, size_t nNoOfBytes) throw( IOException ){ throwIfNotOpen( m_hWin32WriteHandle ); DWORD nNoOfBytesWritten = 0; if( !WriteFile( m_hWin32WriteHandle, pBuffer, nNoOfBytes, &nNoOfBytesWritten, NULL ) ) { throw IOException( -1, "Error while writing bytes to file" ); }}////////////////////////////////////////////////////////////////////////////////size_t Win32File::rewind( size_t nNoOfBytes) throw( more::io::IOException ){ throwIfNotOpen( m_hWin32WriteHandle ); DWORD nCurrPosition = SetFilePointer( m_hWin32WriteHandle, 0, NULL, FILE_CURRENT ); DWORD nNewPosition = SetFilePointer( m_hWin32WriteHandle, 0 - ( LONG ) nNoOfBytes, NULL, FILE_CURRENT ); if( nNewPosition == 0xFFFFFFFF ) { throw IOException( GetLastError( ), "Error while setting file pointer" ); } return ( size_t ) nNewPosition;}////////////////////////////////////////////////////////////////////////////////void Win32File::flush( ) throw( IOException ){ throwIfNotOpen( m_hWin32WriteHandle ); if( !FlushFileBuffers( m_hWin32WriteHandle ) ) { throw IOException( -1, "Error while flushing output stream to file" ); }}////////////////////////////////////////////////////////////////////////////////size_t Win32File::getSize( ) throw( IOException ){ throwIfNotOpen( m_hWin32ReadHandle ); return ( size_t ) GetFileSize( m_hWin32ReadHandle, NULL );}////////////////////////////////////////////////////////////////////////////////Win32FileInputStream::Win32FileInputStream( const p<Win32File>& pWin32File): m_pWin32File( pWin32File ){}////////////////////////////////////////////////////////////////////////////////size_t Win32FileInputStream::available( ) throw( IOException ){ return m_pWin32File -> available( );}////////////////////////////////////////////////////////////////////////////////bool Win32FileInputStream::hasBeenClosed( ) throw( IOException ){ return m_pWin32File -> hasBeenClosed( );}////////////////////////////////////////////////////////////////////////////////size_t Win32FileInputStream::skip( size_t nNoOfBytes) throw( IOException ){ return m_pWin32File -> skip( nNoOfBytes );}////////////////////////////////////////////////////////////////////////////////size_t Win32FileInputStream::read( void* pBuffer, size_t nMaxNoOfBytes) throw( IOException ){ return m_pWin32File -> read( pBuffer, nMaxNoOfBytes );}////////////////////////////////////////////////////////////////////////////////Win32FileOutputStream::Win32FileOutputStream( const p<Win32File>& pWin32File): m_pWin32File( pWin32File ){}////////////////////////////////////////////////////////////////////////////////void Win32FileOutputStream::write( const void* pBuffer, size_t nNoOfBytes) throw( IOException ){ m_pWin32File -> write( pBuffer, nNoOfBytes );}////////////////////////////////////////////////////////////////////////////////size_t Win32FileOutputStream::rewind( size_t nNoOfBytes) throw( more::io::IOException ){ return m_pWin32File -> rewind( nNoOfBytes );}////////////////////////////////////////////////////////////////////////////////bool Win32FileOutputStream::hasBeenClosed( ) throw( IOException ){ return m_pWin32File -> hasBeenClosed( );}////////////////////////////////////////////////////////////////////////////////void Win32FileOutputStream::flush( ) throw( IOException ){ m_pWin32File -> flush( );}////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -