The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:20e0c61e0684 1 /* mbed Microcontroller Library
ganlikun 0:20e0c61e0684 2 * Copyright (c) 2006-2013 ARM Limited
ganlikun 0:20e0c61e0684 3 *
ganlikun 0:20e0c61e0684 4 * Licensed under the Apache License, Version 2.0 (the "License");
ganlikun 0:20e0c61e0684 5 * you may not use this file except in compliance with the License.
ganlikun 0:20e0c61e0684 6 * You may obtain a copy of the License at
ganlikun 0:20e0c61e0684 7 *
ganlikun 0:20e0c61e0684 8 * http://www.apache.org/licenses/LICENSE-2.0
ganlikun 0:20e0c61e0684 9 *
ganlikun 0:20e0c61e0684 10 * Unless required by applicable law or agreed to in writing, software
ganlikun 0:20e0c61e0684 11 * distributed under the License is distributed on an "AS IS" BASIS,
ganlikun 0:20e0c61e0684 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ganlikun 0:20e0c61e0684 13 * See the License for the specific language governing permissions and
ganlikun 0:20e0c61e0684 14 * limitations under the License.
ganlikun 0:20e0c61e0684 15 */
ganlikun 0:20e0c61e0684 16 #ifndef MBED_DIRHANDLE_H
ganlikun 0:20e0c61e0684 17 #define MBED_DIRHANDLE_H
ganlikun 0:20e0c61e0684 18
ganlikun 0:20e0c61e0684 19 #include <stdint.h>
ganlikun 0:20e0c61e0684 20 #include "platform/platform.h"
ganlikun 0:20e0c61e0684 21 #include "platform/FileHandle.h"
ganlikun 0:20e0c61e0684 22 #include "platform/NonCopyable.h"
ganlikun 0:20e0c61e0684 23
ganlikun 0:20e0c61e0684 24 namespace mbed {
ganlikun 0:20e0c61e0684 25 /** \addtogroup platform */
ganlikun 0:20e0c61e0684 26
ganlikun 0:20e0c61e0684 27
ganlikun 0:20e0c61e0684 28 /** Represents a directory stream. Objects of this type are returned
ganlikun 0:20e0c61e0684 29 * by an opendir function. The core functions are read and seek,
ganlikun 0:20e0c61e0684 30 * but only a subset needs to be provided.
ganlikun 0:20e0c61e0684 31 *
ganlikun 0:20e0c61e0684 32 * If a FileSystemLike class defines the opendir method, then the
ganlikun 0:20e0c61e0684 33 * directories of an object of that type can be accessed by
ganlikun 0:20e0c61e0684 34 * DIR *d = opendir("/example/directory") (or opendir("/example")
ganlikun 0:20e0c61e0684 35 * to open the root of the filesystem), and then using readdir(d) etc.
ganlikun 0:20e0c61e0684 36 *
ganlikun 0:20e0c61e0684 37 * The root directory is considered to contain all FileHandle and
ganlikun 0:20e0c61e0684 38 * FileSystem objects, so the DIR* returned by opendir("/") will
ganlikun 0:20e0c61e0684 39 * reflect this.
ganlikun 0:20e0c61e0684 40 *
ganlikun 0:20e0c61e0684 41 * @note to create a directory, @see Dir
ganlikun 0:20e0c61e0684 42 * @note Synchronization level: Set by subclass
ganlikun 0:20e0c61e0684 43 * @ingroup platform
ganlikun 0:20e0c61e0684 44 */
ganlikun 0:20e0c61e0684 45 class DirHandle : private NonCopyable<DirHandle> {
ganlikun 0:20e0c61e0684 46 public:
ganlikun 0:20e0c61e0684 47 virtual ~DirHandle() {}
ganlikun 0:20e0c61e0684 48
ganlikun 0:20e0c61e0684 49 /** Read the next directory entry
ganlikun 0:20e0c61e0684 50 *
ganlikun 0:20e0c61e0684 51 * @param ent The directory entry to fill out
ganlikun 0:20e0c61e0684 52 * @return 1 on reading a filename, 0 at end of directory, negative error on failure
ganlikun 0:20e0c61e0684 53 */
ganlikun 0:20e0c61e0684 54 virtual ssize_t read(struct dirent *ent) = 0;
ganlikun 0:20e0c61e0684 55
ganlikun 0:20e0c61e0684 56 /** Close a directory
ganlikun 0:20e0c61e0684 57 *
ganlikun 0:20e0c61e0684 58 * @return 0 on success, negative error code on failure
ganlikun 0:20e0c61e0684 59 */
ganlikun 0:20e0c61e0684 60 virtual int close() = 0;
ganlikun 0:20e0c61e0684 61
ganlikun 0:20e0c61e0684 62 /** Set the current position of the directory
ganlikun 0:20e0c61e0684 63 *
ganlikun 0:20e0c61e0684 64 * @param offset Offset of the location to seek to,
ganlikun 0:20e0c61e0684 65 * must be a value returned from tell
ganlikun 0:20e0c61e0684 66 */
ganlikun 0:20e0c61e0684 67 virtual void seek(off_t offset) = 0;
ganlikun 0:20e0c61e0684 68
ganlikun 0:20e0c61e0684 69 /** Get the current position of the directory
ganlikun 0:20e0c61e0684 70 *
ganlikun 0:20e0c61e0684 71 * @return Position of the directory that can be passed to rewind
ganlikun 0:20e0c61e0684 72 */
ganlikun 0:20e0c61e0684 73 virtual off_t tell() = 0;
ganlikun 0:20e0c61e0684 74
ganlikun 0:20e0c61e0684 75 /** Rewind the current position to the beginning of the directory
ganlikun 0:20e0c61e0684 76 */
ganlikun 0:20e0c61e0684 77 virtual void rewind() = 0;
ganlikun 0:20e0c61e0684 78
ganlikun 0:20e0c61e0684 79 /** Get the sizeof the directory
ganlikun 0:20e0c61e0684 80 *
ganlikun 0:20e0c61e0684 81 * @return Number of files in the directory
ganlikun 0:20e0c61e0684 82 */
ganlikun 0:20e0c61e0684 83 virtual size_t size()
ganlikun 0:20e0c61e0684 84 {
ganlikun 0:20e0c61e0684 85 off_t off = tell();
ganlikun 0:20e0c61e0684 86 size_t size = 0;
ganlikun 0:20e0c61e0684 87 struct dirent *ent = new struct dirent;
ganlikun 0:20e0c61e0684 88
ganlikun 0:20e0c61e0684 89 rewind();
ganlikun 0:20e0c61e0684 90 while (read(ent) > 0) {
ganlikun 0:20e0c61e0684 91 size += 1;
ganlikun 0:20e0c61e0684 92 }
ganlikun 0:20e0c61e0684 93 seek(off);
ganlikun 0:20e0c61e0684 94
ganlikun 0:20e0c61e0684 95 delete ent;
ganlikun 0:20e0c61e0684 96 return size;
ganlikun 0:20e0c61e0684 97 }
ganlikun 0:20e0c61e0684 98
ganlikun 0:20e0c61e0684 99 /** Closes the directory.
ganlikun 0:20e0c61e0684 100 *
ganlikun 0:20e0c61e0684 101 * @returns
ganlikun 0:20e0c61e0684 102 * 0 on success,
ganlikun 0:20e0c61e0684 103 * -1 on error.
ganlikun 0:20e0c61e0684 104 */
ganlikun 0:20e0c61e0684 105 MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::close")
ganlikun 0:20e0c61e0684 106 virtual int closedir() { return close(); };
ganlikun 0:20e0c61e0684 107
ganlikun 0:20e0c61e0684 108 /** Return the directory entry at the current position, and
ganlikun 0:20e0c61e0684 109 * advances the position to the next entry.
ganlikun 0:20e0c61e0684 110 *
ganlikun 0:20e0c61e0684 111 * @returns
ganlikun 0:20e0c61e0684 112 * A pointer to a dirent structure representing the
ganlikun 0:20e0c61e0684 113 * directory entry at the current position, or NULL on reaching
ganlikun 0:20e0c61e0684 114 * end of directory or error.
ganlikun 0:20e0c61e0684 115 */
ganlikun 0:20e0c61e0684 116 MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::read")
ganlikun 0:20e0c61e0684 117 virtual struct dirent *readdir()
ganlikun 0:20e0c61e0684 118 {
ganlikun 0:20e0c61e0684 119 static struct dirent ent;
ganlikun 0:20e0c61e0684 120 return (read(&ent) > 0) ? &ent : NULL;
ganlikun 0:20e0c61e0684 121 }
ganlikun 0:20e0c61e0684 122
ganlikun 0:20e0c61e0684 123 /** Resets the position to the beginning of the directory.
ganlikun 0:20e0c61e0684 124 */
ganlikun 0:20e0c61e0684 125 MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::rewind")
ganlikun 0:20e0c61e0684 126 virtual void rewinddir() { rewind(); }
ganlikun 0:20e0c61e0684 127
ganlikun 0:20e0c61e0684 128 /** Returns the current position of the DirHandle.
ganlikun 0:20e0c61e0684 129 *
ganlikun 0:20e0c61e0684 130 * @returns
ganlikun 0:20e0c61e0684 131 * the current position,
ganlikun 0:20e0c61e0684 132 * -1 on error.
ganlikun 0:20e0c61e0684 133 */
ganlikun 0:20e0c61e0684 134 MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::tell")
ganlikun 0:20e0c61e0684 135 virtual off_t telldir() { return tell(); }
ganlikun 0:20e0c61e0684 136
ganlikun 0:20e0c61e0684 137 /** Sets the position of the DirHandle.
ganlikun 0:20e0c61e0684 138 *
ganlikun 0:20e0c61e0684 139 * @param location The location to seek to. Must be a value returned by telldir.
ganlikun 0:20e0c61e0684 140 */
ganlikun 0:20e0c61e0684 141 MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::seek")
ganlikun 0:20e0c61e0684 142 virtual void seekdir(off_t location) { seek(location); }
ganlikun 0:20e0c61e0684 143 };
ganlikun 0:20e0c61e0684 144
ganlikun 0:20e0c61e0684 145
ganlikun 0:20e0c61e0684 146 } // namespace mbed
ganlikun 0:20e0c61e0684 147
ganlikun 0:20e0c61e0684 148 #endif /* MBED_DIRHANDLE_H */
ganlikun 0:20e0c61e0684 149