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