initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:638edba3adf6 1 /* mbed Microcontroller Library
yihui 0:638edba3adf6 2 * Copyright (c) 2006-2013 ARM Limited
yihui 0:638edba3adf6 3 *
yihui 0:638edba3adf6 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 0:638edba3adf6 5 * you may not use this file except in compliance with the License.
yihui 0:638edba3adf6 6 * You may obtain a copy of the License at
yihui 0:638edba3adf6 7 *
yihui 0:638edba3adf6 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 0:638edba3adf6 9 *
yihui 0:638edba3adf6 10 * Unless required by applicable law or agreed to in writing, software
yihui 0:638edba3adf6 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 0:638edba3adf6 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 0:638edba3adf6 13 * See the License for the specific language governing permissions and
yihui 0:638edba3adf6 14 * limitations under the License.
yihui 0:638edba3adf6 15 */
yihui 0:638edba3adf6 16 #include "FileSystemLike.h"
yihui 0:638edba3adf6 17
yihui 0:638edba3adf6 18 namespace mbed {
yihui 0:638edba3adf6 19
yihui 0:638edba3adf6 20 class BaseDirHandle : public DirHandle {
yihui 0:638edba3adf6 21 public:
yihui 0:638edba3adf6 22 /*
yihui 0:638edba3adf6 23 We keep track of our current location as the n'th object in the
yihui 0:638edba3adf6 24 FileSystemLike list. Using a Base* instead would cause problems if that
yihui 0:638edba3adf6 25 object were to be destroyed between readdirs.
yihui 0:638edba3adf6 26 Using this method does mean though that destroying/creating objects can
yihui 0:638edba3adf6 27 give unusual results from readdir.
yihui 0:638edba3adf6 28 */
yihui 0:638edba3adf6 29 off_t n;
yihui 0:638edba3adf6 30 struct dirent cur_entry;
yihui 0:638edba3adf6 31
yihui 0:638edba3adf6 32 BaseDirHandle() : n(0), cur_entry() {
yihui 0:638edba3adf6 33 }
yihui 0:638edba3adf6 34
yihui 0:638edba3adf6 35 virtual int closedir() {
yihui 0:638edba3adf6 36 delete this;
yihui 0:638edba3adf6 37 return 0;
yihui 0:638edba3adf6 38 }
yihui 0:638edba3adf6 39
yihui 0:638edba3adf6 40 virtual struct dirent *readdir() {
yihui 0:638edba3adf6 41 FileBase *ptr = FileBase::get(n);
yihui 0:638edba3adf6 42 if (ptr == NULL) return NULL;
yihui 0:638edba3adf6 43
yihui 0:638edba3adf6 44 /* Increment n, so next readdir gets the next item */
yihui 0:638edba3adf6 45 n++;
yihui 0:638edba3adf6 46
yihui 0:638edba3adf6 47 /* Setup cur entry and return a pointer to it */
yihui 0:638edba3adf6 48 std::strncpy(cur_entry.d_name, ptr->getName(), NAME_MAX);
yihui 0:638edba3adf6 49 return &cur_entry;
yihui 0:638edba3adf6 50 }
yihui 0:638edba3adf6 51
yihui 0:638edba3adf6 52 virtual off_t telldir() {
yihui 0:638edba3adf6 53 return n;
yihui 0:638edba3adf6 54 }
yihui 0:638edba3adf6 55
yihui 0:638edba3adf6 56 virtual void seekdir(off_t offset) {
yihui 0:638edba3adf6 57 n = offset;
yihui 0:638edba3adf6 58 }
yihui 0:638edba3adf6 59
yihui 0:638edba3adf6 60 virtual void rewinddir() {
yihui 0:638edba3adf6 61 n = 0;
yihui 0:638edba3adf6 62 }
yihui 0:638edba3adf6 63 };
yihui 0:638edba3adf6 64
yihui 0:638edba3adf6 65 FileSystemLike::FileSystemLike(const char *name) : FileBase(name, FileSystemPathType) {
yihui 0:638edba3adf6 66
yihui 0:638edba3adf6 67 }
yihui 0:638edba3adf6 68
yihui 0:638edba3adf6 69 FileSystemLike::~FileSystemLike() {
yihui 0:638edba3adf6 70
yihui 0:638edba3adf6 71 }
yihui 0:638edba3adf6 72
yihui 0:638edba3adf6 73 DirHandle *FileSystemLike::opendir() {
yihui 0:638edba3adf6 74 return new BaseDirHandle();
yihui 0:638edba3adf6 75 }
yihui 0:638edba3adf6 76
yihui 0:638edba3adf6 77 } // namespace mbed