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