Skip to content

Commit 4eae867

Browse files
FileWrapper: added ReadWholeFile overload for data blob
1 parent 858546c commit 4eae867

File tree

5 files changed

+112
-34
lines changed

5 files changed

+112
-34
lines changed

Common/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ set(SOURCE
5353
src/BasicFileStream.cpp
5454
src/DataBlobImpl.cpp
5555
src/DefaultRawMemoryAllocator.cpp
56+
src/FileWrapper.cpp
5657
src/FixedBlockMemoryAllocator.cpp
5758
src/MemoryFileStream.cpp
5859
src/Serializer.cpp

Common/interface/FileWrapper.hpp

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -30,6 +30,7 @@
3030
#include <vector>
3131

3232
#include "../../Primitives/interface/Errors.hpp"
33+
#include "../../Primitives/interface/DataBlob.h"
3334
#include "../../Platforms/Basic/interface/DebugUtilities.hpp"
3435
#include "../../Platforms/interface/FileSystem.hpp"
3536

@@ -89,30 +90,8 @@ class FileWrapper
8990

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

92-
static bool ReadWholeFile(const char* FilePath, std::vector<Uint8>& Data)
93-
{
94-
VERIFY_EXPR(FilePath != nullptr);
95-
96-
FileWrapper File{FilePath, EFileAccessMode::Read};
97-
if (!File)
98-
{
99-
LOG_ERROR_MESSAGE("Failed to open file ", FilePath);
100-
return false;
101-
}
102-
103-
const auto Size = File->GetSize();
104-
Data.resize(Size);
105-
if (Size > 0)
106-
{
107-
if (!File->Read(Data.data(), Size))
108-
{
109-
LOG_ERROR_MESSAGE("Failed to read file ", FilePath);
110-
return false;
111-
}
112-
}
113-
114-
return true;
115-
}
93+
static bool ReadWholeFile(const char* FilePath, std::vector<Uint8>& Data, bool Silent = false);
94+
static bool ReadWholeFile(const char* FilePath, IDataBlob** ppData, bool Silent = false);
11695

11796
private:
11897
FileWrapper(const FileWrapper&);

Common/src/FileWrapper.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2024 Diligent Graphics LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* In no event and under no legal theory, whether in tort (including negligence),
17+
* contract, or otherwise, unless required by applicable law (such as deliberate
18+
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
19+
* liable for any damages, including any direct, indirect, special, incidental,
20+
* or consequential damages of any character arising as a result of this License or
21+
* out of the use or inability to use the software (including but not limited to damages
22+
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
23+
* all other commercial damages or losses), even if such Contributor has been advised
24+
* of the possibility of such damages.
25+
*/
26+
27+
#include "FileWrapper.hpp"
28+
#include "DataBlobImpl.hpp"
29+
30+
namespace Diligent
31+
{
32+
33+
bool FileWrapper::ReadWholeFile(const char* FilePath, std::vector<Uint8>& Data, bool Silent)
34+
{
35+
if (FilePath == nullptr)
36+
{
37+
DEV_ERROR("File path must not be null");
38+
return false;
39+
}
40+
41+
FileWrapper File{FilePath, EFileAccessMode::Read};
42+
if (!File)
43+
{
44+
if (!Silent)
45+
{
46+
LOG_ERROR_MESSAGE("Failed to open file '", FilePath, "'.");
47+
}
48+
return false;
49+
}
50+
51+
const size_t Size = File->GetSize();
52+
Data.resize(Size);
53+
if (Size > 0)
54+
{
55+
if (!File->Read(Data.data(), Size))
56+
{
57+
if (!Silent)
58+
{
59+
LOG_ERROR_MESSAGE("Failed to read file '", FilePath, "'.");
60+
}
61+
return false;
62+
}
63+
}
64+
65+
return true;
66+
}
67+
68+
bool FileWrapper::ReadWholeFile(const char* FilePath, IDataBlob** ppData, bool Silent)
69+
{
70+
if (ppData == nullptr)
71+
{
72+
DEV_ERROR("Data pointer must not be null");
73+
return false;
74+
}
75+
76+
DEV_CHECK_ERR(*ppData == nullptr, "Data pointer is not null. This may result in memory leak.");
77+
78+
FileWrapper File{FilePath, EFileAccessMode::Read};
79+
if (!File)
80+
{
81+
if (!Silent)
82+
{
83+
LOG_ERROR_MESSAGE("Failed to open file '", FilePath, "'.");
84+
}
85+
return false;
86+
}
87+
88+
RefCntAutoPtr<DataBlobImpl> pData = DataBlobImpl::Create();
89+
if (!File->Read(pData))
90+
{
91+
if (!Silent)
92+
{
93+
LOG_ERROR_MESSAGE("Failed to read file '", FilePath, "'.");
94+
}
95+
return false;
96+
}
97+
98+
*ppData = pData.Detach();
99+
return true;
100+
}
101+
102+
} // namespace Diligent

Platforms/UWP/interface/UWPFileSystem.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -46,7 +46,7 @@ class WindowsStoreFile : public BasicFile
4646
WindowsStoreFile(const FileOpenAttribs& OpenAttribs);
4747
~WindowsStoreFile();
4848

49-
void Read(IDataBlob* pData);
49+
bool Read(IDataBlob* pData);
5050

5151
bool Read(void* Data, size_t BufferSize);
5252

Platforms/UWP/src/UWPFileSystem.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -163,14 +163,10 @@ size_t WindowsStoreFile::GetSize()
163163
return fileInfo.EndOfFile.LowPart;
164164
}
165165

166-
void WindowsStoreFile::Read(IDataBlob* pData)
166+
bool WindowsStoreFile::Read(IDataBlob* pData)
167167
{
168168
pData->Resize(GetSize());
169-
170-
if (!Read(pData->GetDataPtr(), pData->GetSize()))
171-
{
172-
LOG_ERROR_AND_THROW("Failed to read data from file");
173-
}
169+
return Read(pData->GetDataPtr(), pData->GetSize());
174170
}
175171

176172
void WindowsStoreFile::Write(IDataBlob* pData)

0 commit comments

Comments
 (0)