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