forked from neutralinojs/neutralinojs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.cpp
141 lines (122 loc) · 3.29 KB
/
helpers.cpp
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
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <time.h>
#include <ctype.h>
#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) {
stringstream ss(s);
string item;
vector<string> tokens;
while (getline(ss, item, delim)) {
tokens.push_back(item);
}
return tokens;
}
string generateToken() {
srand(time(NULL));
string s = "";
static const char alphanum[] =
"-_"
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < 96; ++i) {
s += alphanum[rand() % (sizeof(alphanum) - 1)];
if(i == 47) {
s += ".";
}
}
return s;
}
/*
* 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 if (*src == '+') {
*dst++ = ' ';
src++;
}
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;
}
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 normalizePath(string &path) {
#if defined(_WIN32)
replace(path.begin(), path.end(), '\\', '/');
#endif
return path;
}
#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