2828#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
2929
3030// private method to read stream with timeout
31- int Stream::timedRead ()
32- {
31+ int Stream::timedRead () {
3332 int c;
3433 _startMillis = millis ();
3534 do {
3635 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
4041}
4142
4243// private method to peek stream with timeout
43- int Stream::timedPeek ()
44- {
44+ int Stream::timedPeek () {
4545 int c;
4646 _startMillis = millis ();
4747 do {
4848 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
5254}
5355
5456// returns peek of the next digit in the stream or -1 if timeout
5557// discards non-numeric characters
56- int Stream::peekNextDigit (LookaheadMode lookahead, bool detectDecimal)
57- {
58+ int Stream::peekNextDigit (LookaheadMode lookahead, bool detectDecimal) {
5859 int c;
5960 while (1 ) {
6061 c = timedPeek ();
6162
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 ;
7978 }
8079 read (); // discard non-numeric
8180 }
@@ -89,30 +88,26 @@ void Stream::setTimeout(unsigned long timeout) // sets the maximum number of mi
8988 _timeout = timeout;
9089}
9190
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) {
9593 return findUntil (target, strlen (target), NULL , 0 );
9694}
9795
9896// reads data from the stream until the target string of given length is found
9997// 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) {
10299 return findUntil (target, length, NULL , 0 );
103100}
104101
105102// 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) {
108104 return findUntil (target, strlen (target), terminator, strlen (terminator));
109105}
110106
111107// reads data from the stream until the target string of the given length is found
112108// search terminated if the terminator string is found
113109// 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) {
116111 if (terminator == NULL ) {
117112 MultiTarget t[1 ] = {{target, targetLen, 0 }};
118113 return findMulti (t, 1 ) == 0 ;
@@ -127,57 +122,58 @@ bool Stream::findUntil(const char *target, size_t targetLen, const char *termina
127122// See LookaheadMode enumeration at the top of the file.
128123// Lookahead is terminated by the first character that is not a valid part of an integer.
129124// 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) {
132126 bool isNegative = false ;
133127 long value = 0 ;
134128 int c;
135129
136130 c = peekNextDigit (lookahead, false );
137131 // 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+ }
140135
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 == ' -' ) {
145140 isNegative = true ;
146- else if (c >= ' 0' && c <= ' 9' ) // is c a digit?
141+ } else if (c >= ' 0' && c <= ' 9' ) { // is c a digit?
147142 value = value * 10 + c - ' 0' ;
143+ }
148144 read (); // consume the character we got with peek
149145 c = timedPeek ();
150- }
151- while ( (c >= ' 0' && c <= ' 9' ) || (char )c == ignore );
146+ } while ((c >= ' 0' && c <= ' 9' ) || (char )c == ignore);
152147
153- if (isNegative)
148+ if (isNegative) {
154149 value = -value;
150+ }
155151 return value;
156152}
157153
158154// 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) {
161156 bool isNegative = false ;
162157 bool isFraction = false ;
163158 double value = 0.0 ;
164159 int c;
165160 double fraction = 1.0 ;
166161
167162 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 == ' -' ) {
176172 isNegative = true ;
177- else if (c == ' .' )
173+ } else if (c == ' .' ) {
178174 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) {
181177 fraction *= 0.1 ;
182178 value = value + fraction * (c - ' 0' );
183179 } else {
@@ -186,11 +182,11 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore)
186182 }
187183 read (); // consume the character we got with peek
188184 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);
191186
192- if (isNegative)
187+ if (isNegative) {
193188 value = -value;
189+ }
194190
195191 return value;
196192}
@@ -200,94 +196,96 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore)
200196// returns the number of characters placed in the buffer
201197// the buffer is NOT null terminated.
202198//
203- size_t Stream::readBytes (char *buffer, size_t length)
204- {
199+ size_t Stream::readBytes (char *buffer, size_t length) {
205200 size_t count = 0 ;
206201 while (count < length) {
207202 int c = timedRead ();
208- if (c < 0 ) break ;
203+ if (c < 0 ) {
204+ break ;
205+ }
209206 *buffer++ = (char )c;
210207 count++;
211208 }
212209 return count;
213210}
214211
215-
216212// as readBytes with terminator character
217213// terminates if length characters have been read, timeout, or if the terminator character detected
218214// returns the number of characters placed in the buffer (0 means no valid data found)
219215
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) {
222217 size_t index = 0 ;
223218 while (index < length) {
224219 int c = timedRead ();
225- if (c < 0 || (char )c == terminator) break ;
220+ if (c < 0 || (char )c == terminator) {
221+ break ;
222+ }
226223 *buffer++ = (char )c;
227224 index++;
228225 }
229- return index; // return number of characters, not including null terminator
226+ return index; // return number of characters, not including null terminator
230227}
231228
232- String Stream::readString ()
233- {
229+ String Stream::readString () {
234230 String ret;
235231 int c = timedRead ();
236- while (c >= 0 )
237- {
232+ while (c >= 0 ) {
238233 ret += (char )c;
239234 c = timedRead ();
240235 }
241236 return ret;
242237}
243238
244- String Stream::readStringUntil (char terminator)
245- {
239+ String Stream::readStringUntil (char terminator) {
246240 String ret;
247241 int c = timedRead ();
248- while (c >= 0 && (char )c != terminator)
249- {
242+ while (c >= 0 && (char )c != terminator) {
250243 ret += (char )c;
251244 c = timedRead ();
252245 }
253246 return ret;
254247}
255248
256- int Stream::findMulti ( struct Stream ::MultiTarget *targets, int tCount) {
249+ int Stream::findMulti (struct Stream ::MultiTarget *targets, int tCount) {
257250 // any zero length target string automatically matches and would make
258251 // 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 ) {
261254 return t - targets;
255+ }
262256 }
263257
264258 while (1 ) {
265259 int c = timedRead ();
266- if (c < 0 )
260+ if (c < 0 ) {
267261 return -1 ;
262+ }
268263
269- for (struct MultiTarget *t = targets; t < targets+ tCount; ++t) {
264+ for (struct MultiTarget *t = targets; t < targets + tCount; ++t) {
270265 // the simple case is if we match, deal with that first.
271266 if ((char )c == t->str [t->index ]) {
272- if (++t->index == t->len )
267+ if (++t->index == t->len ) {
273268 return t - targets;
274- else
269+ } else {
275270 continue ;
271+ }
276272 }
277273
278274 // if not we need to walk back and see if we could have matched further
279275 // down the stream (ie '1112' doesn't match the first position in '11112'
280276 // but it will match the second position so we can't just reset the current
281277 // index to 0 when we find a mismatch.
282- if (t->index == 0 )
278+ if (t->index == 0 ) {
283279 continue ;
280+ }
284281
285282 int origIndex = t->index ;
286283 do {
287284 --t->index ;
288285 // 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 ]) {
290287 continue ;
288+ }
291289
292290 // if it's the only char then we're good, nothing more to check
293291 if (t->index == 0 ) {
@@ -299,8 +297,9 @@ int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {
299297 int diff = origIndex - t->index ;
300298 size_t i;
301299 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]) {
303301 break ;
302+ }
304303 }
305304
306305 // if we successfully got through the previous loop then our current
0 commit comments