From b6669c82d340417ae58a6c927462c3cf2837b0cc Mon Sep 17 00:00:00 2001 From: Ayush Faujdar <131191497+ayushfaujdar@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:23:41 +0530 Subject: [PATCH 1/4] Add Stream::readln() to read LF/CRLF text lines --- api/Stream.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/api/Stream.h b/api/Stream.h index e81c71ba..dbff6be3 100644 --- a/api/Stream.h +++ b/api/Stream.h @@ -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(); @@ -130,4 +139,4 @@ class Stream : public Print } -using arduino::Stream; \ No newline at end of file +using arduino::Stream; From 0a94e0a50d253d5f142994bde6841ab45ae7fe39 Mon Sep 17 00:00:00 2001 From: Ayush Faujdar <131191497+ayushfaujdar@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:25:37 +0530 Subject: [PATCH 2/4] Add Stream::readln() to read LF/CRLF text lines --- api/Stream.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/api/Stream.cpp b/api/Stream.cpp index f6f9bda6..ed02c0fa 100644 --- a/api/Stream.cpp +++ b/api/Stream.cpp @@ -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; From 5619c6ae84d893c58b3a9434e9bd441698e145b2 Mon Sep 17 00:00:00 2001 From: Ayush Faujdar <131191497+ayushfaujdar@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:26:39 +0530 Subject: [PATCH 3/4] Add Stream::readln() to read LF/CRLF text lines --- test/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d8050249..7b8c1e26 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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 From 9a405115949035c84754dcdf9cc986edd9b1dd6d Mon Sep 17 00:00:00 2001 From: Ayush Faujdar <131191497+ayushfaujdar@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:28:26 +0530 Subject: [PATCH 4/4] Add Stream::readln() to read LF/CRLF text lines --- test/src/Stream/test_readln.cpp | 74 +++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 test/src/Stream/test_readln.cpp diff --git a/test/src/Stream/test_readln.cpp b/test/src/Stream/test_readln.cpp new file mode 100644 index 00000000..cc798e24 --- /dev/null +++ b/test/src/Stream/test_readln.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2020 Arduino. All rights reserved. + * + * SPDX-License-Identifier: LGPL-2.1-or-later + */ +/************************************************************************************** + * INCLUDE + **************************************************************************************/ +#include +#include +/************************************************************************************** + * 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); + } +}