Skip to content

Commit

Permalink
FileWrapper: added ReadWholeFile overload for data blob
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 29, 2024
1 parent 858546c commit 4eae867
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 34 deletions.
1 change: 1 addition & 0 deletions Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 4 additions & 25 deletions Common/interface/FileWrapper.hpp
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -30,6 +30,7 @@
#include <vector>

#include "../../Primitives/interface/Errors.hpp"
#include "../../Primitives/interface/DataBlob.h"
#include "../../Platforms/Basic/interface/DebugUtilities.hpp"
#include "../../Platforms/interface/FileSystem.hpp"

Expand Down Expand Up @@ -89,30 +90,8 @@ class FileWrapper

explicit operator bool() const { return m_pFile != nullptr; }

static bool ReadWholeFile(const char* FilePath, std::vector<Uint8>& 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<Uint8>& Data, bool Silent = false);
static bool ReadWholeFile(const char* FilePath, IDataBlob** ppData, bool Silent = false);

private:
FileWrapper(const FileWrapper&);
Expand Down
102 changes: 102 additions & 0 deletions Common/src/FileWrapper.cpp
Original file line number Diff line number Diff line change
@@ -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<Uint8>& 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<DataBlobImpl> 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
4 changes: 2 additions & 2 deletions Platforms/UWP/interface/UWPFileSystem.hpp
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -46,7 +46,7 @@ class WindowsStoreFile : public BasicFile
WindowsStoreFile(const FileOpenAttribs& OpenAttribs);
~WindowsStoreFile();

void Read(IDataBlob* pData);
bool Read(IDataBlob* pData);

bool Read(void* Data, size_t BufferSize);

Expand Down
10 changes: 3 additions & 7 deletions Platforms/UWP/src/UWPFileSystem.cpp
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -163,14 +163,10 @@ size_t WindowsStoreFile::GetSize()
return fileInfo.EndOfFile.LowPart;
}

void WindowsStoreFile::Read(IDataBlob* pData)
bool WindowsStoreFile::Read(IDataBlob* pData)
{
pData->Resize(GetSize());

if (!Read(pData->GetDataPtr(), pData->GetSize()))
{
LOG_ERROR_AND_THROW("Failed to read data from file");
}
return Read(pData->GetDataPtr(), pData->GetSize());
}

void WindowsStoreFile::Write(IDataBlob* pData)
Expand Down

0 comments on commit 4eae867

Please sign in to comment.