Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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