Make protected Stream::parseInt/Float overloads public.

Stream::parseInt & Stream::parseFloat previously had protected
overloads which allowed skipping a custom character. This commit
brings this feature to the public interface.

To keep the public API simpler, the single paramter overload remains
protected. However its functionality is available in the public
interface using the two parameter overload.
This commit is contained in:
Chris--A 2015-07-09 18:32:40 +10:00 committed by Sandeep Mistry
parent db295bda6a
commit 507f2ee84a
2 changed files with 20 additions and 31 deletions

View File

@ -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()
@ -121,17 +120,11 @@ 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(LookaheadMode lookahead)
{
return parseInt(lookahead, NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
}
// as above but 'ignore' is ignored
// this allows format characters (typically commas) in values to be ignored
// 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;
@ -160,16 +153,9 @@ long Stream::parseInt(LookaheadMode lookahead, char ignore)
return value;
}
// as parseInt but returns a floating point value
float Stream::parseFloat(LookaheadMode lookahead)
float Stream::parseFloat(LookaheadMode lookahead, char ignore)
{
return parseFloat(lookahead, NO_SKIP_CHAR);
}
// as above but the given ignore is ignored
// this allows format characters (typically commas) in values to be ignored
float Stream::parseFloat(LookaheadMode lookahead, char ignore){
bool isNegative = false;
bool isFraction = false;
long value = 0;

View File

@ -44,6 +44,8 @@ enum LookaheadMode{
SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped.
};
#define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field
class Stream : public Print
{
protected:
@ -81,12 +83,15 @@ class Stream : public Print
bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); }
long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
// returns the first valid (long) integer value from the current position.
// 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 parseInt(LookaheadMode lookahead = SKIP_ALL); // returns the first valid (long) integer value from the current position.
// initial characters that are not digits (or the minus sign) are skipped
// integer is terminated by the first character that is not a digit.
float parseFloat(LookaheadMode lookahead = SKIP_ALL); // float version of parseInt
float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
// float version of parseInt
size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
@ -104,12 +109,10 @@ class Stream : public Print
protected:
long parseInt(char ignore) { return parseInt(SKIP_ALL, ignore); }
long parseInt(LookaheadMode lookahead, char ignore); // as above but the given ignore is ignored
// as above but 'ignore' is ignored
// this allows format characters (typically commas) in values to be ignored
float parseFloat(char ignore) { return parseFloat(SKIP_ALL, ignore); }
float parseFloat(LookaheadMode lookahead, char ignore); // as above but the given ignore is ignored
float parseFloat(char ignore) { return parseFloat(SKIP_ALL, ignore); }
// These overload exists for compatibility with any class that has derived
// Stream and used parseFloat/Int with a custom ignore character. To keep
// the public API simple, these overload remains protected.
struct MultiTarget {
const char *str; // string you're searching for
@ -122,5 +125,5 @@ class Stream : public Print
int findMulti(struct MultiTarget *targets, int tCount);
};
#undef NO_IGNORE_CHAR
#endif