2011-08-17 10:53:49 -07:00
|
|
|
/*
|
|
|
|
Stream.cpp - adds parsing methods to Stream class
|
|
|
|
Copyright (c) 2008 David A. Mellis. All right reserved.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
Created July 2011
|
|
|
|
parsing functions based on TextFinder library by Michael Margolis
|
2015-03-24 04:13:41 -07:00
|
|
|
|
|
|
|
findMulti/findUntil routines written by Jim Leonard/Xuth
|
2011-08-17 10:53:49 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
#include "Stream.h"
|
|
|
|
|
2011-08-17 11:16:47 -07:00
|
|
|
#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
|
2011-08-17 10:53:49 -07:00
|
|
|
|
|
|
|
// private method to read stream with timeout
|
|
|
|
int Stream::timedRead()
|
|
|
|
{
|
2011-09-09 13:24:47 -07:00
|
|
|
int c;
|
|
|
|
_startMillis = millis();
|
|
|
|
do {
|
|
|
|
c = read();
|
|
|
|
if (c >= 0) return c;
|
|
|
|
} while(millis() - _startMillis < _timeout);
|
|
|
|
return -1; // -1 indicates timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
// private method to peek stream with timeout
|
|
|
|
int Stream::timedPeek()
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
_startMillis = millis();
|
|
|
|
do {
|
|
|
|
c = peek();
|
|
|
|
if (c >= 0) return c;
|
|
|
|
} while(millis() - _startMillis < _timeout);
|
2011-08-17 10:53:49 -07:00
|
|
|
return -1; // -1 indicates timeout
|
|
|
|
}
|
|
|
|
|
2011-09-09 13:24:47 -07:00
|
|
|
// returns peek of the next digit in the stream or -1 if timeout
|
2011-08-17 10:53:49 -07:00
|
|
|
// discards non-numeric characters
|
2015-06-16 05:50:59 -07:00
|
|
|
int Stream::peekNextDigit(LookaheadMode lookahead, bool detectDecimal)
|
2011-08-17 10:53:49 -07:00
|
|
|
{
|
|
|
|
int c;
|
2011-09-09 13:24:47 -07:00
|
|
|
while (1) {
|
|
|
|
c = timedPeek();
|
2015-06-18 20:05:06 -07:00
|
|
|
|
|
|
|
if( c < 0 ||
|
|
|
|
c == '-' ||
|
2015-11-23 13:45:29 -08:00
|
|
|
(c >= '0' && c <= '9') ||
|
|
|
|
(detectDecimal && c == '.')) return c;
|
2015-06-18 20:05:06 -07:00
|
|
|
|
2015-06-16 05:50:59 -07:00
|
|
|
switch( lookahead ){
|
|
|
|
case SKIP_NONE: return -1; // Fail code.
|
|
|
|
case SKIP_WHITESPACE:
|
|
|
|
switch( c ){
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\r':
|
|
|
|
case '\n': break;
|
|
|
|
default: return -1; // Fail code.
|
|
|
|
}
|
2015-11-23 13:45:29 -08:00
|
|
|
case SKIP_ALL:
|
|
|
|
break;
|
2015-06-16 05:50:59 -07:00
|
|
|
}
|
2011-09-09 13:24:47 -07:00
|
|
|
read(); // discard non-numeric
|
2011-08-17 10:53:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Public Methods
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
2011-10-02 06:44:21 -07:00
|
|
|
void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait
|
2011-08-17 10:53:49 -07:00
|
|
|
{
|
2011-10-02 06:44:21 -07:00
|
|
|
_timeout = timeout;
|
2011-08-17 10:53:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// find returns true if the target string is found
|
|
|
|
bool Stream::find(char *target)
|
|
|
|
{
|
2015-03-24 04:13:41 -07:00
|
|
|
return findUntil(target, strlen(target), NULL, 0);
|
2011-08-17 10:53:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// reads data from the stream until the target string of given length is found
|
|
|
|
// returns true if target string is found, false if timed out
|
|
|
|
bool Stream::find(char *target, size_t length)
|
|
|
|
{
|
|
|
|
return findUntil(target, length, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// as find but search ends if the terminator string is found
|
|
|
|
bool Stream::findUntil(char *target, char *terminator)
|
|
|
|
{
|
|
|
|
return findUntil(target, strlen(target), terminator, strlen(terminator));
|
|
|
|
}
|
|
|
|
|
|
|
|
// reads data from the stream until the target string of the given length is found
|
|
|
|
// search terminated if the terminator string is found
|
|
|
|
// returns true if target string is found, false if terminated or timed out
|
|
|
|
bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen)
|
|
|
|
{
|
2015-03-24 04:13:41 -07:00
|
|
|
if (terminator == NULL) {
|
|
|
|
MultiTarget t[1] = {{target, targetLen, 0}};
|
|
|
|
return findMulti(t, 1) == 0 ? true : false;
|
|
|
|
} else {
|
|
|
|
MultiTarget t[2] = {{target, targetLen, 0}, {terminator, termLen, 0}};
|
|
|
|
return findMulti(t, 2) == 0 ? true : false;
|
2011-08-17 10:53:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the first valid (long) integer value from the current position.
|
2015-07-09 01:32:40 -07:00
|
|
|
// lookahead determines how parseInt looks ahead in the stream.
|
|
|
|
// See LookaheadMode enumeration at the top of the file.
|
|
|
|
// Lookahead is terminated by the first character that is not a valid part of an integer.
|
|
|
|
// Once parsing commences, 'ignore' will be skipped in the stream.
|
2015-06-16 05:50:59 -07:00
|
|
|
long Stream::parseInt(LookaheadMode lookahead, char ignore)
|
2011-08-17 10:53:49 -07:00
|
|
|
{
|
2015-03-24 04:13:41 -07:00
|
|
|
bool isNegative = false;
|
2011-08-17 10:53:49 -07:00
|
|
|
long value = 0;
|
|
|
|
int c;
|
|
|
|
|
2015-06-16 05:50:59 -07:00
|
|
|
c = peekNextDigit(lookahead, false);
|
2011-08-17 10:53:49 -07:00
|
|
|
// ignore non numeric leading characters
|
|
|
|
if(c < 0)
|
|
|
|
return 0; // zero returned if timeout
|
|
|
|
|
|
|
|
do{
|
2015-06-16 05:50:59 -07:00
|
|
|
if(c == ignore)
|
2015-11-23 13:45:29 -08:00
|
|
|
; // ignore this character
|
2011-08-17 10:53:49 -07:00
|
|
|
else if(c == '-')
|
|
|
|
isNegative = true;
|
|
|
|
else if(c >= '0' && c <= '9') // is c a digit?
|
|
|
|
value = value * 10 + c - '0';
|
2011-09-09 13:24:47 -07:00
|
|
|
read(); // consume the character we got with peek
|
|
|
|
c = timedPeek();
|
2011-08-17 10:53:49 -07:00
|
|
|
}
|
2015-06-16 05:50:59 -07:00
|
|
|
while( (c >= '0' && c <= '9') || c == ignore );
|
2011-08-17 10:53:49 -07:00
|
|
|
|
|
|
|
if(isNegative)
|
|
|
|
value = -value;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// as parseInt but returns a floating point value
|
2015-07-09 01:32:40 -07:00
|
|
|
float Stream::parseFloat(LookaheadMode lookahead, char ignore)
|
2011-08-17 10:53:49 -07:00
|
|
|
{
|
2015-03-24 04:13:41 -07:00
|
|
|
bool isNegative = false;
|
|
|
|
bool isFraction = false;
|
2011-08-17 10:53:49 -07:00
|
|
|
long value = 0;
|
2015-11-23 13:45:29 -08:00
|
|
|
int c;
|
2011-08-17 10:53:49 -07:00
|
|
|
float fraction = 1.0;
|
|
|
|
|
2015-06-16 05:50:59 -07:00
|
|
|
c = peekNextDigit(lookahead, true);
|
2011-08-17 10:53:49 -07:00
|
|
|
// ignore non numeric leading characters
|
|
|
|
if(c < 0)
|
|
|
|
return 0; // zero returned if timeout
|
|
|
|
|
|
|
|
do{
|
2015-06-16 05:50:59 -07:00
|
|
|
if(c == ignore)
|
2011-08-17 10:53:49 -07:00
|
|
|
; // ignore
|
|
|
|
else if(c == '-')
|
|
|
|
isNegative = true;
|
|
|
|
else if (c == '.')
|
|
|
|
isFraction = true;
|
|
|
|
else if(c >= '0' && c <= '9') { // is c a digit?
|
|
|
|
value = value * 10 + c - '0';
|
|
|
|
if(isFraction)
|
|
|
|
fraction *= 0.1;
|
|
|
|
}
|
2011-09-09 13:24:47 -07:00
|
|
|
read(); // consume the character we got with peek
|
|
|
|
c = timedPeek();
|
2011-08-17 10:53:49 -07:00
|
|
|
}
|
2015-11-23 13:45:29 -08:00
|
|
|
while( (c >= '0' && c <= '9') || (c == '.' && !isFraction) || c == ignore );
|
2011-08-17 10:53:49 -07:00
|
|
|
|
|
|
|
if(isNegative)
|
|
|
|
value = -value;
|
|
|
|
if(isFraction)
|
|
|
|
return value * fraction;
|
|
|
|
else
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read characters from stream into buffer
|
2011-11-19 13:23:19 -08:00
|
|
|
// terminates if length characters have been read, or timeout (see setTimeout)
|
|
|
|
// returns the number of characters placed in the buffer
|
|
|
|
// the buffer is NOT null terminated.
|
|
|
|
//
|
|
|
|
size_t Stream::readBytes(char *buffer, size_t length)
|
2011-08-17 10:53:49 -07:00
|
|
|
{
|
2011-11-19 13:23:19 -08:00
|
|
|
size_t count = 0;
|
|
|
|
while (count < length) {
|
|
|
|
int c = timedRead();
|
|
|
|
if (c < 0) break;
|
|
|
|
*buffer++ = (char)c;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return count;
|
2011-08-17 10:53:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-17 11:16:47 -07:00
|
|
|
// as readBytes with terminator character
|
2011-08-17 10:53:49 -07:00
|
|
|
// terminates if length characters have been read, timeout, or if the terminator character detected
|
|
|
|
// returns the number of characters placed in the buffer (0 means no valid data found)
|
|
|
|
|
2011-11-19 13:23:19 -08:00
|
|
|
size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
|
2011-08-17 10:53:49 -07:00
|
|
|
{
|
2011-11-19 13:23:19 -08:00
|
|
|
if (length < 1) return 0;
|
|
|
|
size_t index = 0;
|
|
|
|
while (index < length) {
|
|
|
|
int c = timedRead();
|
|
|
|
if (c < 0 || c == terminator) break;
|
|
|
|
*buffer++ = (char)c;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return index; // return number of characters, not including null terminator
|
2011-08-17 10:53:49 -07:00
|
|
|
}
|
|
|
|
|
2012-05-16 12:39:34 -07:00
|
|
|
String Stream::readString()
|
|
|
|
{
|
|
|
|
String ret;
|
|
|
|
int c = timedRead();
|
|
|
|
while (c >= 0)
|
|
|
|
{
|
|
|
|
ret += (char)c;
|
|
|
|
c = timedRead();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
String Stream::readStringUntil(char terminator)
|
|
|
|
{
|
|
|
|
String ret;
|
|
|
|
int c = timedRead();
|
|
|
|
while (c >= 0 && c != terminator)
|
|
|
|
{
|
|
|
|
ret += (char)c;
|
|
|
|
c = timedRead();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-24 04:13:41 -07:00
|
|
|
int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {
|
|
|
|
// any zero length target string automatically matches and would make
|
|
|
|
// a mess of the rest of the algorithm.
|
2015-04-21 09:10:32 -07:00
|
|
|
for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
|
2015-03-24 04:13:41 -07:00
|
|
|
if (t->len <= 0)
|
|
|
|
return t - targets;
|
2015-04-21 09:10:32 -07:00
|
|
|
}
|
2015-03-24 04:13:41 -07:00
|
|
|
|
2015-04-21 09:10:32 -07:00
|
|
|
while (1) {
|
2015-03-24 04:13:41 -07:00
|
|
|
int c = timedRead();
|
|
|
|
if (c < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
|
|
|
|
// the simple case is if we match, deal with that first.
|
2015-04-20 15:08:02 -07:00
|
|
|
if (c == t->str[t->index]) {
|
|
|
|
if (++t->index == t->len)
|
|
|
|
return t - targets;
|
2015-04-21 09:07:42 -07:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-24 04:13:41 -07:00
|
|
|
|
|
|
|
// if not we need to walk back and see if we could have matched further
|
|
|
|
// down the stream (ie '1112' doesn't match the first position in '11112'
|
|
|
|
// but it will match the second position so we can't just reset the current
|
|
|
|
// index to 0 when we find a mismatch.
|
|
|
|
if (t->index == 0)
|
2015-04-21 09:10:32 -07:00
|
|
|
continue;
|
2015-03-24 04:13:41 -07:00
|
|
|
|
|
|
|
int origIndex = t->index;
|
|
|
|
do {
|
2015-04-21 09:10:32 -07:00
|
|
|
--t->index;
|
|
|
|
// first check if current char works against the new current index
|
|
|
|
if (c != t->str[t->index])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// if it's the only char then we're good, nothing more to check
|
|
|
|
if (t->index == 0) {
|
|
|
|
t->index++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise we need to check the rest of the found string
|
|
|
|
int diff = origIndex - t->index;
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < t->index; ++i) {
|
|
|
|
if (t->str[i] != t->str[i + diff])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we successfully got through the previous loop then our current
|
|
|
|
// index is good.
|
|
|
|
if (i == t->index) {
|
|
|
|
t->index++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise we just try the next index
|
2015-03-24 04:13:41 -07:00
|
|
|
} while (t->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// unreachable
|
|
|
|
return -1;
|
2015-04-21 09:10:32 -07:00
|
|
|
}
|