Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if !defined(DISABLE_STDIO_FILESTREAM)
- // Disable symbol overrides so that we can use FILE, fopen etc.
- #define FORBIDDEN_SYMBOL_ALLOW_ALL
- #include "winuwp-stream.h"
- #include <Windows.h>
- #include <ppltasks.h>
- using namespace concurrency;
- using namespace Windows::UI::Core;
- using namespace Windows::Storage::Streams;
- WinUWPStream::WinUWPStream(Streams::IRandomAccessStream^ stream)
- {
- _stream = stream;
- clearErr();
- }
- WinUWPStream::~WinUWPStream()
- {
- delete _stream;
- }
- bool WinUWPStream::err() const
- {
- return _error;
- }
- void WinUWPStream::clearErr()
- {
- _error = false;
- _eos = false;
- }
- bool WinUWPStream::eos() const
- {
- return _eos;
- }
- bool WinUWPStream::eof()
- {
- uint32 pos = _stream->Position;
- uint32 size = _stream->Size;
- return pos >= size;
- }
- uint32 WinUWPStream::write(const void * dataPtr, uint32 dataSize)
- {
- //We are not writing outside of the app's sandbox
- return uint32();
- }
- bool WinUWPStream::flush()
- {
- bool done = false;
- bool result = false;
- auto t = create_task(_stream->FlushAsync());
- t.then([&done, &result](bool b) {
- result = b;
- });
- while (!done)
- CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneIfPresent);
- return result;
- }
- int32 WinUWPStream::pos() const
- {
- return (int32)_stream->Position;
- }
- int32 WinUWPStream::size() const
- {
- return (int32)_stream->Size;
- }
- bool WinUWPStream::seek(int32 offs, int whence)
- {
- switch (whence)
- {
- case SEEK_END:
- _stream->Seek(_stream->Size);
- break;
- case SEEK_SET:
- _stream->Seek(0);
- break;
- }
- uint32 pos = _stream->Position;
- _stream->Seek(pos + offs);
- clearErr();
- return true;
- }
- uint32 WinUWPStream::read(void * ptr, uint32 len)
- {
- bool done = false;
- uint32 toReturn = 0;
- if (!eof()) {
- uint32 toRead = (_stream->Size - _stream->Position);
- if (len < toRead)
- toRead = len;
- auto buf = ref new Streams::Buffer(toRead);
- auto t = create_task(_stream->ReadAsync(buf, toRead, Streams::InputStreamOptions::None));
- t.then([&done, &toRead, &toReturn, ptr](Streams::IBuffer ^buffer) {
- try {
- byte* pointer = (byte *)ptr;
- auto arrRef = Platform::ArrayReference<byte>(pointer, toRead);
- auto reader = Streams::DataReader::FromBuffer(buffer);
- reader->ReadBytes(arrRef);
- toReturn = toRead;
- delete reader;
- delete arrRef;
- delete buffer;
- done = true;
- }
- catch (...) {
- toReturn = 0;
- done = true;
- }
- });
- }
- else {
- _error = true;
- _eos = true;
- done = true;
- }
- while (!done)
- CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneIfPresent);
- return toReturn;
- }
- WinUWPStream * WinUWPStream::makeFromPath(const Common::String & path, bool writeMode)
- {
- WinUWPStream* toReturn;
- bool done = false;
- static wchar_t unicodeString[MAX_PATH];
- const char* str = path.c_str();
- MultiByteToWideChar(CP_ACP, 0, str, strlen(str) + 1, unicodeString, sizeof(unicodeString) / sizeof(wchar_t));
- Platform::String^ myPath = ref new Platform::String(unicodeString);
- auto t = create_task(Windows::Storage::StorageFile::GetFileFromPathAsync(myPath));
- t.then([&writeMode](Windows::Storage::StorageFile ^file) -> Windows::Foundation::IAsyncOperation <Streams::IRandomAccessStream ^> ^{
- return file->OpenAsync(FileAccessMode::Read);
- }).then([&toReturn, &done](Streams::IRandomAccessStream ^stream) {
- toReturn = new WinUWPStream(stream);
- done = true;
- });
- while (!done)
- CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneIfPresent);
- toReturn->_path = path;
- return toReturn;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement