diff options
author | David A. Mellis <d.mellis@arduino.cc> | 2012-05-16 15:39:34 -0400 |
---|---|---|
committer | David A. Mellis <d.mellis@arduino.cc> | 2012-05-16 15:39:34 -0400 |
commit | b495294aa32ca838180af57efdb1f04727487d55 (patch) | |
tree | a70f9b867dd4abff66d2cb12edd8b4e73b9ccc0b /cores/arduino/Stream.cpp | |
parent | 9a8976ce56bcdb70815dd58c1764d9e5c3b6fe95 (diff) |
Adding readString() and readStringUntil() to Stream (Adrian McEwen).
This isn't necessarily a particularly efficient implementation (it
allocates memory one character at a time and so may lead to
fragmentation) but it seems to work.
http://code.google.com/p/arduino/issues/detail?id=454
Diffstat (limited to 'cores/arduino/Stream.cpp')
-rw-r--r-- | cores/arduino/Stream.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/cores/arduino/Stream.cpp b/cores/arduino/Stream.cpp index 3d5b905..aafb7fc 100644 --- a/cores/arduino/Stream.cpp +++ b/cores/arduino/Stream.cpp @@ -244,3 +244,27 @@ size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length) return index; // return number of characters, not including null terminator } +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; +} + |