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