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
+
47
+ #define NO_IGNORE_CHAR ' \x01 ' // a char not found in a valid ASCII numeric field
48
+
38
49
class Stream : public Print
39
50
{
40
51
protected:
41
52
unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
42
53
unsigned long _startMillis; // used for timeout measurement
43
54
int timedRead (); // private method to read stream with timeout
44
55
int timedPeek (); // private method to peek stream with timeout
45
- int peekNextDigit (); // returns the next numeric digit in the stream or -1 if timeout
56
+ int peekNextDigit (LookaheadMode lookahead, bool detectDecimal ); // returns the next numeric digit in the stream or -1 if timeout
46
57
47
58
public:
48
59
virtual int available () = 0;
@@ -64,18 +75,23 @@ class Stream : public Print
64
75
bool find (uint8_t *target, size_t length) { return find ((char *)target, length); }
65
76
// returns true if target string is found, false if timed out
66
77
78
+ bool find (char target) { return find (&target, 1 ); }
79
+
67
80
bool findUntil (char *target, char *terminator); // as find but search ends if the terminator string is found
68
81
bool findUntil (uint8_t *target, char *terminator) { return findUntil ((char *)target, terminator); }
69
82
70
83
bool findUntil (char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
71
84
bool findUntil (uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil ((char *)target, targetLen, terminate, termLen); }
72
85
86
+ long parseInt (LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
87
+ // returns the first valid (long) integer value from the current position.
88
+ // lookahead determines how parseInt looks ahead in the stream.
89
+ // See LookaheadMode enumeration at the top of the file.
90
+ // Lookahead is terminated by the first character that is not a valid part of an integer.
91
+ // Once parsing commences, 'ignore' will be skipped in the stream.
73
92
74
- long parseInt (); // returns the first valid (long) integer value from the current position.
75
- // initial characters that are not digits (or the minus sign) are skipped
76
- // integer is terminated by the first character that is not a digit.
77
-
78
- float parseFloat (); // float version of parseInt
93
+ float parseFloat (LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
94
+ // float version of parseInt
79
95
80
96
size_t readBytes ( char *buffer, size_t length); // read chars from stream into buffer
81
97
size_t readBytes ( uint8_t *buffer, size_t length) { return readBytes ((char *)buffer, length); }
@@ -92,11 +108,11 @@ class Stream : public Print
92
108
String readStringUntil (char terminator);
93
109
94
110
protected:
95
- long parseInt (char skipChar); // as above but the given skipChar is ignored
96
- // as above but the given skipChar is ignored
97
- // 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
111
+ long parseInt (char ignore) { return parseInt (SKIP_ALL, ignore); }
112
+ float parseFloat ( char ignore) { return parseFloat (SKIP_ALL, ignore); }
113
+ // These overload exists for compatibility with any class that has derived
114
+ // Stream and used parseFloat/Int with a custom ignore character. To keep
115
+ // the public API simple, these overload remains protected.
100
116
101
117
struct MultiTarget {
102
118
const char *str; // string you're searching for
@@ -109,4 +125,5 @@ class Stream : public Print
109
125
int findMulti (struct MultiTarget *targets, int tCount);
110
126
};
111
127
128
+ #undef NO_IGNORE_CHAR
112
129
#endif
0 commit comments