Norimasa Okamoto / F32RFileSystem

Fork of F12RFileSystem by Norimasa Okamoto

Revision:
7:f9f52d9c0c57
Parent:
6:3c5b3606e019
--- a/F12RFileHandle.cpp	Sat Oct 31 00:30:21 2015 +0000
+++ b/F12RFileHandle.cpp	Wed Nov 11 19:47:04 2015 +0900
@@ -1,90 +1,76 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2015 ARM Limited
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-#include "ff.h"
-#include "ffconf.h"
-#include "mbed_debug.h"
-
-#include "F12RFileHandle.h"
-
-F12RFileHandle::F12RFileHandle(FIL fh) {
-    _fh = fh;
-}
-
-int F12RFileHandle::close() {
-    int retval = f_close(&_fh);
-    delete this;
-    return retval;
-}
-
-ssize_t F12RFileHandle::write(const void* buffer, size_t length) {
-    UINT n;
-    FRESULT res = f_write(&_fh, buffer, length, &n);
-    if (res) {
-        debug_if(FFS_DBG, "f_write() failed: %d", res);
-        return -1;
-    }
-    return n;
-}
-
-ssize_t F12RFileHandle::read(void* buffer, size_t length) {
-    debug_if(FFS_DBG, "read(%d)\n", length);
-    UINT n;
-    FRESULT res = f_read(&_fh, buffer, length, &n);
-    if (res) {
-        debug_if(FFS_DBG, "f_read() failed: %d\n", res);
-        return -1;
-    }
-    return n;
-}
-
-int F12RFileHandle::isatty() {
-    return 0;
-}
-
-off_t F12RFileHandle::lseek(off_t position, int whence) {
-    if (whence == SEEK_END) {
-        position += _fh.fsize;
-    } else if(whence==SEEK_CUR) {
-        position += _fh.fptr;
-    }
-    FRESULT res = f_lseek(&_fh, position);
-    if (res) {
-        debug_if(FFS_DBG, "lseek failed: %d\n", res);
-        return -1;
-    } else {
-        debug_if(FFS_DBG, "lseek OK, returning %i\n", _fh.fptr);
-        return _fh.fptr;
-    }
-}
-
-int F12RFileHandle::fsync() {
-    FRESULT res = f_sync(&_fh);
-    if (res) {
-        debug_if(FFS_DBG, "f_sync() failed: %d\n", res);
-        return -1;
-    }
-    return 0;
-}
-
-off_t F12RFileHandle::flen() {
-    return _fh.fsize;
-}
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2016 ARM Limited
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#include <algorithm>
+#include "F12RFileHandle.h"
+#include "fsdebug.h"
+
+F12RFileHandle::F12RFileHandle(F12RFileSystem& fs_, uint32_t d):fs(fs_) {
+    pos = 0;
+    cluster = fs.storage_peek(d + 26, 2);
+    file_length = fs.storage_peek(d + 28, 4);
+    FS_DBG("cluster=%d", cluster);
+    FS_DBG("file_length=%d", file_length);
+}
+
+int F12RFileHandle::close() {
+    delete this;
+    return 0;
+}
+
+ssize_t F12RFileHandle::write(const void* buffer, size_t length) {
+    return -1;
+}
+
+ssize_t F12RFileHandle::read(void* buffer, size_t length) {
+    FS_DBG("length=%d", length);
+    ssize_t n = 0;
+    while(n < length && pos < file_length) {
+        uint32_t chunk = std::min(length - n, file_length - pos);
+        chunk = std::min(chunk, fs.cluster_tail(pos));
+        FS_TEST_ASSERT(cluster >= 2 && cluster < 0xff8);
+        fs.storage->storage_read((fs.base_data + cluster - 2)*512 + fs.cluster_head(pos), (uint8_t*)buffer + n, chunk);
+        pos += chunk;
+        if (fs.cluster_head(pos) == 0) {
+            cluster = fs.fat_read(cluster);
+        }
+        n += chunk;
+    }
+    return n;
+}
+
+int F12RFileHandle::isatty() {
+    return 0;
+}
+
+off_t F12RFileHandle::lseek(off_t position, int whence) {
+    FS_DBG("position=%d whence=%d", (uint32_t)position, whence);
+    return -1;
+}
+
+int F12RFileHandle::fsync() {
+    return 0;
+}
+
+off_t F12RFileHandle::flen() {
+    FS_DBG("file_length=%d", file_length);
+    return file_length;
+}