roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
directory.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <memory>
4
5
#include "
roo_io/status.h
"
6
7
namespace
roo_io
{
8
9
class
DirectoryImpl;
10
11
// Represent a browsable directory. A directory is like a multipass iterator
12
// over entries.
13
//
14
// Basic usage idiom:
15
//
16
// Directory dir = mount.opendir(path);
17
// while (dir.read()) {
18
// // Use dir.entry()
19
// }
20
// if (dir.failed()) { handleFailure(dir.status()); }
21
//
22
class
Directory
{
23
public
:
24
// Represents a single directory entry. Entries are transient during directory
25
// iteration.
26
class
Entry
{
27
public
:
28
Entry
(
const
Entry
&) =
delete
;
29
Entry
(
Entry
&&) =
default
;
30
Entry
&
operator=
(
Entry
&&) =
default
;
31
32
// Returns the absolute path of the file or directory represented by this
33
// entry.
34
const
char
*
path
()
const
{
return
path_; }
35
36
// Returns a name of the file or directory represented by this entry,
37
// relative to the directory path.
38
const
char
*
name
()
const
{
return
name_; }
39
40
// Returns true if this entry represents a directory.
41
bool
isDirectory
()
const
{
return
is_dir_; }
42
43
private
:
44
Entry
() : path_(
nullptr
), name_(
nullptr
), is_dir_(
false
) {}
45
46
friend
class
Directory
;
47
friend
class
DirectoryImpl
;
48
49
void
set(
const
char
*
path
,
int
name_offset
,
bool
is_dir
);
50
51
const
char
* path_;
52
const
char
* name_;
53
bool
is_dir_;
54
};
55
56
// Creates a directory object with the specified status (default closed).
57
Directory
(
Status
status
=
kClosed
);
58
59
~Directory
() =
default
;
60
Directory
(
Directory
&&
other
) =
default
;
61
62
Directory
&
operator=
(
Directory
&&
other
) =
default
;
63
64
// Returns the absolute path of this directory. Empty if closed.
65
const
char
*
path
()
const
;
66
67
// // Returns the name of this directory, relative to its parent. Empty if
68
// // closed.
69
// const char* name() const;
70
71
// Returns true if the directory object represents an existing, open
72
// directory.
73
bool
isOpen
()
const
{
return
(
status
() ==
kOk
||
status
() ==
kEndOfStream
); }
74
75
// Return true if opening or browsing the directory has failed, i.e. the
76
// state is not one of kOk, kEndOfStream, or kClosed.
77
bool
failed
()
const
{
return
!
isOpen
() &&
status
() !=
kClosed
; }
78
79
// Returns the status of this directory. Can be one of:
80
// * kOk, if the directory object is healthy and browsable,
81
// * kClosed, if the directory was never opened, or if it was closed,
82
// * kEndOfStream, if the directory has been read till the end,
83
// * any error returned by mount.opendir().
84
Status
status
()
const
{
return
status_; }
85
86
// Closes this directory. If the state was an error, leaves it as is;
87
// otherwise, changes the state to kClosed.
88
//
89
// Directory gets auto-closed when destroyed. Therefore, calling close()
90
// explicitly is usually unnecessary.
91
void
close
();
92
93
// If the directory is open, resets the entry index to the beginning, and
94
// resets the state to kOk. Otherwise, does nothing.
95
void
rewind
();
96
97
// Reads a subsequent directory entry. Invalidates the previously read entry.
98
// Returns true on success. If there is no more entries, or if error occurs,
99
// returns false.
100
bool
read
();
101
102
// Returns the details of the last read entry. If read() was never called, or
103
// if it returned false, the contents is undefined and should not be used.
104
// The value gets invalidated by a subsequent call to read(). If you want to
105
// rely on the contents of the entry beyond that, you need to make a copy.
106
const
Entry
&
entry
()
const
{
return
entry_; }
107
108
private
:
109
friend
class
Mount
;
110
111
Directory
(std::unique_ptr<DirectoryImpl>
dir
);
112
113
std::unique_ptr<DirectoryImpl> dir_;
114
Status
status_;
115
116
Entry
entry_;
117
};
118
119
}
// namespace roo_io
roo_io::DirectoryImpl
Definition
directory_impl.h:8
roo_io::Directory::Entry
Definition
directory.h:26
roo_io::Directory::Entry::path
const char * path() const
Definition
directory.h:34
roo_io::Directory::Entry::Entry
Entry(Entry &&)=default
roo_io::Directory::Entry::name
const char * name() const
Definition
directory.h:38
roo_io::Directory::Entry::operator=
Entry & operator=(Entry &&)=default
roo_io::Directory::Entry::Entry
Entry(const Entry &)=delete
roo_io::Directory::Entry::isDirectory
bool isDirectory() const
Definition
directory.h:41
roo_io::Directory
Definition
directory.h:22
roo_io::Directory::path
const char * path() const
Definition
directory.cpp:18
roo_io::Directory::read
bool read()
Definition
directory.cpp:32
roo_io::Directory::isOpen
bool isOpen() const
Definition
directory.h:73
roo_io::Directory::close
void close()
Definition
directory.cpp:9
roo_io::Directory::status
Status status() const
Definition
directory.h:84
roo_io::Directory::entry
const Entry & entry() const
Definition
directory.h:106
roo_io::Directory::Directory
Directory(Directory &&other)=default
roo_io::Directory::~Directory
~Directory()=default
roo_io::Directory::operator=
Directory & operator=(Directory &&other)=default
roo_io::Directory::rewind
void rewind()
Definition
directory.cpp:26
roo_io::Directory::failed
bool failed() const
Definition
directory.h:77
roo_io::Mount
Definition
mount.h:14
roo_io
Definition
byte.h:6
roo_io::basic_string_view
roo::basic_string_view< CharT, Traits > basic_string_view
Definition
string_view.h:8
roo_io::Status
Status
Definition
status.h:7
roo_io::kOk
@ kOk
Definition
status.h:8
roo_io::kClosed
@ kClosed
Definition
status.h:10
roo_io::kEndOfStream
@ kEndOfStream
Definition
status.h:9
status.h
temp_repos
roo_io
src
roo_io
fs
directory.h
Generated by
1.9.8