Skip to content

Commit caa6170

Browse files
committed
Remove floating point usage due to fscanf()
* Replace `fscanf` with a combination of `fgets` and `strtol` as `fscanf` implementation is quite heavy due to the support for floating point. * Update the README as the default ARM compiler is no longer Arm Compiler 5 and the application size has changed.
1 parent 6b1d0a5 commit caa6170

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

README.md

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ cd mbed-os-example-filesystem
4545
#### Compile the example
4646

4747
Invoke `mbed compile`, and specify the name of your platform and your favorite
48-
toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the ARM Compiler 5:
48+
toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the ARM toolchain:
4949

5050
```
5151
mbed compile -m K64F -t ARM
@@ -56,26 +56,25 @@ following result:
5656

5757
```
5858
[snip]
59-
+--------------------------+-------+-------+-------+
60-
| Module | .text | .data | .bss |
61-
+--------------------------+-------+-------+-------+
62-
| Fill | 164 | 0 | 2136 |
63-
| Misc | 54505 | 2556 | 754 |
64-
| drivers | 640 | 0 | 32 |
65-
| features/filesystem | 15793 | 0 | 550 |
66-
| features/storage | 42 | 0 | 184 |
67-
| hal | 418 | 0 | 8 |
68-
| platform | 2355 | 20 | 582 |
69-
| rtos | 135 | 4 | 4 |
70-
| rtos/rtx | 5861 | 20 | 6870 |
71-
| targets/TARGET_Freescale | 8382 | 12 | 384 |
72-
| Subtotals | 88295 | 2612 | 11504 |
73-
+--------------------------+-------+-------+-------+
74-
Allocated Heap: 24576 bytes
75-
Allocated Stack: unknown
76-
Total Static RAM memory (data + bss): 14116 bytes
77-
Total RAM memory (data + bss + heap + stack): 38692 bytes
78-
Total Flash memory (text + data + misc): 91947 bytes
59+
| Module | .text |.data | .bss |
60+
|---------------------|--------|------|--------|
61+
| [lib]/c_w.l | 13137 | 16 | 348 |
62+
| [lib]/fz_wm.l | 34 | 0 | 0 |
63+
| [lib]/libcppabi_w.l | 44 | 0 | 0 |
64+
| [lib]/m_wm.l | 48 | 0 | 0 |
65+
| anon$$obj.o | 32 | 0 | 197888 |
66+
| main.o | 2406 | 0 | 256 |
67+
| mbed-os/components | 5568 | 0 | 0 |
68+
| mbed-os/drivers | 2700 | 0 | 1136 |
69+
| mbed-os/events | 1716 | 0 | 3108 |
70+
| mbed-os/features | 16586 | 0 | 509 |
71+
| mbed-os/hal | 1622 | 4 | 67 |
72+
| mbed-os/platform | 7009 | 64 | 542 |
73+
| mbed-os/rtos | 12132 | 168 | 6634 |
74+
| mbed-os/targets | 19773 | 12 | 985 |
75+
| Subtotals | 82807 | 264 | 211473 |
76+
Total Static RAM memory (data + bss): 211737 bytes
77+
Total Flash memory (text + data): 83071 bytes
7978
8079
Image: ./BUILD/K64F/ARM/mbed-os-example-filesystem.bin
8180
```

main.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
#include "BlockDevice.h"
2121

22+
// Maximum number of elements in buffer
23+
#define BUFFER_MAX_LEN 10
24+
2225
// This will take the system's default block device
2326
BlockDevice *bd = BlockDevice::get_default_instance();
2427

@@ -135,8 +138,19 @@ int main() {
135138
long pos = ftell(f);
136139

137140
// Parse out the number and increment
138-
int32_t number;
139-
fscanf(f, "%d", &number);
141+
char buf[BUFFER_MAX_LEN];
142+
if (!fgets(buf, BUFFER_MAX_LEN, f)) {
143+
error("error: %s (%d)\n", strerror(errno), -errno);
144+
}
145+
char *endptr;
146+
int32_t number = strtol(buf, &endptr, 10);
147+
if (
148+
(errno == ERANGE) || // The number is too small/large
149+
(endptr == buf) || // No character was read
150+
(*endptr && *endptr != '\n') // The whole input was not converted
151+
) {
152+
continue;
153+
}
140154
number += 1;
141155

142156
// Seek to beginning of number

0 commit comments

Comments
 (0)