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