test test test

Dependencies:   mbed

Committer:
mohamedmoawya
Date:
Mon May 25 19:06:11 2020 +0000
Revision:
0:e4c5e6ec922e
snake game tteest

Who changed what in which revision?

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