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