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