Skip to content

Commit d10842a

Browse files
committed
readBytes() and readBytesUntil() handle zero bytes and return # of bytes read.
http://code.google.com/p/arduino/issues/detail?id=586
1 parent f03ff94 commit d10842a

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

cores/arduino/Stream.cpp

+23-22
Original file line numberDiff line numberDiff line change
@@ -208,36 +208,37 @@ float Stream::parseFloat(char skipChar){
208208
}
209209

210210
// read characters from stream into buffer
211-
// terminates if length characters have been read, null is detected or timeout (see setTimeout)
212-
// returns the number of characters placed in the buffer (0 means no valid data found)
213-
int Stream::readBytes( char *buffer, size_t length)
211+
// terminates if length characters have been read, or timeout (see setTimeout)
212+
// returns the number of characters placed in the buffer
213+
// the buffer is NOT null terminated.
214+
//
215+
size_t Stream::readBytes(char *buffer, size_t length)
214216
{
215-
return readBytesUntil( 0, buffer, length);
217+
size_t count = 0;
218+
while (count < length) {
219+
int c = timedRead();
220+
if (c < 0) break;
221+
*buffer++ = (char)c;
222+
count++;
223+
}
224+
return count;
216225
}
217226

218227

219228
// as readBytes with terminator character
220229
// terminates if length characters have been read, timeout, or if the terminator character detected
221230
// returns the number of characters placed in the buffer (0 means no valid data found)
222231

223-
int Stream::readBytesUntil( char terminator, char *buffer, size_t length)
232+
size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
224233
{
225-
unsigned int index = 0;
226-
*buffer = 0;
227-
while(index < length-1 ){
228-
int c = timedRead();
229-
if( c <= 0 ){
230-
return 0; // timeout returns 0 !
231-
}
232-
else if( c == terminator){
233-
buffer[index] = 0; // terminate the string
234-
return index; // data got successfully
235-
}
236-
else{
237-
buffer[index++] = (char)c;
238-
}
239-
}
240-
buffer[index] = 0;
241-
return index; // here if buffer is full before detecting the terminator
234+
if (length < 1) return 0;
235+
size_t index = 0;
236+
while (index < length) {
237+
int c = timedRead();
238+
if (c < 0 || c == terminator) break;
239+
*buffer++ = (char)c;
240+
index++;
241+
}
242+
return index; // return number of characters, not including null terminator
242243
}
243244

cores/arduino/Stream.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ class Stream : public Print
7373

7474
float parseFloat(); // float version of parseInt
7575

76-
int readBytes( char *buffer, size_t length); // read chars from stream into buffer
76+
size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
7777
// terminates if length characters have been read or timeout (see setTimeout)
7878
// returns the number of characters placed in the buffer (0 means no valid data found)
7979

80-
int readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
80+
size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
8181
// terminates if length characters have been read, timeout, or if the terminator character detected
8282
// returns the number of characters placed in the buffer (0 means no valid data found)
8383

0 commit comments

Comments
 (0)