|
| 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 |
0 commit comments