WinRTIO.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include <wrl.h>
  2. #include <string>
  3. #include <DXGI.h>
  4. #include <ppltasks.h>
  5. #include <shcore.h>
  6. #include <unordered_set>
  7. #include "AsyncHelper.h"
  8. #include "StringHelper.h"
  9. #pragma comment(lib, "ShCore.lib")
  10. #define PAL_PATH_NAME "SDLPAL"
  11. static const LARGE_INTEGER liZero = { 0 };
  12. static const void* const _SIGNATURE = &liZero;
  13. /*========================*/
  14. typedef struct WRT_FILE WRT_FILE;
  15. struct WRT_FILE
  16. {
  17. const void* sig;
  18. IStream* stream;
  19. CRITICAL_SECTION cs;
  20. bool readable;
  21. bool writable;
  22. bool binary;
  23. WRT_FILE() : sig(&liZero), stream(nullptr), readable(false), writable(false), binary(false) { InitializeCriticalSectionEx(&cs, 4000, 0); }
  24. WRT_FILE(Windows::Storage::Streams::IRandomAccessStream^ s, bool r, bool w, bool b)
  25. : sig(&liZero), stream(nullptr), readable(r), writable(w), binary(b)
  26. {
  27. HRESULT hr;
  28. InitializeCriticalSectionEx(&cs, 4000, 0);
  29. if (FAILED(hr = CreateStreamOverRandomAccessStream(s, IID_PPV_ARGS(&stream))))
  30. throw ref new Platform::Exception(hr);
  31. }
  32. ~WRT_FILE() { if (stream) stream->Release(); DeleteCriticalSection(&cs); }
  33. };
  34. class CriticalSection
  35. {
  36. public:
  37. CriticalSection(CRITICAL_SECTION& cs) : m_cs(cs) { EnterCriticalSection(&m_cs); }
  38. ~CriticalSection() { LeaveCriticalSection(&m_cs); }
  39. private:
  40. CRITICAL_SECTION& m_cs;
  41. };
  42. class Event
  43. {
  44. public:
  45. Event() : _eventHandle(CreateEventEx(NULL, NULL, 0, EVENT_ALL_ACCESS)) {}
  46. ~Event() { CloseHandle(_eventHandle); }
  47. operator HANDLE() { return _eventHandle; }
  48. private:
  49. HANDLE _eventHandle;
  50. };
  51. extern "C"
  52. errno_t WRT_fopen_s(WRT_FILE ** pFile, const char * _Filename, const char * _Mode)
  53. {
  54. if (nullptr == _Filename || nullptr == _Mode || nullptr == pFile) return EINVAL;
  55. std::wstring path;
  56. Platform::String^ filename;
  57. ConvertString(_Filename, path);
  58. auto ptr = (wchar_t*)path.c_str() + path.length();
  59. while (ptr >= path.c_str() && *ptr != L'/' && *ptr != L'\\') ptr--;
  60. filename = ref new Platform::String(ptr + 1);
  61. path = path.substr(0, ptr - path.c_str());
  62. size_t offset = 0;
  63. while ((offset = path.find(L'/', offset)) != std::wstring::npos)
  64. path[offset++] = L'\\';
  65. Windows::Storage::StorageFolder^ folder = nullptr;
  66. Event eventHandle;
  67. try
  68. {
  69. folder = AWait(Windows::Storage::StorageFolder::GetFolderFromPathAsync(ref new Platform::String(path.c_str())), eventHandle);
  70. }
  71. catch (Platform::AccessDeniedException^)
  72. {
  73. return EACCES;
  74. }
  75. catch (Platform::Exception^)
  76. {
  77. return EIO;
  78. }
  79. WRT_FILE* ret = nullptr;
  80. try
  81. {
  82. Windows::Storage::StorageFile^ file;
  83. bool r, w;
  84. switch (*_Mode)
  85. {
  86. case 'a': file = AWait(folder->CreateFileAsync(filename, Windows::Storage::CreationCollisionOption::OpenIfExists), eventHandle); w = true; r = false; break;
  87. case 'w': file = AWait(folder->CreateFileAsync(filename, Windows::Storage::CreationCollisionOption::ReplaceExisting), eventHandle); w = true; r = false; break;
  88. case 'r': file = AWait(folder->GetFileAsync(filename), eventHandle); w = false; r = true; break;
  89. default: CloseHandle(eventHandle); return EINVAL;
  90. }
  91. if (file)
  92. {
  93. bool b = false;
  94. for (size_t i = 1; i < strlen(_Mode); i++)
  95. {
  96. switch (_Mode[i])
  97. {
  98. case '+': r = w = true; break;
  99. case 'b': b = true; break;
  100. case 't': b = false; break;
  101. default: return EINVAL;
  102. }
  103. }
  104. ret = new WRT_FILE(AWait(file->OpenAsync(w ? Windows::Storage::FileAccessMode::ReadWrite : Windows::Storage::FileAccessMode::Read), eventHandle), r, w, b);
  105. }
  106. }
  107. catch (Platform::Exception^)
  108. {
  109. return EIO;
  110. }
  111. *pFile = ret;
  112. return 0;
  113. }
  114. extern "C"
  115. WRT_FILE* WRT_fopen(const char * _Filename, const char * _Mode)
  116. {
  117. WRT_FILE* ret;
  118. int err = WRT_fopen_s(&ret, _Filename, _Mode);
  119. _set_errno(err);
  120. return err ? nullptr : ret;
  121. }
  122. extern "C"
  123. int WRT_fclose(WRT_FILE * _File)
  124. {
  125. if (!_File || _File->sig != _SIGNATURE) return fclose((FILE*)_File);
  126. delete _File;
  127. return 0;
  128. }
  129. extern "C"
  130. int WRT_feof(WRT_FILE * _File)
  131. {
  132. if (!_File || _File->sig != _SIGNATURE) return feof((FILE*)_File);
  133. STATSTG st;
  134. ULARGE_INTEGER uli;
  135. CriticalSection cs(_File->cs);
  136. if (SUCCEEDED(_File->stream->Seek(liZero, STREAM_SEEK_CUR, &uli)) &&
  137. SUCCEEDED(_File->stream->Stat(&st, STATFLAG_NONAME)))
  138. return uli.QuadPart >= st.cbSize.QuadPart;
  139. else
  140. return 1;
  141. }
  142. extern "C"
  143. int WRT_fseeki64(WRT_FILE * _File, long long _Offset, int _Origin)
  144. {
  145. if (!_File || _File->sig != _SIGNATURE) return _fseeki64((FILE*)_File, _Offset, _Origin);
  146. CriticalSection cs(_File->cs);
  147. LARGE_INTEGER liOffset;
  148. liOffset.QuadPart = _Offset;
  149. return SUCCEEDED(_File->stream->Seek(liOffset, _Origin, NULL)) ? 0 : -1;
  150. }
  151. extern "C"
  152. int WRT_fseek(WRT_FILE * _File, long _Offset, int _Origin)
  153. {
  154. return WRT_fseeki64(_File, _Offset, _Origin);
  155. }
  156. extern "C"
  157. long long WRT_ftelli64(WRT_FILE * _File)
  158. {
  159. if (!_File || _File->sig != _SIGNATURE) return _ftelli64((FILE*)_File);
  160. CriticalSection cs(_File->cs);
  161. unsigned long long pos;
  162. return SUCCEEDED(_File->stream->Seek(liZero, STREAM_SEEK_CUR, (PULARGE_INTEGER)&pos)) ? pos : -1;
  163. }
  164. extern "C"
  165. size_t WRT_ftell(WRT_FILE * _File)
  166. {
  167. return (size_t)WRT_ftelli64(_File);
  168. }
  169. extern "C"
  170. size_t WRT_fread(void * _DstBuf, size_t _ElementSize, size_t _Count, WRT_FILE * _File)
  171. {
  172. if (!_File || _File->sig != _SIGNATURE) return fread(_DstBuf, _ElementSize, _Count, (FILE*)_File);
  173. if (!_File->readable || _ElementSize == 0 || _Count == 0 || !_DstBuf) return 0;
  174. CriticalSection cs(_File->cs);
  175. unsigned long cbRead;
  176. return SUCCEEDED(_File->stream->Read(_DstBuf, _ElementSize * _Count, &cbRead)) ? cbRead / _ElementSize : 0;
  177. }
  178. extern "C"
  179. size_t WRT_fwrite(const void * _Str, size_t _Size, size_t _Count, WRT_FILE * _File)
  180. {
  181. if (!_File || _File->sig != _SIGNATURE) return fwrite(_Str, _Size, _Count, (FILE*)_File);
  182. if (!_File->writable || !_Str || _Size == 0 || _Count == 0) return 0;
  183. CriticalSection cs(_File->cs);
  184. unsigned long cbWrite;
  185. return SUCCEEDED(_File->stream->Write(_Str, _Size * _Count, &cbWrite)) ? cbWrite / _Size : 0;
  186. }
  187. extern "C"
  188. int WRT_fflush(WRT_FILE * _File)
  189. {
  190. if (!_File || _File->sig != _SIGNATURE) return fflush((FILE*)_File);
  191. if (!_File->writable) return 0;
  192. CriticalSection cs(_File->cs);
  193. return SUCCEEDED(_File->stream->Commit(STGC_DEFAULT)) ? 0 : EOF;
  194. }
  195. static int WRT_fgetc_nolock(WRT_FILE * _File)
  196. {
  197. unsigned long cbRead;
  198. unsigned char buf;
  199. return _File->stream->Read(&buf, 1, &cbRead) == S_OK ? buf : EOF;
  200. }
  201. extern "C"
  202. int WRT_fgetc(WRT_FILE * _File)
  203. {
  204. if (!_File || _File->sig != _SIGNATURE) return fgetc((FILE*)_File);
  205. CriticalSection cs(_File->cs);
  206. return _File->readable ? WRT_fgetc_nolock(_File) : EOF;
  207. }
  208. extern "C"
  209. char* WRT_fgets(char * _Buf, int _MaxCount, WRT_FILE * _File)
  210. {
  211. if (!_File || _File->sig != _SIGNATURE) return fgets(_Buf, _MaxCount, (FILE*)_File);
  212. if (!_File->readable || !_Buf || _MaxCount <= 0) return nullptr;
  213. CriticalSection cs(_File->cs);
  214. int n = 0, flag = 0, ch;
  215. while (n < _MaxCount - 1 && EOF != (ch = WRT_fgetc_nolock(_File)))
  216. {
  217. if (flag)
  218. {
  219. if (ch != '\n')
  220. _Buf[n++] = '\r';
  221. flag = 0;
  222. }
  223. if (ch != '\r')
  224. {
  225. _Buf[n++] = ch;
  226. if (ch == '\n') break;
  227. }
  228. else
  229. flag = 1;
  230. }
  231. if (n > 0)
  232. {
  233. _Buf[n] = '\0';
  234. return _Buf;
  235. }
  236. else
  237. return nullptr;
  238. }
  239. extern "C"
  240. int WRT_fputc(int _Ch, WRT_FILE * _File)
  241. {
  242. if (!_File || _File->sig != _SIGNATURE) return fputc(_Ch, (FILE*)_File);
  243. CriticalSection cs(_File->cs);
  244. unsigned long cbWrite;
  245. return _File->writable && _File->stream->Write(&_Ch, 1, &cbWrite) == S_OK ? cbWrite : EOF;
  246. }
  247. extern "C"
  248. int WRT_fputs(const char * _Str, WRT_FILE * _File)
  249. {
  250. if (!_File || _File->sig != _SIGNATURE) return fputs(_Str, (FILE*)_File);
  251. if (!_File->writable || !_Str) return EOF;
  252. const char *start = _Str;
  253. int length = strlen(_Str);
  254. unsigned long cbWrite;
  255. const char crlf[] = { '\r', '\n' };
  256. CriticalSection cs(_File->cs);
  257. while (true)
  258. {
  259. const char *ptr = start;
  260. while (*ptr && *ptr != '\n') ptr++;
  261. if (_File->stream->Write(start, ptr - start, &cbWrite) != S_OK)
  262. return EOF;
  263. if (*ptr == '\n')
  264. {
  265. if (_File->stream->Write(crlf, 2, &cbWrite) != S_OK)
  266. return EOF;
  267. else
  268. start = ptr + 1;
  269. }
  270. else
  271. break;
  272. }
  273. return length;
  274. }