atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 1 | /**************************************************************************
|
| 2 | * Copyright (C) 2016 by Savoir-faire Linux *
|
| 3 | * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
|
| 4 | * Author: Traczyk Andreas <traczyk.andreas@savoirfairelinux.com> *
|
| 5 | * *
|
| 6 | * This program is free software; you can redistribute it and/or modify *
|
| 7 | * it under the terms of the GNU General Public License as published by *
|
| 8 | * the Free Software Foundation; either version 3 of the License, or *
|
| 9 | * (at your option) any later version. *
|
| 10 | * *
|
| 11 | * This program is distributed in the hope that it will be useful, *
|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
| 14 | * GNU General Public License for more details. *
|
| 15 | * *
|
| 16 | * You should have received a copy of the GNU General Public License *
|
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
| 18 | **************************************************************************/
|
Nicolas Jager | b059a66 | 2016-08-30 13:17:30 -0400 | [diff] [blame] | 19 | #pragma once
|
| 20 | #include <pch.h>
|
| 21 |
|
| 22 | using namespace Platform;
|
| 23 | using namespace Windows::Storage;
|
| 24 |
|
| 25 | namespace RingClientUWP
|
| 26 | {
|
| 27 | namespace Utils
|
| 28 | {
|
| 29 |
|
| 30 | task<bool>
|
| 31 | fileExists(StorageFolder^ folder, String^ fileName)
|
| 32 | {
|
| 33 | return create_task(folder->GetFileAsync(fileName))
|
| 34 | .then([](task<StorageFile^> taskResult)
|
| 35 | {
|
| 36 | bool exists;
|
| 37 | try {
|
| 38 | taskResult.get();
|
| 39 | exists = true;
|
| 40 | }
|
| 41 | catch (COMException ^e) {
|
| 42 | if (e->HResult == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) {
|
| 43 | exists = false;
|
| 44 | }
|
| 45 | else {
|
| 46 | throw;
|
| 47 | }
|
| 48 | }
|
| 49 | return exists;
|
| 50 | });
|
| 51 | }
|
| 52 |
|
| 53 | std::string makeString(const std::wstring& wstr)
|
| 54 | {
|
| 55 | auto wideData = wstr.c_str();
|
| 56 | int bufferSize = WideCharToMultiByte(CP_UTF8, 0, wideData, -1, nullptr, 0, NULL, NULL);
|
| 57 |
|
| 58 | std::unique_ptr<char[]> utf8;
|
| 59 | utf8.reset(new char[bufferSize]);
|
| 60 |
|
| 61 | if (WideCharToMultiByte(CP_UTF8, 0, wideData, -1, utf8.get(), bufferSize, NULL, NULL) == 0) {
|
| 62 | return std::string();
|
| 63 | }
|
| 64 |
|
| 65 | return std::string(utf8.get());
|
| 66 | }
|
| 67 |
|
| 68 | std::wstring makeWString(const std::string& str)
|
| 69 | {
|
| 70 | auto utf8Data = str.c_str();
|
| 71 | int bufferSize = MultiByteToWideChar(CP_UTF8, 0, utf8Data, -1, nullptr, 0);
|
| 72 |
|
| 73 | std::unique_ptr<wchar_t[]> wide;
|
| 74 | wide.reset(new wchar_t[bufferSize]);
|
| 75 |
|
| 76 | if (MultiByteToWideChar(CP_UTF8, 0, utf8Data, -1, wide.get(), bufferSize) == 0) {
|
| 77 | return std::wstring();
|
| 78 | }
|
| 79 |
|
| 80 | return std::wstring(wide.get());;
|
| 81 | }
|
| 82 |
|
| 83 | std::string toString(Platform::String ^str)
|
| 84 | {
|
| 85 | std::wstring wsstr(str->Data());
|
| 86 | return makeString(wsstr);
|
| 87 | }
|
| 88 |
|
| 89 | Platform::String^ toPlatformString(const std::string& str)
|
| 90 | {
|
| 91 | std::wstring wsstr = makeWString(str);
|
| 92 | return ref new Platform::String(wsstr.c_str(), wsstr.length());
|
| 93 | }
|
| 94 |
|
Nicolas Jager | 2647eaa | 2016-08-31 12:08:15 -0400 | [diff] [blame] | 95 | Platform::String^ Trim(Platform::String^ s)
|
| 96 | {
|
| 97 | const WCHAR* first = s->Begin();
|
| 98 | const WCHAR* last = s->End();
|
| 99 |
|
| 100 | while (first != last && iswspace(*first))
|
| 101 | ++first;
|
| 102 |
|
| 103 | while (first != last && iswspace(last[-1]))
|
| 104 | --last;
|
| 105 |
|
| 106 | return ref new Platform::String(first, static_cast<unsigned int>(last - first));
|
| 107 | }
|
| 108 |
|
Nicolas Jager | f494bda | 2016-09-16 08:43:43 -0400 | [diff] [blame^] | 109 | /* fix some issue in the daemon --> <...@...> */
|
| 110 | Platform::String^ TrimRingId(Platform::String^ s)
|
| 111 | {
|
| 112 | const WCHAR* first = s->Begin();
|
| 113 | const WCHAR* last = s->End();
|
| 114 |
|
| 115 | while (first != last && *first != '<')
|
| 116 | ++first;
|
| 117 |
|
| 118 | while (first != last && last[-1] != '@')
|
| 119 | --last;
|
| 120 |
|
| 121 | first++;
|
| 122 | last--;
|
| 123 |
|
| 124 | return ref new Platform::String(first, static_cast<unsigned int>(last - first));
|
| 125 | }
|
| 126 |
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 127 | Platform::String^ GetNewGUID()
|
| 128 | {
|
| 129 | GUID result;
|
| 130 | HRESULT hr = CoCreateGuid(&result);
|
| 131 |
|
| 132 | if (SUCCEEDED(hr)) {
|
| 133 | Guid guid(result);
|
| 134 | return guid.ToString();
|
| 135 | }
|
| 136 |
|
| 137 | throw Exception::CreateException(hr);
|
Nicolas Jager | b059a66 | 2016-08-30 13:17:30 -0400 | [diff] [blame] | 138 | }
|
atraczyk | f5be546 | 2016-08-31 14:23:06 -0400 | [diff] [blame] | 139 |
|
| 140 | }
|
| 141 | }
|