inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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