-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjsl-data.cpp
558 lines (457 loc) · 11.3 KB
/
jsl-data.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
jsl-data.cpp
This scource file is part of the jsl-esp32 project.
Author: Lorenzo Pastrana
Copyright © 2019 Lorenzo Pastrana
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see http://www.gnu.org/licenses/.
*/
#include <cstdlib>
#include <sstream>
#include <algorithm>
#define LOG_LOCAL_LEVEL ESP_LOG_NONE
// #define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
constexpr char DATA_LOGTAG[] = "DATA :";
#include <esp_log.h>
#include "jsl-data.h"
jsl_data::~jsl_data()
{
clear();
}
std::string jsl_data::escape(const std::string& _str)
{
std::string str = "";
for(auto i = _str.begin(); i != _str.end(); ++ i)
{
switch(*i)
{
case '\t':
str.push_back('\\');
str.push_back('t');
break;
case '\n':
str.push_back('\\');
str.push_back('n');
break;
case '\r':
str.push_back('\\');
str.push_back('r');
break;
case '\f':
str.push_back('\\');
str.push_back('f');
break;
case '\b':
str.push_back('\\');
str.push_back('b');
break;
case '\0':
str.push_back('\\');
str.push_back('0');
break;
case '\'':
str.push_back('\\');
str.push_back('\'');
break;
case '\"':
str.push_back('\\');
str.push_back('"');
break;
case '\\':
str.push_back('\\');
str.push_back('\\');
break;
// case '/':
// str.push_back('\\');
// str.push_back('/');
// break;
default:
str.push_back(*i);
}
}
return str;
}
std::string jsl_data::to_string(bool _val)
{
std::ostringstream s;
s << ((_val == true) ? "true" : "false");
return s.str();
}
std::string jsl_data::to_string(int32_t _val)
{
std::ostringstream s;
s << _val;
return s.str();
}
std::string jsl_data::to_string(double _val)
{
std::ostringstream s;
s << _val;
return s.str();
}
std::string jsl_data::to_string(const std::string& _val)
{
return to_string(_val.c_str());
}
std::string jsl_data::to_string(const char* _val)
{
return "\"" + escape(_val) + "\"";
}
jsl_data_scal::~jsl_data_scal()
{
clear();
}
void jsl_data_scal::clear()
{
clearStr();
m_type = TYPE_NULL;
m_scal.i = 0;
jsl_data::clear();
}
void jsl_data_scal::fire()
{
jsl_data_pool::fire(*this);
}
jsl_data_scal& jsl_data_scal::from_string(const char* _str)
{
switch (m_type)
{
case TYPE_INT: {
std::stringstream s(_str);
s >> m_scal.i;
break;
}
case TYPE_REAL: { /* -?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)? */
std::stringstream s(_str);
s >> m_scal.d;
break;
}
case TYPE_BOOL:
m_scal.b = (std::string("true") == _str) || (std::string("TRUE") == _str);
break;
case TYPE_STR:
m_scal.s = _str;
break;
default:; // prevent compiler from complaining about other cases
}
return *this;
}
std::string jsl_data_scal::to_string() const
{
switch (m_type)
{
case TYPE_NULL:
return "null";
case TYPE_INT:
return jsl_data::to_string(m_scal.i);
case TYPE_REAL:
return jsl_data::to_string(m_scal.d);
case TYPE_BOOL:
return jsl_data::to_string(m_scal.b);
case TYPE_STR:
return jsl_data::to_string(m_scal.s);
default:
return "";
}
}
const int32_t jsl_data_scal::empty_int = 0;
const double jsl_data_scal::empty_double = 0.;
const bool jsl_data_scal::empty_bool = false;
const std::string jsl_data_scal::empty_str;
jsl_data_dict::~jsl_data_dict()
{
clear();
}
void jsl_data_dict::clear()
{
m_container.clear(); //erase(m_container.begin(),m_container.end());
jsl_data::clear();
}
void jsl_data_dict::fire()
{
jsl_data_pool::fire(*this);
}
void jsl_data_dict::removeChild(const jsl_data& _child)
{
for(auto found = m_container.begin(); found != m_container.end(); ++found)
{
if(found->second == &_child)
{
m_container.erase(found);
return;
}
}
}
void jsl_data_dict::encode(std::ostream& _out, bool _pretty, std::string _tabs) const
{
_out << "{";
char nl = '\0', sp = '\0';
if(_pretty)
{
sp = ' ';
_tabs += '\t';
_out << (nl = '\n');
}
for(auto i = m_container.begin(); i != m_container.end(); ++i)
{
_out << _tabs << '\"' << escape(i->first) << "\":" << sp;
i->second->encode(_out,_pretty,_tabs);
if(i != std::prev(m_container.end())) _out << ',';
_out << nl;
}
if(_pretty) _tabs.pop_back();
_out << _tabs << "}";
if(_pretty && _tabs.size() == 0) _out << nl;
}
jsl_data_vect::~jsl_data_vect()
{
clear();
}
void jsl_data_vect::clear()
{
vect_t().swap(m_container);
jsl_data::clear();
}
void jsl_data_vect::fire()
{
jsl_data_pool::fire(*this);
}
void jsl_data_vect::removeChild(const jsl_data& _child)
{
for(auto found = m_container.begin(); found != m_container.end(); ++found)
{
if(*found == &_child)
{
m_container.erase(found);
return;
}
}
}
void jsl_data_vect::encode(std::ostream& _out, bool _pretty, std::string _tabs) const
{
_out << "{";
char nl = '\0';
if(_pretty)
{
_tabs += '\t';
_out << (nl = '\n');
}
for(auto i = m_container.begin(); i != m_container.end(); ++i)
{
_out << _tabs;
(*i)->encode(_out,_pretty,_tabs);
if(i != std::prev(m_container.end())) _out << ',';
_out << nl;
}
if(_pretty) _tabs.pop_back();
_out << _tabs << "]";
if(_pretty && _tabs.size() == 0) _out << nl;
}
void jsl_data_pool::init(uint16_t _s, uint16_t _d, uint16_t _v)
{
ESP_LOGI("DATA :","JSL_DATA_POOL::INIT");
std::vector<jsl_data_scal>().swap(m_scals);
std::vector<jsl_data_scal*>().swap(m_scals_for_hire);
if(_s != 0)
{
m_scals.resize(_s);
auto s = m_scals.begin();
while(s != m_scals.end())
{
m_scals_for_hire.push_back(&(*s++));
}
}
// ESP_LOGI(DATA_LOGTAG,"Scal pool_init %d => [%d:%d]",_s,m_scals.size(),m_scals_for_hire.size());
std::vector<jsl_data_dict>().swap(m_dicts);
std::vector<jsl_data_dict*>().swap(m_dicts_for_hire);
if(_d != 0)
{
m_dicts.resize(_d);
auto d = m_dicts.begin();
while(d != m_dicts.end())
{
m_dicts_for_hire.push_back(&(*d++));
}
}
// ESP_LOGI(DATA_LOGTAG,"Dict pool_init %d => [%d:%d]",_d,m_dicts.size(),m_dicts_for_hire.size());
std::vector<jsl_data_vect>().swap(m_vects);
std::vector<jsl_data_vect*>().swap(m_vects_for_hire);
if(_v != 0)
{
m_vects.resize(_v);
auto v = m_vects.begin();
while(v != m_vects.end())
{
m_vects_for_hire.push_back(&(*v++));
}
}
// ESP_LOGI(DATA_LOGTAG,"Vect pool_init %d => [%d:%d]",_v,m_vects.size(),m_vects_for_hire.size());
}
jsl_data_scal* jsl_data_pool::hire(int32_t _i)
{
jsl_data_scal* data = hire_scal();
(*data) = _i;
return data;
}
jsl_data_scal* jsl_data_pool::hire(double _d)
{
jsl_data_scal* data = hire_scal();
(*data) = _d;
return data;
}
jsl_data_scal* jsl_data_pool::hire(bool _b)
{
jsl_data_scal* data = hire_scal();
(*data) = _b;
return data;
}
jsl_data_scal* jsl_data_pool::hire(const std::string& _s)
{
jsl_data_scal* data = hire_scal();
(*data) = _s;
return data;
}
jsl_data_scal* jsl_data_pool::hire(const char* _s)
{
jsl_data_scal* data = hire_scal();
(*data) = _s;
return data;
}
jsl_data_scal* jsl_data_pool::hire_scal()
{
if(m_scals_for_hire.size() == 0)
{
return nullptr;
}
jsl_data_scal* data;
data = m_scals_for_hire.back();
m_scals_for_hire.pop_back();
return data;
}
void jsl_data_pool::fire(jsl_data_scal& _data)
{
_data.clear();
if(std::find(m_scals_for_hire.begin(),m_scals_for_hire.end(),&_data) == m_scals_for_hire.end())
{
m_scals_for_hire.push_back(&_data);
}
}
jsl_data_dict* jsl_data_pool::hire_dict()
{
if(m_dicts_for_hire.size() == 0)
{
return nullptr;
}
jsl_data_dict* data;
data = m_dicts_for_hire.back();
m_dicts_for_hire.pop_back();
return data;
}
void jsl_data_pool::fire(jsl_data_dict& _data)
{
for(auto child = _data.begin(); child != _data.end(); ++child)
{
if(child->second != nullptr)
{
child->second->fire();
}
}
_data.clear();
if(std::find(m_dicts_for_hire.begin(),m_dicts_for_hire.end(),&_data) == m_dicts_for_hire.end())
{
m_dicts_for_hire.push_back(&_data);
}
}
jsl_data_vect* jsl_data_pool::hire_vect()
{
if(m_vects_for_hire.size() == 0)
{
return nullptr;
}
jsl_data_vect* data;
data = m_vects_for_hire.back();
m_vects_for_hire.pop_back();
return data;
}
void jsl_data_pool::fire(jsl_data_vect& _data)
{
for(auto child = _data.begin(); child != _data.end(); ++child)
{
if((*child) != nullptr)
{
(*child)->fire();
}
}
_data.clear();
if(std::find(m_vects_for_hire.begin(),m_vects_for_hire.end(),&_data) == m_vects_for_hire.end())
{
m_vects_for_hire.push_back(&_data);
}
}
std::vector<jsl_data_scal> jsl_data_pool::m_scals;
std::vector<jsl_data_scal*> jsl_data_pool::m_scals_for_hire;
std::vector<jsl_data_dict> jsl_data_pool::m_dicts;
std::vector<jsl_data_dict*> jsl_data_pool::m_dicts_for_hire;
std::vector<jsl_data_vect> jsl_data_pool::m_vects;
std::vector<jsl_data_vect*> jsl_data_pool::m_vects_for_hire;
/*
void test_data()
{
ESP_LOGI(LOGTAG, "Test DATA");
ESP_LOGI(LOGTAG, "Data 2 : BOOL");
jsl_data_scal& b = *jsl_data_pool::hire(true);
b.encode(std::cout,true);// << "\n";
ESP_LOGI(LOGTAG, "Data 1 : REAL");
jsl_data_scal& d = *jsl_data_pool::hire(2.3);
d.encode(std::cout,true);// << "\n";
ESP_LOGI(LOGTAG, "Data 0 : INT");
jsl_data_scal& i = *jsl_data_pool::hire(1);
i.encode(std::cout,true);// << "\n";
ESP_LOGI(LOGTAG, "Data 3 : STR");
jsl_data_scal& s = *jsl_data_pool::hire("ok éô à");
s.encode(std::cout,true);// << "\n";
ESP_LOGI(LOGTAG, "Data 2 : Retype to STR");
b = "b to s"; // re-type test
b.encode(std::cout,true);// << "\n";
ESP_LOGI(LOGTAG, "Data 2 : Retype to BOOL");
b = true; // re-type test
b.encode(std::cout,true);// << "\n";
ESP_LOGI(LOGTAG, "Data 4 : DICT");
// jsl_data_dict& dict = *((jsl_data_dict*)jsl_data_pool::hire(jsl_data::TYPE_DICT));
jsl_data_dict& dict = *jsl_data_pool::hire_dict();
dict.set_prop("i",i);
// dict.set_prop("d",d);
dict.set_prop("b",b);
dict.set_prop("s",s);
ESP_LOGI(LOGTAG, "Data 5 : VECT");
// jsl_data_vect& vect = *((jsl_data_vect*)jsl_data_pool::hire(jsl_data::TYPE_VECT));
jsl_data_vect& vect = *jsl_data_pool::hire_vect();
// vect.push_back(i);
vect.push_back(d);
// vect.push_back(b);
// vect.push_back(s);
ESP_LOGI(LOGTAG, "Data 2 : From STR");
d.from_string("2.3456");
ESP_LOGI(LOGTAG, "Data 4 : VECT Child");
dict.set_prop("v",vect);
ESP_LOGI(LOGTAG, "Data 4 : ENCODE");
dict.encode(std::cout,true);// << "\n\n";
// vect.push_back(&dict);
// vect.encode(std::cout,true);
ESP_LOGI(LOGTAG, "Data FIRE");
jsl_data_pool::fire(i);
jsl_data_pool::fire(d);
jsl_data_pool::fire(b);
jsl_data_pool::fire(s);
jsl_data_pool::fire(dict);
jsl_data_pool::fire(vect);
}
*/