Norimasa Okamoto / F32RFileSystem

Fork of F12RFileSystem by Norimasa Okamoto

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers F32RDirHandle.cpp Source File

F32RDirHandle.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2017 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #include "F32RDirHandle.h"
00023 #include "fsdebug.h"
00024 
00025 using namespace mbed;
00026 
00027 F32RDirHandle::F32RDirHandle(F32RFileSystem& fs_, cluster_t cluster_):fs(fs_) {
00028     first_cluster = cluster_;
00029     cluster = cluster_;
00030     pos = 0;
00031 }
00032 
00033 int F32RDirHandle::closedir() {
00034     delete this;
00035     return 0;
00036 }
00037 
00038 static void sfn_to_name(const uint8_t* buf, char* name) {
00039     for(int i = 0; i < 8; i++) {
00040         char c = buf[i];
00041         if (c == '\x20') {
00042             break;
00043         }
00044         *name++ = c;
00045     }
00046     for(int i = 8; i < 8+3; i++) {
00047         char c = buf[i];
00048         if (c == '\x20') {
00049             break;
00050         } else if (i == 8) {
00051             *name++ = '.';
00052         }
00053         *name++ = c;
00054     }
00055     *name = '\0';
00056 }
00057 
00058 struct dirent *F32RDirHandle::readdir() {
00059     FS_DBG("pos=%d cluster=%d", pos, cluster);
00060     while(cluster < 0x0ffffff8) {
00061         FS_TEST_ASSERT(cluster >= 2);
00062         uint8_t buf[32];
00063         fs.storage->Read(fs.base_data + (cluster-2) * fs.cluster_size + fs.cluster_head(pos), buf, sizeof(buf));
00064         FS_DBG_HEX(buf, sizeof(buf));
00065         pos += 32;
00066         if (fs.cluster_head(pos) == 0) {
00067             cluster = fs.fat_read(cluster);
00068         }
00069         if (buf[0] == 0x00) {
00070             break;
00071         } else if (buf[0] != 0xe5 && buf[11] == 0x20) {
00072             sfn_to_name(buf, cur_entry.d_name);
00073             return &cur_entry;
00074         }
00075     }
00076     return NULL;
00077 }
00078 
00079 void F32RDirHandle::rewinddir() {
00080     cluster = first_cluster;
00081     pos = 0;
00082 }
00083 
00084 off_t F32RDirHandle::telldir() {
00085     return pos;
00086 }
00087 
00088 void F32RDirHandle::seekdir(off_t location_) {
00089     // TODO
00090 }
00091