1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-17 12:02:15 +03:00

Reimplement SD.h write methods exactly in File (#5861)

* Reimplement SD.h write methods exactly in File

Replace the individual override with the existing SD.h File's
implementation for all methods of File::write.

Fixes #5846

* Add add'l tests

* Fix Print and File incompatible writes w/casts

Print and File have ambiguous resolutions for single-argument
write(0)s.

Fix by adding explicit methods.  A write of any integer will not be a
const char* write (i.e. won't write a string) but will instead just
write the integer truncated to 8 bits (as makes sense).

* Use 256byte chunks in ::write template

Reduce stack requirements for templated writes to 256bytes, matching the
size uses in WiFiClient/etc. (from 512bytes).  Reduces the chance of
stack overflow.

* Move write(int) methods up to Print.h

Remove some technical debt by moving the ::write(int/short/long) methods
out of FS and HardwareSerial and up into Print.h.
This commit is contained in:
Earle F. Philhower, III
2019-03-12 13:58:00 -07:00
committed by GitHub
parent 192aaa4291
commit 02f54e85fd
4 changed files with 58 additions and 17 deletions

View File

@ -80,7 +80,29 @@ public:
bool isDirectory() const;
// Arduino "class SD" methods for compatibility
size_t write(const char *str) { return write((const uint8_t*)str, strlen(str)); }
template<typename T> size_t write(T &src){
uint8_t obuf[256];
size_t doneLen = 0;
size_t sentLen;
int i;
while (src.available() > sizeof(obuf)){
src.read(obuf, sizeof(obuf));
sentLen = write(obuf, sizeof(obuf));
doneLen = doneLen + sentLen;
if(sentLen != sizeof(obuf)){
return doneLen;
}
}
size_t leftLen = src.available();
src.read(obuf, leftLen);
sentLen = write(obuf, leftLen);
doneLen = doneLen + sentLen;
return doneLen;
}
using Print::write;
void rewindDirectory();
File openNextFile();

View File

@ -152,22 +152,6 @@ public:
{
return uart_write_char(_uart, c);
}
inline size_t write(unsigned long n)
{
return write((uint8_t) n);
}
inline size_t write(long n)
{
return write((uint8_t) n);
}
inline size_t write(unsigned int n)
{
return write((uint8_t) n);
}
inline size_t write(int n)
{
return write((uint8_t) n);
}
size_t write(const uint8_t *buffer, size_t size) override
{
return uart_write(_uart, (const char*)buffer, size);

View File

@ -62,6 +62,13 @@ class Print {
size_t write(const char *buffer, size_t size) {
return write((const uint8_t *) buffer, size);
}
// These handle ambiguity for write(0) case, because (0) can be a pointer or an integer
size_t write(short t) { return write((uint8_t)t); }
size_t write(unsigned short t) { return write((uint8_t)t); }
size_t write(int t) { return write((uint8_t)t); }
size_t write(unsigned int t) { return write((uint8_t)t); }
size_t write(long t) { return write((uint8_t)t); }
size_t write(unsigned long t) { return write((uint8_t)t); }
size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3)));
size_t printf_P(PGM_P format, ...) __attribute__((format(printf, 2, 3)));