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