mbed library sources
common/FileBase.cpp@0:0a673c671a56, 2016-07-27 (annotated)
- Committer:
- ebrus
- Date:
- Wed Jul 27 18:35:32 2016 +0000
- Revision:
- 0:0a673c671a56
4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ebrus | 0:0a673c671a56 | 1 | /* mbed Microcontroller Library |
ebrus | 0:0a673c671a56 | 2 | * Copyright (c) 2006-2013 ARM Limited |
ebrus | 0:0a673c671a56 | 3 | * |
ebrus | 0:0a673c671a56 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ebrus | 0:0a673c671a56 | 5 | * you may not use this file except in compliance with the License. |
ebrus | 0:0a673c671a56 | 6 | * You may obtain a copy of the License at |
ebrus | 0:0a673c671a56 | 7 | * |
ebrus | 0:0a673c671a56 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ebrus | 0:0a673c671a56 | 9 | * |
ebrus | 0:0a673c671a56 | 10 | * Unless required by applicable law or agreed to in writing, software |
ebrus | 0:0a673c671a56 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ebrus | 0:0a673c671a56 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ebrus | 0:0a673c671a56 | 13 | * See the License for the specific language governing permissions and |
ebrus | 0:0a673c671a56 | 14 | * limitations under the License. |
ebrus | 0:0a673c671a56 | 15 | */ |
ebrus | 0:0a673c671a56 | 16 | #include "FileBase.h" |
ebrus | 0:0a673c671a56 | 17 | |
ebrus | 0:0a673c671a56 | 18 | namespace mbed { |
ebrus | 0:0a673c671a56 | 19 | |
ebrus | 0:0a673c671a56 | 20 | FileBase *FileBase::_head = NULL; |
ebrus | 0:0a673c671a56 | 21 | |
ebrus | 0:0a673c671a56 | 22 | FileBase::FileBase(const char *name, PathType t) : _next(NULL), |
ebrus | 0:0a673c671a56 | 23 | _name(name), |
ebrus | 0:0a673c671a56 | 24 | _path_type(t) { |
ebrus | 0:0a673c671a56 | 25 | if (name != NULL) { |
ebrus | 0:0a673c671a56 | 26 | // put this object at head of the list |
ebrus | 0:0a673c671a56 | 27 | _next = _head; |
ebrus | 0:0a673c671a56 | 28 | _head = this; |
ebrus | 0:0a673c671a56 | 29 | } else { |
ebrus | 0:0a673c671a56 | 30 | _next = NULL; |
ebrus | 0:0a673c671a56 | 31 | } |
ebrus | 0:0a673c671a56 | 32 | } |
ebrus | 0:0a673c671a56 | 33 | |
ebrus | 0:0a673c671a56 | 34 | FileBase::~FileBase() { |
ebrus | 0:0a673c671a56 | 35 | if (_name != NULL) { |
ebrus | 0:0a673c671a56 | 36 | // remove this object from the list |
ebrus | 0:0a673c671a56 | 37 | if (_head == this) { // first in the list, so just drop me |
ebrus | 0:0a673c671a56 | 38 | _head = _next; |
ebrus | 0:0a673c671a56 | 39 | } else { // find the object before me, then drop me |
ebrus | 0:0a673c671a56 | 40 | FileBase *p = _head; |
ebrus | 0:0a673c671a56 | 41 | while (p->_next != this) { |
ebrus | 0:0a673c671a56 | 42 | p = p->_next; |
ebrus | 0:0a673c671a56 | 43 | } |
ebrus | 0:0a673c671a56 | 44 | p->_next = _next; |
ebrus | 0:0a673c671a56 | 45 | } |
ebrus | 0:0a673c671a56 | 46 | } |
ebrus | 0:0a673c671a56 | 47 | } |
ebrus | 0:0a673c671a56 | 48 | |
ebrus | 0:0a673c671a56 | 49 | FileBase *FileBase::lookup(const char *name, unsigned int len) { |
ebrus | 0:0a673c671a56 | 50 | FileBase *p = _head; |
ebrus | 0:0a673c671a56 | 51 | while (p != NULL) { |
ebrus | 0:0a673c671a56 | 52 | /* Check that p->_name matches name and is the correct length */ |
ebrus | 0:0a673c671a56 | 53 | if (p->_name != NULL && std::strncmp(p->_name, name, len) == 0 && std::strlen(p->_name) == len) { |
ebrus | 0:0a673c671a56 | 54 | return p; |
ebrus | 0:0a673c671a56 | 55 | } |
ebrus | 0:0a673c671a56 | 56 | p = p->_next; |
ebrus | 0:0a673c671a56 | 57 | } |
ebrus | 0:0a673c671a56 | 58 | return NULL; |
ebrus | 0:0a673c671a56 | 59 | } |
ebrus | 0:0a673c671a56 | 60 | |
ebrus | 0:0a673c671a56 | 61 | FileBase *FileBase::get(int n) { |
ebrus | 0:0a673c671a56 | 62 | FileBase *p = _head; |
ebrus | 0:0a673c671a56 | 63 | int m = 0; |
ebrus | 0:0a673c671a56 | 64 | while (p != NULL) { |
ebrus | 0:0a673c671a56 | 65 | if (m == n) return p; |
ebrus | 0:0a673c671a56 | 66 | |
ebrus | 0:0a673c671a56 | 67 | m++; |
ebrus | 0:0a673c671a56 | 68 | p = p->_next; |
ebrus | 0:0a673c671a56 | 69 | } |
ebrus | 0:0a673c671a56 | 70 | return NULL; |
ebrus | 0:0a673c671a56 | 71 | } |
ebrus | 0:0a673c671a56 | 72 | |
ebrus | 0:0a673c671a56 | 73 | const char* FileBase::getName(void) { |
ebrus | 0:0a673c671a56 | 74 | return _name; |
ebrus | 0:0a673c671a56 | 75 | } |
ebrus | 0:0a673c671a56 | 76 | |
ebrus | 0:0a673c671a56 | 77 | PathType FileBase::getPathType(void) { |
ebrus | 0:0a673c671a56 | 78 | return _path_type; |
ebrus | 0:0a673c671a56 | 79 | } |
ebrus | 0:0a673c671a56 | 80 | |
ebrus | 0:0a673c671a56 | 81 | } // namespace mbed |
ebrus | 0:0a673c671a56 | 82 |