Skip to content

Instantly share code, notes, and snippets.

@peted70
Created June 4, 2018 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peted70/b18e5c120dfbe3a7d24ffa9d133d6714 to your computer and use it in GitHub Desktop.
Save peted70/b18e5c120dfbe3a7d24ffa9d133d6714 to your computer and use it in GitHub Desktop.
void GLTF_Parser::ParseFile(StorageFile^ storageFile)
{
// try to get an IStorageItemHandleAccess interface from the StorageFile
ComPtr<IUnknown> unknown(reinterpret_cast<IUnknown*>(storageFile));
ComPtr<IStorageItemHandleAccess> fileAccessor;
ThrowIfFailed(unknown.As(&fileAccessor));
// Use the IStorageItemHandleAccess::Create method to retrieve an OS HANDLE
shared_ptr<void> fileHandle;
HANDLE file = nullptr;
ThrowIfFailed(fileAccessor->Create(HANDLE_ACCESS_OPTIONS::HAO_READ,
HANDLE_SHARING_OPTIONS::HSO_SHARE_NONE,
HANDLE_OPTIONS::HO_RANDOM_ACCESS,
nullptr,
&file));
// Use RAII-style to ensure that CloseHandle is called when appropriate
bool closed = false;
fileHandle.reset(file, [&closed](HANDLE f) { if (!closed) { CloseHandle(f); closed = true; }});
// Associates a C run-time file descriptor with an existing operating-system file handle.
// https://msdn.microsoft.com/en-us/library/bdts1c9x.aspx
int fd = _open_osfhandle((intptr_t)file, _O_RDONLY);
if (fd == -1)
{
throw std::exception("Unable to open file descriptor!");
}
// Use RAII-style to ensure that _close is called when appropriate
unique_ptr<int, function<int(int*)>> osHandle(&fd,
[&closed](int *fd)
{
int ret = -1;
if (!closed)
{
ret = _close(*fd);
closed = true;
}
return ret;
});
// Open the file descriptor to get a FILE pointer
unique_ptr<FILE, function<int(FILE *)>> fileDescriptor(_fdopen(fd, "r"),
[&closed](FILE *fp)
{
int ret = -1;
if (!closed)
{
ret = fclose(fp);
closed = true;
}
return ret;
});
// wrap our istream in a shared pointer for sharing with the glTF library..
auto str = make_shared<ifstream>(fileDescriptor.get());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment