Nicolas Jager | b059a66 | 2016-08-30 13:17:30 -0400 | [diff] [blame^] | 1 | #pragma once
|
| 2 | #include <pch.h>
|
| 3 |
|
| 4 | using namespace Platform;
|
| 5 | using namespace Windows::Storage;
|
| 6 |
|
| 7 | namespace RingClientUWP
|
| 8 | {
|
| 9 | namespace Utils
|
| 10 | {
|
| 11 |
|
| 12 | task<bool>
|
| 13 | fileExists(StorageFolder^ folder, String^ fileName)
|
| 14 | {
|
| 15 | return create_task(folder->GetFileAsync(fileName))
|
| 16 | .then([](task<StorageFile^> taskResult)
|
| 17 | {
|
| 18 | bool exists;
|
| 19 | try {
|
| 20 | taskResult.get();
|
| 21 | exists = true;
|
| 22 | }
|
| 23 | catch (COMException ^e) {
|
| 24 | if (e->HResult == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) {
|
| 25 | exists = false;
|
| 26 | }
|
| 27 | else {
|
| 28 | throw;
|
| 29 | }
|
| 30 | }
|
| 31 | return exists;
|
| 32 | });
|
| 33 | }
|
| 34 |
|
| 35 | std::string makeString(const std::wstring& wstr)
|
| 36 | {
|
| 37 | auto wideData = wstr.c_str();
|
| 38 | int bufferSize = WideCharToMultiByte(CP_UTF8, 0, wideData, -1, nullptr, 0, NULL, NULL);
|
| 39 |
|
| 40 | std::unique_ptr<char[]> utf8;
|
| 41 | utf8.reset(new char[bufferSize]);
|
| 42 |
|
| 43 | if (WideCharToMultiByte(CP_UTF8, 0, wideData, -1, utf8.get(), bufferSize, NULL, NULL) == 0) {
|
| 44 | return std::string();
|
| 45 | }
|
| 46 |
|
| 47 | return std::string(utf8.get());
|
| 48 | }
|
| 49 |
|
| 50 | std::wstring makeWString(const std::string& str)
|
| 51 | {
|
| 52 | auto utf8Data = str.c_str();
|
| 53 | int bufferSize = MultiByteToWideChar(CP_UTF8, 0, utf8Data, -1, nullptr, 0);
|
| 54 |
|
| 55 | std::unique_ptr<wchar_t[]> wide;
|
| 56 | wide.reset(new wchar_t[bufferSize]);
|
| 57 |
|
| 58 | if (MultiByteToWideChar(CP_UTF8, 0, utf8Data, -1, wide.get(), bufferSize) == 0) {
|
| 59 | return std::wstring();
|
| 60 | }
|
| 61 |
|
| 62 | return std::wstring(wide.get());;
|
| 63 | }
|
| 64 |
|
| 65 | std::string toString(Platform::String ^str)
|
| 66 | {
|
| 67 | std::wstring wsstr(str->Data());
|
| 68 | return makeString(wsstr);
|
| 69 | }
|
| 70 |
|
| 71 | Platform::String^ toPlatformString(const std::string& str)
|
| 72 | {
|
| 73 | std::wstring wsstr = makeWString(str);
|
| 74 | return ref new Platform::String(wsstr.c_str(), wsstr.length());
|
| 75 | }
|
| 76 |
|
| 77 | }
|
Nicolas Jager | eeef17c | 2016-08-16 10:21:54 -0400 | [diff] [blame] | 78 | } |