Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions api/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,39 @@ size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
return index; // return number of characters, not including null terminator
}

// read a line of text from stream into buffer
// terminates on line feed ('\n'), carriage return ('\r') characters are ignored,
// so both Unix (LF) and Windows (CRLF) text files can be read line by line
// terminates if length-1 characters have been read, timeout (see setTimeout), or if a line feed is detected
// the buffer is always null terminated, at most length-1 characters are stored
// so that there is always room for the null terminator
// returns the number of characters placed in the buffer, not including the null terminator
// (-1 if timeout occurred before a line feed was detected, 0 means an empty line was read)
// note that characters read before a timeout are still placed in the buffer
int Stream::readln(char *buffer, int length)
{
if (length <= 0)
return -1;
int index = 0;
int c = 0;
while (index < length - 1) {
c = timedRead();
if (c < 0 || c == '\n') {
break;
}
if (c != '\r') {
*buffer++ = (char)c;
index++;
}
}
*buffer = '\0';
if (c < 0) {
return -1;
} else {
return index; // return number of characters, not including null terminator
}
}

String Stream::readString()
{
String ret;
Expand Down
11 changes: 10 additions & 1 deletion api/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ class Stream : public Print
size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, 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 readln(char *buffer, int length); // read a line of text from stream into buffer
// terminates on line feed ('\n'), carriage return ('\r') characters are ignored,
// so both Unix (LF) and Windows (CRLF) text files can be read line by line
// terminates if length-1 characters have been read, timeout (see setTimeout), or if a line feed is detected
// the buffer is always null terminated, at most length-1 characters are stored
// so that there is always room for the null terminator
// returns the number of characters placed in the buffer, not including the null terminator
// (-1 if timeout occurred before a line feed was detected, 0 means an empty line was read)
// note that characters read before a timeout are still placed in the buffer

// Arduino String functions to be added here
String readString();
Expand Down Expand Up @@ -130,4 +139,4 @@ class Stream : public Print

}

using arduino::Stream;
using arduino::Stream;
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ set(TEST_SRCS
src/Stream/test_parseInt.cpp
src/Stream/test_readBytes.cpp
src/Stream/test_readBytesUntil.cpp
src/Stream/test_readln.cpp
src/Stream/test_readString.cpp
src/Stream/test_readStringUntil.cpp
src/Stream/test_setTimeout.cpp
Expand Down
74 changes: 74 additions & 0 deletions test/src/Stream/test_readln.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2020 Arduino. All rights reserved.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include <catch2/catch_test_macros.hpp>
#include <StreamMock.h>
/**************************************************************************************
* TEST CODE
**************************************************************************************/
TEST_CASE ("Testing readln(char *buffer, int length)", "[Stream-readln-01]")
{
StreamMock mock;
WHEN ("the stream is empty")
{
char buf[32] = {0};
REQUIRE(mock.readln(buf, sizeof(buf)) == -1);
REQUIRE(strncmp(buf, "", sizeof(buf)) == 0);
}
WHEN ("the stream contains a Unix (LF) terminated line")
{
char buf[32] = {0};
mock << "Hello\nWorld\n";
REQUIRE(mock.readln(buf, sizeof(buf)) == strlen("Hello"));
REQUIRE(strncmp(buf, "Hello", sizeof(buf)) == 0);
REQUIRE(mock.readString() == arduino::String("World\n"));
}
WHEN ("the stream contains a Windows (CRLF) terminated line")
{
char buf[32] = {0};
mock << "Hello\r\nWorld\r\n";
REQUIRE(mock.readln(buf, sizeof(buf)) == strlen("Hello"));
REQUIRE(strncmp(buf, "Hello", sizeof(buf)) == 0);
REQUIRE(mock.readString() == arduino::String("World\r\n"));
}
WHEN ("the stream contains carriage returns inside the line")
{
char buf[32] = {0};
mock << "a\rb\n";
REQUIRE(mock.readln(buf, sizeof(buf)) == strlen("ab"));
REQUIRE(strncmp(buf, "ab", sizeof(buf)) == 0);
}
WHEN ("the stream contains an empty line")
{
char buf[32] = {0};
mock << "\n";
REQUIRE(mock.readln(buf, sizeof(buf)) == 0);
REQUIRE(strncmp(buf, "", sizeof(buf)) == 0);
}
WHEN ("the stream contains a line without a terminator")
{
char buf[32] = {0};
mock << "Hello";
REQUIRE(mock.readln(buf, sizeof(buf)) == -1);
REQUIRE(strncmp(buf, "Hello", sizeof(buf)) == 0);
}
WHEN ("the line is longer than the buffer")
{
char buf[4] = {0};
mock << "Hello\n";
REQUIRE(mock.readln(buf, sizeof(buf)) == 3);
REQUIRE(strncmp(buf, "Hel", sizeof(buf)) == 0);
REQUIRE(mock.readln(buf, sizeof(buf)) == strlen("lo"));
REQUIRE(strncmp(buf, "lo", sizeof(buf)) == 0);
}
WHEN ("the buffer length is zero")
{
char buf[32] = {0};
REQUIRE(mock.readln(buf, 0) == -1);
}
}
Loading