Skip to content

Add Stream::readln() to read LF/CRLF text lines - #285

Open
ayushfaujdar wants to merge 4 commits into
arduino:masterfrom
ayushfaujdar:add-stream-readln
Open

ayushfaujdar wants to merge 4 commits into
arduino:masterfrom
ayushfaujdar:add-stream-readln

Conversation

@ayushfaujdar

Copy link
Copy Markdown

Fixes #251
Adds Stream::readln(char buffer, int length): reads one line of text, terminates on LF ('\n') and silently ignores CR ('\r'), so the same code reads Unix (LF) and Windows (CRLF) text files. The buffer is always null-terminated and at most length-1 characters are stored.
Return value: number of characters stored, not counting the null terminator. 0 means an empty line was read; -1 means a timeout happened before a line feed was found (characters read before the timeout are still in the buffer).
Two safety fixes vs the code proposed in the issue: the loop runs while (index < length - 1) so the null terminator always fits (the proposed code could write one byte past the buffer), and length <= 0 returns -1 immediately.
Note: open PR #201 adds a different API, String Stream::readLine(), requested by #152. This readln(char
, int) is the buffer-based C-string API #251 itself proposes, so it is not a duplicate.
Tests: new test_readln.cpp, 8 scenarios, 19 assertions. Full suite passes (991 assertions in 307 test cases).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In stream.cpp add readln, to simplify reading Windows and Linux text files

2 participants