28
28
#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
29
29
30
30
// private method to read stream with timeout
31
- int Stream::timedRead ()
32
- {
31
+ int Stream::timedRead () {
33
32
int c;
34
33
_startMillis = millis ();
35
34
do {
36
35
c = read ();
37
- if (c >= 0 ) return c;
38
- } while (millis () - _startMillis < _timeout);
39
- return -1 ; // -1 indicates timeout
36
+ if (c >= 0 ) {
37
+ return c;
38
+ }
39
+ } while (millis () - _startMillis < _timeout);
40
+ return -1 ; // -1 indicates timeout
40
41
}
41
42
42
43
// private method to peek stream with timeout
43
- int Stream::timedPeek ()
44
- {
44
+ int Stream::timedPeek () {
45
45
int c;
46
46
_startMillis = millis ();
47
47
do {
48
48
c = peek ();
49
- if (c >= 0 ) return c;
50
- } while (millis () - _startMillis < _timeout);
51
- return -1 ; // -1 indicates timeout
49
+ if (c >= 0 ) {
50
+ return c;
51
+ }
52
+ } while (millis () - _startMillis < _timeout);
53
+ return -1 ; // -1 indicates timeout
52
54
}
53
55
54
56
// returns peek of the next digit in the stream or -1 if timeout
55
57
// discards non-numeric characters
56
- int Stream::peekNextDigit (LookaheadMode lookahead, bool detectDecimal)
57
- {
58
+ int Stream::peekNextDigit (LookaheadMode lookahead, bool detectDecimal) {
58
59
int c;
59
60
while (1 ) {
60
61
c = timedPeek ();
61
62
62
- if ( c < 0 ||
63
- c == ' -' ||
64
- (c >= ' 0' && c <= ' 9' ) ||
65
- (detectDecimal && c == ' .' )) return c;
66
-
67
- switch ( lookahead ){
68
- case SKIP_NONE: return -1 ; // Fail code.
69
- case SKIP_WHITESPACE:
70
- switch ( c ){
71
- case ' ' :
72
- case ' \t ' :
73
- case ' \r ' :
74
- case ' \n ' : break ;
75
- default : return -1 ; // Fail code.
76
- }
77
- case SKIP_ALL:
78
- break ;
63
+ if (c < 0 || c == ' -' || (c >= ' 0' && c <= ' 9' ) || (detectDecimal && c == ' .' )) {
64
+ return c;
65
+ }
66
+
67
+ switch (lookahead) {
68
+ case SKIP_NONE: return -1 ; // Fail code.
69
+ case SKIP_WHITESPACE:
70
+ switch (c) {
71
+ case ' ' :
72
+ case ' \t ' :
73
+ case ' \r ' :
74
+ case ' \n ' : break ;
75
+ default : return -1 ; // Fail code.
76
+ }
77
+ case SKIP_ALL: break ;
79
78
}
80
79
read (); // discard non-numeric
81
80
}
@@ -89,30 +88,26 @@ void Stream::setTimeout(unsigned long timeout) // sets the maximum number of mi
89
88
_timeout = timeout;
90
89
}
91
90
92
- // find returns true if the target string is found
93
- bool Stream::find (const char *target)
94
- {
91
+ // find returns true if the target string is found
92
+ bool Stream::find (const char *target) {
95
93
return findUntil (target, strlen (target), NULL , 0 );
96
94
}
97
95
98
96
// reads data from the stream until the target string of given length is found
99
97
// returns true if target string is found, false if timed out
100
- bool Stream::find (const char *target, size_t length)
101
- {
98
+ bool Stream::find (const char *target, size_t length) {
102
99
return findUntil (target, length, NULL , 0 );
103
100
}
104
101
105
102
// as find but search ends if the terminator string is found
106
- bool Stream::findUntil (const char *target, const char *terminator)
107
- {
103
+ bool Stream::findUntil (const char *target, const char *terminator) {
108
104
return findUntil (target, strlen (target), terminator, strlen (terminator));
109
105
}
110
106
111
107
// reads data from the stream until the target string of the given length is found
112
108
// search terminated if the terminator string is found
113
109
// returns true if target string is found, false if terminated or timed out
114
- bool Stream::findUntil (const char *target, size_t targetLen, const char *terminator, size_t termLen)
115
- {
110
+ bool Stream::findUntil (const char *target, size_t targetLen, const char *terminator, size_t termLen) {
116
111
if (terminator == NULL ) {
117
112
MultiTarget t[1 ] = {{target, targetLen, 0 }};
118
113
return findMulti (t, 1 ) == 0 ;
@@ -127,57 +122,58 @@ bool Stream::findUntil(const char *target, size_t targetLen, const char *termina
127
122
// See LookaheadMode enumeration at the top of the file.
128
123
// Lookahead is terminated by the first character that is not a valid part of an integer.
129
124
// Once parsing commences, 'ignore' will be skipped in the stream.
130
- long Stream::parseInt (LookaheadMode lookahead, char ignore)
131
- {
125
+ long Stream::parseInt (LookaheadMode lookahead, char ignore) {
132
126
bool isNegative = false ;
133
127
long value = 0 ;
134
128
int c;
135
129
136
130
c = peekNextDigit (lookahead, false );
137
131
// ignore non numeric leading characters
138
- if (c < 0 )
139
- return 0 ; // zero returned if timeout
132
+ if (c < 0 ) {
133
+ return 0 ; // zero returned if timeout
134
+ }
140
135
141
- do {
142
- if ((char )c == ignore)
143
- ; // ignore this character
144
- else if (c == ' -' )
136
+ do {
137
+ if ((char )c == ignore)
138
+ ; // ignore this character
139
+ else if (c == ' -' ) {
145
140
isNegative = true ;
146
- else if (c >= ' 0' && c <= ' 9' ) // is c a digit?
141
+ } else if (c >= ' 0' && c <= ' 9' ) { // is c a digit?
147
142
value = value * 10 + c - ' 0' ;
143
+ }
148
144
read (); // consume the character we got with peek
149
145
c = timedPeek ();
150
- }
151
- while ( (c >= ' 0' && c <= ' 9' ) || (char )c == ignore );
146
+ } while ((c >= ' 0' && c <= ' 9' ) || (char )c == ignore);
152
147
153
- if (isNegative)
148
+ if (isNegative) {
154
149
value = -value;
150
+ }
155
151
return value;
156
152
}
157
153
158
154
// as parseInt but returns a floating point value
159
- float Stream::parseFloat (LookaheadMode lookahead, char ignore)
160
- {
155
+ float Stream::parseFloat (LookaheadMode lookahead, char ignore) {
161
156
bool isNegative = false ;
162
157
bool isFraction = false ;
163
158
double value = 0.0 ;
164
159
int c;
165
160
double fraction = 1.0 ;
166
161
167
162
c = peekNextDigit (lookahead, true );
168
- // ignore non numeric leading characters
169
- if (c < 0 )
170
- return 0 ; // zero returned if timeout
171
-
172
- do {
173
- if ((char )c == ignore)
174
- ; // ignore
175
- else if (c == ' -' )
163
+ // ignore non numeric leading characters
164
+ if (c < 0 ) {
165
+ return 0 ; // zero returned if timeout
166
+ }
167
+
168
+ do {
169
+ if ((char )c == ignore)
170
+ ; // ignore
171
+ else if (c == ' -' ) {
176
172
isNegative = true ;
177
- else if (c == ' .' )
173
+ } else if (c == ' .' ) {
178
174
isFraction = true ;
179
- else if (c >= ' 0' && c <= ' 9' ) { // is c a digit?
180
- if (isFraction) {
175
+ } else if (c >= ' 0' && c <= ' 9' ) { // is c a digit?
176
+ if (isFraction) {
181
177
fraction *= 0.1 ;
182
178
value = value + fraction * (c - ' 0' );
183
179
} else {
@@ -186,11 +182,11 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore)
186
182
}
187
183
read (); // consume the character we got with peek
188
184
c = timedPeek ();
189
- }
190
- while ( (c >= ' 0' && c <= ' 9' ) || (c == ' .' && !isFraction) || (char )c == ignore );
185
+ } while ((c >= ' 0' && c <= ' 9' ) || (c == ' .' && !isFraction) || (char )c == ignore);
191
186
192
- if (isNegative)
187
+ if (isNegative) {
193
188
value = -value;
189
+ }
194
190
195
191
return value;
196
192
}
@@ -200,94 +196,96 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore)
200
196
// returns the number of characters placed in the buffer
201
197
// the buffer is NOT null terminated.
202
198
//
203
- size_t Stream::readBytes (char *buffer, size_t length)
204
- {
199
+ size_t Stream::readBytes (char *buffer, size_t length) {
205
200
size_t count = 0 ;
206
201
while (count < length) {
207
202
int c = timedRead ();
208
- if (c < 0 ) break ;
203
+ if (c < 0 ) {
204
+ break ;
205
+ }
209
206
*buffer++ = (char )c;
210
207
count++;
211
208
}
212
209
return count;
213
210
}
214
211
215
-
216
212
// as readBytes with terminator character
217
213
// terminates if length characters have been read, timeout, or if the terminator character detected
218
214
// returns the number of characters placed in the buffer (0 means no valid data found)
219
215
220
- size_t Stream::readBytesUntil (char terminator, char *buffer, size_t length)
221
- {
216
+ size_t Stream::readBytesUntil (char terminator, char *buffer, size_t length) {
222
217
size_t index = 0 ;
223
218
while (index < length) {
224
219
int c = timedRead ();
225
- if (c < 0 || (char )c == terminator) break ;
220
+ if (c < 0 || (char )c == terminator) {
221
+ break ;
222
+ }
226
223
*buffer++ = (char )c;
227
224
index ++;
228
225
}
229
- return index ; // return number of characters, not including null terminator
226
+ return index ; // return number of characters, not including null terminator
230
227
}
231
228
232
- String Stream::readString ()
233
- {
229
+ String Stream::readString () {
234
230
String ret;
235
231
int c = timedRead ();
236
- while (c >= 0 )
237
- {
232
+ while (c >= 0 ) {
238
233
ret += (char )c;
239
234
c = timedRead ();
240
235
}
241
236
return ret;
242
237
}
243
238
244
- String Stream::readStringUntil (char terminator)
245
- {
239
+ String Stream::readStringUntil (char terminator) {
246
240
String ret;
247
241
int c = timedRead ();
248
- while (c >= 0 && (char )c != terminator)
249
- {
242
+ while (c >= 0 && (char )c != terminator) {
250
243
ret += (char )c;
251
244
c = timedRead ();
252
245
}
253
246
return ret;
254
247
}
255
248
256
- int Stream::findMulti ( struct Stream ::MultiTarget *targets, int tCount) {
249
+ int Stream::findMulti (struct Stream ::MultiTarget *targets, int tCount) {
257
250
// any zero length target string automatically matches and would make
258
251
// a mess of the rest of the algorithm.
259
- for (struct MultiTarget *t = targets; t < targets+ tCount; ++t) {
260
- if (t->len <= 0 )
252
+ for (struct MultiTarget *t = targets; t < targets + tCount; ++t) {
253
+ if (t->len <= 0 ) {
261
254
return t - targets;
255
+ }
262
256
}
263
257
264
258
while (1 ) {
265
259
int c = timedRead ();
266
- if (c < 0 )
260
+ if (c < 0 ) {
267
261
return -1 ;
262
+ }
268
263
269
- for (struct MultiTarget *t = targets; t < targets+ tCount; ++t) {
264
+ for (struct MultiTarget *t = targets; t < targets + tCount; ++t) {
270
265
// the simple case is if we match, deal with that first.
271
266
if ((char )c == t->str [t->index ]) {
272
- if (++t->index == t->len )
267
+ if (++t->index == t->len ) {
273
268
return t - targets;
274
- else
269
+ } else {
275
270
continue ;
271
+ }
276
272
}
277
273
278
274
// if not we need to walk back and see if we could have matched further
279
275
// down the stream (ie '1112' doesn't match the first position in '11112'
280
276
// but it will match the second position so we can't just reset the current
281
277
// index to 0 when we find a mismatch.
282
- if (t->index == 0 )
278
+ if (t->index == 0 ) {
283
279
continue ;
280
+ }
284
281
285
282
int origIndex = t->index ;
286
283
do {
287
284
--t->index ;
288
285
// first check if current char works against the new current index
289
- if ((char )c != t->str [t->index ])
286
+ if ((char )c != t->str [t->index ]) {
290
287
continue ;
288
+ }
291
289
292
290
// if it's the only char then we're good, nothing more to check
293
291
if (t->index == 0 ) {
@@ -299,8 +297,9 @@ int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {
299
297
int diff = origIndex - t->index ;
300
298
size_t i;
301
299
for (i = 0 ; i < t->index ; ++i) {
302
- if (t->str [i] != t->str [i + diff])
300
+ if (t->str [i] != t->str [i + diff]) {
303
301
break ;
302
+ }
304
303
}
305
304
306
305
// if we successfully got through the previous loop then our current
0 commit comments