-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
76 lines (70 loc) · 2.1 KB
/
utils.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
#include "utils.h"
// ========================================================================================================
// URL Decode
// See: https://circuits4you.com/2019/03/21/esp8266-url-encode-decode-example/
// ========================================================================================================
String urldecode(String str){
String encodedString="";
char c;
char code0;
char code1;
for (int i =0; i < str.length(); i++){
c=str.charAt(i);
if (c == '+'){
encodedString+=' ';
}else if (c == '%') {
i++;
code0=str.charAt(i);
i++;
code1=str.charAt(i);
c = (h2int(code0) << 4) | h2int(code1);
encodedString+=c;
} else{
encodedString+=c;
}
yield();
}
return encodedString;
}
unsigned char h2int(char c){
if (c >= '0' && c <='9'){
return((unsigned char)c - '0');
}
if (c >= 'a' && c <='f'){
return((unsigned char)c - 'a' + 10);
}
if (c >= 'A' && c <='F'){
return((unsigned char)c - 'A' + 10);
}
return(0);
}
// ========================================================================================================
// Tokenizing strings
// See: https://forum.arduino.cc/t/how-to-split-a-string-with-space-and-store-the-items-in-array/888813/8
// ========================================================================================================
bool get_token(String &from, String &to, uint8_t index, char separator)
{
uint16_t start = 0, idx = 0;
uint8_t cur = 0;
while (idx < from.length())
{
if (from.charAt(idx) == separator)
{
if (cur == index)
{
to = from.substring(start, idx);
return true;
}
cur++;
while ((idx < from.length() - 1) && (from.charAt(idx + 1) == separator)) idx++;
start = idx + 1;
}
idx++;
}
if ((cur == index) && (start < from.length()))
{
to = from.substring(start, from.length());
return true;
}
return false;
}