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