28
28
// compatability macros for testing
29
29
/*
30
30
#define getInt() parseInt()
31
- #define getInt(skipChar ) parseInt(skipchar )
31
+ #define getInt(ignore ) parseInt(ignore )
32
32
#define getFloat() parseFloat()
33
- #define getFloat(skipChar ) parseFloat(skipChar )
33
+ #define getFloat(ignore ) parseFloat(ignore )
34
34
#define getString( pre_string, post_string, buffer, length)
35
35
readBytesBetween( pre_string, terminator, buffer, length)
36
36
*/
37
37
38
+ // This enumeration provides the lookahead options for parseInt(), parseFloat()
39
+ // The rules set out here are used until either the first valid character is found
40
+ // or a time out occurs due to lack of input.
41
+ enum LookaheadMode{
42
+ SKIP_ALL, // All invalid characters are ignored.
43
+ SKIP_NONE, // Nothing is skipped, and the stream is not touched unless the first waiting character is valid.
44
+ SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped.
45
+ };
46
+
38
47
class Stream : public Print
39
48
{
40
49
protected:
41
50
unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
42
51
unsigned long _startMillis; // used for timeout measurement
43
52
int timedRead (); // private method to read stream with timeout
44
53
int timedPeek (); // private method to peek stream with timeout
45
- int peekNextDigit ( bool detectDecimal ); // returns the next numeric digit in the stream or -1 if timeout
54
+ int peekNextDigit (LookaheadMode lookahead, bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout
46
55
47
56
public:
48
57
virtual int available () = 0;
@@ -73,11 +82,11 @@ class Stream : public Print
73
82
bool findUntil (uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil ((char *)target, targetLen, terminate, termLen); }
74
83
75
84
76
- long parseInt (); // returns the first valid (long) integer value from the current position.
85
+ long parseInt (LookaheadMode lookahead = SKIP_ALL ); // returns the first valid (long) integer value from the current position.
77
86
// initial characters that are not digits (or the minus sign) are skipped
78
87
// integer is terminated by the first character that is not a digit.
79
88
80
- float parseFloat (); // float version of parseInt
89
+ float parseFloat (LookaheadMode lookahead = SKIP_ALL ); // float version of parseInt
81
90
82
91
size_t readBytes ( char *buffer, size_t length); // read chars from stream into buffer
83
92
size_t readBytes ( uint8_t *buffer, size_t length) { return readBytes ((char *)buffer, length); }
@@ -94,11 +103,13 @@ class Stream : public Print
94
103
String readStringUntil (char terminator);
95
104
96
105
protected:
97
- long parseInt (char skipChar); // as above but the given skipChar is ignored
98
- // as above but the given skipChar is ignored
106
+ long parseInt (char ignore) { return parseInt (SKIP_ALL, ignore); }
107
+ long parseInt (LookaheadMode lookahead, char ignore); // as above but the given ignore is ignored
108
+ // as above but 'ignore' is ignored
99
109
// this allows format characters (typically commas) in values to be ignored
100
-
101
- float parseFloat (char skipChar); // as above but the given skipChar is ignored
110
+
111
+ float parseFloat (char ignore) { return parseFloat (SKIP_ALL, ignore); }
112
+ float parseFloat (LookaheadMode lookahead, char ignore); // as above but the given ignore is ignored
102
113
103
114
struct MultiTarget {
104
115
const char *str; // string you're searching for
0 commit comments