void CB::String::ToANSI(const CB::CString& strText, int8* szOutText, const uint32 uBufferLen){ if(strText.IsEmpty()){ throw CB::Exception::CZeroLengthException(L"strText", L"Cannot convert from zero length string.", CR_INFO()); } if(szOutText == 0){ throw CB::Exception::CNullPointerException(L"szOutText", L"Cannot convert to buffer with null pointer.", CR_INFO()); } if(uBufferLen < strText.GetLength()){ throw CB::Exception::CInvalidArgumentException(L"uLength", CB::String::FromUInt32(uBufferLen), L"Output buffer must have length at least equal as input string.", CR_INFO()); } try{ #ifdef WIN32 if(!WideCharToMultiByte(CP_ACP, 0, &strText[0], strText.GetLength(), reinterpret_cast<char*>(szOutText), uBufferLen, 0, 0)){ DWORD dwError = GetLastError(); throw CB::Exception::CInvalidVarValueException(L"dwError", CB::String::FromUInt32(dwError), L"Error while converting from UTF16 to ANSI.", CR_INFO()); } #else #error ANSI - UNICODE only on win32 platform implemented. #endif } catch(CB::Exception::CException& Exception){ throw CB::Exception::CException( L"Error while converting form UTF-16 to ANSII string.", CR_INFO(), Exception); } }
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()); } }
const CB::CString CB::String::FromUTF8(const int8* szText, const uint32 uLength){ if(szText == 0){ throw CB::Exception::CNullPointerException(L"szText", L"Cannot convert from null pointer.", CR_INFO()); } if(uLength == 0){ throw CB::Exception::CZeroLengthException(L"szText", L"Connot convert from zero length string.", CR_INFO()); } try{ CB::CString strReturn; #ifdef WIN32 uint32 uLen = MultiByteToWideChar(CP_UTF8, 0, reinterpret_cast<const char*>(szText), uLength, 0, 0); strReturn.Resize(uLen); if(!MultiByteToWideChar(CP_UTF8, 0, reinterpret_cast<const char*>(szText), uLength, &strReturn[0], uLen)){ DWORD dwError = GetLastError(); throw CB::Exception::CInvalidVarValueException(L"dwError", CB::String::FromUInt32(dwError), L"Error while converting from UTF-8 to UTF-16.", CR_INFO()); } #else #error ANSI - UNICODE only on win32 platform implemented. #endif return strReturn; } catch(CB::Exception::CException& Exception){ throw CB::Exception::CException( L"Error while converting from UTF-8 to UTF-16 string.", CR_INFO(), Exception); } }
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 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()); } }
IOGLBaseShader::IOGLBaseShader(CRefPtr<COGLDevice> pDevice, const Graphic::ShaderType uType, const Graphic::ShaderVersion uVersion, CGenum uSourceType, CGprofile uProfile, const CString& strSource, const CString& strEntryPoint) : m_uVersion(uVersion), m_uType(uType), m_uProgram(0), m_bBinded(false), Manage::IManagedObject<COGLDevice, IOGLBaseShader>(pDevice) { if(!cgIsProfileSupported(uProfile)){ throw Exception::CInvalidArgumentException(L"uProfile", String::FromANSI(reinterpret_cast<const int8*>(cgGetProfileString(uProfile))), L"Unsupported shader profile.", CR_INFO()); } auto szSource = String::ToANSI(strSource); auto szEntryPoint = String::ToANSI(strEntryPoint); this->m_uProgram = cgCreateProgram(this->GetParent()->GetCGC().Get(), uSourceType, reinterpret_cast<const char*>(szSource.GetPointer()), uProfile, reinterpret_cast<const char*>(szEntryPoint.GetPointer()), 0); if(!cgIsProgram(this->m_uProgram)){ CR_THROW(L"Failed to create shader program."); } cgCompileProgram(this->m_uProgram); if(!cgIsProgramCompiled(this->m_uProgram)){ CR_THROW(L"Failed to compile program."); } cgGLLoadProgram(this->m_uProgram); }
/* Loops on short writes, but return -EIO if vfs_write() returns zero */ ssize_t cr_uwrite(cr_errbuf_t *eb, struct file *file, const void *buf, size_t count) { ssize_t retval; ssize_t bytes_left = count; const char *p = buf; while (bytes_left) { const ssize_t w = vfs_write(file, p, CR_TRIM_XFER(bytes_left), &file->f_pos); if (w <= 0) { CR_ERR_EB(eb, "vfs_write returned %ld", (long int)w); retval = w; if (!retval) retval = -EIO; /* Map zero -> EIO */ goto out; } bytes_left -= w; p += w; } retval = count; #if CRI_DEBUG if (cr_write_fault_rate) { unsigned int x; get_random_bytes(&x, sizeof(x)); if (!(x % cr_write_fault_rate)) { CR_INFO("injecting WRITE fault"); retval = -EFAULT; } } #endif out: return retval; }
/* Loops on short reads, but return -EIO if vfs_read() returns zero */ ssize_t cr_uread(cr_errbuf_t *eb, struct file *file, void *buf, size_t count) { ssize_t retval; ssize_t bytes_left = count; char *p = buf; while (bytes_left) { const ssize_t r = vfs_read(file, p, CR_TRIM_XFER(bytes_left), &file->f_pos); if (r <= 0) { CR_ERR_EB(eb, "vfs_read returned %ld", (long int)r); retval = r; if (!retval) retval = -EIO; /* Map zero -> EIO */ goto out; } bytes_left -= r; p += r; } retval = count; #if CRI_DEBUG if (cr_read_fault_rate) { unsigned int x; get_random_bytes(&x, sizeof(x)); if (!(x % cr_read_fault_rate)) { CR_INFO("injecting READ fault"); retval = -EFAULT; } } #endif out: return retval; }
void CWindow::SetSize(const Math::CSize& Size){ DWORD dwStyle = 0, dwExStyle = 0; if(!GetMSWindowStyle(this->m_uStyle, dwStyle, dwExStyle)){ throw Exception::CInvalidArgumentException(L"uStyle", String::ToString(m_uStyle), L"Unrecognized window style.", CR_INFO()); } RECT winRect = {0, 0, Size.Width, Size.Height}; if(!AdjustWindowRectEx(&winRect, dwStyle, FALSE, dwExStyle)){ throw Exception::CWindowException(GetLastError(), L"Failed to adjust client area rect.", CR_INFO()); } if(!SetWindowPos(this->m_hWindow, 0, 0, 0, winRect.right - winRect.left, winRect.bottom - winRect.top, SWP_NOMOVE | SWP_NOZORDER)){ throw Exception::CWindowException(GetLastError(), L"Failed to set new window size, Size: " + Size.ToString(), CR_INFO()); } }
CWindow::CWindow(CRefPtr<CWindowManager> pMng, const CString& strClass, const CString& strTitle, const Window::Style uStyle, const Math::CSize& Size, const Math::CPoint& Position) : Manage::IManagedObject<CWindowManager, CWindow>(pMng), m_hWindow(0), m_uStyle(uStyle) { Log::Write(L"Initializing window " + strTitle + L" of class " + strClass + L".", Log::LogLevel::Debug); DWORD dwStyle = 0, dwExStyle = 0; if(!GetMSWindowStyle(uStyle, dwStyle, dwExStyle)){ throw Exception::CInvalidArgumentException(L"uStyle", String::ToString(uStyle), L"Unrecognized window style.", CR_INFO()); } Log::Write(L"Selected window style: " + String::ToString(uStyle) + L".", Log::LogLevel::Debug); HINSTANCE hInstance = GetModuleHandleW(0); RECT winRect = {0, 0, Size.Width, Size.Height}; if(!AdjustWindowRectEx(&winRect, dwStyle, FALSE, dwExStyle)){ CR_THROW(L"Failed to adjust client area rect."); } this->m_hWindow = CreateWindowExW(WS_EX_OVERLAPPEDWINDOW, strClass.GetPointer(), strTitle.GetPointer(), WS_OVERLAPPEDWINDOW, Position.X, Position.Y, winRect.right - winRect.left, winRect.bottom - winRect.top, 0, 0, hInstance, this); if(this->m_hWindow == 0 || this->m_hWindow == INVALID_HANDLE_VALUE){ throw Exception::CWindowException(GetLastError(), L"Fatal error durring window creation.", CR_INFO()); } this->OnClose.SetCombiner(ORCombiner); this->OnCreate.SetCombiner(ORCombiner); this->OnEvent.SetCombiner(ORCombiner); this->OnFocusChange.SetCombiner(ORCombiner); this->OnFocusGain.SetCombiner(ORCombiner); this->OnFocusLost.SetCombiner(ORCombiner); this->OnPositionChange.SetCombiner(ORCombiner); this->OnSizeChange.SetCombiner(ORCombiner); this->OnVisibilityChange.SetCombiner(ORCombiner); this->OnChar.SetCombiner(ORCombiner); this->OnKeyDown.SetCombiner(ORCombiner); this->OnKeyUp.SetCombiner(ORCombiner); this->OnMouseMove.SetCombiner(ORCombiner); this->OnMouseButtonUp.SetCombiner(ORCombiner); this->OnMouseButtonDown.SetCombiner(ORCombiner); Log::Write(L"Window initialized.", Log::LogLevel::Debug); }
CWindowsConsoleStream::~CWindowsConsoleStream(){ this->m_hConsole = 0; if(!FreeConsole()){ throw Exception::CStreamException( L"Failed to free console stream.", CR_INFO()); } }
const CVector2D CVector2D::Div(const CVector2D& Vector) const{ if(Vector.IsPartialZero()){ throw Exception::CInvalidArgumentException(L"Vector", Vector.ToString(), L"Division by Zero", CR_INFO()); } return CVector2D(this->X / Vector.X, this->Y / Vector.Y); }
const CVector2D CVector2D::Div(const float32 fValue) const{ if(fValue == 0.0f){ throw Exception::CInvalidArgumentException(L"fValue", CB::String::ToString(fValue), L"Division by Zero", CR_INFO()); } return CVector2D(this->X / fValue, this->Y / fValue); }
void CModuleManager::UnloadModule(const CString& strModuleName){ try{ this->m_ModuleDictionary.DeleteByKey(strModuleName); } catch(Exception::CException& Exception){ throw Exception::CModuleException(strModuleName, L"Failed to unload module.", CR_INFO(), Exception); } }
void CFileStreamWIN::Close(){ if(this->m_pHandle){ if(!CloseHandle(this->m_pHandle)){ throw CB::Exception::CFileStreamException(this->m_strFilename, L"Error while closing file stream.", CR_INFO()); } this->m_pHandle = 0; } }
const bool CFileStreamWIN::IsEndOfStream() const{ try{ return this->GetPos() >= this->GetLength(); } catch(CB::Exception::CException& Exception){ throw CB::Exception::CFileStreamException(this->m_strFilename, L"Error while determining end of stream.", CR_INFO(), Exception); } }
const Math::CSize CWindow::GetSize() const{ RECT rect; Memory::SetZero(rect); if(!GetClientRect(this->m_hWindow, &rect)){ throw Exception::CWindowException(GetLastError(), L"Failed to get window size.", CR_INFO()); } return Math::CSize(rect.right - rect.left, rect.bottom - rect.top); }
const bool CFileStreamWIN::IsBeginingOfStream() const{ try{ return this->GetPos() == 0; } catch(CB::Exception::CException& Exception){ throw CB::Exception::CFileStreamException(this->m_strFilename, L"Error while determining begining of stream.", CR_INFO(), Exception); } }
const Math::CPoint CWindow::GetPosition() const{ RECT rect; Memory::SetZero(rect); if(!GetWindowRect(this->m_hWindow, &rect)){ throw Exception::CWindowException(GetLastError(), L"Failed to get window position.", CR_INFO()); } return Math::CPoint(rect.left, rect.top); }
void CFileStreamWIN::Write(CRefPtr<IO::IStream> pInStream, const uint32 uSizeInBytes){ if(!this->IsOpen()){ throw CB::Exception::CFileStreamException(this->m_strFilename, L"File stream already closed.", CR_INFO()); } try{ Collection::CList<byte> Buffer(uSizeInBytes); Memory::SetZeroArray(Buffer); pInStream->Read(Buffer); dynamic_cast<IO::IStream*>(this)->Write(Buffer); } catch(Exception::CException& Exception){ throw Exception::CFileStreamException(this->m_strFilename, L"Error while writing stream from stream.", CR_INFO(), Exception); } }
float32& CVector2D::Get(const uint32 uIndex){ switch (uIndex){ case 0: return this->X; case 1: return this->Y; default: throw Exception::CInvalidArgumentException(L"uIndex", CB::String::FromUInt32(uIndex), L"Index out of range.", CR_INFO()); } }
int32& CPoint::operator[](const uint32 uIndex){ switch (uIndex) { case 0: return this->X; case 1: return this->Y; default: throw Exception::CInvalidArgumentException(L"uIndex", String::ToString(uIndex), L"Index out of range.", CR_INFO()); } }
void CModuleManager::LoadModule(const CString& strModuleName){ try{ if(this->m_ModuleDictionary.Contains(strModuleName)){ return; } CModule module(strModuleName); module.Load(); this->m_ModuleDictionary.Add(strModuleName, module); } catch(Exception::CModuleNotFoundException& Exception){ throw Exception::CModuleNotFoundException(strModuleName, CR_INFO(), Exception); } catch(Exception::CModuleLoadException& Exception){ throw Exception::CModuleLoadException(strModuleName, L"Internal loading error.", CR_INFO(), Exception); } catch(Exception::CException& Exception){ throw Exception::CModuleException(strModuleName, L"Unknown internal error.", CR_INFO(), Exception); } }
void CTextWriter::WriteLine(const CString& strValue){ switch (this->m_uEncoding){ case String::Encoding::UTF16: this->WriteLineUTF16(strValue); break; case String::Encoding::UTF8: this->WriteLineUTF8(strValue); break; case String::Encoding::ANSI: this->WriteLineANSI(strValue); break; default: throw Exception::CInvalidVarValueException(L"m_uEncoding", String::ToString(this->m_uEncoding), L"Unknown encoding type.", 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; }
void CWindowsConsoleStream::Write(CRefPtr<IStream> pInStream, const uint32 uSizeInBytes){ try{ Collection::CList<byte> Data(uSizeInBytes); pInStream->Read(Data); dynamic_cast<IO::IStream*>(this)->Write(Data); } catch(Exception::CException& Exception){ throw Exception::CStreamException( L"Error while writing stream to console.", CR_INFO(), Exception); } }
CRefPtr<Graphic::IBaseTexture> IOGLBaseShader::GetSampler(const CString& strName) const{ auto uParam = this->GetParameter(strName); uint32 uIndex = 0; if(Collection::TryFind(this->m_uSamplerParams, uParam, uIndex)){ return this->m_pSamplerTextures[uIndex].GetCast<Graphic::IBaseTexture>(); } throw Exception::CInvalidArgumentException(L"strName", strName, L"Parameter doesn't have an set texture.", CR_INFO()); }