2828// compatability macros for testing
2929/*
3030#define getInt() parseInt()
31- #define getInt(skipChar ) parseInt(skipchar )
31+ #define getInt(ignore ) parseInt(ignore )
3232#define getFloat() parseFloat()
33- #define getFloat(skipChar ) parseFloat(skipChar )
33+ #define getFloat(ignore ) parseFloat(ignore )
3434#define getString( pre_string, post_string, buffer, length)
3535readBytesBetween( pre_string, terminator, buffer, length)
3636*/
3737
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+
3849class Stream : public Print
3950{
4051 protected:
4152 unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
4253 unsigned long _startMillis; // used for timeout measurement
4354 int timedRead (); // private method to read stream with timeout
4455 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
4657
4758 public:
4859 virtual int available () = 0;
@@ -64,18 +75,23 @@ class Stream : public Print
6475 bool find (uint8_t *target, size_t length) { return find ((char *)target, length); }
6576 // returns true if target string is found, false if timed out
6677
78+ bool find (char target) { return find (&target, 1 ); }
79+
6780 bool findUntil (char *target, char *terminator); // as find but search ends if the terminator string is found
6881 bool findUntil (uint8_t *target, char *terminator) { return findUntil ((char *)target, terminator); }
6982
7083 bool findUntil (char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
7184 bool findUntil (uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil ((char *)target, targetLen, terminate, termLen); }
7285
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.
7392
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
7995
8096 size_t readBytes ( char *buffer, size_t length); // read chars from stream into buffer
8197 size_t readBytes ( uint8_t *buffer, size_t length) { return readBytes ((char *)buffer, length); }
@@ -92,11 +108,11 @@ class Stream : public Print
92108 String readStringUntil (char terminator);
93109
94110 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.
100116
101117 struct MultiTarget {
102118 const char *str; // string you're searching for
@@ -109,4 +125,5 @@ class Stream : public Print
109125 int findMulti (struct MultiTarget *targets, int tCount);
110126};
111127
128+ #undef NO_IGNORE_CHAR
112129#endif
0 commit comments