forked version of FATFileSystemEx
Fork of FATFileSystem by
FATFileHandle.cpp@4:c9dcfcfd0c5a, 2014-08-20 (annotated)
- Committer:
- bkc_mbed
- Date:
- Wed Aug 20 00:20:41 2014 +0000
- Revision:
- 4:c9dcfcfd0c5a
- Parent:
- 2:b6669c987c8e
added tiny mode
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:46ce1e16c870 | 1 | /* mbed Microcontroller Library |
emilmont | 1:46ce1e16c870 | 2 | * Copyright (c) 2006-2012 ARM Limited |
emilmont | 1:46ce1e16c870 | 3 | * |
emilmont | 1:46ce1e16c870 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
emilmont | 1:46ce1e16c870 | 5 | * of this software and associated documentation files (the "Software"), to deal |
emilmont | 1:46ce1e16c870 | 6 | * in the Software without restriction, including without limitation the rights |
emilmont | 1:46ce1e16c870 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
emilmont | 1:46ce1e16c870 | 8 | * copies of the Software, and to permit persons to whom the Software is |
emilmont | 1:46ce1e16c870 | 9 | * furnished to do so, subject to the following conditions: |
emilmont | 1:46ce1e16c870 | 10 | * |
emilmont | 1:46ce1e16c870 | 11 | * The above copyright notice and this permission notice shall be included in |
emilmont | 1:46ce1e16c870 | 12 | * all copies or substantial portions of the Software. |
emilmont | 1:46ce1e16c870 | 13 | * |
emilmont | 1:46ce1e16c870 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
emilmont | 1:46ce1e16c870 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
emilmont | 1:46ce1e16c870 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
emilmont | 1:46ce1e16c870 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
emilmont | 1:46ce1e16c870 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
emilmont | 1:46ce1e16c870 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
emilmont | 1:46ce1e16c870 | 20 | * SOFTWARE. |
emilmont | 1:46ce1e16c870 | 21 | */ |
emilmont | 1:46ce1e16c870 | 22 | #include "ff.h" |
emilmont | 1:46ce1e16c870 | 23 | #include "ffconf.h" |
emilmont | 2:b6669c987c8e | 24 | #include "mbed_debug.h" |
emilmont | 1:46ce1e16c870 | 25 | |
emilmont | 1:46ce1e16c870 | 26 | #include "FATFileHandle.h" |
emilmont | 1:46ce1e16c870 | 27 | |
emilmont | 1:46ce1e16c870 | 28 | FATFileHandle::FATFileHandle(FIL fh) { |
emilmont | 1:46ce1e16c870 | 29 | _fh = fh; |
emilmont | 1:46ce1e16c870 | 30 | } |
emilmont | 1:46ce1e16c870 | 31 | |
emilmont | 1:46ce1e16c870 | 32 | int FATFileHandle::close() { |
emilmont | 1:46ce1e16c870 | 33 | int retval = f_close(&_fh); |
emilmont | 1:46ce1e16c870 | 34 | delete this; |
emilmont | 1:46ce1e16c870 | 35 | return retval; |
emilmont | 1:46ce1e16c870 | 36 | } |
emilmont | 1:46ce1e16c870 | 37 | |
emilmont | 1:46ce1e16c870 | 38 | ssize_t FATFileHandle::write(const void* buffer, size_t length) { |
bkc_mbed | 4:c9dcfcfd0c5a | 39 | #ifdef FAT_TINY |
bkc_mbed | 4:c9dcfcfd0c5a | 40 | return -1; |
bkc_mbed | 4:c9dcfcfd0c5a | 41 | #else |
emilmont | 1:46ce1e16c870 | 42 | UINT n; |
bkc_mbed | 4:c9dcfcfd0c5a | 43 | |
emilmont | 1:46ce1e16c870 | 44 | FRESULT res = f_write(&_fh, buffer, length, &n); |
emilmont | 1:46ce1e16c870 | 45 | if (res) { |
emilmont | 1:46ce1e16c870 | 46 | debug_if(FFS_DBG, "f_write() failed: %d", res); |
emilmont | 1:46ce1e16c870 | 47 | return -1; |
emilmont | 1:46ce1e16c870 | 48 | } |
emilmont | 1:46ce1e16c870 | 49 | return n; |
bkc_mbed | 4:c9dcfcfd0c5a | 50 | #endif |
emilmont | 1:46ce1e16c870 | 51 | } |
emilmont | 1:46ce1e16c870 | 52 | |
emilmont | 1:46ce1e16c870 | 53 | ssize_t FATFileHandle::read(void* buffer, size_t length) { |
emilmont | 1:46ce1e16c870 | 54 | debug_if(FFS_DBG, "read(%d)\n", length); |
emilmont | 1:46ce1e16c870 | 55 | UINT n; |
emilmont | 1:46ce1e16c870 | 56 | FRESULT res = f_read(&_fh, buffer, length, &n); |
emilmont | 1:46ce1e16c870 | 57 | if (res) { |
emilmont | 1:46ce1e16c870 | 58 | debug_if(FFS_DBG, "f_read() failed: %d\n", res); |
emilmont | 1:46ce1e16c870 | 59 | return -1; |
emilmont | 1:46ce1e16c870 | 60 | } |
emilmont | 1:46ce1e16c870 | 61 | return n; |
emilmont | 1:46ce1e16c870 | 62 | } |
emilmont | 1:46ce1e16c870 | 63 | |
emilmont | 1:46ce1e16c870 | 64 | int FATFileHandle::isatty() { |
emilmont | 1:46ce1e16c870 | 65 | return 0; |
emilmont | 1:46ce1e16c870 | 66 | } |
emilmont | 1:46ce1e16c870 | 67 | |
emilmont | 1:46ce1e16c870 | 68 | off_t FATFileHandle::lseek(off_t position, int whence) { |
emilmont | 1:46ce1e16c870 | 69 | if (whence == SEEK_END) { |
emilmont | 1:46ce1e16c870 | 70 | position += _fh.fsize; |
emilmont | 1:46ce1e16c870 | 71 | } else if(whence==SEEK_CUR) { |
emilmont | 1:46ce1e16c870 | 72 | position += _fh.fptr; |
emilmont | 1:46ce1e16c870 | 73 | } |
emilmont | 1:46ce1e16c870 | 74 | FRESULT res = f_lseek(&_fh, position); |
emilmont | 1:46ce1e16c870 | 75 | if (res) { |
emilmont | 1:46ce1e16c870 | 76 | debug_if(FFS_DBG, "lseek failed: %d\n", res); |
emilmont | 1:46ce1e16c870 | 77 | return -1; |
emilmont | 1:46ce1e16c870 | 78 | } else { |
emilmont | 1:46ce1e16c870 | 79 | debug_if(FFS_DBG, "lseek OK, returning %i\n", _fh.fptr); |
emilmont | 1:46ce1e16c870 | 80 | return _fh.fptr; |
emilmont | 1:46ce1e16c870 | 81 | } |
emilmont | 1:46ce1e16c870 | 82 | } |
emilmont | 1:46ce1e16c870 | 83 | |
emilmont | 1:46ce1e16c870 | 84 | int FATFileHandle::fsync() { |
bkc_mbed | 4:c9dcfcfd0c5a | 85 | #ifdef FAT_TINY |
bkc_mbed | 4:c9dcfcfd0c5a | 86 | return -1; |
bkc_mbed | 4:c9dcfcfd0c5a | 87 | #else |
emilmont | 1:46ce1e16c870 | 88 | FRESULT res = f_sync(&_fh); |
emilmont | 1:46ce1e16c870 | 89 | if (res) { |
emilmont | 1:46ce1e16c870 | 90 | debug_if(FFS_DBG, "f_sync() failed: %d\n", res); |
emilmont | 1:46ce1e16c870 | 91 | return -1; |
emilmont | 1:46ce1e16c870 | 92 | } |
emilmont | 1:46ce1e16c870 | 93 | return 0; |
bkc_mbed | 4:c9dcfcfd0c5a | 94 | #endif |
emilmont | 1:46ce1e16c870 | 95 | } |
emilmont | 1:46ce1e16c870 | 96 | |
emilmont | 1:46ce1e16c870 | 97 | off_t FATFileHandle::flen() { |
emilmont | 1:46ce1e16c870 | 98 | return _fh.fsize; |
emilmont | 1:46ce1e16c870 | 99 | } |