📄 win32file.cpp
字号:
//// This file is part of the "More for C++" library//// Copyright (c) 1999-2003 by Thorsten Goertz (thorsten@morefor.org)//// The "More for C++" library is free software; you can redistribute it and/or// modify it under the terms of the license that comes with this package.//// Read "license.txt" for more details.//// THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED// WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES// OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.////////////////////////////////////////////////////////////////////////////////#ifndef _WIN32#define _WIN32#endif#include <windows.h>#include <more/create.hpp>#include "win32file.hpp"using namespace more;using namespace more::io;using namespace more::os;using namespace more::os::win32;////////////////////////////////////////////////////////////////////////////////Win32Directory::Win32Entry::Win32Entry( const p<Directory::Entry>& pParent, const String& sName): m_sName( sName ), m_pParent( pParent ){ try { if( pParent != 0 && pParent -> isDirectory( ) ) { m_sFullName = pParent -> getFullName( ) + '\\' + sName; } } catch( ... ) { }}////////////////////////////////////////////////////////////////////////////////Win32Directory::Win32Entry::Win32Entry( const String& sFullName){ try { size_t nLengthOfFullName = sFullName.getLength( ); String sCheckedFullName( sFullName ); char cFullName[2048]; char* pcName; if( nLengthOfFullName > 0 && sFullName[nLengthOfFullName - 1] == '\\' ) { sCheckedFullName = sFullName.getPrefix( nLengthOfFullName - 1 ); } GetFullPathName( sCheckedFullName, 2047, cFullName, &pcName ); if( pcName ) { m_sName = pcName; m_sFullName = cFullName; } } catch( ... ) { }}////////////////////////////////////////////////////////////////////////////////String Win32Directory::Win32Entry::getName( ) throw( IOException ){ throwIfInvalid( ); return m_sName;}////////////////////////////////////////////////////////////////////////////////String Win32Directory::Win32Entry::getFullName( ) throw( IOException ){ throwIfInvalid( ); return m_sFullName;}////////////////////////////////////////////////////////////////////////////////String Win32Directory::Win32Entry::getExtension( ) throw( IOException ){ throwIfInvalid( ); String sResult; const char* pcDot = strrchr( m_sName, '.' ); if( pcDot != 0 ) { sResult = ++pcDot; } return sResult;}////////////////////////////////////////////////////////////////////////////////p<Directory::Entry> Win32Directory::Win32Entry::getParent( ) throw( IOException ){ throwIfInvalid( ); if( m_pParent == 0 ) { const char* pcFullName = m_sFullName; char* pcLastBackslash = strrchr( pcFullName, '\\' ); String sFullNameOfParent( m_sFullName.getPrefix( pcLastBackslash - pcFullName ) ); m_pParent = CREATE Win32Entry( sFullNameOfParent ); } return m_pParent;}////////////////////////////////////////////////////////////////////////////////bool Win32Directory::Win32Entry::isDirectory( ) throw( IOException ){ throwIfInvalid( ); DWORD nFileAttributes = GetFileAttributes( m_sFullName ); return ( nFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) != 0;}////////////////////////////////////////////////////////////////////////////////bool Win32Directory::Win32Entry::isReadOnly( ) throw( IOException ){ throwIfInvalid( ); DWORD nFileAttributes = GetFileAttributes( m_sFullName ); if( nFileAttributes == 0 || nFileAttributes == 0xFFFFFFFF ) { throw IOException( -1, "Invalid directory entry" ); } return ( nFileAttributes & FILE_ATTRIBUTE_READONLY ) != 0;}////////////////////////////////////////////////////////////////////////////////size_t Win32Directory::Win32Entry::getSize( ) throw( IOException ){ throwIfInvalid( ); size_t nResult = 0; try { Win32File file( this ); file.open( File::READ_ONLY ); nResult = file.getSize( ); file.close( ); } catch( const IOException& ) { throw IOException( -1, "Invalid directory entry" ); } return nResult;}////////////////////////////////////////////////////////////////////////////////void Win32Directory::Win32Entry::throwIfInvalid( ) const{ if( m_sFullName.getLength( ) == 0 ) { throw IOException( -1, "Invalid directory entry" ); }}////////////////////////////////////////////////////////////////////////////////Win32Directory::Win32Directory( const p<Directory>& pParent, const String& sName): m_hFindHandle( INVALID_HANDLE_VALUE ){ try { m_sFullName = pParent -> getDescriptor( ) -> getFullName( ) + '\\' + sName; } catch( ... ) { }}////////////////////////////////////////////////////////////////////////////////Win32Directory::Win32Directory( const p<Directory::Entry>& pDescriptor): m_pDescriptor( pDescriptor ), m_hFindHandle( INVALID_HANDLE_VALUE ){ try { m_sFullName = pDescriptor -> getFullName( ); } catch( ... ) { }}////////////////////////////////////////////////////////////////////////////////Win32Directory::Win32Directory( const String& sFullName): m_sFullName( sFullName ), m_hFindHandle( INVALID_HANDLE_VALUE ){}////////////////////////////////////////////////////////////////////////////////void Win32Directory::finalize( ){ if( m_hFindHandle != INVALID_HANDLE_VALUE ) { FindClose( m_hFindHandle ); }}////////////////////////////////////////////////////////////////////////////////bool Win32Directory::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> Win32Directory::getDescriptor( ) throw( IOException ){ if( m_pDescriptor == 0 ) { m_pDescriptor = CREATE Win32Directory::Win32Entry( m_sFullName ); } return m_pDescriptor;}////////////////////////////////////////////////////////////////////////////////p<Directory> Win32Directory::getParent( ){ p<Directory> pResult; p<Directory::Entry> pDescriptor = getDescriptor( ); p<Directory::Entry> pParentDescriptor = pDescriptor -> getParent( ); if( pParentDescriptor != 0 ) { pResult = CREATE Win32Directory( pParentDescriptor ); } return pResult;}////////////////////////////////////////////////////////////////////////////////p<Directory::Entry> Win32Directory::getFirstEntry( ) throw( IOException ){ p<Directory::Entry> pResult; p<Directory::Entry> pDescriptor = getDescriptor( ); String sFullMask( pDescriptor -> getFullName( ) ); bool bFoundEntry; WIN32_FIND_DATA findData; sFullMask << "\\*"; if( m_hFindHandle != INVALID_HANDLE_VALUE ) { FindClose( m_hFindHandle ); } m_hFindHandle = FindFirstFile( sFullMask, &findData ); if( m_hFindHandle == INVALID_HANDLE_VALUE ) { throw IOException( -1, "Win32Directory - FindFirstFile failed" ); } bFoundEntry = true; while( bFoundEntry && ( strcmp( findData.cFileName, "." ) == 0 || strcmp( findData.cFileName, ".." ) == 0 ) ) { bFoundEntry = ( FindNextFile( m_hFindHandle, &findData ) == TRUE ); } if( bFoundEntry ) { pResult = CREATE Win32Entry( getDescriptor( ), findData.cFileName ); } else { FindClose( m_hFindHandle ); m_hFindHandle = INVALID_HANDLE_VALUE; } return pResult;}////////////////////////////////////////////////////////////////////////////////p<Directory::Entry> Win32Directory::getNextEntry( ) throw( IOException ){ p<Directory::Entry> pResult; WIN32_FIND_DATA findData; if( m_hFindHandle == INVALID_HANDLE_VALUE ) { throw IOException( -1, "Win32Directory - invalid call of getNextEntry" ); } if( FindNextFile( m_hFindHandle, &findData ) ) { pResult = CREATE Win32Entry( getDescriptor( ), findData.cFileName ); } else { FindClose( m_hFindHandle ); m_hFindHandle = INVALID_HANDLE_VALUE; } return pResult;}////////////////////////////////////////////////////////////////////////////////Win32File::Win32File( const p<Directory>& pParent, const String& sName): m_pDescriptor( 0 ), m_hWin32ReadHandle( INVALID_HANDLE_VALUE ), m_hWin32WriteHandle( INVALID_HANDLE_VALUE ){ try { m_sFullName = pParent -> getDescriptor( ) -> getFullName( ) + '\\' + sName; } catch( ... ) { }}////////////////////////////////////////////////////////////////////////////////Win32File::Win32File( const p<Directory::Entry>& pDescriptor): m_pDescriptor( pDescriptor ), m_hWin32ReadHandle( INVALID_HANDLE_VALUE ), m_hWin32WriteHandle( INVALID_HANDLE_VALUE ){ try { m_sFullName = pDescriptor -> getFullName( ); } catch( ... ) { }}////////////////////////////////////////////////////////////////////////////////Win32File::Win32File( const String& sFullName): m_sFullName( sFullName ), m_pDescriptor( 0 ), m_hWin32ReadHandle( INVALID_HANDLE_VALUE ), m_hWin32WriteHandle( INVALID_HANDLE_VALUE ){}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -