BA / Mbed OS BaBoRo_test2
Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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