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