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 Ada Core Technologies, Inc. --
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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
31 ------------------------------------------------------------------------------
33 with Ada
.Characters
.Handling
;
34 with Ada
.Strings
.Fixed
;
35 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
);
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
80 Buffer
: String (1 .. 2_048
);
85 Open
(Dir
, Directory
);
88 Read
(Dir
, Buffer
, Last
);
92 Dir_Entry
: constant String := Buffer
(1 .. Last
);
93 Pathname
: constant String
94 := Make_Pathname
(Directory
, Dir_Entry
);
96 if Regexp
.Match
(Dir_Entry
, File_Regexp
) then
101 Action
(Pathname
, Index
, Quit
);
111 -- Recursively call for sub-directories, except for . and ..
113 if not (Dir_Entry
= "." or else Dir_Entry
= "..")
114 and then OS_Lib
.Is_Directory
(Pathname
)
116 Read_Directory
(Pathname
);
125 Read_Directory
(Root_Directory
);
128 -----------------------
129 -- Wildcard_Iterator --
130 -----------------------
132 procedure Wildcard_Iterator
(Path
: Path_Name
) is
134 Index
: Natural := 0;
138 File_Pattern
: String;
139 Suffix_Pattern
: String);
140 -- Read entries in Directory and call user's callback if the entry
141 -- match File_Pattern and Suffix_Pattern is empty otherwise it will go
142 -- down one more directory level by calling Next_Level routine above.
145 (Current_Path
: String;
146 Suffix_Path
: String);
147 -- Extract next File_Pattern from Suffix_Path and call Read routine
155 (Current_Path
: String;
156 Suffix_Path
: String)
159 SP
: String renames Suffix_Path
;
163 and then SP
(SP
'First) = '.'
164 and then Strings
.Maps
.Is_In
(SP
(SP
'First + 1), Dir_Seps
)
166 -- Starting with "./"
168 DS
:= Strings
.Fixed
.Index
169 (SP
(SP
'First + 2 .. SP
'Last),
176 Read
(Current_Path
& ".", "*", "");
181 Read
(Current_Path
& ".",
182 SP
(SP
'First + 2 .. DS
- 1),
187 and then SP
(SP
'First .. SP
'First + 1) = ".."
188 and then Strings
.Maps
.Is_In
(SP
(SP
'First + 2), Dir_Seps
)
190 -- Starting with "../"
192 DS
:= Strings
.Fixed
.Index
193 (SP
(SP
'First + 3 .. SP
'Last),
200 Read
(Current_Path
& "..", "*", "");
205 Read
(Current_Path
& "..",
206 SP
(SP
'First + 4 .. DS
- 1),
210 elsif Current_Path
= ""
211 and then SP
'Length > 1
212 and then Characters
.Handling
.Is_Letter
(SP
(SP
'First))
213 and then SP
(SP
'First + 1) = ':'
215 -- Starting with "<drive>:"
218 and then Strings
.Maps
.Is_In
(SP
(SP
'First + 2), Dir_Seps
)
220 -- Starting with "<drive>:\"
222 DS
:= Strings
.Fixed
.Index
223 (SP
(SP
'First + 3 .. SP
'Last), Dir_Seps
);
227 -- Se have "<drive>:\dir"
229 Read
(SP
(SP
'First .. SP
'First + 1),
230 SP
(SP
'First + 3 .. SP
'Last),
234 -- We have "<drive>:\dir\kkk"
236 Read
(SP
(SP
'First .. SP
'First + 1),
237 SP
(SP
'First + 3 .. DS
- 1),
242 -- Starting with "<drive>:"
244 DS
:= Strings
.Fixed
.Index
245 (SP
(SP
'First + 2 .. SP
'Last), Dir_Seps
);
249 -- We have "<drive>:dir"
251 Read
(SP
(SP
'First .. SP
'First + 1),
252 SP
(SP
'First + 2 .. SP
'Last),
256 -- We have "<drive>:dir/kkk"
258 Read
(SP
(SP
'First .. SP
'First + 1),
259 SP
(SP
'First + 2 .. DS
- 1),
265 elsif Strings
.Maps
.Is_In
(SP
(SP
'First), Dir_Seps
) then
269 DS
:= Strings
.Fixed
.Index
270 (SP
(SP
'First + 1 .. SP
'Last),
278 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
& '.',
303 Read
(Current_Path
& '.',
304 SP
(SP
'First .. DS
- 1),
315 Quit
: Boolean := False;
316 -- Global state to be able to exit all recursive calls.
320 File_Pattern
: String;
321 Suffix_Pattern
: String)
323 File_Regexp
: constant Regexp
.Regexp
:=
324 Regexp
.Compile
(File_Pattern
, Glob
=> True);
326 Buffer
: String (1 .. 2_048
);
330 if OS_Lib
.Is_Directory
(Directory
) then
331 Open
(Dir
, Directory
);
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
352 if Suffix_Pattern
= "" then
354 -- No more matching needed, call user's callback
359 Action
(Pathname
, Index
, Quit
);
367 exit Dir_Iterator
when Quit
;
373 (Directory
& Dir_Separator
& Dir_Entry
,
380 exit Dir_Iterator
when Quit
;
382 end loop Dir_Iterator
;
389 Next_Level
("", Path
);
390 end Wildcard_Iterator
;
392 end GNAT
.Directory_Operations
.Iteration
;