From e43a586f0d53f33c51dfe1a63cddaf04383b3c52 Mon Sep 17 00:00:00 2001 From: apicquot Date: Mon, 4 Feb 2019 03:08:54 -0500 Subject: [PATCH] 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 --- cores/esp8266/FS.cpp | 15 +++++++++++++++ cores/esp8266/FS.h | 3 ++- cores/esp8266/Stream.h | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/FS.cpp b/cores/esp8266/FS.cpp index 6ae11e178..90de4f3c4 100644 --- a/cores/esp8266/FS.cpp +++ b/cores/esp8266/FS.cpp @@ -121,6 +121,21 @@ const char* File::name() const { return _p->name(); } +String File::readString() +{ + String ret; + ret.reserve(size() - position()); + char temp[256+1]; + int countRead = readBytes(temp, sizeof(temp)-1); + while (countRead > 0) + { + temp[countRead] = 0; + ret += temp; + countRead = readBytes(temp, sizeof(temp)-1); + } + return ret; +} + File Dir::openFile(const char* mode) { if (!_impl) { return File(); diff --git a/cores/esp8266/FS.h b/cores/esp8266/FS.h index 79620f96c..3cf34bb74 100644 --- a/cores/esp8266/FS.h +++ b/cores/esp8266/FS.h @@ -72,7 +72,8 @@ public: void close(); operator bool() const; const char* name() const; - + String readString() override; + protected: FileImplPtr _p; }; diff --git a/cores/esp8266/Stream.h b/cores/esp8266/Stream.h index 290971cf4..fa786dddc 100644 --- a/cores/esp8266/Stream.h +++ b/cores/esp8266/Stream.h @@ -101,7 +101,7 @@ class Stream: public Print { // returns the number of characters placed in the buffer (0 means no valid data found) // Arduino String functions to be added here - String readString(); + virtual String readString(); String readStringUntil(char terminator); protected: