Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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