mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Fri Sep 02 15:07:44 2016 +0100
Revision:
144:ef7eb2e8f9f7
Parent:
0:9b334a45a8ff
This updates the lib to the mbed lib v125

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 144:ef7eb2e8f9f7 1 /* mbed Microcontroller Library
<> 144:ef7eb2e8f9f7 2 * Copyright (c) 2006-2013 ARM Limited
<> 144:ef7eb2e8f9f7 3 *
<> 144:ef7eb2e8f9f7 4 * Licensed under the Apache License, Version 2.0 (the "License");
<> 144:ef7eb2e8f9f7 5 * you may not use this file except in compliance with the License.
<> 144:ef7eb2e8f9f7 6 * You may obtain a copy of the License at
<> 144:ef7eb2e8f9f7 7 *
<> 144:ef7eb2e8f9f7 8 * http://www.apache.org/licenses/LICENSE-2.0
<> 144:ef7eb2e8f9f7 9 *
<> 144:ef7eb2e8f9f7 10 * Unless required by applicable law or agreed to in writing, software
<> 144:ef7eb2e8f9f7 11 * distributed under the License is distributed on an "AS IS" BASIS,
<> 144:ef7eb2e8f9f7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 144:ef7eb2e8f9f7 13 * See the License for the specific language governing permissions and
<> 144:ef7eb2e8f9f7 14 * limitations under the License.
<> 144:ef7eb2e8f9f7 15 */
<> 144:ef7eb2e8f9f7 16 #include "FileSystemLike.h"
<> 144:ef7eb2e8f9f7 17
<> 144:ef7eb2e8f9f7 18 namespace mbed {
<> 144:ef7eb2e8f9f7 19
<> 144:ef7eb2e8f9f7 20 class BaseDirHandle : public DirHandle {
<> 144:ef7eb2e8f9f7 21 public:
<> 144:ef7eb2e8f9f7 22 /*
<> 144:ef7eb2e8f9f7 23 We keep track of our current location as the n'th object in the
<> 144:ef7eb2e8f9f7 24 FileSystemLike list. Using a Base* instead would cause problems if that
<> 144:ef7eb2e8f9f7 25 object were to be destroyed between readdirs.
<> 144:ef7eb2e8f9f7 26 Using this method does mean though that destroying/creating objects can
<> 144:ef7eb2e8f9f7 27 give unusual results from readdir.
<> 144:ef7eb2e8f9f7 28 */
<> 144:ef7eb2e8f9f7 29 off_t n;
<> 144:ef7eb2e8f9f7 30 struct dirent cur_entry;
<> 144:ef7eb2e8f9f7 31
<> 144:ef7eb2e8f9f7 32 BaseDirHandle() : n(0), cur_entry() {
<> 144:ef7eb2e8f9f7 33 }
<> 144:ef7eb2e8f9f7 34
<> 144:ef7eb2e8f9f7 35 virtual int closedir() {
<> 144:ef7eb2e8f9f7 36 // No lock can be used in destructor
<> 144:ef7eb2e8f9f7 37 delete this;
<> 144:ef7eb2e8f9f7 38 return 0;
<> 144:ef7eb2e8f9f7 39 }
<> 144:ef7eb2e8f9f7 40
<> 144:ef7eb2e8f9f7 41 virtual struct dirent *readdir() {
<> 144:ef7eb2e8f9f7 42 lock();
<> 144:ef7eb2e8f9f7 43 FileBase *ptr = FileBase::get(n);
<> 144:ef7eb2e8f9f7 44 if (ptr == NULL) {
<> 144:ef7eb2e8f9f7 45 unlock();
<> 144:ef7eb2e8f9f7 46 return NULL;
<> 144:ef7eb2e8f9f7 47 }
<> 144:ef7eb2e8f9f7 48
<> 144:ef7eb2e8f9f7 49 /* Increment n, so next readdir gets the next item */
<> 144:ef7eb2e8f9f7 50 n++;
<> 144:ef7eb2e8f9f7 51
<> 144:ef7eb2e8f9f7 52 /* Setup cur entry and return a pointer to it */
<> 144:ef7eb2e8f9f7 53 std::strncpy(cur_entry.d_name, ptr->getName(), NAME_MAX);
<> 144:ef7eb2e8f9f7 54 unlock();
<> 144:ef7eb2e8f9f7 55 return &cur_entry;
<> 144:ef7eb2e8f9f7 56 }
<> 144:ef7eb2e8f9f7 57
<> 144:ef7eb2e8f9f7 58 virtual off_t telldir() {
<> 144:ef7eb2e8f9f7 59 lock();
<> 144:ef7eb2e8f9f7 60 off_t offset = n;
<> 144:ef7eb2e8f9f7 61 unlock();
<> 144:ef7eb2e8f9f7 62 return offset;
<> 144:ef7eb2e8f9f7 63 }
<> 144:ef7eb2e8f9f7 64
<> 144:ef7eb2e8f9f7 65 virtual void seekdir(off_t offset) {
<> 144:ef7eb2e8f9f7 66 lock();
<> 144:ef7eb2e8f9f7 67 n = offset;
<> 144:ef7eb2e8f9f7 68 unlock();
<> 144:ef7eb2e8f9f7 69 }
<> 144:ef7eb2e8f9f7 70
<> 144:ef7eb2e8f9f7 71 virtual void rewinddir() {
<> 144:ef7eb2e8f9f7 72 lock();
<> 144:ef7eb2e8f9f7 73 n = 0;
<> 144:ef7eb2e8f9f7 74 unlock();
<> 144:ef7eb2e8f9f7 75 }
<> 144:ef7eb2e8f9f7 76
<> 144:ef7eb2e8f9f7 77 protected:
<> 144:ef7eb2e8f9f7 78 PlatformMutex _mutex;
<> 144:ef7eb2e8f9f7 79
<> 144:ef7eb2e8f9f7 80 virtual void lock() {
<> 144:ef7eb2e8f9f7 81 _mutex.lock();
<> 144:ef7eb2e8f9f7 82 }
<> 144:ef7eb2e8f9f7 83
<> 144:ef7eb2e8f9f7 84 virtual void unlock() {
<> 144:ef7eb2e8f9f7 85 _mutex.unlock();
<> 144:ef7eb2e8f9f7 86 }
<> 144:ef7eb2e8f9f7 87 };
<> 144:ef7eb2e8f9f7 88
<> 144:ef7eb2e8f9f7 89 FileSystemLike::FileSystemLike(const char *name) : FileBase(name, FileSystemPathType) {
<> 144:ef7eb2e8f9f7 90
<> 144:ef7eb2e8f9f7 91 }
<> 144:ef7eb2e8f9f7 92
<> 144:ef7eb2e8f9f7 93 FileSystemLike::~FileSystemLike() {
<> 144:ef7eb2e8f9f7 94
<> 144:ef7eb2e8f9f7 95 }
<> 144:ef7eb2e8f9f7 96
<> 144:ef7eb2e8f9f7 97 DirHandle *FileSystemLike::opendir() {
<> 144:ef7eb2e8f9f7 98 return new BaseDirHandle();
<> 144:ef7eb2e8f9f7 99 }
<> 144:ef7eb2e8f9f7 100
<> 144:ef7eb2e8f9f7 101 } // namespace mbed