forked from sticilface/ESPmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESPMAN.cpp
177 lines (132 loc) · 4.09 KB
/
ESPMAN.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
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
#include "ESPMAN.h"
int ESPMAN::JSONpackage::parse(char * data, int size) {
using namespace ESPMAN;
if (!data) {
return EMPTY_BUFFER;
}
if (_isArray) {
_root = _jsonBuffer.parseArray(_data.get(),size);
} else {
_root = _jsonBuffer.parseObject(_data.get(),size);
}
if (!_root.success()) {
return JSON_PARSE_ERROR;
}
return 0;
}
int ESPMAN::JSONpackage::parseSPIFS(const char * file, FS & fs) {
using namespace ESPMAN;
File f = fs.open(file, "r");
int totalBytes = f.size();
if (!f) {
return SPIFFS_FILE_OPEN_ERROR;
}
if (totalBytes > MAX_BUFFER_SIZE) {
return FILE_TOO_LARGE;
}
_data = std::unique_ptr<char[]>(new char[totalBytes]);
if (!_data) {
return MALLOC_FAIL;
}
int position = 0;
int bytesleft = totalBytes;
while ((f.available() > -1) && (bytesleft > 0)) {
// get available data size
int sizeAvailable = f.available();
if (sizeAvailable) {
int readBytes = sizeAvailable;
// read only the asked bytes
if (readBytes > bytesleft) {
readBytes = bytesleft;
}
// get new position in buffer
char * buf = &_data.get()[position];
// read data
int bytesread = f.readBytes(_data.get(), readBytes);
bytesleft -= bytesread;
position += bytesread;
}
// time for network streams
delay(0);
}
f.close();
if (_isArray) {
_root = _jsonBuffer.parseArray(_data.get(),totalBytes);
} else {
_root = _jsonBuffer.parseObject(_data.get(),totalBytes);
}
if (!_root.success()) {
return JSON_PARSE_ERROR;
}
return 0;
}
ESPMAN::myString::myString(const char *cstr)
{
if (cstr) {
buffer = strdup(cstr);
}
}
ESPMAN::myString::myString(const ESPMAN::myString &str)
{
//Serial.print("[ESPMAN::myString::myString(const ESPMAN::myString &str)]\n");
if (buffer) {
free(buffer);
buffer = nullptr;
}
if (str.buffer) {
buffer = strdup(str.buffer);
}
}
ESPMAN::myString & ESPMAN::myString::operator =(const char *cstr)
{
//Serial.printf("[ESPMAN::myString::operator =(const char *cstr)] cstr = %s\n", (cstr)? cstr : "null");
if (buffer) { free(buffer); }
if (cstr) {
buffer = strdup(cstr);
} else {
buffer = nullptr;
}
return *this;
}
ESPMAN::myString & ESPMAN::myString::operator =(const myString &rhs)
{
//Serial.printf("[ESPMAN::myString::operator =(const myString &str)] rhs = %s\n", (rhs.buffer)? rhs.buffer : "null" );
if (buffer) { free(buffer); }
if (rhs.buffer) {
buffer = strdup(rhs.buffer);
} else {
buffer = nullptr;
}
return *this;
}
ESPMAN::myString::operator bool() const {
return (buffer && strlen(buffer) > 0);
}
// untested
bool ESPMAN::myString::operator ==(const myString &rhs)
{
if (!rhs.buffer && !buffer) {
return true;
}
if (rhs.buffer && buffer) {
return strcmp(buffer, rhs.buffer) == 0;
}
return false;
}
// untested
bool ESPMAN::myString::operator ==(char * cstr)
{
if (!cstr && !buffer) {
return true;
}
if (cstr && buffer) {
return strcmp(buffer, cstr) == 0;
}
return false;
}
ESPMAN::myString::~myString() {
if (buffer) {
//Serial.printf("[ESPMAN::myString::~myString()] %s\n", buffer);
free(buffer);
}
}