forked from neutralinojs/neutralinojs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.cpp
121 lines (104 loc) · 2.61 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
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <time.h>
#include <ctype.h>
#include "helpers.h"
#include "lib/json/json.hpp"
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 < 48; ++i) {
s += alphanum[rand() % (sizeof(alphanum) - 1)];
}
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;
}
json makeMissingArgErrorPayload() {
return helpers::makeErrorPayload("NE_RT_NATRTER", "Missing mandatory arguments");
}
json makeErrorPayload(const string &code, const string &message) {
json error;
error["code"] = code;
error["message"] = message;
return error;
}
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;
}
} // namespace helpers