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