Skip to content

Commit e43a586

Browse files
apicquotdevyte
authored andcommitted
Improved readString() for File (#5445)
* added new boards * corrected block size error * added board to last commit working well * override of readString for File class Stream::readString requires a timeout of 1 seconds + multiple memory resize * correct indent return if size 0 before reserve * correct indent * good indent * stricter test for end of string * same implementation than Stream by replacing timeRead by read * reading file by block of 256 * make sure there is an end of string * fixed bug for file size multiple of 256 string::concate(char*) needs a string terminator to work
1 parent 8c01516 commit e43a586

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

Diff for: cores/esp8266/FS.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ const char* File::name() const {
121121
return _p->name();
122122
}
123123

124+
String File::readString()
125+
{
126+
String ret;
127+
ret.reserve(size() - position());
128+
char temp[256+1];
129+
int countRead = readBytes(temp, sizeof(temp)-1);
130+
while (countRead > 0)
131+
{
132+
temp[countRead] = 0;
133+
ret += temp;
134+
countRead = readBytes(temp, sizeof(temp)-1);
135+
}
136+
return ret;
137+
}
138+
124139
File Dir::openFile(const char* mode) {
125140
if (!_impl) {
126141
return File();

Diff for: cores/esp8266/FS.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class File : public Stream
7272
void close();
7373
operator bool() const;
7474
const char* name() const;
75-
75+
String readString() override;
76+
7677
protected:
7778
FileImplPtr _p;
7879
};

Diff for: cores/esp8266/Stream.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class Stream: public Print {
101101
// returns the number of characters placed in the buffer (0 means no valid data found)
102102

103103
// Arduino String functions to be added here
104-
String readString();
104+
virtual String readString();
105105
String readStringUntil(char terminator);
106106

107107
protected:

0 commit comments

Comments
 (0)