init

Dependencies:   mbed

Committer:
Nathan Yonkee
Date:
Fri Mar 02 07:16:49 2018 -0700
Revision:
10:46a4cf51ee38
Parent:
9:d58e77ebd769
remove mbed-os

Who changed what in which revision?

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