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