WinRTIO.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. if (path.size() > 0)
  66. {
  67. if (path.back() == L':') path.append(L"\\");
  68. }
  69. else
  70. {
  71. path.assign(Windows::Storage::ApplicationData::Current->LocalFolder->Path->Data());
  72. }
  73. Windows::Storage::StorageFolder^ folder = nullptr;
  74. Event eventHandle;
  75. try
  76. {
  77. folder = AWait(Windows::Storage::StorageFolder::GetFolderFromPathAsync(ref new Platform::String(path.c_str())), eventHandle);
  78. }
  79. catch (Platform::AccessDeniedException^)
  80. {
  81. return EACCES;
  82. }
  83. catch (Platform::Exception^)
  84. {
  85. return EIO;
  86. }
  87. WRT_FILE* ret = nullptr;
  88. try
  89. {
  90. Windows::Storage::StorageFile^ file;
  91. bool r, w;
  92. switch (*_Mode)
  93. {
  94. case 'a': file = AWait(folder->CreateFileAsync(filename, Windows::Storage::CreationCollisionOption::OpenIfExists), eventHandle); w = true; r = false; break;
  95. case 'w': file = AWait(folder->CreateFileAsync(filename, Windows::Storage::CreationCollisionOption::ReplaceExisting), eventHandle); w = true; r = false; break;
  96. case 'r': file = AWait(folder->GetFileAsync(filename), eventHandle); w = false; r = true; break;
  97. default: CloseHandle(eventHandle); return EINVAL;
  98. }
  99. if (file)
  100. {
  101. bool b = false;
  102. for (size_t i = 1; i < strlen(_Mode); i++)
  103. {
  104. switch (_Mode[i])
  105. {
  106. case '+': r = w = true; break;
  107. case 'b': b = true; break;
  108. case 't': b = false; break;
  109. default: return EINVAL;
  110. }
  111. }
  112. ret = new WRT_FILE(AWait(file->OpenAsync(w ? Windows::Storage::FileAccessMode::ReadWrite : Windows::Storage::FileAccessMode::Read), eventHandle), r, w, b);
  113. }
  114. }
  115. catch (Platform::Exception^)
  116. {
  117. return EIO;
  118. }
  119. *pFile = ret;
  120. return 0;
  121. }
  122. extern "C"
  123. WRT_FILE* WRT_fopen(const char * _Filename, const char * _Mode)
  124. {
  125. WRT_FILE* ret;
  126. int err = WRT_fopen_s(&ret, _Filename, _Mode);
  127. _set_errno(err);
  128. return err ? nullptr : ret;
  129. }
  130. extern "C"
  131. int WRT_fclose(WRT_FILE * _File)
  132. {
  133. if (!_File || _File->sig != _SIGNATURE) return fclose((FILE*)_File);
  134. delete _File;
  135. return 0;
  136. }
  137. extern "C"
  138. int WRT_feof(WRT_FILE * _File)
  139. {
  140. if (!_File || _File->sig != _SIGNATURE) return feof((FILE*)_File);
  141. STATSTG st;
  142. ULARGE_INTEGER uli;
  143. CriticalSection cs(_File->cs);
  144. if (SUCCEEDED(_File->stream->Seek(liZero, STREAM_SEEK_CUR, &uli)) &&
  145. SUCCEEDED(_File->stream->Stat(&st, STATFLAG_NONAME)))
  146. return uli.QuadPart >= st.cbSize.QuadPart;
  147. else
  148. return 1;
  149. }
  150. extern "C"
  151. int WRT_fseeki64(WRT_FILE * _File, long long _Offset, int _Origin)
  152. {
  153. if (!_File || _File->sig != _SIGNATURE) return _fseeki64((FILE*)_File, _Offset, _Origin);
  154. CriticalSection cs(_File->cs);
  155. LARGE_INTEGER liOffset;
  156. liOffset.QuadPart = _Offset;
  157. return SUCCEEDED(_File->stream->Seek(liOffset, _Origin, NULL)) ? 0 : -1;
  158. }
  159. extern "C"
  160. int WRT_fseek(WRT_FILE * _File, long _Offset, int _Origin)
  161. {
  162. return WRT_fseeki64(_File, _Offset, _Origin);
  163. }
  164. extern "C"
  165. long long WRT_ftelli64(WRT_FILE * _File)
  166. {
  167. if (!_File || _File->sig != _SIGNATURE) return _ftelli64((FILE*)_File);
  168. CriticalSection cs(_File->cs);
  169. unsigned long long pos;
  170. return SUCCEEDED(_File->stream->Seek(liZero, STREAM_SEEK_CUR, (PULARGE_INTEGER)&pos)) ? pos : -1;
  171. }
  172. extern "C"
  173. size_t WRT_ftell(WRT_FILE * _File)
  174. {
  175. return (size_t)WRT_ftelli64(_File);
  176. }
  177. extern "C"
  178. size_t WRT_fread(void * _DstBuf, size_t _ElementSize, size_t _Count, WRT_FILE * _File)
  179. {
  180. if (!_File || _File->sig != _SIGNATURE) return fread(_DstBuf, _ElementSize, _Count, (FILE*)_File);
  181. if (!_File->readable || _ElementSize == 0 || _Count == 0 || !_DstBuf) return 0;
  182. CriticalSection cs(_File->cs);
  183. unsigned long cbRead;
  184. return SUCCEEDED(_File->stream->Read(_DstBuf, _ElementSize * _Count, &cbRead)) ? cbRead / _ElementSize : 0;
  185. }
  186. extern "C"
  187. size_t WRT_fwrite(const void * _Str, size_t _Size, size_t _Count, WRT_FILE * _File)
  188. {
  189. if (!_File || _File->sig != _SIGNATURE) return fwrite(_Str, _Size, _Count, (FILE*)_File);
  190. if (!_File->writable || !_Str || _Size == 0 || _Count == 0) return 0;
  191. CriticalSection cs(_File->cs);
  192. unsigned long cbWrite;
  193. return SUCCEEDED(_File->stream->Write(_Str, _Size * _Count, &cbWrite)) ? cbWrite / _Size : 0;
  194. }
  195. extern "C"
  196. int WRT_fflush(WRT_FILE * _File)
  197. {
  198. if (!_File || _File->sig != _SIGNATURE) return fflush((FILE*)_File);
  199. if (!_File->writable) return 0;
  200. CriticalSection cs(_File->cs);
  201. return SUCCEEDED(_File->stream->Commit(STGC_DEFAULT)) ? 0 : EOF;
  202. }
  203. static int WRT_fgetc_nolock(WRT_FILE * _File)
  204. {
  205. unsigned long cbRead;
  206. unsigned char buf;
  207. return _File->stream->Read(&buf, 1, &cbRead) == S_OK ? buf : EOF;
  208. }
  209. extern "C"
  210. int WRT_fgetc(WRT_FILE * _File)
  211. {
  212. if (!_File || _File->sig != _SIGNATURE) return fgetc((FILE*)_File);
  213. CriticalSection cs(_File->cs);
  214. return _File->readable ? WRT_fgetc_nolock(_File) : EOF;
  215. }
  216. extern "C"
  217. char* WRT_fgets(char * _Buf, int _MaxCount, WRT_FILE * _File)
  218. {
  219. if (!_File || _File->sig != _SIGNATURE) return fgets(_Buf, _MaxCount, (FILE*)_File);
  220. if (!_File->readable || !_Buf || _MaxCount <= 0) return nullptr;
  221. CriticalSection cs(_File->cs);
  222. int n = 0, flag = 0, ch;
  223. while (n < _MaxCount - 1 && EOF != (ch = WRT_fgetc_nolock(_File)))
  224. {
  225. if (flag)
  226. {
  227. if (ch != '\n')
  228. _Buf[n++] = '\r';
  229. flag = 0;
  230. }
  231. if (ch != '\r')
  232. {
  233. _Buf[n++] = ch;
  234. if (ch == '\n') break;
  235. }
  236. else
  237. flag = 1;
  238. }
  239. if (n > 0)
  240. {
  241. _Buf[n] = '\0';
  242. return _Buf;
  243. }
  244. else
  245. return nullptr;
  246. }
  247. extern "C"
  248. int WRT_fputc(int _Ch, WRT_FILE * _File)
  249. {
  250. if (!_File || _File->sig != _SIGNATURE) return fputc(_Ch, (FILE*)_File);
  251. CriticalSection cs(_File->cs);
  252. unsigned long cbWrite;
  253. return _File->writable && _File->stream->Write(&_Ch, 1, &cbWrite) == S_OK ? cbWrite : EOF;
  254. }
  255. extern "C"
  256. int WRT_fputs(const char * _Str, WRT_FILE * _File)
  257. {
  258. if (!_File || _File->sig != _SIGNATURE) return fputs(_Str, (FILE*)_File);
  259. if (!_File->writable || !_Str) return EOF;
  260. const char *start = _Str;
  261. int length = strlen(_Str);
  262. unsigned long cbWrite;
  263. const char crlf[] = { '\r', '\n' };
  264. CriticalSection cs(_File->cs);
  265. while (true)
  266. {
  267. const char *ptr = start;
  268. while (*ptr && *ptr != '\n') ptr++;
  269. if (_File->stream->Write(start, ptr - start, &cbWrite) != S_OK)
  270. return EOF;
  271. if (*ptr == '\n')
  272. {
  273. if (_File->stream->Write(crlf, 2, &cbWrite) != S_OK)
  274. return EOF;
  275. else
  276. start = ptr + 1;
  277. }
  278. else
  279. break;
  280. }
  281. return length;
  282. }