Modified for BG96

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Thu Sep 06 13:40:20 2018 +0100
Revision:
187:0387e8f68319
Parent:
167:e84263d55307
mbed-dev library. Release version 163

Who changed what in which revision?

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