1
1
/*
2
- Stream.h - base class for character-based streams.
3
- Copyright (c) 2010 David A. Mellis. All right reserved.
2
+ Stream.h - base class for character-based streams.
3
+ Copyright (c) 2010 David A. Mellis. All right reserved.
4
4
5
- This library is free software; you can redistribute it and/or
6
- modify it under the terms of the GNU Lesser General Public
7
- License as published by the Free Software Foundation; either
8
- version 2.1 of the License, or (at your option) any later version.
5
+ This library is free software; you can redistribute it and/or
6
+ modify it under the terms of the GNU Lesser General Public
7
+ License as published by the Free Software Foundation; either
8
+ version 2.1 of the License, or (at your option) any later version.
9
9
10
- This library is distributed in the hope that it will be useful,
11
- but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
- Lesser General Public License for more details.
10
+ This library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ Lesser General Public License for more details.
14
14
15
- You should have received a copy of the GNU Lesser General Public
16
- License along with this library; if not, write to the Free Software
17
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15
+ You should have received a copy of the GNU Lesser General Public
16
+ License along with this library; if not, write to the Free Software
17
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
18
19
- parsing functions based on TextFinder library by Michael Margolis
20
- */
19
+ parsing functions based on TextFinder library by Michael Margolis
20
+ */
21
21
22
- #ifndef Stream_h
23
- #define Stream_h
22
+ #pragma once
24
23
25
24
#include < inttypes.h>
26
25
#include " Print.h"
27
26
28
27
// compatibility macros for testing
29
28
/*
30
- #define getInt() parseInt()
31
- #define getInt(skipChar) parseInt(skipchar)
32
- #define getFloat() parseFloat()
33
- #define getFloat(skipChar) parseFloat(skipChar)
34
- #define getString( pre_string, post_string, buffer, length)
35
- readBytesBetween( pre_string, terminator, buffer, length)
36
- */
29
+ #define getInt() parseInt()
30
+ #define getInt(ignore) parseInt(ignore)
31
+ #define getFloat() parseFloat()
32
+ #define getFloat(ignore) parseFloat(ignore)
33
+ #define getString( pre_string, post_string, buffer, length)
34
+ readBytesBetween( pre_string, terminator, buffer, length)
35
+ */
36
+
37
+ // This enumeration provides the lookahead options for parseInt(), parseFloat()
38
+ // The rules set out here are used until either the first valid character is found
39
+ // or a time out occurs due to lack of input.
40
+ enum LookaheadMode {
41
+ SKIP_ALL, // All invalid characters are ignored.
42
+ SKIP_NONE, // Nothing is skipped, and the stream is not touched unless the first waiting character is valid.
43
+ SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped.
44
+ };
45
+
46
+ #define NO_IGNORE_CHAR ' \x01 ' // a char not found in a valid ASCII numeric field
37
47
38
48
class Stream : public Print {
39
49
protected:
40
- unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
41
- unsigned long _startMillis; // used for timeout measurement
42
- int timedRead (); // private method to read stream with timeout
43
- int timedPeek (); // private method to peek stream with timeout
44
- int peekNextDigit (); // returns the next numeric digit in the stream or -1 if timeout
50
+ unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
51
+ unsigned long _startMillis; // used for timeout measurement
52
+ int timedRead (); // private method to read stream with timeout
53
+ int timedPeek (); // private method to peek stream with timeout
54
+ int peekNextDigit (LookaheadMode lookahead, bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout
45
55
46
56
public:
47
57
virtual int available () = 0;
48
58
virtual int read () = 0;
49
59
virtual int peek () = 0;
50
60
51
- Stream () : _startMillis( 0 ) {
61
+ Stream () {
52
62
_timeout = 1000 ;
53
63
}
54
- virtual ~Stream () {}
55
64
56
65
// parsing methods
57
66
58
67
void setTimeout (unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
59
- unsigned long getTimeout (void );
68
+ unsigned long getTimeout (void ) {
69
+ return _timeout;
70
+ }
60
71
61
72
bool find (const char *target); // reads data from the stream until the target string is found
62
- bool find (uint8_t *target) {
63
- return find ((char *)target);
73
+ bool find (const uint8_t *target) {
74
+ return find ((const char *)target);
64
75
}
65
76
// returns true if target string is found, false if timed out (see setTimeout)
66
77
67
78
bool find (const char *target, size_t length); // reads data from the stream until the target string of given length is found
68
79
bool find (const uint8_t *target, size_t length) {
69
- return find ((char *)target, length);
80
+ return find ((const char *)target, length);
70
81
}
71
82
// returns true if target string is found, false if timed out
72
83
@@ -76,22 +87,26 @@ class Stream : public Print {
76
87
77
88
bool findUntil (const char *target, const char *terminator); // as find but search ends if the terminator string is found
78
89
bool findUntil (const uint8_t *target, const char *terminator) {
79
- return findUntil ((char *)target, terminator);
90
+ return findUntil ((const char *)target, terminator);
80
91
}
81
92
82
93
bool findUntil (const char *target, size_t targetLen, const char *terminate, size_t termLen); // as above but search ends if the terminate string is found
83
94
bool findUntil (const uint8_t *target, size_t targetLen, const char *terminate, size_t termLen) {
84
- return findUntil ((char *)target, targetLen, terminate, termLen);
95
+ return findUntil ((const char *)target, targetLen, terminate, termLen);
85
96
}
86
97
87
- long parseInt (); // returns the first valid (long) integer value from the current position.
88
- // initial characters that are not digits (or the minus sign) are skipped
89
- // integer is terminated by the first character that is not a digit.
98
+ long parseInt (LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
99
+ // returns the first valid (long) integer value from the current position.
100
+ // lookahead determines how parseInt looks ahead in the stream.
101
+ // See LookaheadMode enumeration at the top of the file.
102
+ // Lookahead is terminated by the first character that is not a valid part of an integer.
103
+ // Once parsing commences, 'ignore' will be skipped in the stream.
90
104
91
- float parseFloat (); // float version of parseInt
105
+ float parseFloat (LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
106
+ // float version of parseInt
92
107
93
- virtual size_t readBytes (char *buffer, size_t length); // read chars from stream into buffer
94
- virtual size_t readBytes (uint8_t *buffer, size_t length) {
108
+ size_t readBytes (char *buffer, size_t length); // read chars from stream into buffer
109
+ size_t readBytes (uint8_t *buffer, size_t length) {
95
110
return readBytes ((char *)buffer, length);
96
111
}
97
112
// terminates if length characters have been read or timeout (see setTimeout)
@@ -105,15 +120,19 @@ class Stream : public Print {
105
120
// returns the number of characters placed in the buffer (0 means no valid data found)
106
121
107
122
// Arduino String functions to be added here
108
- virtual String readString ();
123
+ String readString ();
109
124
String readStringUntil (char terminator);
110
125
111
126
protected:
112
- long parseInt (char skipChar); // as above but the given skipChar is ignored
113
- // as above but the given skipChar is ignored
114
- // this allows format characters (typically commas) in values to be ignored
115
-
116
- float parseFloat (char skipChar); // as above but the given skipChar is ignored
127
+ long parseInt (char ignore) {
128
+ return parseInt (SKIP_ALL, ignore);
129
+ }
130
+ float parseFloat (char ignore) {
131
+ return parseFloat (SKIP_ALL, ignore);
132
+ }
133
+ // These overload exists for compatibility with any class that has derived
134
+ // Stream and used parseFloat/Int with a custom ignore character. To keep
135
+ // the public API simple, these overload remains protected.
117
136
118
137
struct MultiTarget {
119
138
const char *str; // string you're searching for
@@ -126,4 +145,4 @@ class Stream : public Print {
126
145
int findMulti (struct MultiTarget *targets, int tCount);
127
146
};
128
147
129
- #endif
148
+ #undef NO_IGNORE_CHAR
0 commit comments