Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

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