FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Thu May 09 14:36:51 2019 +0000
Revision:
140:d8634e76ecce
Parent:
129:b47c28c7eaaf
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

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