t
Fork of mbed-dev by
drivers/FileSystemLike.cpp@178:c26431f84b0d, 2017-11-09 (annotated)
- Committer:
- amithy
- Date:
- Thu Nov 09 22:14:37 2017 +0000
- Revision:
- 178:c26431f84b0d
- Parent:
- 149:156823d33999
- Child:
- 160:d5399cc887bb
test export
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /* mbed Microcontroller Library |
<> | 149:156823d33999 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 149:156823d33999 | 3 | * |
<> | 149:156823d33999 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 149:156823d33999 | 5 | * you may not use this file except in compliance with the License. |
<> | 149:156823d33999 | 6 | * You may obtain a copy of the License at |
<> | 149:156823d33999 | 7 | * |
<> | 149:156823d33999 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 149:156823d33999 | 9 | * |
<> | 149:156823d33999 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 149:156823d33999 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 149:156823d33999 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 149:156823d33999 | 13 | * See the License for the specific language governing permissions and |
<> | 149:156823d33999 | 14 | * limitations under the License. |
<> | 149:156823d33999 | 15 | */ |
<> | 149:156823d33999 | 16 | #include "drivers/FileSystemLike.h" |
<> | 149:156823d33999 | 17 | |
<> | 149:156823d33999 | 18 | namespace mbed { |
<> | 149:156823d33999 | 19 | |
<> | 149:156823d33999 | 20 | class BaseDirHandle : public DirHandle { |
<> | 149:156823d33999 | 21 | public: |
<> | 149:156823d33999 | 22 | /* |
<> | 149:156823d33999 | 23 | We keep track of our current location as the n'th object in the |
<> | 149:156823d33999 | 24 | FileSystemLike list. Using a Base* instead would cause problems if that |
<> | 149:156823d33999 | 25 | object were to be destroyed between readdirs. |
<> | 149:156823d33999 | 26 | Using this method does mean though that destroying/creating objects can |
<> | 149:156823d33999 | 27 | give unusual results from readdir. |
<> | 149:156823d33999 | 28 | */ |
<> | 149:156823d33999 | 29 | off_t n; |
<> | 149:156823d33999 | 30 | struct dirent cur_entry; |
<> | 149:156823d33999 | 31 | |
<> | 149:156823d33999 | 32 | BaseDirHandle() : n(0), cur_entry() { |
<> | 149:156823d33999 | 33 | } |
<> | 149:156823d33999 | 34 | |
<> | 149:156823d33999 | 35 | virtual int closedir() { |
<> | 149:156823d33999 | 36 | // No lock can be used in destructor |
<> | 149:156823d33999 | 37 | delete this; |
<> | 149:156823d33999 | 38 | return 0; |
<> | 149:156823d33999 | 39 | } |
<> | 149:156823d33999 | 40 | |
<> | 149:156823d33999 | 41 | virtual struct dirent *readdir() { |
<> | 149:156823d33999 | 42 | lock(); |
<> | 149:156823d33999 | 43 | FileBase *ptr = FileBase::get(n); |
<> | 149:156823d33999 | 44 | if (ptr == NULL) { |
<> | 149:156823d33999 | 45 | unlock(); |
<> | 149:156823d33999 | 46 | return NULL; |
<> | 149:156823d33999 | 47 | } |
<> | 149:156823d33999 | 48 | |
<> | 149:156823d33999 | 49 | /* Increment n, so next readdir gets the next item */ |
<> | 149:156823d33999 | 50 | n++; |
<> | 149:156823d33999 | 51 | |
<> | 149:156823d33999 | 52 | /* Setup cur entry and return a pointer to it */ |
<> | 149:156823d33999 | 53 | std::strncpy(cur_entry.d_name, ptr->getName(), NAME_MAX); |
<> | 149:156823d33999 | 54 | unlock(); |
<> | 149:156823d33999 | 55 | return &cur_entry; |
<> | 149:156823d33999 | 56 | } |
<> | 149:156823d33999 | 57 | |
<> | 149:156823d33999 | 58 | virtual off_t telldir() { |
<> | 149:156823d33999 | 59 | lock(); |
<> | 149:156823d33999 | 60 | off_t offset = n; |
<> | 149:156823d33999 | 61 | unlock(); |
<> | 149:156823d33999 | 62 | return offset; |
<> | 149:156823d33999 | 63 | } |
<> | 149:156823d33999 | 64 | |
<> | 149:156823d33999 | 65 | virtual void seekdir(off_t offset) { |
<> | 149:156823d33999 | 66 | lock(); |
<> | 149:156823d33999 | 67 | n = offset; |
<> | 149:156823d33999 | 68 | unlock(); |
<> | 149:156823d33999 | 69 | } |
<> | 149:156823d33999 | 70 | |
<> | 149:156823d33999 | 71 | virtual void rewinddir() { |
<> | 149:156823d33999 | 72 | lock(); |
<> | 149:156823d33999 | 73 | n = 0; |
<> | 149:156823d33999 | 74 | unlock(); |
<> | 149:156823d33999 | 75 | } |
<> | 149:156823d33999 | 76 | |
<> | 149:156823d33999 | 77 | protected: |
<> | 149:156823d33999 | 78 | PlatformMutex _mutex; |
<> | 149:156823d33999 | 79 | |
<> | 149:156823d33999 | 80 | virtual void lock() { |
<> | 149:156823d33999 | 81 | _mutex.lock(); |
<> | 149:156823d33999 | 82 | } |
<> | 149:156823d33999 | 83 | |
<> | 149:156823d33999 | 84 | virtual void unlock() { |
<> | 149:156823d33999 | 85 | _mutex.unlock(); |
<> | 149:156823d33999 | 86 | } |
<> | 149:156823d33999 | 87 | }; |
<> | 149:156823d33999 | 88 | |
<> | 149:156823d33999 | 89 | FileSystemLike::FileSystemLike(const char *name) : FileBase(name, FileSystemPathType) { |
<> | 149:156823d33999 | 90 | |
<> | 149:156823d33999 | 91 | } |
<> | 149:156823d33999 | 92 | |
<> | 149:156823d33999 | 93 | FileSystemLike::~FileSystemLike() { |
<> | 149:156823d33999 | 94 | |
<> | 149:156823d33999 | 95 | } |
<> | 149:156823d33999 | 96 | |
<> | 149:156823d33999 | 97 | DirHandle *FileSystemLike::opendir() { |
<> | 149:156823d33999 | 98 | return new BaseDirHandle(); |
<> | 149:156823d33999 | 99 | } |
<> | 149:156823d33999 | 100 | |
<> | 149:156823d33999 | 101 | } // namespace mbed |