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-2005 Adacore, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada.Characters.Handling;
35 with Ada.Strings.Fixed;
36 with Ada.Strings.Maps;
40 package body GNAT.Directory_Operations.Iteration is
49 (Root_Directory : Dir_Name_Str;
50 File_Pattern : String)
52 File_Regexp : constant Regexp.Regexp := Regexp.Compile (File_Pattern);
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
81 Buffer : String (1 .. 2_048);
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
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)
118 Read_Directory (Pathname);
127 Read_Directory (Root_Directory);
130 -----------------------
131 -- Wildcard_Iterator --
132 -----------------------
134 procedure Wildcard_Iterator (Path : Path_Name) is
136 Index : Natural := 0;
140 File_Pattern : String;
141 Suffix_Pattern : String);
142 -- Read entries in Directory and call user's callback if the entry
143 -- match File_Pattern and Suffix_Pattern is empty otherwise it will go
144 -- down one more directory level by calling Next_Level routine above.
147 (Current_Path : String;
148 Suffix_Path : String);
149 -- Extract next File_Pattern from Suffix_Path and call Read routine
157 (Current_Path : String;
158 Suffix_Path : String)
161 SP : String renames Suffix_Path;
165 and then SP (SP'First) = '.'
166 and then Strings.Maps.Is_In (SP (SP'First + 1), Dir_Seps)
168 -- Starting with "./"
170 DS := Strings.Fixed.Index
171 (SP (SP'First + 2 .. SP'Last),
178 Read (Current_Path & ".", "*", "");
183 Read (Current_Path & ".",
184 SP (SP'First + 2 .. DS - 1),
189 and then SP (SP'First .. SP'First + 1) = ".."
190 and then Strings.Maps.Is_In (SP (SP'First + 2), Dir_Seps)
192 -- Starting with "../"
194 DS := Strings.Fixed.Index
195 (SP (SP'First + 3 .. SP'Last), Dir_Seps);
201 Read (Current_Path & "..", "*", "");
206 Read (Current_Path & "..",
207 SP (SP'First + 3 .. DS - 1),
211 elsif Current_Path = ""
212 and then SP'Length > 1
213 and then Characters.Handling.Is_Letter (SP (SP'First))
214 and then SP (SP'First + 1) = ':'
216 -- Starting with "<drive>:"
219 and then Strings.Maps.Is_In (SP (SP'First + 2), Dir_Seps)
221 -- Starting with "<drive>:\"
223 DS := Strings.Fixed.Index
224 (SP (SP'First + 3 .. SP'Last), Dir_Seps);
228 -- We have "<drive>:\dir"
230 Read (SP (SP'First .. SP'First + 2),
231 SP (SP'First + 3 .. SP'Last),
235 -- We have "<drive>:\dir\kkk"
237 Read (SP (SP'First .. SP'First + 2),
238 SP (SP'First + 3 .. DS - 1),
243 -- Starting with "<drive>:" and the drive letter not followed
244 -- by a directory separator. The proper semantic on Windows is
245 -- to read the content of the current selected directory on
246 -- this drive. For example, if drive C current selected
247 -- directory is c:\temp the suffix pattern "c:m*" is
248 -- equivalent to c:\temp\m*.
250 DS := Strings.Fixed.Index
251 (SP (SP'First + 2 .. SP'Last), Dir_Seps);
255 -- We have "<drive>:dir"
260 -- We have "<drive>:dir/kkk"
262 Read (SP (SP'First .. DS - 1), "", SP (DS .. SP'Last));
266 elsif Strings.Maps.Is_In (SP (SP'First), Dir_Seps) then
270 DS := Strings.Fixed.Index
271 (SP (SP'First + 1 .. SP'Last), Dir_Seps);
277 Read (Current_Path, SP (SP'First + 1 .. SP'Last), "");
279 -- We have "/dir/kkk"
282 SP (SP'First + 1 .. DS - 1),
287 -- Starting with a name
289 DS := Strings.Fixed.Index (SP, Dir_Seps);
295 Read (Current_Path & '.', SP, "");
299 Read (Current_Path & '.',
300 SP (SP'First .. DS - 1),
311 Quit : Boolean := False;
312 -- Global state to be able to exit all recursive calls
316 File_Pattern : String;
317 Suffix_Pattern : String)
319 File_Regexp : constant Regexp.Regexp :=
320 Regexp.Compile (File_Pattern, Glob => True);
322 Buffer : String (1 .. 2_048);
326 if OS_Lib.Is_Directory (Directory & Dir_Separator) then
327 Open (Dir, Directory & Dir_Separator);
330 Read (Dir, Buffer, Last);
331 exit Dir_Iterator when Last = 0;
334 Dir_Entry : constant String := Buffer (1 .. Last);
335 Pathname : constant String :=
336 Directory & Dir_Separator & Dir_Entry;
338 -- Handle "." and ".." only if explicit use in the
342 ((Dir_Entry = "." and then File_Pattern /= ".")
344 (Dir_Entry = ".." and then File_Pattern /= ".."))
346 if Regexp.Match (Dir_Entry, File_Regexp) then
347 if Suffix_Pattern = "" then
349 -- No more matching needed, call user's callback
354 Action (Pathname, Index, Quit);
365 (Directory & Dir_Separator & Dir_Entry,
372 -- Exit if Quit set by call to Action, either at this level
373 -- or at at some lower recursive call to Next_Level.
375 exit Dir_Iterator when Quit;
376 end loop Dir_Iterator;
387 Next_Level ("", Path);
388 end Wildcard_Iterator;
390 end GNAT.Directory_Operations.Iteration;