mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
platform/DirHandle.h@178:79309dc6340a, 2017-11-23 (annotated)
- Committer:
- AnnaBridge
- Date:
- Thu Nov 23 11:57:25 2017 +0000
- Revision:
- 178:79309dc6340a
- Parent:
- 168:9672193075cf
mbed-dev library. Release version 156
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. |
AnnaBridge | 167:e84263d55307 | 108 | */ |
AnnaBridge | 167:e84263d55307 | 109 | MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::close") |
AnnaBridge | 167:e84263d55307 | 110 | virtual int closedir() { return close(); }; |
AnnaBridge | 167:e84263d55307 | 111 | |
AnnaBridge | 167:e84263d55307 | 112 | /** Return the directory entry at the current position, and |
AnnaBridge | 167:e84263d55307 | 113 | * advances the position to the next entry. |
AnnaBridge | 167:e84263d55307 | 114 | * |
AnnaBridge | 167:e84263d55307 | 115 | * @returns |
AnnaBridge | 167:e84263d55307 | 116 | * A pointer to a dirent structure representing the |
AnnaBridge | 167:e84263d55307 | 117 | * directory entry at the current position, or NULL on reaching |
AnnaBridge | 167:e84263d55307 | 118 | * end of directory or error. |
AnnaBridge | 167:e84263d55307 | 119 | */ |
AnnaBridge | 167:e84263d55307 | 120 | MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::read") |
AnnaBridge | 167:e84263d55307 | 121 | virtual struct dirent *readdir() |
AnnaBridge | 167:e84263d55307 | 122 | { |
AnnaBridge | 167:e84263d55307 | 123 | static struct dirent ent; |
AnnaBridge | 167:e84263d55307 | 124 | return (read(&ent) > 0) ? &ent : NULL; |
AnnaBridge | 167:e84263d55307 | 125 | } |
AnnaBridge | 167:e84263d55307 | 126 | |
AnnaBridge | 167:e84263d55307 | 127 | /** Resets the position to the beginning of the directory. |
AnnaBridge | 167:e84263d55307 | 128 | */ |
AnnaBridge | 167:e84263d55307 | 129 | MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::rewind") |
AnnaBridge | 167:e84263d55307 | 130 | virtual void rewinddir() { rewind(); } |
AnnaBridge | 167:e84263d55307 | 131 | |
AnnaBridge | 167:e84263d55307 | 132 | /** Returns the current position of the DirHandle. |
AnnaBridge | 167:e84263d55307 | 133 | * |
AnnaBridge | 167:e84263d55307 | 134 | * @returns |
AnnaBridge | 167:e84263d55307 | 135 | * the current position, |
AnnaBridge | 167:e84263d55307 | 136 | * -1 on error. |
AnnaBridge | 167:e84263d55307 | 137 | */ |
AnnaBridge | 167:e84263d55307 | 138 | MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::tell") |
AnnaBridge | 167:e84263d55307 | 139 | virtual off_t telldir() { return tell(); } |
AnnaBridge | 167:e84263d55307 | 140 | |
AnnaBridge | 167:e84263d55307 | 141 | /** Sets the position of the DirHandle. |
AnnaBridge | 167:e84263d55307 | 142 | * |
AnnaBridge | 167:e84263d55307 | 143 | * @param location The location to seek to. Must be a value returned by telldir. |
AnnaBridge | 167:e84263d55307 | 144 | */ |
AnnaBridge | 167:e84263d55307 | 145 | MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::seek") |
AnnaBridge | 167:e84263d55307 | 146 | virtual void seekdir(off_t location) { seek(location); } |
AnnaBridge | 167:e84263d55307 | 147 | }; |
AnnaBridge | 167:e84263d55307 | 148 | |
AnnaBridge | 178:79309dc6340a | 149 | /**@}*/ |
AnnaBridge | 167:e84263d55307 | 150 | |
AnnaBridge | 178:79309dc6340a | 151 | /**@}*/ |
AnnaBridge | 167:e84263d55307 | 152 | } // namespace mbed |
AnnaBridge | 167:e84263d55307 | 153 | |
AnnaBridge | 167:e84263d55307 | 154 | #endif /* MBED_DIRHANDLE_H */ |