BA / Mbed OS BaBoRo1
Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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