[gnoduino: 219/237] readBytes() and readBytesUntil() handle zero bytes and return # of bytes read.



commit 52300aa36823660cd3005729b475b42e84e28fc7
Author: David A. Mellis <d mellis arduino cc>
Date:   Sat Nov 19 16:23:19 2011 -0500

    readBytes() and readBytesUntil() handle zero bytes and return # of bytes read.
    
    http://code.google.com/p/arduino/issues/detail?id=586

 arduino/cores/arduino/Stream.cpp |   45 +++++++++++++++++++------------------
 arduino/cores/arduino/Stream.h   |    4 +-
 2 files changed, 25 insertions(+), 24 deletions(-)
---
diff --git a/arduino/cores/arduino/Stream.cpp b/arduino/cores/arduino/Stream.cpp
index a7d9d38..5fad8dd 100644
--- a/arduino/cores/arduino/Stream.cpp
+++ b/arduino/cores/arduino/Stream.cpp
@@ -208,11 +208,20 @@ float Stream::parseFloat(char skipChar){
 }
 
 // read characters from stream into buffer
-// terminates if length characters have been read, null is detected or timeout (see setTimeout)
-// returns the number of characters placed in the buffer (0 means no valid data found)
-int Stream::readBytes( char *buffer, size_t length)
+// 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)
 {
-   return readBytesUntil( 0, buffer, length);
+  size_t count = 0;
+  while (count < length) {
+    int c = timedRead();
+    if (c < 0) break;
+    *buffer++ = (char)c;
+    count++;
+  }
+  return count;
 }
 
 
@@ -220,24 +229,16 @@ int Stream::readBytes( char *buffer, size_t length)
 // 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)
 
-int Stream::readBytesUntil( char terminator, char *buffer, size_t length)
+size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
 {
-    unsigned int index = 0;
-    *buffer = 0;
-    while(index < length-1 ){
-      int c = timedRead();
-      if( c <= 0 ){
-        return 0;   // timeout returns 0 !
-      }
-      else if( c == terminator){
-        buffer[index] = 0; // terminate the string
-        return index;               // data got successfully
-      }
-      else{
-        buffer[index++] = (char)c;
-      }
-    }
-    buffer[index] = 0;
-    return index; // here if buffer is full before detecting the terminator
+  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
 }
 
diff --git a/arduino/cores/arduino/Stream.h b/arduino/cores/arduino/Stream.h
index a3b25c5..13f11be 100644
--- a/arduino/cores/arduino/Stream.h
+++ b/arduino/cores/arduino/Stream.h
@@ -73,11 +73,11 @@ class Stream : public Print
 
   float parseFloat();               // float version of parseInt
 
-  int readBytes( char *buffer, size_t length); // read chars from stream into buffer
+  size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
   // terminates if length characters have been read or timeout (see setTimeout)
   // returns the number of characters placed in the buffer (0 means no valid data found)
 
-  int readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
+  size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
   // 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)
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]