diff --git a/Common/CMakeLists.txt b/Common/CMakeLists.txt index 81a65eec3..73d2ce9dd 100644 --- a/Common/CMakeLists.txt +++ b/Common/CMakeLists.txt @@ -53,6 +53,7 @@ set(SOURCE src/BasicFileStream.cpp src/DataBlobImpl.cpp src/DefaultRawMemoryAllocator.cpp + src/FileWrapper.cpp src/FixedBlockMemoryAllocator.cpp src/MemoryFileStream.cpp src/Serializer.cpp diff --git a/Common/interface/FileWrapper.hpp b/Common/interface/FileWrapper.hpp index 15b29d61f..16d9bfe44 100644 --- a/Common/interface/FileWrapper.hpp +++ b/Common/interface/FileWrapper.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2024 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -30,6 +30,7 @@ #include #include "../../Primitives/interface/Errors.hpp" +#include "../../Primitives/interface/DataBlob.h" #include "../../Platforms/Basic/interface/DebugUtilities.hpp" #include "../../Platforms/interface/FileSystem.hpp" @@ -89,30 +90,8 @@ class FileWrapper explicit operator bool() const { return m_pFile != nullptr; } - static bool ReadWholeFile(const char* FilePath, std::vector& Data) - { - VERIFY_EXPR(FilePath != nullptr); - - FileWrapper File{FilePath, EFileAccessMode::Read}; - if (!File) - { - LOG_ERROR_MESSAGE("Failed to open file ", FilePath); - return false; - } - - const auto Size = File->GetSize(); - Data.resize(Size); - if (Size > 0) - { - if (!File->Read(Data.data(), Size)) - { - LOG_ERROR_MESSAGE("Failed to read file ", FilePath); - return false; - } - } - - return true; - } + static bool ReadWholeFile(const char* FilePath, std::vector& Data, bool Silent = false); + static bool ReadWholeFile(const char* FilePath, IDataBlob** ppData, bool Silent = false); private: FileWrapper(const FileWrapper&); diff --git a/Common/src/FileWrapper.cpp b/Common/src/FileWrapper.cpp new file mode 100644 index 000000000..d36e6865c --- /dev/null +++ b/Common/src/FileWrapper.cpp @@ -0,0 +1,102 @@ +/* + * Copyright 2024 Diligent Graphics LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#include "FileWrapper.hpp" +#include "DataBlobImpl.hpp" + +namespace Diligent +{ + +bool FileWrapper::ReadWholeFile(const char* FilePath, std::vector& Data, bool Silent) +{ + if (FilePath == nullptr) + { + DEV_ERROR("File path must not be null"); + return false; + } + + FileWrapper File{FilePath, EFileAccessMode::Read}; + if (!File) + { + if (!Silent) + { + LOG_ERROR_MESSAGE("Failed to open file '", FilePath, "'."); + } + return false; + } + + const size_t Size = File->GetSize(); + Data.resize(Size); + if (Size > 0) + { + if (!File->Read(Data.data(), Size)) + { + if (!Silent) + { + LOG_ERROR_MESSAGE("Failed to read file '", FilePath, "'."); + } + return false; + } + } + + return true; +} + +bool FileWrapper::ReadWholeFile(const char* FilePath, IDataBlob** ppData, bool Silent) +{ + if (ppData == nullptr) + { + DEV_ERROR("Data pointer must not be null"); + return false; + } + + DEV_CHECK_ERR(*ppData == nullptr, "Data pointer is not null. This may result in memory leak."); + + FileWrapper File{FilePath, EFileAccessMode::Read}; + if (!File) + { + if (!Silent) + { + LOG_ERROR_MESSAGE("Failed to open file '", FilePath, "'."); + } + return false; + } + + RefCntAutoPtr pData = DataBlobImpl::Create(); + if (!File->Read(pData)) + { + if (!Silent) + { + LOG_ERROR_MESSAGE("Failed to read file '", FilePath, "'."); + } + return false; + } + + *ppData = pData.Detach(); + return true; +} + +} // namespace Diligent