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;
@@ -71,11 +80,11 @@ class Stream : public Print
71
80
bool findUntil (uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil ((char *)target, targetLen, terminate, termLen); }
72
81
73
82
74
- long parseInt (); // returns the first valid (long) integer value from the current position.
83
+ long parseInt (LookaheadMode lookahead = SKIP_ALL ); // returns the first valid (long) integer value from the current position.
75
84
// initial characters that are not digits (or the minus sign) are skipped
76
85
// integer is terminated by the first character that is not a digit.
77
86
78
- float parseFloat (); // float version of parseInt
87
+ float parseFloat (LookaheadMode lookahead = SKIP_ALL ); // float version of parseInt
79
88
80
89
size_t readBytes ( char *buffer, size_t length); // read chars from stream into buffer
81
90
size_t readBytes ( uint8_t *buffer, size_t length) { return readBytes ((char *)buffer, length); }
@@ -92,11 +101,13 @@ class Stream : public Print
92
101
String readStringUntil (char terminator);
93
102
94
103
protected:
95
- long parseInt (char skipChar); // as above but the given skipChar is ignored
96
- // as above but the given skipChar is ignored
104
+ long parseInt (char ignore) { return parseInt (SKIP_ALL, ignore); }
105
+ long parseInt (LookaheadMode lookahead, char ignore); // as above but the given ignore is ignored
106
+ // as above but 'ignore' is ignored
97
107
// this allows format characters (typically commas) in values to be ignored
98
-
99
- float parseFloat (char skipChar); // as above but the given skipChar is ignored
108
+
109
+ float parseFloat (char ignore) { return parseFloat (SKIP_ALL, ignore); }
110
+ float parseFloat (LookaheadMode lookahead, char ignore); // as above but the given ignore is ignored
100
111
101
112
struct MultiTarget {
102
113
const char *str; // string you're searching for
0 commit comments