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