test

Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

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