dhgdh
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
mbd_os/hal/common/FileBase.cpp@1:55a6170b404f, 2016-09-17 (annotated)
- Committer:
- nexpaq
- Date:
- Sat Sep 17 16:32:05 2016 +0000
- Revision:
- 1:55a6170b404f
checking in for sharing
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 "FileBase.h" |
nexpaq | 1:55a6170b404f | 17 | |
nexpaq | 1:55a6170b404f | 18 | namespace mbed { |
nexpaq | 1:55a6170b404f | 19 | |
nexpaq | 1:55a6170b404f | 20 | FileBase *FileBase::_head = NULL; |
nexpaq | 1:55a6170b404f | 21 | SingletonPtr<PlatformMutex> FileBase::_mutex; |
nexpaq | 1:55a6170b404f | 22 | |
nexpaq | 1:55a6170b404f | 23 | FileBase::FileBase(const char *name, PathType t) : _next(NULL), |
nexpaq | 1:55a6170b404f | 24 | _name(name), |
nexpaq | 1:55a6170b404f | 25 | _path_type(t) { |
nexpaq | 1:55a6170b404f | 26 | _mutex->lock(); |
nexpaq | 1:55a6170b404f | 27 | if (name != NULL) { |
nexpaq | 1:55a6170b404f | 28 | // put this object at head of the list |
nexpaq | 1:55a6170b404f | 29 | _next = _head; |
nexpaq | 1:55a6170b404f | 30 | _head = this; |
nexpaq | 1:55a6170b404f | 31 | } else { |
nexpaq | 1:55a6170b404f | 32 | _next = NULL; |
nexpaq | 1:55a6170b404f | 33 | } |
nexpaq | 1:55a6170b404f | 34 | _mutex->unlock(); |
nexpaq | 1:55a6170b404f | 35 | } |
nexpaq | 1:55a6170b404f | 36 | |
nexpaq | 1:55a6170b404f | 37 | FileBase::~FileBase() { |
nexpaq | 1:55a6170b404f | 38 | _mutex->lock(); |
nexpaq | 1:55a6170b404f | 39 | if (_name != NULL) { |
nexpaq | 1:55a6170b404f | 40 | // remove this object from the list |
nexpaq | 1:55a6170b404f | 41 | if (_head == this) { // first in the list, so just drop me |
nexpaq | 1:55a6170b404f | 42 | _head = _next; |
nexpaq | 1:55a6170b404f | 43 | } else { // find the object before me, then drop me |
nexpaq | 1:55a6170b404f | 44 | FileBase *p = _head; |
nexpaq | 1:55a6170b404f | 45 | while (p->_next != this) { |
nexpaq | 1:55a6170b404f | 46 | p = p->_next; |
nexpaq | 1:55a6170b404f | 47 | } |
nexpaq | 1:55a6170b404f | 48 | p->_next = _next; |
nexpaq | 1:55a6170b404f | 49 | } |
nexpaq | 1:55a6170b404f | 50 | } |
nexpaq | 1:55a6170b404f | 51 | _mutex->unlock(); |
nexpaq | 1:55a6170b404f | 52 | } |
nexpaq | 1:55a6170b404f | 53 | |
nexpaq | 1:55a6170b404f | 54 | FileBase *FileBase::lookup(const char *name, unsigned int len) { |
nexpaq | 1:55a6170b404f | 55 | _mutex->lock(); |
nexpaq | 1:55a6170b404f | 56 | FileBase *p = _head; |
nexpaq | 1:55a6170b404f | 57 | while (p != NULL) { |
nexpaq | 1:55a6170b404f | 58 | /* Check that p->_name matches name and is the correct length */ |
nexpaq | 1:55a6170b404f | 59 | if (p->_name != NULL && std::strncmp(p->_name, name, len) == 0 && std::strlen(p->_name) == len) { |
nexpaq | 1:55a6170b404f | 60 | _mutex->unlock(); |
nexpaq | 1:55a6170b404f | 61 | return p; |
nexpaq | 1:55a6170b404f | 62 | } |
nexpaq | 1:55a6170b404f | 63 | p = p->_next; |
nexpaq | 1:55a6170b404f | 64 | } |
nexpaq | 1:55a6170b404f | 65 | _mutex->unlock(); |
nexpaq | 1:55a6170b404f | 66 | return NULL; |
nexpaq | 1:55a6170b404f | 67 | } |
nexpaq | 1:55a6170b404f | 68 | |
nexpaq | 1:55a6170b404f | 69 | FileBase *FileBase::get(int n) { |
nexpaq | 1:55a6170b404f | 70 | _mutex->lock(); |
nexpaq | 1:55a6170b404f | 71 | FileBase *p = _head; |
nexpaq | 1:55a6170b404f | 72 | int m = 0; |
nexpaq | 1:55a6170b404f | 73 | while (p != NULL) { |
nexpaq | 1:55a6170b404f | 74 | if (m == n) { |
nexpaq | 1:55a6170b404f | 75 | _mutex->unlock(); |
nexpaq | 1:55a6170b404f | 76 | return p; |
nexpaq | 1:55a6170b404f | 77 | } |
nexpaq | 1:55a6170b404f | 78 | |
nexpaq | 1:55a6170b404f | 79 | m++; |
nexpaq | 1:55a6170b404f | 80 | p = p->_next; |
nexpaq | 1:55a6170b404f | 81 | } |
nexpaq | 1:55a6170b404f | 82 | _mutex->unlock(); |
nexpaq | 1:55a6170b404f | 83 | return NULL; |
nexpaq | 1:55a6170b404f | 84 | } |
nexpaq | 1:55a6170b404f | 85 | |
nexpaq | 1:55a6170b404f | 86 | const char* FileBase::getName(void) { |
nexpaq | 1:55a6170b404f | 87 | // Constant read so no lock needed |
nexpaq | 1:55a6170b404f | 88 | return _name; |
nexpaq | 1:55a6170b404f | 89 | } |
nexpaq | 1:55a6170b404f | 90 | |
nexpaq | 1:55a6170b404f | 91 | PathType FileBase::getPathType(void) { |
nexpaq | 1:55a6170b404f | 92 | // Constant read so no lock needed |
nexpaq | 1:55a6170b404f | 93 | return _path_type; |
nexpaq | 1:55a6170b404f | 94 | } |
nexpaq | 1:55a6170b404f | 95 | |
nexpaq | 1:55a6170b404f | 96 | } // namespace mbed |
nexpaq | 1:55a6170b404f | 97 |