@@ -208,36 +208,37 @@ float Stream::parseFloat(char skipChar){
208
208
}
209
209
210
210
// 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)
214
216
{
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;
216
225
}
217
226
218
227
219
228
// as readBytes with terminator character
220
229
// terminates if length characters have been read, timeout, or if the terminator character detected
221
230
// returns the number of characters placed in the buffer (0 means no valid data found)
222
231
223
- int Stream::readBytesUntil ( char terminator, char *buffer, size_t length)
232
+ size_t Stream::readBytesUntil (char terminator, char *buffer, size_t length)
224
233
{
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
242
243
}
243
244
0 commit comments