1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . D I R E C T O R Y _ O P E R A T I O N S . I T E R A T I O N --
9 -- Copyright (C) 2001-2024, AdaCore --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with Ada
.Characters
.Handling
;
33 with Ada
.Strings
.Fixed
;
34 with Ada
.Strings
.Maps
;
39 package body GNAT
.Directory_Operations
.Iteration
is
48 (Root_Directory
: Dir_Name_Str
;
49 File_Pattern
: String)
51 File_Regexp
: constant Regexp
.Regexp
:= Regexp
.Compile
(File_Pattern
);
53 Quit
: Boolean := False;
55 procedure Read_Directory
(Directory
: Dir_Name_Str
);
56 -- Open Directory and read all entries. This routine is called
57 -- recursively for each sub-directories.
59 function Make_Pathname
(Dir
, File
: String) return String;
60 -- Returns the pathname for File by adding Dir as prefix
66 function Make_Pathname
(Dir
, File
: String) return String is
68 if Dir
(Dir
'Last) = '/' or else Dir
(Dir
'Last) = '\' then
71 return Dir
& Dir_Separator
& File
;
79 procedure Read_Directory
(Directory
: Dir_Name_Str
) is
80 Buffer
: String (1 .. 2_048
);
84 pragma Warnings
(Off
, Dir
);
87 Open
(Dir
, Directory
);
90 Read
(Dir
, Buffer
, Last
);
94 Dir_Entry
: constant String := Buffer
(1 .. Last
);
95 Pathname
: constant String :=
96 Make_Pathname
(Directory
, Dir_Entry
);
99 if Regexp
.Match
(Dir_Entry
, File_Regexp
) then
103 Action
(Pathname
, Index
, Quit
);
113 -- Recursively call for sub-directories, except for . and ..
115 if not (Dir_Entry
= "." or else Dir_Entry
= "..")
116 and then OS_Lib
.Is_Directory
(Pathname
)
117 and then not OS_Lib
.Is_Symbolic_Link
(Pathname
)
119 Read_Directory
(Pathname
);
129 Read_Directory
(Root_Directory
);
132 -----------------------
133 -- Wildcard_Iterator --
134 -----------------------
136 procedure Wildcard_Iterator
(Path
: Path_Name
) is
138 Index
: Natural := 0;
142 File_Pattern
: String;
143 Suffix_Pattern
: String);
144 -- Read entries in Directory and call user's callback if the entry match
145 -- File_Pattern and Suffix_Pattern is empty; otherwise go down one more
146 -- directory level by calling Next_Level routine below.
149 (Current_Path
: String;
150 Suffix_Path
: String);
151 -- Extract next File_Pattern from Suffix_Path and call Read routine
159 (Current_Path
: String;
160 Suffix_Path
: String)
163 SP
: String renames Suffix_Path
;
167 and then SP
(SP
'First) = '.'
168 and then Strings
.Maps
.Is_In
(SP
(SP
'First + 1), Dir_Seps
)
170 -- Starting with "./"
172 DS
:= Strings
.Fixed
.Index
173 (SP
(SP
'First + 2 .. SP
'Last),
180 Read
(Current_Path
& ".", "*", "");
185 Read
(Current_Path
& ".",
186 SP
(SP
'First + 2 .. DS
- 1),
191 and then SP
(SP
'First .. SP
'First + 1) = ".."
192 and then Strings
.Maps
.Is_In
(SP
(SP
'First + 2), Dir_Seps
)
194 -- Starting with "../"
196 DS
:= Strings
.Fixed
.Index
197 (SP
(SP
'First + 3 .. SP
'Last), Dir_Seps
);
203 Read
(Current_Path
& "..", "*", "");
208 Read
(Current_Path
& "..",
209 SP
(SP
'First + 3 .. DS
- 1),
213 elsif Current_Path
= ""
214 and then SP
'Length > 1
215 and then Characters
.Handling
.Is_Letter
(SP
(SP
'First))
216 and then SP
(SP
'First + 1) = ':'
218 -- Starting with "<drive>:"
221 and then Strings
.Maps
.Is_In
(SP
(SP
'First + 2), Dir_Seps
)
223 -- Starting with "<drive>:\"
225 DS
:= Strings
.Fixed
.Index
226 (SP
(SP
'First + 3 .. SP
'Last), Dir_Seps
);
230 -- We have "<drive>:\dir"
232 Read
(SP
(SP
'First .. SP
'First + 2),
233 SP
(SP
'First + 3 .. SP
'Last),
237 -- We have "<drive>:\dir\kkk"
239 Read
(SP
(SP
'First .. SP
'First + 2),
240 SP
(SP
'First + 3 .. DS
- 1),
245 -- Starting with "<drive>:" and the drive letter not followed
246 -- by a directory separator. The proper semantic on Windows is
247 -- to read the content of the current selected directory on
248 -- this drive. For example, if drive C current selected
249 -- directory is c:\temp the suffix pattern "c:m*" is
250 -- equivalent to c:\temp\m*.
252 DS
:= Strings
.Fixed
.Index
253 (SP
(SP
'First + 2 .. SP
'Last), Dir_Seps
);
257 -- We have "<drive>:dir"
262 -- We have "<drive>:dir/kkk"
264 Read
(SP
(SP
'First .. DS
- 1), "", SP
(DS
.. SP
'Last));
268 elsif Strings
.Maps
.Is_In
(SP
(SP
'First), Dir_Seps
) then
272 DS
:= Strings
.Fixed
.Index
273 (SP
(SP
'First + 1 .. SP
'Last), Dir_Seps
);
279 Read
(Current_Path
, SP
(SP
'First + 1 .. SP
'Last), "");
281 -- We have "/dir/kkk"
284 SP
(SP
'First + 1 .. DS
- 1),
289 -- Starting with a name
291 DS
:= Strings
.Fixed
.Index
(SP
, Dir_Seps
);
297 Read
(Current_Path
& '.', SP
, "");
301 Read
(Current_Path
& '.',
302 SP
(SP
'First .. DS
- 1),
313 Quit
: Boolean := False;
314 -- Global state to be able to exit all recursive calls
318 File_Pattern
: String;
319 Suffix_Pattern
: String)
321 File_Regexp
: constant Regexp
.Regexp
:=
322 Regexp
.Compile
(File_Pattern
, Glob
=> True);
325 pragma Warnings
(Off
, Dir
);
327 Buffer
: String (1 .. 2_048
);
331 if OS_Lib
.Is_Directory
(Directory
& Dir_Separator
) then
332 Open
(Dir
, Directory
& Dir_Separator
);
335 Read
(Dir
, Buffer
, Last
);
336 exit Dir_Iterator
when Last
= 0;
339 Dir_Entry
: constant String := Buffer
(1 .. Last
);
340 Pathname
: constant String :=
341 Directory
& Dir_Separator
& Dir_Entry
;
343 -- Handle "." and ".." only if explicit use in the
347 ((Dir_Entry
= "." and then File_Pattern
/= ".")
349 (Dir_Entry
= ".." and then File_Pattern
/= ".."))
351 if Regexp
.Match
(Dir_Entry
, File_Regexp
) then
352 if Suffix_Pattern
= "" then
354 -- No more matching needed, call user's callback
359 Action
(Pathname
, Index
, Quit
);
370 (Directory
& Dir_Separator
& Dir_Entry
,
377 -- Exit if Quit set by call to Action, either at this level
378 -- or at some lower recursive call to Next_Level.
380 exit Dir_Iterator
when Quit
;
381 end loop Dir_Iterator
;
387 -- Start of processing for Wildcard_Iterator
394 Next_Level
("", Path
);
395 end Wildcard_Iterator
;
397 end GNAT
.Directory_Operations
.Iteration
;