aboutsummaryrefslogtreecommitdiff
path: root/cores/arduino/Stream.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cores/arduino/Stream.cpp')
-rw-r--r--cores/arduino/Stream.cpp66
1 files changed, 34 insertions, 32 deletions
diff --git a/cores/arduino/Stream.cpp b/cores/arduino/Stream.cpp
index b31942f..f665465 100644
--- a/cores/arduino/Stream.cpp
+++ b/cores/arduino/Stream.cpp
@@ -26,7 +26,6 @@
#include "Stream.h"
#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
-#define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
// private method to read stream with timeout
int Stream::timedRead()
@@ -54,14 +53,30 @@ int Stream::timedPeek()
// returns peek of the next digit in the stream or -1 if timeout
// discards non-numeric characters
-int Stream::peekNextDigit()
+int Stream::peekNextDigit(LookaheadMode lookahead, bool detectDecimal)
{
int c;
while (1) {
c = timedPeek();
- if (c < 0) return c; // timeout
- if (c == '-') return c;
- if (c >= '0' && c <= '9') return c;
+
+ if( c < 0 ||
+ c == '-' ||
+ (c >= '0' && c <= '9') ||
+ (detectDecimal && c == '.')) return c;
+
+ 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.
+ }
+ case SKIP_ALL:
+ break;
+ }
read(); // discard non-numeric
}
}
@@ -107,31 +122,25 @@ bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t
}
}
-
// returns the first valid (long) integer value from the current position.
-// initial characters that are not digits (or the minus sign) are skipped
-// function is terminated by the first character that is not a digit.
-long Stream::parseInt()
-{
- return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
-}
-
-// as above but a given skipChar is ignored
-// this allows format characters (typically commas) in values to be ignored
-long Stream::parseInt(char skipChar)
+// 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.
+long Stream::parseInt(LookaheadMode lookahead, char ignore)
{
bool isNegative = false;
long value = 0;
int c;
- c = peekNextDigit();
+ c = peekNextDigit(lookahead, false);
// ignore non numeric leading characters
if(c < 0)
return 0; // zero returned if timeout
do{
- if(c == skipChar)
- ; // ignore this charactor
+ if(c == ignore)
+ ; // ignore this character
else if(c == '-')
isNegative = true;
else if(c >= '0' && c <= '9') // is c a digit?
@@ -139,36 +148,29 @@ long Stream::parseInt(char skipChar)
read(); // consume the character we got with peek
c = timedPeek();
}
- while( (c >= '0' && c <= '9') || c == skipChar );
+ while( (c >= '0' && c <= '9') || c == ignore );
if(isNegative)
value = -value;
return value;
}
-
// as parseInt but returns a floating point value
-float Stream::parseFloat()
+float Stream::parseFloat(LookaheadMode lookahead, char ignore)
{
- return parseFloat(NO_SKIP_CHAR);
-}
-
-// as above but the given skipChar is ignored
-// this allows format characters (typically commas) in values to be ignored
-float Stream::parseFloat(char skipChar){
bool isNegative = false;
bool isFraction = false;
long value = 0;
- char c;
+ int c;
float fraction = 1.0;
- c = peekNextDigit();
+ c = peekNextDigit(lookahead, true);
// ignore non numeric leading characters
if(c < 0)
return 0; // zero returned if timeout
do{
- if(c == skipChar)
+ if(c == ignore)
; // ignore
else if(c == '-')
isNegative = true;
@@ -182,7 +184,7 @@ float Stream::parseFloat(char skipChar){
read(); // consume the character we got with peek
c = timedPeek();
}
- while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
+ while( (c >= '0' && c <= '9') || (c == '.' && !isFraction) || c == ignore );
if(isNegative)
value = -value;