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