Mbed OS Reference
Loading...
Searching...
No Matches
DirHandle.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2019 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#ifndef MBED_DIRHANDLE_H
18#define MBED_DIRHANDLE_H
19
20#include <stdint.h>
21#include "platform/mbed_toolchain.h"
22#include "platform/NonCopyable.h"
23
24namespace mbed {
25/** \addtogroup platform-public-api */
26/** @{*/
27/**
28 * \defgroup platform_DirHandle DirHandle functions
29 * @{
30 */
31
32
33/** Represents a directory stream. An opendir function returns
34 * objects of this type. The core functions are read and seek,
35 * but only a subset needs to be provided.
36 *
37 * If a FileSystemLike class defines the opendir method, then you
38 * can access the directories of an object of that type by either:
39 * @code
40 * DIR *d = opendir("/example/directory");
41 * @endcode
42 * or
43 * @code
44 * DIR *d = opendir("/example");
45 * @endcode
46 * to open the root of the file system.
47 *
48 * The root directory is considered to contain all FileHandle and
49 * FileSystem objects, so the DIR pointer returned by opendir("/")
50 * reflects this.
51 *
52 * @note to create a directory, @see Dir
53 * @note Synchronization level: Set by subclass
54 */
55class DirHandle : private NonCopyable<DirHandle> {
56public:
57 virtual ~DirHandle() {}
58
59 /** Read the next directory entry
60 *
61 * @param ent The directory entry to fill out
62 * @return 1 on reading a filename, 0 at end of directory, negative error on failure
63 */
64 virtual ssize_t read(struct dirent *ent) = 0;
65
66 /** Close a directory
67 *
68 * @return 0 on success, negative error code on failure
69 */
70 virtual int close() = 0;
71
72 /** Set the current position of the directory
73 *
74 * @param offset Offset of the location to seek to,
75 * must be a value returned from tell
76 */
77 virtual void seek(off_t offset) = 0;
78
79 /** Get the current position of the directory
80 *
81 * @return Position of the directory that can be passed to rewind
82 */
83 virtual off_t tell() = 0;
84
85 /** Rewind the current position to the beginning of the directory
86 */
87 virtual void rewind() = 0;
88
89 /** Get the sizeof the directory
90 *
91 * @return Number of files in the directory
92 */
93 virtual size_t size()
94 {
95 off_t off = tell();
96 size_t size = 0;
97 struct dirent *ent = new struct dirent;
98
99 rewind();
100 while (read(ent) > 0) {
101 size += 1;
102 }
103 seek(off);
104
105 delete ent;
106 return size;
107 }
108};
109
110/**@}*/
111
112/**@}*/
113} // namespace mbed
114
115#endif /* MBED_DIRHANDLE_H */
Represents a directory stream.
Definition: DirHandle.h:55
virtual void seek(off_t offset)=0
Set the current position of the directory.
virtual size_t size()
Get the sizeof the directory.
Definition: DirHandle.h:93
virtual ssize_t read(struct dirent *ent)=0
Read the next directory entry.
virtual int close()=0
Close a directory.
virtual void rewind()=0
Rewind the current position to the beginning of the directory.
virtual off_t tell()=0
Get the current position of the directory.
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:162