readFile (and File.read) now uses Flat Strings to allow more memory efficient reads (fix #932)

This commit is contained in:
Gordon Williams 2016-10-14 15:37:19 +01:00
parent 03907c76f5
commit c7f1a04dd4
3 changed files with 16 additions and 3 deletions

View File

@ -23,7 +23,8 @@
nRF52: BLE HID support and switchable Nordic UART
Fix STM32 regression where pinMode was set when it shouldn't have been
Add Third option to pinMode to allow the pin mode to be set while keeping it 'unforced'
save and dump now keep track of whether pin mode had been forced or not
Save and dump now keep track of whether pin mode had been forced or not
readFile (and File.read) now uses Flat Strings to allow more memory efficient reads (fix #932)
1v87 : Add support for compiling with float-abi=hard (even if it doesn't give us real-world benefits)
Add shortcut for quick execution of common call types

View File

@ -440,6 +440,7 @@ size_t jswrap_file_write(JsVar* parent, JsVar* buffer) {
Read data in a file in byte size chunks
*/
JsVar *jswrap_file_read(JsVar* parent, int length) {
if (length<0) length=0;
JsVar *buffer = 0;
JsvStringIterator it;
FRESULT res = 0;
@ -448,8 +449,19 @@ JsVar *jswrap_file_read(JsVar* parent, int length) {
JsFile file;
if (fileGetFromVar(&file, parent)) {
if(file.data.mode == FM_READ || file.data.mode == FM_READ_WRITE) {
char buf[32];
size_t actual = 0;
#ifndef LINUX
// if we're able to load this into a flat string, do it!
size_t len = f_size(&file.data.handle)-f_tell(&file.data.handle);
if (len>(size_t)length) len=(size_t)length;
buffer = jsvNewFlatStringOfLength(len);
if (buffer) {
res = f_read(&file.data.handle, jsvGetFlatStringPointer(buffer), len, &actual);
if (res) jsfsReportError("Unable to read file", res);
return buffer;
}
#endif
char buf[32];
while (bytesRead < (size_t)length) {
size_t requested = (size_t)length - bytesRead;

View File

@ -164,7 +164,7 @@ JsVar *jswrap_pin_getMode(JsVar *parent) {
Set the mode of the given pin. See [`pinMode`](#l__global_pinMode) for more information on pin modes.
*/
void jswrap_pin_mode(JsVar *parent, JsVar *mode) {
jswrap_io_pinMode(jshGetPinFromVar(parent), mode);
jswrap_io_pinMode(jshGetPinFromVar(parent), mode, false);
}
/*JSON{