Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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