/
redgpu
/
ezEngine
Обзор
Документация
Войти
/
redgpu
/
ezEngine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
Code/Engine/Foundation/IO/FileSystem/Implementation/FileWriter.cpp
87 строк
2 KB
Ingrater
Clang-format fixes and update to 18.1.1 (#1234)
10 мар 2024, 15:51
Не верифицирован
10 мар 2024, 15:51
2f16acf
Код
Авторство
О чём код?
#include <Foundation/FoundationPCH.h> #include <Foundation/IO/FileSystem/FileWriter.h> ezResult ezFileWriter::Open(ezStringView sFile, ezUInt32 uiCacheSize /*= 1024 * 1024*/, ezFileShareMode::Enum fileShareMode /*= ezFileShareMode::Exclusive*/, bool bAllowFileEvents /*= true*/) { uiCacheSize = ezMath::Clamp<ezUInt32>(uiCacheSize, 1024, 1024 * 1024 * 32); m_pDataDirWriter = GetFileWriter(sFile, fileShareMode, bAllowFileEvents); if (!m_pDataDirWriter) return EZ_FAILURE; m_Cache.SetCountUninitialized(uiCacheSize); m_uiCacheWritePosition = 0; return EZ_SUCCESS; } void ezFileWriter::Close() { if (!m_pDataDirWriter) return; Flush().IgnoreResult(); m_pDataDirWriter->Close(); m_pDataDirWriter = nullptr; } ezResult ezFileWriter::Flush() { const ezResult res = m_pDataDirWriter->Write(&m_Cache[0], m_uiCacheWritePosition); m_uiCacheWritePosition = 0; return res; } ezResult ezFileWriter::WriteBytes(const void* pWriteBuffer, ezUInt64 uiBytesToWrite) { EZ_ASSERT_DEV(m_pDataDirWriter != nullptr, "The file has not been opened (successfully)."); if (uiBytesToWrite > m_Cache.GetCount()) { // if there is more incoming data than what our cache can hold, there is no point in storing a copy // instead we can just pass the entire data through right away if (m_uiCacheWritePosition > 0) { EZ_SUCCEED_OR_RETURN(Flush()); } return m_pDataDirWriter->Write(pWriteBuffer, uiBytesToWrite); } else { ezUInt8* pBuffer = (ezUInt8*)pWriteBuffer; while (uiBytesToWrite > 0) { // determine chunk size to be written ezUInt64 uiChunkSize = uiBytesToWrite; const ezUInt64 uiRemainingCache = m_Cache.GetCount() - m_uiCacheWritePosition; if (uiRemainingCache < uiBytesToWrite) uiChunkSize = uiRemainingCache; // copy memory ezMemoryUtils::Copy(&m_Cache[(ezUInt32)m_uiCacheWritePosition], pBuffer, (ezUInt32)uiChunkSize); pBuffer += uiChunkSize; m_uiCacheWritePosition += uiChunkSize; uiBytesToWrite -= uiChunkSize; // if the cache is full or nearly full, flush it to disk if (m_uiCacheWritePosition + 32 >= m_Cache.GetCount()) { if (Flush() == EZ_FAILURE) return EZ_FAILURE; } } return EZ_SUCCESS; } }