mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

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