ahmed ahmed
/
samir2_alti_logger
dd
FileSystem/FatFileSystem/FATFileHandle.cpp@0:caecb0d1e7d3, 2015-04-08 (annotated)
- Committer:
- pfe
- Date:
- Wed Apr 08 08:27:55 2015 +0000
- Revision:
- 0:caecb0d1e7d3
ddd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pfe | 0:caecb0d1e7d3 | 1 | /* mbed Microcontroller Library - FATFileHandle |
pfe | 0:caecb0d1e7d3 | 2 | * Copyright (c) 2008, sford |
pfe | 0:caecb0d1e7d3 | 3 | */ |
pfe | 0:caecb0d1e7d3 | 4 | |
pfe | 0:caecb0d1e7d3 | 5 | #include "FATFileHandle.h" |
pfe | 0:caecb0d1e7d3 | 6 | |
pfe | 0:caecb0d1e7d3 | 7 | #include <stdio.h> |
pfe | 0:caecb0d1e7d3 | 8 | #include <stdlib.h> |
pfe | 0:caecb0d1e7d3 | 9 | #include "ff.h" |
pfe | 0:caecb0d1e7d3 | 10 | #include "FATFileSystem.h" |
pfe | 0:caecb0d1e7d3 | 11 | |
pfe | 0:caecb0d1e7d3 | 12 | namespace mbed { |
pfe | 0:caecb0d1e7d3 | 13 | |
pfe | 0:caecb0d1e7d3 | 14 | #if FFSDEBUG_ENABLED |
pfe | 0:caecb0d1e7d3 | 15 | static const char *FR_ERRORS[] = { |
pfe | 0:caecb0d1e7d3 | 16 | "FR_OK = 0", |
pfe | 0:caecb0d1e7d3 | 17 | "FR_NOT_READY", |
pfe | 0:caecb0d1e7d3 | 18 | "FR_NO_FILE", |
pfe | 0:caecb0d1e7d3 | 19 | "FR_NO_PATH", |
pfe | 0:caecb0d1e7d3 | 20 | "FR_INVALID_NAME", |
pfe | 0:caecb0d1e7d3 | 21 | "FR_INVALID_DRIVE", |
pfe | 0:caecb0d1e7d3 | 22 | "FR_DENIED", |
pfe | 0:caecb0d1e7d3 | 23 | "FR_EXIST", |
pfe | 0:caecb0d1e7d3 | 24 | "FR_RW_ERROR", |
pfe | 0:caecb0d1e7d3 | 25 | "FR_WRITE_PROTECTED", |
pfe | 0:caecb0d1e7d3 | 26 | "FR_NOT_ENABLED", |
pfe | 0:caecb0d1e7d3 | 27 | "FR_NO_FILESYSTEM", |
pfe | 0:caecb0d1e7d3 | 28 | "FR_INVALID_OBJECT", |
pfe | 0:caecb0d1e7d3 | 29 | "FR_MKFS_ABORTED" |
pfe | 0:caecb0d1e7d3 | 30 | }; |
pfe | 0:caecb0d1e7d3 | 31 | #endif |
pfe | 0:caecb0d1e7d3 | 32 | |
pfe | 0:caecb0d1e7d3 | 33 | FATFileHandle::FATFileHandle(FIL fh) { |
pfe | 0:caecb0d1e7d3 | 34 | _fh = fh; |
pfe | 0:caecb0d1e7d3 | 35 | } |
pfe | 0:caecb0d1e7d3 | 36 | |
pfe | 0:caecb0d1e7d3 | 37 | int FATFileHandle::close() { |
pfe | 0:caecb0d1e7d3 | 38 | FFSDEBUG("close\n"); |
pfe | 0:caecb0d1e7d3 | 39 | int retval = f_close(&_fh); |
pfe | 0:caecb0d1e7d3 | 40 | delete this; |
pfe | 0:caecb0d1e7d3 | 41 | return retval; |
pfe | 0:caecb0d1e7d3 | 42 | } |
pfe | 0:caecb0d1e7d3 | 43 | |
pfe | 0:caecb0d1e7d3 | 44 | ssize_t FATFileHandle::write(const void* buffer, size_t length) { |
pfe | 0:caecb0d1e7d3 | 45 | FFSDEBUG("write(%d)\n", length); |
pfe | 0:caecb0d1e7d3 | 46 | UINT n; |
pfe | 0:caecb0d1e7d3 | 47 | FRESULT res = f_write(&_fh, buffer, length, &n); |
pfe | 0:caecb0d1e7d3 | 48 | if(res) { |
pfe | 0:caecb0d1e7d3 | 49 | FFSDEBUG("f_write() failed (%d, %s)", res, FR_ERRORS[res]); |
pfe | 0:caecb0d1e7d3 | 50 | return -1; |
pfe | 0:caecb0d1e7d3 | 51 | } |
pfe | 0:caecb0d1e7d3 | 52 | return n; |
pfe | 0:caecb0d1e7d3 | 53 | } |
pfe | 0:caecb0d1e7d3 | 54 | |
pfe | 0:caecb0d1e7d3 | 55 | ssize_t FATFileHandle::read(void* buffer, size_t length) { |
pfe | 0:caecb0d1e7d3 | 56 | FFSDEBUG("read(%d)\n", length); |
pfe | 0:caecb0d1e7d3 | 57 | UINT n; |
pfe | 0:caecb0d1e7d3 | 58 | FRESULT res = f_read(&_fh, buffer, length, &n); |
pfe | 0:caecb0d1e7d3 | 59 | if(res) { |
pfe | 0:caecb0d1e7d3 | 60 | FFSDEBUG("f_read() failed (%d, %s)\n", res, FR_ERRORS[res]); |
pfe | 0:caecb0d1e7d3 | 61 | return -1; |
pfe | 0:caecb0d1e7d3 | 62 | } |
pfe | 0:caecb0d1e7d3 | 63 | return n; |
pfe | 0:caecb0d1e7d3 | 64 | } |
pfe | 0:caecb0d1e7d3 | 65 | |
pfe | 0:caecb0d1e7d3 | 66 | int FATFileHandle::isatty() { |
pfe | 0:caecb0d1e7d3 | 67 | return 0; |
pfe | 0:caecb0d1e7d3 | 68 | } |
pfe | 0:caecb0d1e7d3 | 69 | |
pfe | 0:caecb0d1e7d3 | 70 | off_t FATFileHandle::lseek(off_t position, int whence) { |
pfe | 0:caecb0d1e7d3 | 71 | FFSDEBUG("lseek(%i,%i)\n",position,whence); |
pfe | 0:caecb0d1e7d3 | 72 | if(whence == SEEK_END) { |
pfe | 0:caecb0d1e7d3 | 73 | position += _fh.fsize; |
pfe | 0:caecb0d1e7d3 | 74 | } else if(whence==SEEK_CUR) { |
pfe | 0:caecb0d1e7d3 | 75 | position += _fh.fptr; |
pfe | 0:caecb0d1e7d3 | 76 | } |
pfe | 0:caecb0d1e7d3 | 77 | FRESULT res = f_lseek(&_fh, position); |
pfe | 0:caecb0d1e7d3 | 78 | if(res) { |
pfe | 0:caecb0d1e7d3 | 79 | FFSDEBUG("lseek failed (%d, %s)\n", res, FR_ERRORS[res]); |
pfe | 0:caecb0d1e7d3 | 80 | return -1; |
pfe | 0:caecb0d1e7d3 | 81 | } else { |
pfe | 0:caecb0d1e7d3 | 82 | FFSDEBUG("lseek OK, returning %i\n", _fh.fptr); |
pfe | 0:caecb0d1e7d3 | 83 | return _fh.fptr; |
pfe | 0:caecb0d1e7d3 | 84 | } |
pfe | 0:caecb0d1e7d3 | 85 | } |
pfe | 0:caecb0d1e7d3 | 86 | |
pfe | 0:caecb0d1e7d3 | 87 | int FATFileHandle::fsync() { |
pfe | 0:caecb0d1e7d3 | 88 | FFSDEBUG("fsync()\n"); |
pfe | 0:caecb0d1e7d3 | 89 | FRESULT res = f_sync(&_fh); |
pfe | 0:caecb0d1e7d3 | 90 | if (res) { |
pfe | 0:caecb0d1e7d3 | 91 | FFSDEBUG("f_sync() failed (%d, %s)\n", res, FR_ERRORS[res]); |
pfe | 0:caecb0d1e7d3 | 92 | return -1; |
pfe | 0:caecb0d1e7d3 | 93 | } |
pfe | 0:caecb0d1e7d3 | 94 | return 0; |
pfe | 0:caecb0d1e7d3 | 95 | } |
pfe | 0:caecb0d1e7d3 | 96 | |
pfe | 0:caecb0d1e7d3 | 97 | off_t FATFileHandle::flen() { |
pfe | 0:caecb0d1e7d3 | 98 | FFSDEBUG("flen\n"); |
pfe | 0:caecb0d1e7d3 | 99 | return _fh.fsize; |
pfe | 0:caecb0d1e7d3 | 100 | } |
pfe | 0:caecb0d1e7d3 | 101 | |
pfe | 0:caecb0d1e7d3 | 102 | } // namespace mbed |