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