Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NetServices FatFileSystem csv_parser mbed MQTTClient RF12B DNSResolver SDFileSystem
FatFileSystem/FATFileHandle.cpp@0:a29a0225f203, 2012-04-02 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Mon Apr 02 22:05:20 2012 +0000
- Revision:
- 0:a29a0225f203
Initial version
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| SomeRandomBloke | 0:a29a0225f203 | 1 | /* mbed Microcontroller Library - FATFileHandle |
| SomeRandomBloke | 0:a29a0225f203 | 2 | * Copyright (c) 2008, sford |
| SomeRandomBloke | 0:a29a0225f203 | 3 | */ |
| SomeRandomBloke | 0:a29a0225f203 | 4 | |
| SomeRandomBloke | 0:a29a0225f203 | 5 | #include "FATFileHandle.h" |
| SomeRandomBloke | 0:a29a0225f203 | 6 | |
| SomeRandomBloke | 0:a29a0225f203 | 7 | #include <stdio.h> |
| SomeRandomBloke | 0:a29a0225f203 | 8 | #include <stdlib.h> |
| SomeRandomBloke | 0:a29a0225f203 | 9 | #include "ff.h" |
| SomeRandomBloke | 0:a29a0225f203 | 10 | #include "FATFileSystem.h" |
| SomeRandomBloke | 0:a29a0225f203 | 11 | |
| SomeRandomBloke | 0:a29a0225f203 | 12 | namespace mbed { |
| SomeRandomBloke | 0:a29a0225f203 | 13 | |
| SomeRandomBloke | 0:a29a0225f203 | 14 | #if FFSDEBUG_ENABLED |
| SomeRandomBloke | 0:a29a0225f203 | 15 | static const char *FR_ERRORS[] = { |
| SomeRandomBloke | 0:a29a0225f203 | 16 | "FR_OK = 0", |
| SomeRandomBloke | 0:a29a0225f203 | 17 | "FR_NOT_READY", |
| SomeRandomBloke | 0:a29a0225f203 | 18 | "FR_NO_FILE", |
| SomeRandomBloke | 0:a29a0225f203 | 19 | "FR_NO_PATH", |
| SomeRandomBloke | 0:a29a0225f203 | 20 | "FR_INVALID_NAME", |
| SomeRandomBloke | 0:a29a0225f203 | 21 | "FR_INVALID_DRIVE", |
| SomeRandomBloke | 0:a29a0225f203 | 22 | "FR_DENIED", |
| SomeRandomBloke | 0:a29a0225f203 | 23 | "FR_EXIST", |
| SomeRandomBloke | 0:a29a0225f203 | 24 | "FR_RW_ERROR", |
| SomeRandomBloke | 0:a29a0225f203 | 25 | "FR_WRITE_PROTECTED", |
| SomeRandomBloke | 0:a29a0225f203 | 26 | "FR_NOT_ENABLED", |
| SomeRandomBloke | 0:a29a0225f203 | 27 | "FR_NO_FILESYSTEM", |
| SomeRandomBloke | 0:a29a0225f203 | 28 | "FR_INVALID_OBJECT", |
| SomeRandomBloke | 0:a29a0225f203 | 29 | "FR_MKFS_ABORTED" |
| SomeRandomBloke | 0:a29a0225f203 | 30 | }; |
| SomeRandomBloke | 0:a29a0225f203 | 31 | #endif |
| SomeRandomBloke | 0:a29a0225f203 | 32 | |
| SomeRandomBloke | 0:a29a0225f203 | 33 | FATFileHandle::FATFileHandle(FIL fh) { |
| SomeRandomBloke | 0:a29a0225f203 | 34 | _fh = fh; |
| SomeRandomBloke | 0:a29a0225f203 | 35 | } |
| SomeRandomBloke | 0:a29a0225f203 | 36 | |
| SomeRandomBloke | 0:a29a0225f203 | 37 | int FATFileHandle::close() { |
| SomeRandomBloke | 0:a29a0225f203 | 38 | FFSDEBUG("close\n"); |
| SomeRandomBloke | 0:a29a0225f203 | 39 | int retval = f_close(&_fh); |
| SomeRandomBloke | 0:a29a0225f203 | 40 | delete this; |
| SomeRandomBloke | 0:a29a0225f203 | 41 | return retval; |
| SomeRandomBloke | 0:a29a0225f203 | 42 | } |
| SomeRandomBloke | 0:a29a0225f203 | 43 | |
| SomeRandomBloke | 0:a29a0225f203 | 44 | ssize_t FATFileHandle::write(const void* buffer, size_t length) { |
| SomeRandomBloke | 0:a29a0225f203 | 45 | FFSDEBUG("write(%d)\n", length); |
| SomeRandomBloke | 0:a29a0225f203 | 46 | UINT n; |
| SomeRandomBloke | 0:a29a0225f203 | 47 | FRESULT res = f_write(&_fh, buffer, length, &n); |
| SomeRandomBloke | 0:a29a0225f203 | 48 | if(res) { |
| SomeRandomBloke | 0:a29a0225f203 | 49 | FFSDEBUG("f_write() failed (%d, %s)", res, FR_ERRORS[res]); |
| SomeRandomBloke | 0:a29a0225f203 | 50 | return -1; |
| SomeRandomBloke | 0:a29a0225f203 | 51 | } |
| SomeRandomBloke | 0:a29a0225f203 | 52 | return n; |
| SomeRandomBloke | 0:a29a0225f203 | 53 | } |
| SomeRandomBloke | 0:a29a0225f203 | 54 | |
| SomeRandomBloke | 0:a29a0225f203 | 55 | ssize_t FATFileHandle::read(void* buffer, size_t length) { |
| SomeRandomBloke | 0:a29a0225f203 | 56 | FFSDEBUG("read(%d)\n", length); |
| SomeRandomBloke | 0:a29a0225f203 | 57 | UINT n; |
| SomeRandomBloke | 0:a29a0225f203 | 58 | FRESULT res = f_read(&_fh, buffer, length, &n); |
| SomeRandomBloke | 0:a29a0225f203 | 59 | if(res) { |
| SomeRandomBloke | 0:a29a0225f203 | 60 | FFSDEBUG("f_read() failed (%d, %s)\n", res, FR_ERRORS[res]); |
| SomeRandomBloke | 0:a29a0225f203 | 61 | return -1; |
| SomeRandomBloke | 0:a29a0225f203 | 62 | } |
| SomeRandomBloke | 0:a29a0225f203 | 63 | return n; |
| SomeRandomBloke | 0:a29a0225f203 | 64 | } |
| SomeRandomBloke | 0:a29a0225f203 | 65 | |
| SomeRandomBloke | 0:a29a0225f203 | 66 | int FATFileHandle::isatty() { |
| SomeRandomBloke | 0:a29a0225f203 | 67 | return 0; |
| SomeRandomBloke | 0:a29a0225f203 | 68 | } |
| SomeRandomBloke | 0:a29a0225f203 | 69 | |
| SomeRandomBloke | 0:a29a0225f203 | 70 | off_t FATFileHandle::lseek(off_t position, int whence) { |
| SomeRandomBloke | 0:a29a0225f203 | 71 | FFSDEBUG("lseek(%i,%i)\n",position,whence); |
| SomeRandomBloke | 0:a29a0225f203 | 72 | if(whence == SEEK_END) { |
| SomeRandomBloke | 0:a29a0225f203 | 73 | position += _fh.fsize; |
| SomeRandomBloke | 0:a29a0225f203 | 74 | } else if(whence==SEEK_CUR) { |
| SomeRandomBloke | 0:a29a0225f203 | 75 | position += _fh.fptr; |
| SomeRandomBloke | 0:a29a0225f203 | 76 | } |
| SomeRandomBloke | 0:a29a0225f203 | 77 | FRESULT res = f_lseek(&_fh, position); |
| SomeRandomBloke | 0:a29a0225f203 | 78 | if(res) { |
| SomeRandomBloke | 0:a29a0225f203 | 79 | FFSDEBUG("lseek failed (%d, %s)\n", res, FR_ERRORS[res]); |
| SomeRandomBloke | 0:a29a0225f203 | 80 | return -1; |
| SomeRandomBloke | 0:a29a0225f203 | 81 | } else { |
| SomeRandomBloke | 0:a29a0225f203 | 82 | FFSDEBUG("lseek OK, returning %i\n", _fh.fptr); |
| SomeRandomBloke | 0:a29a0225f203 | 83 | return _fh.fptr; |
| SomeRandomBloke | 0:a29a0225f203 | 84 | } |
| SomeRandomBloke | 0:a29a0225f203 | 85 | } |
| SomeRandomBloke | 0:a29a0225f203 | 86 | |
| SomeRandomBloke | 0:a29a0225f203 | 87 | int FATFileHandle::fsync() { |
| SomeRandomBloke | 0:a29a0225f203 | 88 | FFSDEBUG("fsync()\n"); |
| SomeRandomBloke | 0:a29a0225f203 | 89 | FRESULT res = f_sync(&_fh); |
| SomeRandomBloke | 0:a29a0225f203 | 90 | if (res) { |
| SomeRandomBloke | 0:a29a0225f203 | 91 | FFSDEBUG("f_sync() failed (%d, %s)\n", res, FR_ERRORS[res]); |
| SomeRandomBloke | 0:a29a0225f203 | 92 | return -1; |
| SomeRandomBloke | 0:a29a0225f203 | 93 | } |
| SomeRandomBloke | 0:a29a0225f203 | 94 | return 0; |
| SomeRandomBloke | 0:a29a0225f203 | 95 | } |
| SomeRandomBloke | 0:a29a0225f203 | 96 | |
| SomeRandomBloke | 0:a29a0225f203 | 97 | off_t FATFileHandle::flen() { |
| SomeRandomBloke | 0:a29a0225f203 | 98 | FFSDEBUG("flen\n"); |
| SomeRandomBloke | 0:a29a0225f203 | 99 | return _fh.fsize; |
| SomeRandomBloke | 0:a29a0225f203 | 100 | } |
| SomeRandomBloke | 0:a29a0225f203 | 101 | |
| SomeRandomBloke | 0:a29a0225f203 | 102 | } // namespace mbed |