Rtos API example

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     _mutex->lock();
00029     if (name != NULL) {
00030         // put this object at head of the list
00031         _next = _head;
00032         _head = this;
00033     } else {
00034         _next = NULL;
00035     }
00036     _mutex->unlock();
00037 }
00038 
00039 FileBase::~FileBase() {
00040     _mutex->lock();
00041     if (_name != NULL) {
00042         // remove this object from the list
00043         if (_head == this) { // first in the list, so just drop me
00044             _head = _next;
00045         } else {             // find the object before me, then drop me
00046             FileBase *p = _head;
00047             while (p->_next != this) {
00048                 p = p->_next;
00049             }
00050             p->_next = _next;
00051         }
00052     }
00053     _mutex->unlock();
00054 
00055     if (getPathType() == FilePathType) {
00056         extern void remove_filehandle(FileHandle *file);
00057         remove_filehandle(static_cast<FileHandle*>(static_cast<FileLike*>(this)));
00058     }
00059 }
00060 
00061 FileBase *FileBase::lookup(const char *name, unsigned int len) {
00062     _mutex->lock();
00063     FileBase *p = _head;
00064     while (p != NULL) {
00065         /* Check that p->_name matches name and is the correct length */
00066         if (p->_name != NULL && std::strncmp(p->_name, name, len) == 0 && std::strlen(p->_name) == len) {
00067             _mutex->unlock();
00068             return p;
00069         }
00070         p = p->_next;
00071     }
00072     _mutex->unlock();
00073     return NULL;
00074 }
00075 
00076 FileBase *FileBase::get(int n) {
00077     _mutex->lock();
00078     FileBase *p = _head;
00079     int m = 0;
00080     while (p != NULL) {
00081         if (m == n) {
00082             _mutex->unlock();
00083             return p;
00084         }
00085 
00086         m++;
00087         p = p->_next;
00088     }
00089     _mutex->unlock();
00090     return NULL;
00091 }
00092 
00093 const char* FileBase::getName(void) {
00094     // Constant read so no lock needed
00095     return _name;
00096 }
00097 
00098 PathType FileBase::getPathType(void) {
00099     // Constant read so no lock needed
00100     return _path_type;
00101 }
00102 
00103 } // namespace mbed
00104