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;
@@ -55,6 +66,7 @@ class Stream : public Print
5566// parsing methods
5667
5768 void setTimeout (unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
69+ unsigned long getTimeout (void ) { return _timeout; }
5870
5971 bool find (char *target); // reads data from the stream until the target string is found
6072 bool find (uint8_t *target) { return find ((char *)target); }
@@ -64,18 +76,23 @@ class Stream : public Print
6476 bool find (uint8_t *target, size_t length) { return find ((char *)target, length); }
6577 // returns true if target string is found, false if timed out
6678
79+ bool find (char target) { return find (&target, 1 ); }
80+
6781 bool findUntil (char *target, char *terminator); // as find but search ends if the terminator string is found
6882 bool findUntil (uint8_t *target, char *terminator) { return findUntil ((char *)target, terminator); }
6983
7084 bool findUntil (char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
7185 bool findUntil (uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil ((char *)target, targetLen, terminate, termLen); }
7286
87+ long parseInt (LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
88+ // returns the first valid (long) integer value from the current position.
89+ // lookahead determines how parseInt looks ahead in the stream.
90+ // See LookaheadMode enumeration at the top of the file.
91+ // Lookahead is terminated by the first character that is not a valid part of an integer.
92+ // Once parsing commences, 'ignore' will be skipped in the stream.
7393
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
94+ float parseFloat (LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
95+ // float version of parseInt
7996
8097 size_t readBytes ( char *buffer, size_t length); // read chars from stream into buffer
8198 size_t readBytes ( uint8_t *buffer, size_t length) { return readBytes ((char *)buffer, length); }
@@ -92,11 +109,11 @@ class Stream : public Print
92109 String readStringUntil (char terminator);
93110
94111 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
112+ long parseInt (char ignore) { return parseInt (SKIP_ALL, ignore); }
113+ float parseFloat ( char ignore) { return parseFloat (SKIP_ALL, ignore); }
114+ // These overload exists for compatibility with any class that has derived
115+ // Stream and used parseFloat/Int with a custom ignore character. To keep
116+ // the public API simple, these overload remains protected.
100117
101118 struct MultiTarget {
102119 const char *str; // string you're searching for
@@ -109,4 +126,5 @@ class Stream : public Print
109126 int findMulti (struct MultiTarget *targets, int tCount);
110127};
111128
129+ #undef NO_IGNORE_CHAR
112130#endif
0 commit comments