mbed library sources
Fork of mbed-src by
Diff: cpp/FileSystemLike.cpp
- Revision:
- 2:143cac498751
- Parent:
- 1:62685faffa05
diff -r 62685faffa05 -r 143cac498751 cpp/FileSystemLike.cpp --- a/cpp/FileSystemLike.cpp Thu Nov 29 15:41:14 2012 +0000 +++ b/cpp/FileSystemLike.cpp Mon Feb 18 11:44:18 2013 +0000 @@ -1,67 +1,22 @@ /* mbed Microcontroller Library - * Copyright (c) 2006-2012 ARM Limited + * Copyright (c) 2006-2013 ARM Limited * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. + * http://www.apache.org/licenses/LICENSE-2.0 * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #include "FileSystemLike.h" namespace mbed { -FileSystemLike::FileSystemLike(const char *name) { - _name = name; - - if (name != NULL) { - // put this object at head of the list - _next = _head; - _head = this; - } else { - _next = NULL; - } -} - -FileSystemLike::~FileSystemLike() { - if (_name != NULL) { - // remove this object from the list - if (_head == this) { // first in the list, so just drop me - _head = _next; - } else { // find the object before me, then drop me - FileSystemLike* p = _head; - while (p->_next != this) { - p = p->_next; - } - p->_next = _next; - } - } -} - -FileSystemLike *FileSystemLike::lookup(const char *name, unsigned int len) { - FileSystemLike *p = _head; - while (p != NULL) { - /* Check that p->_name matches name and is the correct length */ - if (p->_name != NULL && std::strncmp(p->_name, name, len) == 0 && std::strlen(p->_name) == len) { - return p; - } - p = p->_next; - } - return NULL; -} - class BaseDirHandle : public DirHandle { public: /* @@ -73,85 +28,51 @@ */ off_t n; struct dirent cur_entry; - + BaseDirHandle() { n = 0; } - + virtual int closedir() { delete this; return 0; } - + virtual struct dirent *readdir() { - FileSystemLike *ptr = FileSystemLike::_head; - /* Find the n'th FileLike or FileSystemLike object in the Base list */ - int m = 0; - while (1) { - if (ptr == NULL) { - /* No such object */ - return NULL; - } - if (m == n) break; - - m++; - ptr = ptr->_next; - } + FileBase *ptr = FileBase::get(n); + if (ptr == NULL) return NULL; + /* Increment n, so next readdir gets the next item */ n++; - /* Setup curentry and return a pointer to it */ - std::strncpy(cur_entry.d_name, ptr->_name, NAME_MAX); + + /* Setup cur entry and return a pointer to it */ + std::strncpy(cur_entry.d_name, ptr->getName(), NAME_MAX); return &cur_entry; } - + virtual off_t telldir() { return n; } - - virtual void seekdir(off_t offset) { + + virtual void seekdir(off_t offset) { n = offset; } - + virtual void rewinddir() { n = 0; } }; +FileSystemLike::FileSystemLike(const char *name) : FileBase(name, FileSystemPathType) { + +} + +FileSystemLike::~FileSystemLike() { + +} + DirHandle *FileSystemLike::opendir() { return new BaseDirHandle(); } -FileSystemLike *FileSystemLike::_head = NULL; - - -FilePath::FilePath(const char* file_path) : file_name(NULL), fs(NULL) { - if ((file_path[0] != '/') || (file_path[1] == 0)) return; - - const char* file_system = &file_path[1]; - file_name = file_system; - int len = 0; - while (true) { - char c = *file_name; - if (c == '/') { // end of object name - file_name++; // point to one char after the '/' - break; - } - if (c == 0) { // end of object name, with no filename - break; - } - len++; - file_name++; - } - - fs = FileSystemLike::lookup(file_system, len); -} - -const char* FilePath::fileName(void) { - return file_name; -} - -FileSystemLike* FilePath::fileSystem(void) { - return fs; -} - } // namespace mbed