forked from neutralinojs/neutralinojs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.cpp
More file actions
205 lines (176 loc) · 5.01 KB
/
Copy pathhelpers.cpp
File metadata and controls
205 lines (176 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <chrono>
#include <iomanip>
#include <ctype.h>
#include <random>
#include <optional>
#include "helpers.h"
#include "lib/json/json.hpp"
#if defined(_WIN32)
#include <string>
#include <windows.h>
#endif
using namespace std;
using json = nlohmann::json;
namespace helpers {
vector<string> split(const string &s, char delim, unsigned int stopAfter) {
stringstream ss(s);
string item;
vector<string> tokens;
while (getline(ss, item, delim)) {
tokens.push_back(item);
if(stopAfter !=-1 && tokens.size() == stopAfter - 1) {
delim = '\n';
}
}
return tokens;
}
vector<string> splitTwo(const string &s, char delim) {
return split(s, delim, 2);
}
string generateToken() {
static const string characters =
"-_"
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
const int length = 96;
string token;
token.reserve(length);
random_device rd;
mt19937 generator(rd());
uniform_int_distribution<int> distribution(0, characters.size() - 1);
for (int i = 0; i < length; ++i) {
if (i == 47) {
token += ".";
}
token += characters[distribution(generator)];
}
return token;
}
/*
* https://stackoverflow.com/a/14530993 - mini url decoder
*/
void urldecode(char *dst, const char *src) {
char a, b;
while (*src) {
if ((*src == '%') &&
((a = src[1]) && (b = src[2])) &&
(isxdigit(a) && isxdigit(b))) {
if (a >= 'a')
a -= 'a' - 'A';
if (a >= 'A')
a -= ('A' - 10);
else
a -= '0';
if (b >= 'a')
b -= 'a' - 'A';
if (b >= 'A')
b -= ('A' - 10);
else
b -= '0';
*dst++ = 16 * a + b;
src += 3;
}
else {
*dst++ = *src++;
}
}
*dst++ = '\0';
}
char* cStrCopy(const string &str) {
char *text = new char[str.size() + 1];
copy(str.begin(), str.end(), text);
text[str.size()] = '\0';
// delete[] text from the initiator
return text;
}
bool hasRequiredFields(const json &input, const vector<string> &keys) {
for(const string &key: keys) {
if(!helpers::hasField(input, key)) {
return false;
}
}
return true;
}
optional<string> missingRequiredField(const json &input, const vector<string> &keys) {
for(const string &key: keys) {
if(!helpers::hasField(input, key)) {
return key;
}
}
return {};
}
bool hasField(const json &input, const string &key) {
return input.contains(key) && !input[key].is_null();
}
vector<string> getModes() {
return {"window", "browser", "cloud", "chrome"};
}
string appModeToStr(settings::AppMode mode) {
switch(mode) {
case settings::AppModeWindow:
return "window";
case settings::AppModeBrowser:
return "browser";
case settings::AppModeCloud:
return "cloud";
case settings::AppModeChrome:
return "chrome";
default:
return "invalid";
}
}
string getCurrentTimestamp() {
auto now = chrono::system_clock::now();
auto nowTimeT = chrono::system_clock::to_time_t(now);
auto nowMs = chrono::duration_cast<chrono::milliseconds>(
now.time_since_epoch())
.count() % 1000;
ostringstream oss;
oss << put_time(gmtime(&nowTimeT), "%Y-%m-%dT%H:%M:%S")
<< "." << setfill('0') << setw(3) << nowMs << "Z";
return oss.str();
}
string normalizePath(string &path) {
#if defined(_WIN32)
replace(path.begin(), path.end(), '\\', '/');
#endif
if(path.size() > 1 && path.back() == '/') {
path.pop_back();
}
return path;
}
string unNormalizePath(string &path) {
#if defined(_WIN32)
replace(path.begin(), path.end(), '/', '\\');
#endif
return path;
}
string jsonToString(const json &obj) {
return obj.dump(-1, ' ', false, json::error_handler_t::replace);
}
#if defined(_WIN32)
wstring str2wstr(const string &str) {
int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), nullptr, 0);
wstring ret(len, '\0');
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), (LPWSTR)ret.data(), (int)ret.size());
return ret;
}
string wstr2str(const wstring &str) {
int len = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), (int)str.size(), nullptr, 0, nullptr, nullptr);
string ret(len, '\0');
WideCharToMultiByte(CP_UTF8, 0, str.c_str(), (int)str.size(), (LPSTR)ret.data(), (int)ret.size(), nullptr, nullptr);
return ret;
}
string wcstr2str(const wchar_t* wstr) {
int count = WideCharToMultiByte(CP_UTF8, 0, wstr, wcslen(wstr), NULL, 0, NULL, NULL);
string str(count, 0);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &str[0], count, NULL, NULL);
return str;
}
#endif
} // namespace helpers