arduino / arduino/ArduinoCore-API

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

Aperta
#251 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
enhancement
Lingua principale
C++
Stelle
306
Fork
150
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

### 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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

Inizia in stream.cpp confrontando il comportamento richiesto di readln con readBytesUntil e l’API Stream esistente. Implementa la terminazione con LF, l’ignoranza di CR, la gestione del timeout, la terminazione null e i valori restituiti descritti nell’issue, quindi verifica la dichiarazione dell’API e i test Stream pertinenti, se presenti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
cpp
Ambito
api
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.