mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

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