/*JSON{ "type" : "kill", "generate" : "jswrap_file_kill" }*/ void jswrap_file_kill() { JsVar *arr = fsGetArray(false); if (arr) { JsvObjectIterator it; jsvObjectIteratorNew(&it, arr); while (jsvObjectIteratorHasValue(&it)) { JsVar *file = jsvObjectIteratorGetValue(&it); jswrap_file_close(file); jsvUnLock(file); jsvObjectIteratorNext(&it); } jsvObjectIteratorFree(&it); jsvRemoveAllChildren(arr); jsvUnLock(arr); } // close fs library #ifndef LINUX if (fat_initialised) { fat_initialised = false; f_mount(0, 0, 0); } #endif #ifdef SD_CARD_ANYWHERE sdSPISetup(0, PIN_UNDEFINED); #endif }
/*JSON{ "type" : "method", "class" : "File", "name" : "read", "generate" : "jswrap_file_read", "description" : [ "Read data in a file in byte size chunks"], "params" : [ ["length", "int32", "is an integer specifying the number of bytes to read."] ], "return" : [ "JsVar", "A string containing the characters that were read" ] }*/ JsVar *jswrap_file_read(JsVar* parent, int length) { JsVar *buffer = 0; JsvStringIterator it; FRESULT res = 0; size_t bytesRead = 0; if (jsfsInit()) { 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; while (bytesRead < (size_t)length) { size_t requested = (size_t)length - bytesRead; if (requested > sizeof( buf )) requested = sizeof( buf ); actual = 0; #ifndef LINUX res = f_read(&file.data.handle, buf, requested, &actual); if(res) break; #else actual = fread(buf, 1, requested, file.data.handle); #endif if (actual>0) { if (!buffer) { buffer = jsvNewFromEmptyString(); if (!buffer) return 0; // out of memory jsvStringIteratorNew(&it, buffer, 0); } size_t i; for (i=0;i<actual;i++) jsvStringIteratorAppend(&it, buf[i]); } bytesRead += actual; if(actual != requested) break; } fileSetVar(&file); } } } if (res) jsfsReportError("Unable to read file", res); if (buffer) jsvStringIteratorFree(&it); // automatically close this file if we're at the end of it if (bytesRead!=(size_t)length) jswrap_file_close(parent); return buffer; }
/*JSON{ "type":"kill", "generate" : "jswrap_file_kill" }*/ void jswrap_file_kill() { { JsVar *arr = fsGetArray(false); if (arr) { JsvArrayIterator it; jsvArrayIteratorNew(&it, arr); while (jsvArrayIteratorHasElement(&it)) { JsVar *file = jsvArrayIteratorGetElement(&it); jswrap_file_close(file); jsvUnLock(file); jsvArrayIteratorNext(&it); } jsvArrayIteratorFree(&it); jsvRemoveAllChildren(arr); jsvUnLock(arr); } } }
JsVar *jswrap_fs_readdir(JsVar *path) { JsVar *arr = 0; // undefined unless we can open card char pathStr[JS_DIR_BUF_SIZE] = ""; if (!jsvIsUndefined(path)) jsvGetString(path, pathStr, JS_DIR_BUF_SIZE); #ifdef LINUX if (!pathStr[0]) strcpy(pathStr, "."); // deal with empty readdir #endif FRESULT res = 0; if (jsfsInit()) { #ifndef LINUX DIR dirs; if ((res=f_opendir(&dirs, pathStr)) == FR_OK) { FILINFO Finfo; #if _USE_LFN!=0 char lfnBuf[_MAX_LFN+1]; Finfo.lfname = lfnBuf; Finfo.lfsize = sizeof(lfnBuf); #endif #else DIR *dir = opendir(pathStr); if(dir) { #endif arr = jsvNewWithFlags(JSV_ARRAY); if (arr) { // could be out of memory #ifndef LINUX while (((res=f_readdir(&dirs, &Finfo)) == FR_OK) && Finfo.fname[0]) { char *fn = GET_FILENAME(Finfo); #else struct dirent *pDir=NULL; while((pDir = readdir(dir)) != NULL) { char *fn = (*pDir).d_name; #endif JsVar *fnVar = jsvNewFromString(fn); if (fnVar) {// out of memory? jsvArrayPush(arr, fnVar); jsvUnLock(fnVar); } } } #ifdef LINUX closedir(dir); #endif } } if (res) jsfsReportError("Unable to list files", res); return arr; } /*JSON{ "type" : "staticmethod", "class" : "fs", "name" : "writeFile", "generate_full" : " jswrap_fs_writeOrAppendFile(path, data, false)", "description" : [ "Write the data to the given file", "NOTE: Espruino does not yet support Async file IO, so this function behaves like the 'Sync' version." ], "params" : [ [ "path", "JsVar", "The path of the file to write" ], [ "data", "JsVar", "The data to write to the file" ] ], "return" : [ "bool", "True on success, false on failure" ] }*/ /*JSON{ "type" : "staticmethod", "class" : "fs", "name" : "writeFileSync", "ifndef" : "SAVE_ON_FLASH", "generate_full" : " jswrap_fs_writeOrAppendFile(path, data, false)", "description" : [ "Write the data to the given file" ], "params" : [ [ "path", "JsVar", "The path of the file to write" ], [ "data", "JsVar", "The data to write to the file" ] ], "return" : [ "bool", "True on success, false on failure" ] }*/ /*JSON{ "type" : "staticmethod", "class" : "fs", "name" : "appendFile", "generate_full" : " jswrap_fs_writeOrAppendFile(path, data, true)", "description" : [ "Append the data to the given file, created a new file if it doesn't exist", "NOTE: Espruino does not yet support Async file IO, so this function behaves like the 'Sync' version." ], "params" : [ [ "path", "JsVar", "The path of the file to write" ], [ "data", "JsVar", "The data to write to the file" ] ], "return" : [ "bool", "True on success, false on failure" ] }*/ /*JSON{ "type" : "staticmethod", "class" : "fs", "name" : "appendFileSync", "ifndef" : "SAVE_ON_FLASH", "generate_full" : "jswrap_fs_writeOrAppendFile(path, data, true)", "description" : [ "Append the data to the given file, created a new file if it doesn't exist" ], "params" : [ [ "path", "JsVar", "The path of the file to write" ], [ "data", "JsVar", "The data to write to the file" ] ], "return" : [ "bool", "True on success, false on failure" ] }*/ bool jswrap_fs_writeOrAppendFile(JsVar *path, JsVar *data, bool append) { JsVar *fMode = jsvNewFromString(append ? "a" : "w"); JsVar *f = jswrap_E_openFile(path, fMode); jsvUnLock(fMode); if (!f) return 0; size_t amt = jswrap_file_write(f, data); jswrap_file_close(f); jsvUnLock(f); return amt>0; } /*JSON{ "type" : "staticmethod", "class" : "fs", "name" : "readFile", "generate" : "jswrap_fs_readFile", "description" : [ "Read all data from a file and return as a string", "NOTE: Espruino does not yet support Async file IO, so this function behaves like the 'Sync' version." ], "params" : [ [ "path", "JsVar", "The path of the file to read" ] ], "return" : [ "JsVar", "A string containing the contents of the file (or undefined if the file doesn't exist)" ] }*/ /*JSON{ "type" : "staticmethod", "class" : "fs", "name" : "readFileSync", "ifndef" : "SAVE_ON_FLASH", "generate" : "jswrap_fs_readFile", "description" : [ "Read all data from a file and return as a string.","**Note:** The size of files you can load using this method is limited by the amount of available RAM. To read files a bit at a time, see the `File` class." ], "params" : [ [ "path", "JsVar", "The path of the file to read" ] ], "return" : [ "JsVar", "A string containing the contents of the file (or undefined if the file doesn't exist)" ] }*/ JsVar *jswrap_fs_readFile(JsVar *path) { JsVar *fMode = jsvNewFromString("r"); JsVar *f = jswrap_E_openFile(path, fMode); jsvUnLock(fMode); if (!f) return 0; JsVar *buffer = jswrap_file_read(f, 0x7FFFFFFF); jswrap_file_close(f); jsvUnLock(f); return buffer; }