takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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         return _default;
00092     }
00093     _mutex->unlock();
00094     return NULL;
00095 }
00096 
00097 FileBase *FileBase::get(int n)
00098 {
00099     _mutex->lock();
00100     FileBase *p = _head;
00101     int m = 0;
00102     while (p != NULL) {
00103         if (m == n) {
00104             _mutex->unlock();
00105             return p;
00106         }
00107 
00108         m++;
00109         p = p->_next;
00110     }
00111     _mutex->unlock();
00112     return NULL;
00113 }
00114 
00115 const char *FileBase::getName(void)
00116 {
00117     // Constant read so no lock needed
00118     return _name;
00119 }
00120 
00121 PathType FileBase::getPathType(void)
00122 {
00123     // Constant read so no lock needed
00124     return _path_type;
00125 }
00126 
00127 } // namespace mbed
00128