Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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