Kaif Kutchwala 201267448 ELEC2645 Project

Dependencies:   mbed

Committer:
KaifK
Date:
Tue May 26 15:50:46 2020 +0000
Revision:
31:e1f80d181779
Parent:
21:d5b1160f349f
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
KaifK 21:d5b1160f349f 1 /* mbed Microcontroller Library
KaifK 21:d5b1160f349f 2 * Copyright (c) 2006-2012 ARM Limited
KaifK 21:d5b1160f349f 3 *
KaifK 21:d5b1160f349f 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
KaifK 21:d5b1160f349f 5 * of this software and associated documentation files (the "Software"), to deal
KaifK 21:d5b1160f349f 6 * in the Software without restriction, including without limitation the rights
KaifK 21:d5b1160f349f 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
KaifK 21:d5b1160f349f 8 * copies of the Software, and to permit persons to whom the Software is
KaifK 21:d5b1160f349f 9 * furnished to do so, subject to the following conditions:
KaifK 21:d5b1160f349f 10 *
KaifK 21:d5b1160f349f 11 * The above copyright notice and this permission notice shall be included in
KaifK 21:d5b1160f349f 12 * all copies or substantial portions of the Software.
KaifK 21:d5b1160f349f 13 *
KaifK 21:d5b1160f349f 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
KaifK 21:d5b1160f349f 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
KaifK 21:d5b1160f349f 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
KaifK 21:d5b1160f349f 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
KaifK 21:d5b1160f349f 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
KaifK 21:d5b1160f349f 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
KaifK 21:d5b1160f349f 20 * SOFTWARE.
KaifK 21:d5b1160f349f 21 */
KaifK 21:d5b1160f349f 22 #include <string.h>
KaifK 21:d5b1160f349f 23 #include "ff.h"
KaifK 21:d5b1160f349f 24 #include "FATDirHandle.h"
KaifK 21:d5b1160f349f 25
KaifK 21:d5b1160f349f 26 using namespace mbed;
KaifK 21:d5b1160f349f 27
KaifK 21:d5b1160f349f 28 FATDirHandle::FATDirHandle(const FATFS_DIR &the_dir) {
KaifK 21:d5b1160f349f 29 dir = the_dir;
KaifK 21:d5b1160f349f 30 }
KaifK 21:d5b1160f349f 31
KaifK 21:d5b1160f349f 32 int FATDirHandle::closedir() {
KaifK 21:d5b1160f349f 33 int retval = f_closedir(&dir);
KaifK 21:d5b1160f349f 34 delete this;
KaifK 21:d5b1160f349f 35 return retval;
KaifK 21:d5b1160f349f 36 }
KaifK 21:d5b1160f349f 37
KaifK 21:d5b1160f349f 38 struct dirent *FATDirHandle::readdir() {
KaifK 21:d5b1160f349f 39 FILINFO finfo;
KaifK 21:d5b1160f349f 40
KaifK 21:d5b1160f349f 41 #if _USE_LFN
KaifK 21:d5b1160f349f 42 finfo.lfname = cur_entry.d_name;
KaifK 21:d5b1160f349f 43 finfo.lfsize = sizeof(cur_entry.d_name);
KaifK 21:d5b1160f349f 44 #endif // _USE_LFN
KaifK 21:d5b1160f349f 45
KaifK 21:d5b1160f349f 46 FRESULT res = f_readdir(&dir, &finfo);
KaifK 21:d5b1160f349f 47
KaifK 21:d5b1160f349f 48 #if _USE_LFN
KaifK 21:d5b1160f349f 49 if(res != 0 || finfo.fname[0]==0) {
KaifK 21:d5b1160f349f 50 return NULL;
KaifK 21:d5b1160f349f 51 } else {
KaifK 21:d5b1160f349f 52 if(cur_entry.d_name[0]==0) {
KaifK 21:d5b1160f349f 53 // No long filename so use short filename.
KaifK 21:d5b1160f349f 54 memcpy(cur_entry.d_name, finfo.fname, sizeof(finfo.fname));
KaifK 21:d5b1160f349f 55 }
KaifK 21:d5b1160f349f 56 return &cur_entry;
KaifK 21:d5b1160f349f 57 }
KaifK 21:d5b1160f349f 58 #else
KaifK 21:d5b1160f349f 59 if(res != 0 || finfo.fname[0]==0) {
KaifK 21:d5b1160f349f 60 return NULL;
KaifK 21:d5b1160f349f 61 } else {
KaifK 21:d5b1160f349f 62 memcpy(cur_entry.d_name, finfo.fname, sizeof(finfo.fname));
KaifK 21:d5b1160f349f 63 return &cur_entry;
KaifK 21:d5b1160f349f 64 }
KaifK 21:d5b1160f349f 65 #endif /* _USE_LFN */
KaifK 21:d5b1160f349f 66 }
KaifK 21:d5b1160f349f 67
KaifK 21:d5b1160f349f 68 void FATDirHandle::rewinddir() {
KaifK 21:d5b1160f349f 69 dir.index = 0;
KaifK 21:d5b1160f349f 70 }
KaifK 21:d5b1160f349f 71
KaifK 21:d5b1160f349f 72 off_t FATDirHandle::telldir() {
KaifK 21:d5b1160f349f 73 return dir.index;
KaifK 21:d5b1160f349f 74 }
KaifK 21:d5b1160f349f 75
KaifK 21:d5b1160f349f 76 void FATDirHandle::seekdir(off_t location) {
KaifK 21:d5b1160f349f 77 dir.index = location;
KaifK 21:d5b1160f349f 78 }
KaifK 21:d5b1160f349f 79
KaifK 21:d5b1160f349f 80 ssize_t FATDirHandle::read(struct dirent *ent) {
KaifK 21:d5b1160f349f 81 struct dirent *temp = readdir();
KaifK 21:d5b1160f349f 82 if (!temp) {
KaifK 21:d5b1160f349f 83 return 0;
KaifK 21:d5b1160f349f 84 }
KaifK 21:d5b1160f349f 85
KaifK 21:d5b1160f349f 86 memcpy(ent, temp, sizeof(*ent));
KaifK 21:d5b1160f349f 87 return 1;
KaifK 21:d5b1160f349f 88 }
KaifK 21:d5b1160f349f 89