arduino / arduino/ArduinoCore-API

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

Offen
#251 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
enhancement
Vorherrschende Sprache
C++
Sterne
306
Forks
150
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

### API component

stream.cpp

### Description

Text files can be read using readBytesUntil with the LineFeed character but with Windows files the CR character has to be dealt with. readln would terminate on LF and ignore CR so that the user just sees c-strings representing the text on each line.

### Is this a breaking change?

No

### Additional information

I have been using this code as a solution:
`// as readBytes but terminates on LF (10), ignores CR (13)
// 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 Stream::readln(char *buffer, int length) {
int index = 0;
int c = 0;
while (index < length) {
c = timedRead();
if (c < 0 || c == 10) {
break;
}
if (c != 13){
*buffer++ = (char)c;
index++;
}
}
*buffer = '\0';
if (c < 0){
return -1;
}else{
return index; // return number of characters, not including null terminator
}
}`

As mentioned earlier, text files can be read using readBytesUntil with the LineFeed character but with Windows files the CR character has to be dealt with. The advantages of readln are that:

- it neatly encapsulates the issues
- it works with both LF and CRLF terminated strings
- no additional cleanup work needed by the programmer

A quick Google search shows a number of questions in Stack Overflow, etc. requesting a solution to reading lines from text files.
I am using the function with LittleFS to read configuration key=value pairs from text files, and to read lines from html files for modification, and removal.
For me it is simple, and works reliably.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Beginne in stream.cpp, indem du das angeforderte Verhalten von readln mit readBytesUntil und der bestehenden Stream API vergleichst. Implementiere die im Issue beschriebenen Aspekte zur LF-Terminierung, zum Ignorieren von CR, zur Timeout-Behandlung, zur Null-Terminierung und zu den Rückgabewerten und überprüfe anschließend die API-Deklaration und die relevanten Stream-Tests, sofern vorhanden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
cpp
Bereich
api
Issue-Typ
Feature
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.