J&W / Mbed 2 deprecated Rejestrator

Dependencies:   mbed Rejestrator

Dependents:   Rejestrator

Committer:
Waldek
Date:
Sat Apr 18 17:01:57 2015 +0000
Revision:
0:fa31f8461c63
working version, stop

Who changed what in which revision?

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