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