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