1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 | with
Ada.Iterator_Interfaces,
Ada.Directories,
Ada.Containers.Vectors;
private
with
Ada.Finalization;
package Shell.Directories
--
-- Allows directories to be treated as a container using the new
-- for Each of Container
-- loop
-- Operate_on (Each);
-- end loop;
--
-- See 'Test_Iterate_Directory' for a simple example.
--
is
type Directory is tagged private
with
Default_Iterator => Iterate,
Iterator_Element => Constant_Reference_Type,
Constant_Indexing => Element_Value;
function To_Directory (Path : in String;
Recurse : in Boolean := False) return Directory;
function Path (Container : in Directory) return String;
type Cursor is private;
function Has_Element (Pos : Cursor) return Boolean;
subtype Directory_Entry_Type is Ada.Directories.Directory_Entry_Type;
type Constant_Reference_Type (Element : not null access constant Directory_Entry_Type)
is private
with
Implicit_Dereference => Element;
package Directory_Iterators is new Ada.Iterator_Interfaces (Cursor, Has_Element);
function Iterate (Container : Directory) return Directory_Iterators.Forward_Iterator'Class;
function Element_Value (Container : Directory;
Pos : Cursor) return Constant_Reference_Type;
private
type Directory is tagged
record
Path : Unbounded_String;
Recurse : Boolean;
end record;
type Constant_Reference_Type (Element : not null access constant Directory_Entry_Type) is null record;
subtype Search_Type is Ada.Directories.Search_Type;
type Search_Access is access all Search_Type;
type Directory_Access is access all Directory;
type Directory_Entry_Access is access all Directory_Entry_Type;
-- Entries
--
package Entry_Vectors is new ada.Containers.Vectors (Index_Type => Positive,
Element_Type => Directory_Entry_Access);
type Entries is new Entry_Vectors.Vector with null record;
-- Cursor
--
type Cursor is
record
Container : Directory_Access;
Directory_Entry : Directory_Entry_Access;
end record;
No_Element : constant Cursor := Cursor' (Container => null,
Directory_Entry => null);
-- String Vectors
--
package Strings_Vector is new Ada.Containers.Vectors (Index_Type => Positive,
Element_Type => Unbounded_String);
-- Iterator
--
type Iterator_State is
record
Prior : Directory_Entry_Access; -- Used to free expired directory entries.
Subdirs : Strings_Vector.Vector; -- Used to recurse through sub-directories.
end record;
type Iterator_State_Access is access all Iterator_State;
type Iterator is new Ada.Finalization.Controlled
and Directory_Iterators.Forward_Iterator with
record
Container : Directory_Access;
Search : Search_Access; -- Access is due to Search_Type being limited.
State : Iterator_State_Access; -- Access allows modifying the Iterator state in functions.
end record;
overriding
function First (Object : in Iterator) return Cursor;
overriding
function Next (Object : in Iterator;
Position : in Cursor) return Cursor;
overriding
procedure Finalize (Object : in out Iterator);
end Shell.Directories;
|