arduino / arduino/ArduinoCore-API

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

オープン
#251 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る
enhancement
主要言語
C++
スター
306
フォーク
150
PR マージ指標
30日以内にマージされた PR はありません

説明

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

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

調査の方向性

stream.cpp で、要求された readln の動作を readBytesUntil および既存の Stream API と比較することから始めてください。issue に記載されている LF 終端、CR の無視、タイムアウト処理、null 終端、戻り値を実装し、その後、API 宣言と関連する Stream テストがあれば確認してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
cpp
領域
api
issue の種類
機能追加
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
48/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。