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