DirectXTK12/Src/BinaryReader.cpp

107 строки
2.7 KiB
C++
Исходник Постоянная ссылка Обычный вид История

2016-08-22 21:17:57 +03:00
//--------------------------------------------------------------------------------------
// File: BinaryReader.cpp
//
2021-02-27 10:01:15 +03:00
// Copyright (c) Microsoft Corporation.
2018-02-24 09:08:23 +03:00
// Licensed under the MIT License.
2016-08-22 21:17:57 +03:00
//
// http://go.microsoft.com/fwlink/?LinkId=248929
2018-02-24 09:08:23 +03:00
// http://go.microsoft.com/fwlink/?LinkID=615561
2016-08-22 21:17:57 +03:00
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "BinaryReader.h"
using namespace DirectX;
// Constructor reads from the filesystem.
2019-12-13 10:02:12 +03:00
BinaryReader::BinaryReader(_In_z_ wchar_t const* fileName) noexcept(false) :
2016-10-29 02:02:21 +03:00
mPos(nullptr),
mEnd(nullptr)
2016-08-22 21:17:57 +03:00
{
size_t dataSize;
HRESULT hr = ReadEntireFile(fileName, mOwnedData, &dataSize);
2018-03-16 22:03:02 +03:00
if (FAILED(hr))
2016-08-22 21:17:57 +03:00
{
2020-01-20 02:24:50 +03:00
DebugTrace("ERROR: BinaryReader failed (%08X) to load '%ls'\n",
static_cast<unsigned int>(hr), fileName);
2021-01-06 00:31:14 +03:00
throw std::runtime_error("BinaryReader");
2016-08-22 21:17:57 +03:00
}
mPos = mOwnedData.get();
mEnd = mOwnedData.get() + dataSize;
}
// Constructor reads from an existing memory buffer.
2019-12-13 10:02:12 +03:00
BinaryReader::BinaryReader(_In_reads_bytes_(dataSize) uint8_t const* dataBlob, size_t dataSize) noexcept :
2016-10-29 02:02:21 +03:00
mPos(dataBlob),
mEnd(dataBlob + dataSize)
2016-08-22 21:17:57 +03:00
{
}
// Reads from the filesystem into memory.
2021-01-06 11:39:32 +03:00
HRESULT BinaryReader::ReadEntireFile(
_In_z_ wchar_t const* fileName,
_Inout_ std::unique_ptr<uint8_t[]>& data,
_Out_ size_t* dataSize)
2016-08-22 21:17:57 +03:00
{
2021-01-06 11:39:32 +03:00
if (!fileName || !dataSize)
return E_INVALIDARG;
*dataSize = 0;
2016-08-22 21:17:57 +03:00
// Open the file.
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
ScopedHandle hFile(safe_handle(CreateFile2(
fileName,
GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING,
nullptr)));
2016-08-22 21:17:57 +03:00
#else
ScopedHandle hFile(safe_handle(CreateFileW(
fileName,
GENERIC_READ, FILE_SHARE_READ,
nullptr,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr)));
2016-08-22 21:17:57 +03:00
#endif
if (!hFile)
return HRESULT_FROM_WIN32(GetLastError());
// Get the file size.
FILE_STANDARD_INFO fileInfo;
if (!GetFileInformationByHandleEx(hFile.get(), FileStandardInfo, &fileInfo, sizeof(fileInfo)))
{
return HRESULT_FROM_WIN32(GetLastError());
}
// File is too big for 32-bit allocation, so reject read.
if (fileInfo.EndOfFile.HighPart > 0)
return E_FAIL;
// Create enough space for the file data.
data.reset(new uint8_t[fileInfo.EndOfFile.LowPart]);
if (!data)
return E_OUTOFMEMORY;
// Read the data in.
DWORD bytesRead = 0;
if (!ReadFile(hFile.get(), data.get(), fileInfo.EndOfFile.LowPart, &bytesRead, nullptr))
{
return HRESULT_FROM_WIN32(GetLastError());
}
if (bytesRead < fileInfo.EndOfFile.LowPart)
return E_FAIL;
*dataSize = bytesRead;
2018-03-16 22:03:02 +03:00
2016-08-22 21:17:57 +03:00
return S_OK;
}