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
7namespace roo_io {
8
9class 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//
22class 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).
58
59 ~Directory() = default;
61
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
const char * path() const
Definition directory.h:34
Entry(Entry &&)=default
const char * name() const
Definition directory.h:38
Entry & operator=(Entry &&)=default
Entry(const Entry &)=delete
bool isDirectory() const
Definition directory.h:41
const char * path() const
Definition directory.cpp:18
bool isOpen() const
Definition directory.h:73
Status status() const
Definition directory.h:84
const Entry & entry() const
Definition directory.h:106
Directory(Directory &&other)=default
~Directory()=default
Directory & operator=(Directory &&other)=default
bool failed() const
Definition directory.h:77
Definition byte.h:6
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8
Status
Definition status.h:7
@ kOk
Definition status.h:8
@ kClosed
Definition status.h:10
@ kEndOfStream
Definition status.h:9