arduino / arduino/ArduinoCore-API

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

Ouverte
#251 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub
enhancement
Langage dominant
C++
Étoiles
306
Forks
150
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Piste de recherche

Commencez dans stream.cpp en comparant le comportement demandé de readln avec readBytesUntil et l’API Stream existante. Implémentez la terminaison par LF, l’ignorance de CR, la gestion du délai d’attente, la terminaison par caractère nul et les valeurs de retour décrites dans l’issue, puis vérifiez la déclaration de l’API et les tests Stream pertinents s’ils existent.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
cpp
Domaine
api
Type d'issue
Fonctionnalité
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
48/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.