blob: 05d54270b6132b02c7aecc2af3d95c7cbc611bd9 [file] [log] [blame]
Nicolas Jagerb059a662016-08-30 13:17:30 -04001#pragma once
2#include <pch.h>
3
4using namespace Platform;
5using namespace Windows::Storage;
6
7namespace RingClientUWP
8{
9namespace Utils
10{
11
12task<bool>
13fileExists(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
35std::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
50std::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
65std::string toString(Platform::String ^str)
66{
67 std::wstring wsstr(str->Data());
68 return makeString(wsstr);
69}
70
71Platform::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 Jagereeef17c2016-08-16 10:21:54 -040078}