mbed-os
Fork of mbed-os by
drivers/FileBase.cpp@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2006-2013 ARM Limited |
elessair | 0:f269e3021894 | 3 | * |
elessair | 0:f269e3021894 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elessair | 0:f269e3021894 | 5 | * you may not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 6 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 7 | * |
elessair | 0:f269e3021894 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 9 | * |
elessair | 0:f269e3021894 | 10 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
elessair | 0:f269e3021894 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 13 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 14 | * limitations under the License. |
elessair | 0:f269e3021894 | 15 | */ |
elessair | 0:f269e3021894 | 16 | #include "drivers/FileBase.h" |
elessair | 0:f269e3021894 | 17 | |
elessair | 0:f269e3021894 | 18 | namespace mbed { |
elessair | 0:f269e3021894 | 19 | |
elessair | 0:f269e3021894 | 20 | FileBase *FileBase::_head = NULL; |
elessair | 0:f269e3021894 | 21 | SingletonPtr<PlatformMutex> FileBase::_mutex; |
elessair | 0:f269e3021894 | 22 | |
elessair | 0:f269e3021894 | 23 | FileBase::FileBase(const char *name, PathType t) : _next(NULL), |
elessair | 0:f269e3021894 | 24 | _name(name), |
elessair | 0:f269e3021894 | 25 | _path_type(t) { |
elessair | 0:f269e3021894 | 26 | _mutex->lock(); |
elessair | 0:f269e3021894 | 27 | if (name != NULL) { |
elessair | 0:f269e3021894 | 28 | // put this object at head of the list |
elessair | 0:f269e3021894 | 29 | _next = _head; |
elessair | 0:f269e3021894 | 30 | _head = this; |
elessair | 0:f269e3021894 | 31 | } else { |
elessair | 0:f269e3021894 | 32 | _next = NULL; |
elessair | 0:f269e3021894 | 33 | } |
elessair | 0:f269e3021894 | 34 | _mutex->unlock(); |
elessair | 0:f269e3021894 | 35 | } |
elessair | 0:f269e3021894 | 36 | |
elessair | 0:f269e3021894 | 37 | FileBase::~FileBase() { |
elessair | 0:f269e3021894 | 38 | _mutex->lock(); |
elessair | 0:f269e3021894 | 39 | if (_name != NULL) { |
elessair | 0:f269e3021894 | 40 | // remove this object from the list |
elessair | 0:f269e3021894 | 41 | if (_head == this) { // first in the list, so just drop me |
elessair | 0:f269e3021894 | 42 | _head = _next; |
elessair | 0:f269e3021894 | 43 | } else { // find the object before me, then drop me |
elessair | 0:f269e3021894 | 44 | FileBase *p = _head; |
elessair | 0:f269e3021894 | 45 | while (p->_next != this) { |
elessair | 0:f269e3021894 | 46 | p = p->_next; |
elessair | 0:f269e3021894 | 47 | } |
elessair | 0:f269e3021894 | 48 | p->_next = _next; |
elessair | 0:f269e3021894 | 49 | } |
elessair | 0:f269e3021894 | 50 | } |
elessair | 0:f269e3021894 | 51 | _mutex->unlock(); |
elessair | 0:f269e3021894 | 52 | } |
elessair | 0:f269e3021894 | 53 | |
elessair | 0:f269e3021894 | 54 | FileBase *FileBase::lookup(const char *name, unsigned int len) { |
elessair | 0:f269e3021894 | 55 | _mutex->lock(); |
elessair | 0:f269e3021894 | 56 | FileBase *p = _head; |
elessair | 0:f269e3021894 | 57 | while (p != NULL) { |
elessair | 0:f269e3021894 | 58 | /* Check that p->_name matches name and is the correct length */ |
elessair | 0:f269e3021894 | 59 | if (p->_name != NULL && std::strncmp(p->_name, name, len) == 0 && std::strlen(p->_name) == len) { |
elessair | 0:f269e3021894 | 60 | _mutex->unlock(); |
elessair | 0:f269e3021894 | 61 | return p; |
elessair | 0:f269e3021894 | 62 | } |
elessair | 0:f269e3021894 | 63 | p = p->_next; |
elessair | 0:f269e3021894 | 64 | } |
elessair | 0:f269e3021894 | 65 | _mutex->unlock(); |
elessair | 0:f269e3021894 | 66 | return NULL; |
elessair | 0:f269e3021894 | 67 | } |
elessair | 0:f269e3021894 | 68 | |
elessair | 0:f269e3021894 | 69 | FileBase *FileBase::get(int n) { |
elessair | 0:f269e3021894 | 70 | _mutex->lock(); |
elessair | 0:f269e3021894 | 71 | FileBase *p = _head; |
elessair | 0:f269e3021894 | 72 | int m = 0; |
elessair | 0:f269e3021894 | 73 | while (p != NULL) { |
elessair | 0:f269e3021894 | 74 | if (m == n) { |
elessair | 0:f269e3021894 | 75 | _mutex->unlock(); |
elessair | 0:f269e3021894 | 76 | return p; |
elessair | 0:f269e3021894 | 77 | } |
elessair | 0:f269e3021894 | 78 | |
elessair | 0:f269e3021894 | 79 | m++; |
elessair | 0:f269e3021894 | 80 | p = p->_next; |
elessair | 0:f269e3021894 | 81 | } |
elessair | 0:f269e3021894 | 82 | _mutex->unlock(); |
elessair | 0:f269e3021894 | 83 | return NULL; |
elessair | 0:f269e3021894 | 84 | } |
elessair | 0:f269e3021894 | 85 | |
elessair | 0:f269e3021894 | 86 | const char* FileBase::getName(void) { |
elessair | 0:f269e3021894 | 87 | // Constant read so no lock needed |
elessair | 0:f269e3021894 | 88 | return _name; |
elessair | 0:f269e3021894 | 89 | } |
elessair | 0:f269e3021894 | 90 | |
elessair | 0:f269e3021894 | 91 | PathType FileBase::getPathType(void) { |
elessair | 0:f269e3021894 | 92 | // Constant read so no lock needed |
elessair | 0:f269e3021894 | 93 | return _path_type; |
elessair | 0:f269e3021894 | 94 | } |
elessair | 0:f269e3021894 | 95 | |
elessair | 0:f269e3021894 | 96 | } // namespace mbed |
elessair | 0:f269e3021894 | 97 |