void CFileStreamWIN::SetPos(const uint32 uPos, const IO::Direction uDirection, const IO::StreamPos uType){ if(!this->IsOpen()){ throw CB::Exception::CFileStreamException(this->m_strFilename, L"File stream already closed.", CR_INFO()); } DWORD dwType = 0; switch (uType) { case IO::StreamPos::Begin: dwType = FILE_BEGIN; break; case IO::StreamPos::Current: dwType = FILE_CURRENT; break; case IO::StreamPos::End: dwType = FILE_END; break; default: throw CB::Exception::CInvalidArgumentException(L"uType", String::ToString(uType), L"Unknown position type for file stream.", CR_INFO()); } LONG iPosition = (LONG)uPos * (uDirection == IO::Direction::Forward ? 1 : -1); DWORD dwPos = SetFilePointer(this->m_pHandle, iPosition, NULL, dwType); if(dwPos == INVALID_SET_FILE_POINTER){ DWORD dwError = GetLastError(); throw Exception::CInvalidFileOperationException(L"File Position", CWindowsError(dwError).GetMessage(), this->m_strFilename, L"Cannot set file stream position.", CR_INFO()); } }
void CFileStreamWIN::Write(const void* pData, const uint32 uSizeInBytes, const uint32 uNumberOfElements){ if(!this->IsOpen()){ throw CB::Exception::CFileStreamException(this->m_strFilename, L"File stream already closed.", CR_INFO()); } DWORD dwToWrite = uSizeInBytes * uNumberOfElements; DWORD dwWriten = 0; if(!WriteFile(this->m_pHandle, pData, dwToWrite, &dwWriten, NULL)){ DWORD uError = GetLastError(); switch (uError){ case ERROR_INSUFFICIENT_BUFFER: throw CB::Exception::CFileStreamException(this->m_strFilename, L"Insufficient buffer to write data.", CR_INFO()); default: throw CB::Exception::CInvalidFileOperationException(L"Write", CWindowsError(uError).GetMessage(), this->m_strFilename, L"Unknown error while writing data to file stream.", CR_INFO()); } } if(dwToWrite != dwWriten){ throw CB::Exception::CInvalidFileOperationException(L"Write", L"Bytes written: " + CB::String::FromUInt32(dwWriten), this->m_strFilename, L"Invalid number of bytes writen to file stream.", CR_INFO()); } }
void CWindowsConsoleStream::Write(const void* pData, const uint32 uSizeInBytes, const uint32 uNumberOfElements){ Collection::CList<byte> Data; try{ Data.Set(uSizeInBytes * uNumberOfElements, (byte*)pData); for(uint32 uIndex = 0; uIndex < Data.GetLength(); uIndex++){ if(Data[uIndex] == (byte)'\n'){ if(uIndex > 0 && Data[uIndex - 1] == (byte)'\r'){ continue; } Data.Insert(uIndex, (byte)'\r'); uIndex++; } } } catch(Exception::CException& Exception){ throw Exception::CStreamException( L"Error while converting data for console buffer write.", CR_INFO(), Exception); } DWORD dwCount = 0; if(!WriteConsoleA(this->m_hConsole, Data.GetPointer(), Data.GetSizeInBytes(), &dwCount, 0)){ throw Exception::CStreamException( L"Failed to write to console buffer. " + CWindowsError(GetLastError()).GetMessage(), CR_INFO()); } }
void CDirectoryWIN::Close(){ if(this->m_hFindHandle){ if(!FindClose(this->m_hFindHandle)){ DWORD dwError = GetLastError(); throw Exception::CDirectoryException(this->m_strPath, CWindowsError(dwError).GetMessage(), CR_INFO()); } this->m_hFindHandle = 0; this->m_bEndOfEntries = true; } }
void CDirectoryWIN::ToFirstEntry(){ this->Close(); this->m_hFindHandle = FindFirstFileW(this->m_strPath.GetPointer(), &this->m_FindData); if(this->m_hFindHandle == INVALID_HANDLE_VALUE || this->m_hFindHandle == 0){ DWORD dwError = GetLastError(); throw Exception::CDirectoryException(this->m_strPath, CWindowsError(dwError).GetMessage(), CR_INFO()); } this->m_bEndOfEntries = false; }
CWindowsConsoleStream::CWindowsConsoleStream() : m_hConsole(0) { if(!AllocConsole()){ //throw Exception::CStreamException( // L"Failed to allocate new console.", CR_INFO()); } this->m_hConsole = GetStdHandle(STD_OUTPUT_HANDLE); if(this->m_hConsole == 0 || this->m_hConsole == INVALID_HANDLE_VALUE){ throw Exception::CStreamException( L"Failed to aquire console handle. " + CWindowsError(GetLastError()).GetMessage(), CR_INFO()); } }
const uint32 CFileStreamWIN::GetPos() const{ if(!this->IsOpen()){ throw CB::Exception::CFileStreamException(this->m_strFilename, L"File stream already closed.", CR_INFO()); } DWORD dwPos = SetFilePointer(this->m_pHandle, 0, NULL, FILE_CURRENT); if(dwPos == INVALID_SET_FILE_POINTER){ DWORD dwError = GetLastError(); throw Exception::CInvalidFileOperationException(L"File Position", CWindowsError(dwError).GetMessage(), this->m_strFilename, L"Cannot get file stream position.", CR_INFO()); } return dwPos; }
const uint32 CFileStreamWIN::GetLength() const{ if(!this->IsOpen()){ throw CB::Exception::CFileStreamException(this->m_strFilename, L"File stream already closed.", CR_INFO()); } DWORD dwSize = GetFileSize(this->m_pHandle, 0); if(dwSize == INVALID_FILE_SIZE){ DWORD dwError = GetLastError(); throw CB::Exception::CInvalidFileOperationException(L"File Size", CWindowsError(dwError).GetMessage(), this->m_strFilename, L"Cannot get file stream size.", CR_INFO()); } return dwSize; }
void CDirectoryWIN::NextEntry(){ if(this->m_bEndOfEntries){ return; } if(!this->m_hFindHandle){ throw Exception::CDirectoryException(this->m_strPath, L"Search already closed.", CR_INFO()); } if(!FindNextFileW(this->m_hFindHandle, &this->m_FindData)){ DWORD dwError = GetLastError(); if(dwError == ERROR_NO_MORE_FILES){ this->m_bEndOfEntries = true; } else{ throw Exception::CDirectoryException(this->m_strPath, CWindowsError(dwError).GetMessage(), CR_INFO()); } } }
void CFileStreamWIN::Read(void* pData, const uint32 uSizeInBytes, const uint32 uNumberOfElements){ if(!this->IsOpen()){ throw CB::Exception::CFileStreamException(this->m_strFilename, L"File stream already closed.", CR_INFO()); } DWORD dwToRead = uSizeInBytes * uNumberOfElements; DWORD dwReaded = 0; if(!ReadFile(this->m_pHandle, pData, dwToRead, &dwReaded, NULL)){ DWORD uError = GetLastError(); switch (uError){ case ERROR_INSUFFICIENT_BUFFER: throw Exception::CFileStreamException(this->m_strFilename, L"Insufficient buffer to read data.", CR_INFO()); default: throw CB::Exception::CInvalidFileOperationException(L"Read", CWindowsError(uError).GetMessage(), this->m_strFilename, L"Unknown error while reading data from file stream.", CR_INFO()); } } }
CFileStreamWIN::CFileStreamWIN(const CString& strFilename, const IO::File::AccessType uAccess, const IO::File::OpenAction uAction, const IO::File::ShareType uShare) : m_pHandle(0), m_strFilename(strFilename), m_uAccess(uAccess), m_uAction(uAction), m_uShare(uShare){ if(this->m_strFilename.IsEmpty()){ throw CB::Exception::CZeroLengthArgumentException(L"strFilename", L"Cannot open file with no filename.", CR_INFO()); } DWORD dwAccess = 0; DWORD dwCreate = 0; DWORD dwShare = 0; switch (uAccess){ case CB::IO::File::AccessType::ReadOnly: dwAccess = GENERIC_READ; break; case CB::IO::File::AccessType::WriteOnly: dwAccess = GENERIC_WRITE; break; case CB::IO::File::AccessType::ReadAndWrite: dwAccess = GENERIC_READ | GENERIC_WRITE; break; default: throw CB::Exception::CInvalidArgumentException(L"uAccess", String::ToString(uAccess), L"Invalid file access value.", CR_INFO()); } switch (uAction) { case IO::File::OpenAction::Open: dwCreate = OPEN_EXISTING; break; case IO::File::OpenAction::Create: dwCreate = CREATE_NEW; break; case IO::File::OpenAction::AlwaysOpen: dwCreate = OPEN_ALWAYS; break; case IO::File::OpenAction::AlwaysCreate: dwCreate = CREATE_ALWAYS; break; default: throw CB::Exception::CInvalidArgumentException(L"uAction", String::ToString(uAction), L"Invalid file open action value.", CR_INFO()); } switch (uShare) { case IO::File::ShareType::None: dwShare = 0; break; case IO::File::ShareType::WriteOnly: dwShare = FILE_SHARE_WRITE; break; case IO::File::ShareType::ReadOnly: dwShare = FILE_SHARE_READ; break; case IO::File::ShareType::ReadAndWrite: dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE; break; default: throw Exception::CInvalidArgumentException(L"uShare", String::ToString(uShare), L"Invalid file share type value.", CR_INFO()); } this->m_pHandle = CreateFileW(this->m_strFilename.GetPointer(), dwAccess, dwShare, NULL, dwCreate, FILE_ATTRIBUTE_NORMAL, NULL); if(this->m_pHandle == 0 || this->m_pHandle == INVALID_HANDLE_VALUE){ DWORD uError = GetLastError(); switch (uError) { case ERROR_SHARING_VIOLATION: throw Exception::CFileSharingException(uShare, uAction, uAccess, strFilename, L"Cannot open file because it's shared by another stream.", CR_INFO()); case ERROR_FILE_EXISTS: throw Exception::CFileAlreadyExistsException(strFilename, CR_INFO()); case ERROR_FILE_NOT_FOUND: throw Exception::CFileNotExistsException(strFilename, CR_INFO()); case ERROR_FILE_READ_ONLY: throw Exception::CFileAccessException(uAccess, strFilename, L"File is read only.", CR_INFO()); default: throw Exception::CFileStreamException(strFilename, CWindowsError(uError).GetMessage(), CR_INFO()); } } }