2 Stream.cpp - adds parsing methods to Stream class
3 Copyright (c) 2008 David A. Mellis. All right reserved.
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.
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.
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
20 parsing functions based on TextFinder library by Michael Margolis
26 #define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
27 #define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
29 // private method to read stream with timeout
30 int Stream::timedRead()
33 _startMillis = millis();
37 } while(millis() - _startMillis < _timeout);
38 return -1; // -1 indicates timeout
41 // private method to peek stream with timeout
42 int Stream::timedPeek()
45 _startMillis = millis();
49 } while(millis() - _startMillis < _timeout);
50 return -1; // -1 indicates timeout
53 // returns peek of the next digit in the stream or -1 if timeout
54 // discards non-numeric characters
55 int Stream::peekNextDigit()
60 if (c < 0) return c; // timeout
61 if (c == '-') return c;
62 if (c >= '0' && c <= '9') return c;
63 read(); // discard non-numeric
68 //////////////////////////////////////////////////////////////
70 void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait
75 // find returns true if the target string is found
76 bool Stream::find(char *target)
78 return findUntil(target, NULL);
81 // reads data from the stream until the target string of given length is found
82 // returns true if target string is found, false if timed out
83 bool Stream::find(char *target, size_t length)
85 return findUntil(target, length, NULL, 0);
88 // as find but search ends if the terminator string is found
89 bool Stream::findUntil(char *target, char *terminator)
91 return findUntil(target, strlen(target), terminator, strlen(terminator));
94 // reads data from the stream until the target string of the given length is found
95 // search terminated if the terminator string is found
96 // returns true if target string is found, false if terminated or timed out
97 bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen)
99 size_t index = 0; // maximum target string length is 64k bytes!
100 size_t termIndex = 0;
104 return true; // return true if target is a null string
105 while( (c = timedRead()) > 0){
107 if(c != target[index])
108 index = 0; // reset index if any char does not match
110 if( c == target[index]){
111 //////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1);
112 if(++index >= targetLen){ // return true if all chars in the target match
117 if(termLen > 0 && c == terminator[termIndex]){
118 if(++termIndex >= termLen)
119 return false; // return false if terminate string found before target string
128 // returns the first valid (long) integer value from the current position.
129 // initial characters that are not digits (or the minus sign) are skipped
130 // function is terminated by the first character that is not a digit.
131 long Stream::parseInt()
133 return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
136 // as above but a given skipChar is ignored
137 // this allows format characters (typically commas) in values to be ignored
138 long Stream::parseInt(char skipChar)
140 boolean isNegative = false;
145 // ignore non numeric leading characters
147 return 0; // zero returned if timeout
151 ; // ignore this charactor
154 else if(c >= '0' && c <= '9') // is c a digit?
155 value = value * 10 + c - '0';
156 read(); // consume the character we got with peek
159 while( (c >= '0' && c <= '9') || c == skipChar );
167 // as parseInt but returns a floating point value
168 float Stream::parseFloat()
170 return parseFloat(NO_SKIP_CHAR);
173 // as above but the given skipChar is ignored
174 // this allows format characters (typically commas) in values to be ignored
175 float Stream::parseFloat(char skipChar){
176 boolean isNegative = false;
177 boolean isFraction = false;
180 float fraction = 1.0;
183 // ignore non numeric leading characters
185 return 0; // zero returned if timeout
194 else if(c >= '0' && c <= '9') { // is c a digit?
195 value = value * 10 + c - '0';
199 read(); // consume the character we got with peek
202 while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
207 return value * fraction;
212 // read characters from stream into buffer
213 // terminates if length characters have been read, or timeout (see setTimeout)
214 // returns the number of characters placed in the buffer
215 // the buffer is NOT null terminated.
217 size_t Stream::readBytes(char *buffer, size_t length)
220 while (count < length) {
230 // as readBytes with terminator character
231 // terminates if length characters have been read, timeout, or if the terminator character detected
232 // returns the number of characters placed in the buffer (0 means no valid data found)
234 size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
236 if (length < 1) return 0;
238 while (index < length) {
240 if (c < 0 || c == terminator) break;
244 return index; // return number of characters, not including null terminator
247 String Stream::readString()
259 String Stream::readStringUntil(char terminator)
263 while (c >= 0 && c != terminator)