FAT

Dependents:   SDFileSystem_Axelta

Committer:
gauthibit
Date:
Fri Nov 11 06:39:00 2016 +0000
Revision:
0:6547fbc0f7c0
FAT

Who changed what in which revision?

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