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