blob: 2f6e4f4514aa1304df4dcdc0234cb2abcb36723d [file] [log] [blame]
atraczykf5be5462016-08-31 14:23:06 -04001/**************************************************************************
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 Jagerb059a662016-08-30 13:17:30 -040019#pragma once
20#include <pch.h>
21
22using namespace Platform;
23using namespace Windows::Storage;
24
25namespace RingClientUWP
26{
27namespace Utils
28{
29
30task<bool>
31fileExists(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
53std::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
68std::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
83std::string toString(Platform::String ^str)
84{
85 std::wstring wsstr(str->Data());
86 return makeString(wsstr);
87}
88
89Platform::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 Jager2647eaa2016-08-31 12:08:15 -040095Platform::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 Jagerf494bda2016-09-16 08:43:43 -0400109/* fix some issue in the daemon --> <...@...> */
110Platform::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
atraczyk5c395ea2016-09-20 17:28:09 -0400127/* fix some issue in the daemon --> remove "@..." */
128Platform::String^ TrimRingId2(Platform::String^ s)
129{
130 const WCHAR* first = s->Begin();
131 const WCHAR* last = s->End();
132
133 while (first != last && last[-1] != '@')
134 --last;
135
136 last--;
137
138 return ref new Platform::String(first, static_cast<unsigned int>(last - first));
139}
140
atraczykf5be5462016-08-31 14:23:06 -0400141Platform::String^ GetNewGUID()
142{
143 GUID result;
144 HRESULT hr = CoCreateGuid(&result);
145
146 if (SUCCEEDED(hr)) {
147 Guid guid(result);
148 return guid.ToString();
149 }
150
151 throw Exception::CreateException(hr);
Nicolas Jagerb059a662016-08-30 13:17:30 -0400152}
atraczykf5be5462016-08-31 14:23:06 -0400153
atraczyk21e55dd2016-09-19 15:46:55 -0400154std::string
155getStringFromFile(const std::string& filename)
156{
157 std::ifstream file(filename);
158 return std::string((std::istreambuf_iterator<char>(file)),
159 (std::istreambuf_iterator<char>()));
160}
161
atraczykf5be5462016-08-31 14:23:06 -0400162}
163}