Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

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