Advertisement
Guest User

winuwp-stream.cpp

a guest
Oct 17th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.66 KB | None | 0 0
  1. #if !defined(DISABLE_STDIO_FILESTREAM)
  2.  
  3. // Disable symbol overrides so that we can use FILE, fopen etc.
  4. #define FORBIDDEN_SYMBOL_ALLOW_ALL
  5.  
  6. #include "winuwp-stream.h"
  7. #include <Windows.h>
  8. #include <ppltasks.h>
  9.  
  10. using namespace concurrency;
  11. using namespace Windows::UI::Core;
  12. using namespace Windows::Storage::Streams;
  13.  
  14.  
  15. WinUWPStream::WinUWPStream(Streams::IRandomAccessStream^ stream)
  16. {
  17.     _stream = stream;
  18.     clearErr();
  19. }
  20.  
  21. WinUWPStream::~WinUWPStream()
  22. {
  23.     delete _stream;
  24. }
  25.  
  26. bool WinUWPStream::err() const
  27. {
  28.     return _error;
  29. }
  30.  
  31. void WinUWPStream::clearErr()
  32. {
  33.     _error = false;
  34.     _eos = false;
  35. }
  36.  
  37. bool WinUWPStream::eos() const
  38. {
  39.     return _eos;
  40. }
  41.  
  42. bool WinUWPStream::eof()
  43. {
  44.     uint32 pos = _stream->Position;
  45.     uint32 size = _stream->Size;
  46.  
  47.     return pos >= size;
  48. }
  49.  
  50. uint32 WinUWPStream::write(const void * dataPtr, uint32 dataSize)
  51. {
  52.     //We are not writing outside of the app's sandbox
  53.     return uint32();
  54. }
  55.  
  56. bool WinUWPStream::flush()
  57. {
  58.     bool done = false;
  59.     bool result = false;
  60.  
  61.     auto t = create_task(_stream->FlushAsync());
  62.  
  63.     t.then([&done, &result](bool b) {
  64.         result = b;
  65.     });
  66.  
  67.     while (!done)
  68.         CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneIfPresent);
  69.  
  70.     return result;
  71. }
  72.  
  73. int32 WinUWPStream::pos() const
  74. {
  75.     return (int32)_stream->Position;
  76. }
  77.  
  78. int32 WinUWPStream::size() const
  79. {
  80.     return (int32)_stream->Size;
  81. }
  82.  
  83. bool WinUWPStream::seek(int32 offs, int whence)
  84. {
  85.     switch (whence)
  86.     {
  87.     case SEEK_END:
  88.         _stream->Seek(_stream->Size);
  89.         break;
  90.     case SEEK_SET:
  91.         _stream->Seek(0);
  92.         break;
  93.     }
  94.     uint32 pos = _stream->Position;
  95.     _stream->Seek(pos + offs);
  96.  
  97.     clearErr();
  98.  
  99.     return true;
  100. }
  101.  
  102. uint32 WinUWPStream::read(void * ptr, uint32 len)
  103. {
  104.     bool done = false;
  105.     uint32 toReturn = 0;
  106.  
  107.     if (!eof()) {
  108.         uint32 toRead = (_stream->Size - _stream->Position);
  109.         if (len < toRead)
  110.             toRead = len;
  111.        
  112.         auto buf = ref new Streams::Buffer(toRead);
  113.         auto t = create_task(_stream->ReadAsync(buf, toRead, Streams::InputStreamOptions::None));
  114.  
  115.         t.then([&done, &toRead, &toReturn, ptr](Streams::IBuffer ^buffer) {
  116.             try {
  117.                 byte* pointer = (byte *)ptr;
  118.                 auto arrRef = Platform::ArrayReference<byte>(pointer, toRead);
  119.                 auto reader = Streams::DataReader::FromBuffer(buffer);
  120.                 reader->ReadBytes(arrRef);
  121.                 toReturn = toRead;
  122.                 delete reader;
  123.                 delete arrRef;
  124.                 delete buffer;
  125.                 done = true;
  126.             }
  127.             catch (...) {
  128.                 toReturn = 0;
  129.                 done = true;
  130.             }
  131.         });
  132.     }
  133.     else {
  134.         _error = true;
  135.         _eos = true;
  136.         done = true;
  137.     }
  138.     while (!done)
  139.         CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneIfPresent);
  140.  
  141.     return toReturn;
  142. }
  143.  
  144.  
  145. WinUWPStream * WinUWPStream::makeFromPath(const Common::String & path, bool writeMode)
  146. {
  147.     WinUWPStream* toReturn;
  148.     bool done = false;
  149.  
  150.     static wchar_t unicodeString[MAX_PATH];
  151.     const char* str = path.c_str();
  152.     MultiByteToWideChar(CP_ACP, 0, str, strlen(str) + 1, unicodeString, sizeof(unicodeString) / sizeof(wchar_t));
  153.  
  154.     Platform::String^ myPath = ref new Platform::String(unicodeString);
  155.  
  156.     auto t = create_task(Windows::Storage::StorageFile::GetFileFromPathAsync(myPath));
  157.  
  158.     t.then([&writeMode](Windows::Storage::StorageFile ^file) -> Windows::Foundation::IAsyncOperation <Streams::IRandomAccessStream ^> ^{
  159.         return file->OpenAsync(FileAccessMode::Read);
  160.     }).then([&toReturn, &done](Streams::IRandomAccessStream ^stream) {
  161.         toReturn = new WinUWPStream(stream);
  162.         done = true;
  163.     });
  164.    
  165.     while (!done)
  166.         CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneIfPresent);
  167.  
  168.     toReturn->_path = path;
  169.     return toReturn;
  170. }
  171.  
  172. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement