pfodParser 5.0.1 The pfodParser library is handles commands sent from the Android pfodApp, pfodApp supports WiFi, BLE, Bluetooth and SMS connections

pfodStreamString.h
Go to the documentation of this file.
1 #ifndef PFOD_STREAM_STRING_H
2 #define PFOD_STREAM_STRING_H
3 // String Print class to capture JSON output
4 class pfodStreamString : public Stream, public String {
5 public:
6
7 size_t escapeJSON(uint8_t c) {
8 // note should test that concat actually succeeds
9 switch (c) {
10 case '"':
11 concat('\\');
12 concat('"');
13 break;
14 case '\\':
15 concat('\\');
16 concat('\\');
17 break;
18 case '/':
19 concat('\\');
20 concat('/');
21 break;
22 case '\b':
23 concat('\\');
24 concat('b');
25 break;
26 case '\f':
27 concat('\\');
28 concat('f');
29 break;
30 case '\n':
31 concat('\\');
32 concat('n');
33 break;
34 case '\r':
35 concat('\\');
36 concat('r');
37 break;
38 case '\t':
39 concat('\\');
40 concat('t');
41 break;
42 default:
43 // Handle remaining control characters (0x00-0x07, 0x0E-0x1F) with \u sequences
44 if (c <= 0x1F) {
45 concat('\\');
46 concat('u');
47 concat('0');
48 concat('0');
49 // Convert to hex
50 uint8_t high = (c >> 4) & 0x0F;
51 uint8_t low = c & 0x0F;
52 concat(high < 10 ? '0' + high : 'A' + high - 10);
53 concat(low < 10 ? '0' + low : 'A' + low - 10);
54 } else {
55 concat((char)c);
56 }
57 break;
58 }
59 return 1;
60 }
61
62 public:
63
64 size_t write(const uint8_t *data, size_t size) {
65 if (size && data) {
66 const unsigned int newlen = length() + size;
67 if (reserve(newlen + 1)) {
68 size_t n = 0;
69 while (size--) {
70 n += write(*data++); // to do the replacements
71 }
72 return n;
73 }
74 }
75 return 0;
76 }
77
78
79 size_t write(uint8_t c) {
80 size_t n = 0;
81 if (concat((char)c)) {
82 n += 1;
83 }
84 return n;
85 }
86
87 int available() {
88 return length();
89 }
90
91 int read() {
92 if (length()) {
93 char c = charAt(0);
94 remove(0, 1);
95 return c;
96 }
97 return -1;
98 }
99
100 int peek() {
101 if (length()) {
102 char c = charAt(0);
103 return c;
104 }
105 return -1;
106 }
107
108 void flush() {}
109 void clear() {
110 copy("",0);
111 }
112 };
113
114 #endif
pfodStreamString
Definition: pfodStreamString.h:4
pfodStreamString::write
size_t write(const uint8_t *data, size_t size)
Definition: pfodStreamString.h:64
pfodStreamString::write
size_t write(uint8_t c)
Definition: pfodStreamString.h:79
pfodStreamString::available
int available()
Definition: pfodStreamString.h:87
pfodStreamString::read
int read()
Definition: pfodStreamString.h:91
pfodStreamString::clear
void clear()
Definition: pfodStreamString.h:109
pfodStreamString::peek
int peek()
Definition: pfodStreamString.h:100
pfodStreamString::flush
void flush()
Definition: pfodStreamString.h:108
pfodStreamString::escapeJSON
size_t escapeJSON(uint8_t c)
Definition: pfodStreamString.h:7