publication inutile mais obligée

Committer:
adrevong
Date:
Tue Jan 28 11:14:21 2020 +0000
Revision:
0:8b2a15992487

        

Who changed what in which revision?

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