mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
Merge pull request #664 from martinayotte/esp8266
fix dtostrf() issue, add Dir::fileSize
This commit is contained in:
commit
69e68943b1
@ -144,6 +144,14 @@ String Dir::fileName() {
|
|||||||
return _impl->fileName();
|
return _impl->fileName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t Dir::fileSize() {
|
||||||
|
if (!_impl) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _impl->fileSize();
|
||||||
|
}
|
||||||
|
|
||||||
bool Dir::next() {
|
bool Dir::next() {
|
||||||
if (!_impl) {
|
if (!_impl) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -78,6 +78,7 @@ public:
|
|||||||
|
|
||||||
File openFile(const char* mode);
|
File openFile(const char* mode);
|
||||||
String fileName();
|
String fileName();
|
||||||
|
size_t fileSize();
|
||||||
bool next();
|
bool next();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -56,6 +56,7 @@ public:
|
|||||||
virtual ~DirImpl() { }
|
virtual ~DirImpl() { }
|
||||||
virtual FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) = 0;
|
virtual FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) = 0;
|
||||||
virtual const char* fileName() = 0;
|
virtual const char* fileName() = 0;
|
||||||
|
virtual size_t fileSize() = 0;
|
||||||
virtual bool next() = 0;
|
virtual bool next() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -148,6 +148,7 @@ char* ultoa(unsigned long value, char* result, int base) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
|
char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
|
||||||
|
bool negative = false;
|
||||||
|
|
||||||
if (isnan(number)) {
|
if (isnan(number)) {
|
||||||
strcpy(s, "nan");
|
strcpy(s, "nan");
|
||||||
@ -158,50 +159,65 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (number > 4294967040.0 || number < -4294967040.0) {
|
|
||||||
strcpy(s, "ovf");
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
char* out = s;
|
char* out = s;
|
||||||
int signInt_Part = 1;
|
|
||||||
|
int fillme = width; // how many cells to fill for the integer part
|
||||||
|
if (prec > 0) {
|
||||||
|
fillme -= (prec+1);
|
||||||
|
}
|
||||||
|
|
||||||
// Handle negative numbers
|
// Handle negative numbers
|
||||||
if (number < 0.0) {
|
if (number < 0.0) {
|
||||||
signInt_Part = -1;
|
negative = true;
|
||||||
|
fillme--;
|
||||||
number = -number;
|
number = -number;
|
||||||
}
|
}
|
||||||
|
|
||||||
// calc left over digits
|
|
||||||
if (prec > 0)
|
|
||||||
{
|
|
||||||
width -= (prec + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Round correctly so that print(1.999, 2) prints as "2.00"
|
// Round correctly so that print(1.999, 2) prints as "2.00"
|
||||||
double rounding = 0.5;
|
// I optimized out most of the divisions
|
||||||
|
double rounding = 2.0;
|
||||||
for (uint8_t i = 0; i < prec; ++i)
|
for (uint8_t i = 0; i < prec; ++i)
|
||||||
rounding /= 10.0;
|
rounding *= 10.0;
|
||||||
|
rounding = 1.0 / rounding;
|
||||||
|
|
||||||
number += rounding;
|
number += rounding;
|
||||||
|
|
||||||
// Extract the integer part of the number and print it
|
// Figure out how big our number really is
|
||||||
unsigned long int_part = (unsigned long)number;
|
double tenpow = 1.0;
|
||||||
double remainder = number - (double)int_part;
|
int digitcount = 1;
|
||||||
out += sprintf(out, "%*ld", width, int_part * signInt_Part);
|
while (number >= 10.0 * tenpow) {
|
||||||
|
tenpow *= 10.0;
|
||||||
// Print the decimal point, but only if there are digits beyond
|
digitcount++;
|
||||||
if (prec > 0) {
|
|
||||||
*out = '.';
|
|
||||||
++out;
|
|
||||||
|
|
||||||
|
|
||||||
for (unsigned char decShift = prec; decShift > 0; decShift--) {
|
|
||||||
remainder *= 10.0;
|
|
||||||
}
|
|
||||||
sprintf(out, "%0*d", prec, (int)remainder);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
number /= tenpow;
|
||||||
|
fillme -= digitcount;
|
||||||
|
|
||||||
|
// Pad unused cells with spaces
|
||||||
|
while (fillme-- > 0) {
|
||||||
|
*out++ = ' ';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle negative sign
|
||||||
|
if (negative) *out++ = '-';
|
||||||
|
|
||||||
|
// Print the digits, and if necessary, the decimal point
|
||||||
|
digitcount += prec;
|
||||||
|
int8_t digit = 0;
|
||||||
|
while (digitcount-- > 0) {
|
||||||
|
digit = (int8_t)number;
|
||||||
|
if (digit > 9) digit = 9; // insurance
|
||||||
|
*out++ = (char)('0' | digit);
|
||||||
|
if ((digitcount == prec) && (prec > 0)) {
|
||||||
|
*out++ = '.';
|
||||||
|
}
|
||||||
|
number -= digit;
|
||||||
|
number *= 10.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure the string is terminated
|
||||||
|
*out = 0;
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -308,6 +308,13 @@ public:
|
|||||||
return (const char*) _dirent.name;
|
return (const char*) _dirent.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t fileSize() override {
|
||||||
|
if (!_valid)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return _dirent.size;
|
||||||
|
}
|
||||||
|
|
||||||
bool next() override {
|
bool next() override {
|
||||||
spiffs_dirent* result = SPIFFS_readdir(&_dir, &_dirent);
|
spiffs_dirent* result = SPIFFS_readdir(&_dir, &_dirent);
|
||||||
_valid = (result != nullptr);
|
_valid = (result != nullptr);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user