Skip to content

Commit d075d8b

Browse files
committed
Merge pull request #664 from martinayotte/esp8266
fix dtostrf() issue, add Dir::fileSize
2 parents 87d199f + 670e40e commit d075d8b

File tree

5 files changed

+66
-33
lines changed

5 files changed

+66
-33
lines changed

Diff for: hardware/esp8266com/esp8266/cores/esp8266/FS.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ String Dir::fileName() {
144144
return _impl->fileName();
145145
}
146146

147+
size_t Dir::fileSize() {
148+
if (!_impl) {
149+
return 0;
150+
}
151+
152+
return _impl->fileSize();
153+
}
154+
147155
bool Dir::next() {
148156
if (!_impl) {
149157
return false;

Diff for: hardware/esp8266com/esp8266/cores/esp8266/FS.h

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class Dir {
7878

7979
File openFile(const char* mode);
8080
String fileName();
81+
size_t fileSize();
8182
bool next();
8283

8384
protected:

Diff for: hardware/esp8266com/esp8266/cores/esp8266/FSImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class DirImpl {
5656
virtual ~DirImpl() { }
5757
virtual FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) = 0;
5858
virtual const char* fileName() = 0;
59+
virtual size_t fileSize() = 0;
5960
virtual bool next() = 0;
6061
};
6162

Diff for: hardware/esp8266com/esp8266/cores/esp8266/core_esp8266_noniso.c

+49-33
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ char* ultoa(unsigned long value, char* result, int base) {
148148
}
149149

150150
char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
151-
151+
bool negative = false;
152+
152153
if (isnan(number)) {
153154
strcpy(s, "nan");
154155
return s;
@@ -158,50 +159,65 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
158159
return s;
159160
}
160161

161-
if (number > 4294967040.0 || number < -4294967040.0) {
162-
strcpy(s, "ovf");
163-
return s;
164-
}
165-
166162
char* out = s;
167-
int signInt_Part = 1;
168-
163+
164+
int fillme = width; // how many cells to fill for the integer part
165+
if (prec > 0) {
166+
fillme -= (prec+1);
167+
}
168+
169169
// Handle negative numbers
170170
if (number < 0.0) {
171-
signInt_Part = -1;
171+
negative = true;
172+
fillme--;
172173
number = -number;
173174
}
174175

175-
// calc left over digits
176-
if (prec > 0)
177-
{
178-
width -= (prec + 1);
179-
}
180-
181176
// Round correctly so that print(1.999, 2) prints as "2.00"
182-
double rounding = 0.5;
177+
// I optimized out most of the divisions
178+
double rounding = 2.0;
183179
for (uint8_t i = 0; i < prec; ++i)
184-
rounding /= 10.0;
180+
rounding *= 10.0;
181+
rounding = 1.0 / rounding;
185182

186183
number += rounding;
187-
188-
// Extract the integer part of the number and print it
189-
unsigned long int_part = (unsigned long)number;
190-
double remainder = number - (double)int_part;
191-
out += sprintf(out, "%*ld", width, int_part * signInt_Part);
192-
193-
// Print the decimal point, but only if there are digits beyond
194-
if (prec > 0) {
195-
*out = '.';
196-
++out;
197-
198-
199-
for (unsigned char decShift = prec; decShift > 0; decShift--) {
200-
remainder *= 10.0;
201-
}
202-
sprintf(out, "%0*d", prec, (int)remainder);
184+
185+
// Figure out how big our number really is
186+
double tenpow = 1.0;
187+
int digitcount = 1;
188+
while (number >= 10.0 * tenpow) {
189+
tenpow *= 10.0;
190+
digitcount++;
191+
}
192+
193+
number /= tenpow;
194+
fillme -= digitcount;
195+
196+
// Pad unused cells with spaces
197+
while (fillme-- > 0) {
198+
*out++ = ' ';
199+
}
200+
201+
// Handle negative sign
202+
if (negative) *out++ = '-';
203+
204+
// Print the digits, and if necessary, the decimal point
205+
digitcount += prec;
206+
int8_t digit = 0;
207+
while (digitcount-- > 0) {
208+
digit = (int8_t)number;
209+
if (digit > 9) digit = 9; // insurance
210+
*out++ = (char)('0' | digit);
211+
if ((digitcount == prec) && (prec > 0)) {
212+
*out++ = '.';
213+
}
214+
number -= digit;
215+
number *= 10.0;
203216
}
204217

218+
// make sure the string is terminated
219+
*out = 0;
205220
return s;
206221
}
207222

223+

Diff for: hardware/esp8266com/esp8266/cores/esp8266/spiffs_api.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,13 @@ class SPIFFSDirImpl : public DirImpl {
308308
return (const char*) _dirent.name;
309309
}
310310

311+
size_t fileSize() override {
312+
if (!_valid)
313+
return 0;
314+
315+
return _dirent.size;
316+
}
317+
311318
bool next() override {
312319
spiffs_dirent* result = SPIFFS_readdir(&_dir, &_dirent);
313320
_valid = (result != nullptr);

0 commit comments

Comments
 (0)