s

Fork of FATFileSystem by Chaiyaporn Boonyasathian

Committer:
cha45689
Date:
Fri Dec 02 19:37:14 2016 +0000
Revision:
0:9296bb2a98a8
ss

Who changed what in which revision?

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