mbed library sources, include can_api for nucleo-f091rc
Dependents: CanNucleoF0_example
Fork of mbed-src by
cpp/FileSystemLike.cpp@0:fd0d7bdfcdc2, 2012-11-20 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Nov 20 17:24:08 2012 +0000
- Revision:
- 0:fd0d7bdfcdc2
- Child:
- 1:62685faffa05
mbed sources
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:fd0d7bdfcdc2 | 1 | /* mbed Microcontroller Library |
mbed_official | 0:fd0d7bdfcdc2 | 2 | * Copyright (c) 2006-2012 ARM Limited |
mbed_official | 0:fd0d7bdfcdc2 | 3 | * |
mbed_official | 0:fd0d7bdfcdc2 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
mbed_official | 0:fd0d7bdfcdc2 | 5 | * of this software and associated documentation files (the "Software"), to deal |
mbed_official | 0:fd0d7bdfcdc2 | 6 | * in the Software without restriction, including without limitation the rights |
mbed_official | 0:fd0d7bdfcdc2 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mbed_official | 0:fd0d7bdfcdc2 | 8 | * copies of the Software, and to permit persons to whom the Software is |
mbed_official | 0:fd0d7bdfcdc2 | 9 | * furnished to do so, subject to the following conditions: |
mbed_official | 0:fd0d7bdfcdc2 | 10 | * |
mbed_official | 0:fd0d7bdfcdc2 | 11 | * The above copyright notice and this permission notice shall be included in |
mbed_official | 0:fd0d7bdfcdc2 | 12 | * all copies or substantial portions of the Software. |
mbed_official | 0:fd0d7bdfcdc2 | 13 | * |
mbed_official | 0:fd0d7bdfcdc2 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mbed_official | 0:fd0d7bdfcdc2 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mbed_official | 0:fd0d7bdfcdc2 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mbed_official | 0:fd0d7bdfcdc2 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mbed_official | 0:fd0d7bdfcdc2 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 0:fd0d7bdfcdc2 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
mbed_official | 0:fd0d7bdfcdc2 | 20 | * SOFTWARE. |
mbed_official | 0:fd0d7bdfcdc2 | 21 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 22 | #include "FileSystemLike.h" |
mbed_official | 0:fd0d7bdfcdc2 | 23 | |
mbed_official | 0:fd0d7bdfcdc2 | 24 | namespace mbed { |
mbed_official | 0:fd0d7bdfcdc2 | 25 | |
mbed_official | 0:fd0d7bdfcdc2 | 26 | FileSystemLike::FileSystemLike(const char *name) { |
mbed_official | 0:fd0d7bdfcdc2 | 27 | _name = name; |
mbed_official | 0:fd0d7bdfcdc2 | 28 | |
mbed_official | 0:fd0d7bdfcdc2 | 29 | if (name != NULL) { |
mbed_official | 0:fd0d7bdfcdc2 | 30 | // put this object at head of the list |
mbed_official | 0:fd0d7bdfcdc2 | 31 | _next = _head; |
mbed_official | 0:fd0d7bdfcdc2 | 32 | _head = this; |
mbed_official | 0:fd0d7bdfcdc2 | 33 | } else { |
mbed_official | 0:fd0d7bdfcdc2 | 34 | _next = NULL; |
mbed_official | 0:fd0d7bdfcdc2 | 35 | } |
mbed_official | 0:fd0d7bdfcdc2 | 36 | } |
mbed_official | 0:fd0d7bdfcdc2 | 37 | |
mbed_official | 0:fd0d7bdfcdc2 | 38 | FileSystemLike::~FileSystemLike() { |
mbed_official | 0:fd0d7bdfcdc2 | 39 | if (_name != NULL) { |
mbed_official | 0:fd0d7bdfcdc2 | 40 | // remove this object from the list |
mbed_official | 0:fd0d7bdfcdc2 | 41 | if (_head == this) { // first in the list, so just drop me |
mbed_official | 0:fd0d7bdfcdc2 | 42 | _head = _next; |
mbed_official | 0:fd0d7bdfcdc2 | 43 | } else { // find the object before me, then drop me |
mbed_official | 0:fd0d7bdfcdc2 | 44 | FileSystemLike* p = _head; |
mbed_official | 0:fd0d7bdfcdc2 | 45 | while (p->_next != this) { |
mbed_official | 0:fd0d7bdfcdc2 | 46 | p = p->_next; |
mbed_official | 0:fd0d7bdfcdc2 | 47 | } |
mbed_official | 0:fd0d7bdfcdc2 | 48 | p->_next = _next; |
mbed_official | 0:fd0d7bdfcdc2 | 49 | } |
mbed_official | 0:fd0d7bdfcdc2 | 50 | } |
mbed_official | 0:fd0d7bdfcdc2 | 51 | } |
mbed_official | 0:fd0d7bdfcdc2 | 52 | |
mbed_official | 0:fd0d7bdfcdc2 | 53 | FileSystemLike *FileSystemLike::lookup(const char *name, unsigned int len) { |
mbed_official | 0:fd0d7bdfcdc2 | 54 | FileSystemLike *p = _head; |
mbed_official | 0:fd0d7bdfcdc2 | 55 | while (p != NULL) { |
mbed_official | 0:fd0d7bdfcdc2 | 56 | /* Check that p->_name matches name and is the correct length */ |
mbed_official | 0:fd0d7bdfcdc2 | 57 | if (p->_name != NULL && std::strncmp(p->_name, name, len) == 0 && std::strlen(p->_name) == len) { |
mbed_official | 0:fd0d7bdfcdc2 | 58 | return p; |
mbed_official | 0:fd0d7bdfcdc2 | 59 | } |
mbed_official | 0:fd0d7bdfcdc2 | 60 | p = p->_next; |
mbed_official | 0:fd0d7bdfcdc2 | 61 | } |
mbed_official | 0:fd0d7bdfcdc2 | 62 | return NULL; |
mbed_official | 0:fd0d7bdfcdc2 | 63 | } |
mbed_official | 0:fd0d7bdfcdc2 | 64 | |
mbed_official | 0:fd0d7bdfcdc2 | 65 | class BaseDirHandle : public DirHandle { |
mbed_official | 0:fd0d7bdfcdc2 | 66 | public: |
mbed_official | 0:fd0d7bdfcdc2 | 67 | /* |
mbed_official | 0:fd0d7bdfcdc2 | 68 | We keep track of our current location as the n'th object in the |
mbed_official | 0:fd0d7bdfcdc2 | 69 | FileSystemLike list. Using a Base* instead would cause problems if that |
mbed_official | 0:fd0d7bdfcdc2 | 70 | object were to be destroyed between readdirs. |
mbed_official | 0:fd0d7bdfcdc2 | 71 | Using this method does mean though that destroying/creating objects can |
mbed_official | 0:fd0d7bdfcdc2 | 72 | give unusual results from readdir. |
mbed_official | 0:fd0d7bdfcdc2 | 73 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 74 | off_t n; |
mbed_official | 0:fd0d7bdfcdc2 | 75 | struct dirent cur_entry; |
mbed_official | 0:fd0d7bdfcdc2 | 76 | |
mbed_official | 0:fd0d7bdfcdc2 | 77 | BaseDirHandle() { |
mbed_official | 0:fd0d7bdfcdc2 | 78 | n = 0; |
mbed_official | 0:fd0d7bdfcdc2 | 79 | } |
mbed_official | 0:fd0d7bdfcdc2 | 80 | |
mbed_official | 0:fd0d7bdfcdc2 | 81 | virtual int closedir() { |
mbed_official | 0:fd0d7bdfcdc2 | 82 | delete this; |
mbed_official | 0:fd0d7bdfcdc2 | 83 | return 0; |
mbed_official | 0:fd0d7bdfcdc2 | 84 | } |
mbed_official | 0:fd0d7bdfcdc2 | 85 | |
mbed_official | 0:fd0d7bdfcdc2 | 86 | virtual struct dirent *readdir() { |
mbed_official | 0:fd0d7bdfcdc2 | 87 | FileSystemLike *ptr = FileSystemLike::_head; |
mbed_official | 0:fd0d7bdfcdc2 | 88 | /* Find the n'th FileLike or FileSystemLike object in the Base list */ |
mbed_official | 0:fd0d7bdfcdc2 | 89 | int m = 0; |
mbed_official | 0:fd0d7bdfcdc2 | 90 | while (1) { |
mbed_official | 0:fd0d7bdfcdc2 | 91 | if (ptr == NULL) { |
mbed_official | 0:fd0d7bdfcdc2 | 92 | /* No such object */ |
mbed_official | 0:fd0d7bdfcdc2 | 93 | return NULL; |
mbed_official | 0:fd0d7bdfcdc2 | 94 | } |
mbed_official | 0:fd0d7bdfcdc2 | 95 | if (m == n) break; |
mbed_official | 0:fd0d7bdfcdc2 | 96 | |
mbed_official | 0:fd0d7bdfcdc2 | 97 | m++; |
mbed_official | 0:fd0d7bdfcdc2 | 98 | ptr = ptr->_next; |
mbed_official | 0:fd0d7bdfcdc2 | 99 | } |
mbed_official | 0:fd0d7bdfcdc2 | 100 | /* Increment n, so next readdir gets the next item */ |
mbed_official | 0:fd0d7bdfcdc2 | 101 | n++; |
mbed_official | 0:fd0d7bdfcdc2 | 102 | /* Setup curentry and return a pointer to it */ |
mbed_official | 0:fd0d7bdfcdc2 | 103 | std::strncpy(cur_entry.d_name, ptr->_name, NAME_MAX); |
mbed_official | 0:fd0d7bdfcdc2 | 104 | return &cur_entry; |
mbed_official | 0:fd0d7bdfcdc2 | 105 | } |
mbed_official | 0:fd0d7bdfcdc2 | 106 | |
mbed_official | 0:fd0d7bdfcdc2 | 107 | virtual off_t telldir() { |
mbed_official | 0:fd0d7bdfcdc2 | 108 | return n; |
mbed_official | 0:fd0d7bdfcdc2 | 109 | } |
mbed_official | 0:fd0d7bdfcdc2 | 110 | |
mbed_official | 0:fd0d7bdfcdc2 | 111 | virtual void seekdir(off_t offset) { |
mbed_official | 0:fd0d7bdfcdc2 | 112 | n = offset; |
mbed_official | 0:fd0d7bdfcdc2 | 113 | } |
mbed_official | 0:fd0d7bdfcdc2 | 114 | |
mbed_official | 0:fd0d7bdfcdc2 | 115 | virtual void rewinddir() { |
mbed_official | 0:fd0d7bdfcdc2 | 116 | n = 0; |
mbed_official | 0:fd0d7bdfcdc2 | 117 | } |
mbed_official | 0:fd0d7bdfcdc2 | 118 | }; |
mbed_official | 0:fd0d7bdfcdc2 | 119 | |
mbed_official | 0:fd0d7bdfcdc2 | 120 | DirHandle *FileSystemLike::opendir() { |
mbed_official | 0:fd0d7bdfcdc2 | 121 | return new BaseDirHandle(); |
mbed_official | 0:fd0d7bdfcdc2 | 122 | } |
mbed_official | 0:fd0d7bdfcdc2 | 123 | |
mbed_official | 0:fd0d7bdfcdc2 | 124 | FileSystemLike *FileSystemLike::_head = NULL; |
mbed_official | 0:fd0d7bdfcdc2 | 125 | |
mbed_official | 0:fd0d7bdfcdc2 | 126 | |
mbed_official | 0:fd0d7bdfcdc2 | 127 | FilePath::FilePath(const char* file_path) : file_name(NULL), fs(NULL) { |
mbed_official | 0:fd0d7bdfcdc2 | 128 | if ((file_path[0] != '/') || (file_path[1] == 0)) return; |
mbed_official | 0:fd0d7bdfcdc2 | 129 | |
mbed_official | 0:fd0d7bdfcdc2 | 130 | const char* file_system = &file_path[1]; |
mbed_official | 0:fd0d7bdfcdc2 | 131 | file_name = file_system; |
mbed_official | 0:fd0d7bdfcdc2 | 132 | int len = 0; |
mbed_official | 0:fd0d7bdfcdc2 | 133 | while (true) { |
mbed_official | 0:fd0d7bdfcdc2 | 134 | char c = *file_name++; |
mbed_official | 0:fd0d7bdfcdc2 | 135 | if (c == '/') { // end of object name |
mbed_official | 0:fd0d7bdfcdc2 | 136 | /* file now points to one char after the '/' */ |
mbed_official | 0:fd0d7bdfcdc2 | 137 | break; |
mbed_official | 0:fd0d7bdfcdc2 | 138 | } |
mbed_official | 0:fd0d7bdfcdc2 | 139 | if (c == 0) { // end of object name, with no filename |
mbed_official | 0:fd0d7bdfcdc2 | 140 | file_name = NULL; |
mbed_official | 0:fd0d7bdfcdc2 | 141 | break; |
mbed_official | 0:fd0d7bdfcdc2 | 142 | } |
mbed_official | 0:fd0d7bdfcdc2 | 143 | len++; |
mbed_official | 0:fd0d7bdfcdc2 | 144 | } |
mbed_official | 0:fd0d7bdfcdc2 | 145 | |
mbed_official | 0:fd0d7bdfcdc2 | 146 | fs = FileSystemLike::lookup(file_system, len); |
mbed_official | 0:fd0d7bdfcdc2 | 147 | } |
mbed_official | 0:fd0d7bdfcdc2 | 148 | |
mbed_official | 0:fd0d7bdfcdc2 | 149 | const char* FilePath::fileName(void) { |
mbed_official | 0:fd0d7bdfcdc2 | 150 | return file_name; |
mbed_official | 0:fd0d7bdfcdc2 | 151 | } |
mbed_official | 0:fd0d7bdfcdc2 | 152 | |
mbed_official | 0:fd0d7bdfcdc2 | 153 | FileSystemLike* FilePath::fileSystem(void) { |
mbed_official | 0:fd0d7bdfcdc2 | 154 | return fs; |
mbed_official | 0:fd0d7bdfcdc2 | 155 | } |
mbed_official | 0:fd0d7bdfcdc2 | 156 | |
mbed_official | 0:fd0d7bdfcdc2 | 157 | FileSystemLike* FilePath::getFileSystem(const char* path) { |
mbed_official | 0:fd0d7bdfcdc2 | 158 | FilePath f(path); |
mbed_official | 0:fd0d7bdfcdc2 | 159 | return f.fs; |
mbed_official | 0:fd0d7bdfcdc2 | 160 | } |
mbed_official | 0:fd0d7bdfcdc2 | 161 | |
mbed_official | 0:fd0d7bdfcdc2 | 162 | } // namespace mbed |