The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Wed Jun 21 17:31:38 2017 +0100
Revision:
145:64910690c574
Child:
146:22da6e220af6
Release 145 of the mbed library.

Who changed what in which revision?

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