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