Thomas Lyp / DevLibMemoryController

Dependencies:   FastPWM

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FileBase.cpp Source File

FileBase.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "platform/FileBase.h"
00017 #include "platform/FileLike.h"
00018 #include "platform/FileHandle.h"
00019 
00020 namespace mbed {
00021 
00022 FileBase *FileBase::_head = NULL;
00023 SingletonPtr<PlatformMutex>  FileBase::_mutex;
00024 
00025 FileBase::FileBase(const char *name, PathType t) : _next(NULL),
00026     _name(name),
00027     _path_type(t)
00028 {
00029     _mutex->lock();
00030     if (name != NULL) {
00031         // put this object at head of the list
00032         _next = _head;
00033         _head = this;
00034     } else {
00035         _next = NULL;
00036     }
00037     _mutex->unlock();
00038 }
00039 
00040 FileBase::~FileBase()
00041 {
00042     _mutex->lock();
00043     if (_name != NULL) {
00044         // remove this object from the list
00045         if (_head == this) { // first in the list, so just drop me
00046             _head = _next;
00047         } else {             // find the object before me, then drop me
00048             FileBase *p = _head;
00049             while (p->_next != this) {
00050                 p = p->_next;
00051             }
00052             p->_next = _next;
00053         }
00054     }
00055     _mutex->unlock();
00056 
00057     if (getPathType() == FilePathType) {
00058         extern void remove_filehandle(FileHandle * file);
00059         remove_filehandle(static_cast<FileHandle *>(static_cast<FileLike *>(this)));
00060     }
00061 }
00062 
00063 FileBase *FileBase::lookup(const char *name, unsigned int len)
00064 {
00065     _mutex->lock();
00066     FileBase *p = _head;
00067     while (p != NULL) {
00068         /* Check that p->_name matches name and is the correct length */
00069         if (p->_name != NULL && std::strncmp(p->_name, name, len) == 0 && std::strlen(p->_name) == len) {
00070             _mutex->unlock();
00071             return p;
00072         }
00073         p = p->_next;
00074     }
00075     _mutex->unlock();
00076     return NULL;
00077 }
00078 
00079 FileBase *FileBase::get(int n)
00080 {
00081     _mutex->lock();
00082     FileBase *p = _head;
00083     int m = 0;
00084     while (p != NULL) {
00085         if (m == n) {
00086             _mutex->unlock();
00087             return p;
00088         }
00089 
00090         m++;
00091         p = p->_next;
00092     }
00093     _mutex->unlock();
00094     return NULL;
00095 }
00096 
00097 const char *FileBase::getName(void)
00098 {
00099     // Constant read so no lock needed
00100     return _name;
00101 }
00102 
00103 PathType FileBase::getPathType(void)
00104 {
00105     // Constant read so no lock needed
00106     return _path_type;
00107 }
00108 
00109 } // namespace mbed
00110