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-2015, 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
;
38 package body GNAT
.Directory_Operations
.Iteration
is
47 (Root_Directory
: Dir_Name_Str
;
48 File_Pattern
: String)
50 File_Regexp
: constant Regexp
.Regexp
:= Regexp
.Compile
(File_Pattern
);
54 procedure Read_Directory
(Directory
: Dir_Name_Str
);
55 -- Open Directory and read all entries. This routine is called
56 -- recursively for each sub-directories.
58 function Make_Pathname
(Dir
, File
: String) return String;
59 -- Returns the pathname for File by adding Dir as prefix
65 function Make_Pathname
(Dir
, File
: String) return String is
67 if Dir
(Dir
'Last) = '/' or else Dir
(Dir
'Last) = '\' then
70 return Dir
& Dir_Separator
& File
;
78 procedure Read_Directory
(Directory
: Dir_Name_Str
) is
79 Buffer
: String (1 .. 2_048
);
83 pragma Warnings
(Off
, Dir
);
86 Open
(Dir
, Directory
);
89 Read
(Dir
, Buffer
, Last
);
93 Dir_Entry
: constant String := Buffer
(1 .. Last
);
94 Pathname
: constant String :=
95 Make_Pathname
(Directory
, Dir_Entry
);
98 if Regexp
.Match
(Dir_Entry
, File_Regexp
) then
102 Action
(Pathname
, Index
, Quit
);
112 -- Recursively call for sub-directories, except for . and ..
114 if not (Dir_Entry
= "." or else Dir_Entry
= "..")
115 and then OS_Lib
.Is_Directory
(Pathname
)
117 Read_Directory
(Pathname
);
128 Read_Directory
(Root_Directory
);
131 -----------------------
132 -- Wildcard_Iterator --
133 -----------------------
135 procedure Wildcard_Iterator
(Path
: Path_Name
) is
137 Index
: Natural := 0;
141 File_Pattern
: String;
142 Suffix_Pattern
: String);
143 -- Read entries in Directory and call user's callback if the entry
144 -- match File_Pattern and Suffix_Pattern is empty otherwise it will go
145 -- down one more directory level by calling Next_Level routine above.
148 (Current_Path
: String;
149 Suffix_Path
: String);
150 -- Extract next File_Pattern from Suffix_Path and call Read routine
158 (Current_Path
: String;
159 Suffix_Path
: String)
162 SP
: String renames Suffix_Path
;
166 and then SP
(SP
'First) = '.'
167 and then Strings
.Maps
.Is_In
(SP
(SP
'First + 1), Dir_Seps
)
169 -- Starting with "./"
171 DS
:= Strings
.Fixed
.Index
172 (SP
(SP
'First + 2 .. SP
'Last),
179 Read
(Current_Path
& ".", "*", "");
184 Read
(Current_Path
& ".",
185 SP
(SP
'First + 2 .. DS
- 1),
190 and then SP
(SP
'First .. SP
'First + 1) = ".."
191 and then Strings
.Maps
.Is_In
(SP
(SP
'First + 2), Dir_Seps
)
193 -- Starting with "../"
195 DS
:= Strings
.Fixed
.Index
196 (SP
(SP
'First + 3 .. SP
'Last), Dir_Seps
);
202 Read
(Current_Path
& "..", "*", "");
207 Read
(Current_Path
& "..",
208 SP
(SP
'First + 3 .. DS
- 1),
212 elsif Current_Path
= ""
213 and then SP
'Length > 1
214 and then Characters
.Handling
.Is_Letter
(SP
(SP
'First))
215 and then SP
(SP
'First + 1) = ':'
217 -- Starting with "<drive>:"
220 and then Strings
.Maps
.Is_In
(SP
(SP
'First + 2), Dir_Seps
)
222 -- Starting with "<drive>:\"
224 DS
:= Strings
.Fixed
.Index
225 (SP
(SP
'First + 3 .. SP
'Last), Dir_Seps
);
229 -- We have "<drive>:\dir"
231 Read
(SP
(SP
'First .. SP
'First + 2),
232 SP
(SP
'First + 3 .. SP
'Last),
236 -- We have "<drive>:\dir\kkk"
238 Read
(SP
(SP
'First .. SP
'First + 2),
239 SP
(SP
'First + 3 .. DS
- 1),
244 -- Starting with "<drive>:" and the drive letter not followed
245 -- by a directory separator. The proper semantic on Windows is
246 -- to read the content of the current selected directory on
247 -- this drive. For example, if drive C current selected
248 -- directory is c:\temp the suffix pattern "c:m*" is
249 -- equivalent to c:\temp\m*.
251 DS
:= Strings
.Fixed
.Index
252 (SP
(SP
'First + 2 .. SP
'Last), Dir_Seps
);
256 -- We have "<drive>:dir"
261 -- We have "<drive>:dir/kkk"
263 Read
(SP
(SP
'First .. DS
- 1), "", SP
(DS
.. SP
'Last));
267 elsif Strings
.Maps
.Is_In
(SP
(SP
'First), Dir_Seps
) then
271 DS
:= Strings
.Fixed
.Index
272 (SP
(SP
'First + 1 .. SP
'Last), Dir_Seps
);
278 Read
(Current_Path
, SP
(SP
'First + 1 .. SP
'Last), "");
280 -- We have "/dir/kkk"
283 SP
(SP
'First + 1 .. DS
- 1),
288 -- Starting with a name
290 DS
:= Strings
.Fixed
.Index
(SP
, Dir_Seps
);
296 Read
(Current_Path
& '.', SP
, "");
300 Read
(Current_Path
& '.',
301 SP
(SP
'First .. DS
- 1),
312 Quit
: Boolean := False;
313 -- Global state to be able to exit all recursive calls
317 File_Pattern
: String;
318 Suffix_Pattern
: String)
320 File_Regexp
: constant Regexp
.Regexp
:=
321 Regexp
.Compile
(File_Pattern
, Glob
=> True);
324 pragma Warnings
(Off
, Dir
);
326 Buffer
: String (1 .. 2_048
);
330 if OS_Lib
.Is_Directory
(Directory
& Dir_Separator
) then
331 Open
(Dir
, Directory
& Dir_Separator
);
334 Read
(Dir
, Buffer
, Last
);
335 exit Dir_Iterator
when Last
= 0;
338 Dir_Entry
: constant String := Buffer
(1 .. Last
);
339 Pathname
: constant String :=
340 Directory
& Dir_Separator
& Dir_Entry
;
342 -- Handle "." and ".." only if explicit use in the
346 ((Dir_Entry
= "." and then File_Pattern
/= ".")
348 (Dir_Entry
= ".." and then File_Pattern
/= ".."))
350 if Regexp
.Match
(Dir_Entry
, File_Regexp
) then
351 if Suffix_Pattern
= "" then
353 -- No more matching needed, call user's callback
358 Action
(Pathname
, Index
, Quit
);
369 (Directory
& Dir_Separator
& Dir_Entry
,
376 -- Exit if Quit set by call to Action, either at this level
377 -- or at some lower recursive call to Next_Level.
379 exit Dir_Iterator
when Quit
;
380 end loop Dir_Iterator
;
386 -- Start of processing for Wildcard_Iterator
393 Next_Level
("", Path
);
394 end Wildcard_Iterator
;
396 end GNAT
.Directory_Operations
.Iteration
;