------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
-- --
|
-- --
|
-- GNAT COMPILER COMPONENTS --
|
-- GNAT COMPILER COMPONENTS --
|
-- --
|
-- --
|
-- 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 --
|
-- 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 --
|
-- --
|
-- --
|
-- B o d y --
|
-- B o d y --
|
-- --
|
-- --
|
-- Copyright (C) 2001-2010, AdaCore --
|
-- Copyright (C) 2001-2010, AdaCore --
|
-- --
|
-- --
|
-- GNAT is free software; you can redistribute it and/or modify it under --
|
-- GNAT is free software; you can redistribute it and/or modify it under --
|
-- terms of the GNU General Public License as published by the Free Soft- --
|
-- terms of the GNU General Public License as published by the Free Soft- --
|
-- ware Foundation; either version 3, or (at your option) any later ver- --
|
-- ware Foundation; either version 3, or (at your option) any later ver- --
|
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
|
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
|
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
|
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
|
-- or FITNESS FOR A PARTICULAR PURPOSE. --
|
-- or FITNESS FOR A PARTICULAR PURPOSE. --
|
-- --
|
-- --
|
-- As a special exception under Section 7 of GPL version 3, you are granted --
|
-- As a special exception under Section 7 of GPL version 3, you are granted --
|
-- additional permissions described in the GCC Runtime Library Exception, --
|
-- additional permissions described in the GCC Runtime Library Exception, --
|
-- version 3.1, as published by the Free Software Foundation. --
|
-- version 3.1, as published by the Free Software Foundation. --
|
-- --
|
-- --
|
-- You should have received a copy of the GNU General Public License and --
|
-- You should have received a copy of the GNU General Public License and --
|
-- a copy of the GCC Runtime Library Exception along with this program; --
|
-- a copy of the GCC Runtime Library Exception along with this program; --
|
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
|
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
|
-- <http://www.gnu.org/licenses/>. --
|
-- <http://www.gnu.org/licenses/>. --
|
-- --
|
-- --
|
-- GNAT was originally developed by the GNAT team at New York University. --
|
-- GNAT was originally developed by the GNAT team at New York University. --
|
-- Extensive contributions were provided by Ada Core Technologies Inc. --
|
-- Extensive contributions were provided by Ada Core Technologies Inc. --
|
-- --
|
-- --
|
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
|
|
with Ada.Characters.Handling;
|
with Ada.Characters.Handling;
|
with Ada.Strings.Fixed;
|
with Ada.Strings.Fixed;
|
with Ada.Strings.Maps;
|
with Ada.Strings.Maps;
|
with GNAT.OS_Lib;
|
with GNAT.OS_Lib;
|
with GNAT.Regexp;
|
with GNAT.Regexp;
|
|
|
package body GNAT.Directory_Operations.Iteration is
|
package body GNAT.Directory_Operations.Iteration is
|
|
|
use Ada;
|
use Ada;
|
|
|
----------
|
----------
|
-- Find --
|
-- Find --
|
----------
|
----------
|
|
|
procedure Find
|
procedure Find
|
(Root_Directory : Dir_Name_Str;
|
(Root_Directory : Dir_Name_Str;
|
File_Pattern : String)
|
File_Pattern : String)
|
is
|
is
|
File_Regexp : constant Regexp.Regexp := Regexp.Compile (File_Pattern);
|
File_Regexp : constant Regexp.Regexp := Regexp.Compile (File_Pattern);
|
Index : Natural := 0;
|
Index : Natural := 0;
|
Quit : Boolean;
|
Quit : Boolean;
|
|
|
procedure Read_Directory (Directory : Dir_Name_Str);
|
procedure Read_Directory (Directory : Dir_Name_Str);
|
-- Open Directory and read all entries. This routine is called
|
-- Open Directory and read all entries. This routine is called
|
-- recursively for each sub-directories.
|
-- recursively for each sub-directories.
|
|
|
function Make_Pathname (Dir, File : String) return String;
|
function Make_Pathname (Dir, File : String) return String;
|
-- Returns the pathname for File by adding Dir as prefix
|
-- Returns the pathname for File by adding Dir as prefix
|
|
|
-------------------
|
-------------------
|
-- Make_Pathname --
|
-- Make_Pathname --
|
-------------------
|
-------------------
|
|
|
function Make_Pathname (Dir, File : String) return String is
|
function Make_Pathname (Dir, File : String) return String is
|
begin
|
begin
|
if Dir (Dir'Last) = '/' or else Dir (Dir'Last) = '\' then
|
if Dir (Dir'Last) = '/' or else Dir (Dir'Last) = '\' then
|
return Dir & File;
|
return Dir & File;
|
else
|
else
|
return Dir & Dir_Separator & File;
|
return Dir & Dir_Separator & File;
|
end if;
|
end if;
|
end Make_Pathname;
|
end Make_Pathname;
|
|
|
--------------------
|
--------------------
|
-- Read_Directory --
|
-- Read_Directory --
|
--------------------
|
--------------------
|
|
|
procedure Read_Directory (Directory : Dir_Name_Str) is
|
procedure Read_Directory (Directory : Dir_Name_Str) is
|
Buffer : String (1 .. 2_048);
|
Buffer : String (1 .. 2_048);
|
Last : Natural;
|
Last : Natural;
|
|
|
Dir : Dir_Type;
|
Dir : Dir_Type;
|
pragma Warnings (Off, Dir);
|
pragma Warnings (Off, Dir);
|
|
|
begin
|
begin
|
Open (Dir, Directory);
|
Open (Dir, Directory);
|
|
|
loop
|
loop
|
Read (Dir, Buffer, Last);
|
Read (Dir, Buffer, Last);
|
exit when Last = 0;
|
exit when Last = 0;
|
|
|
declare
|
declare
|
Dir_Entry : constant String := Buffer (1 .. Last);
|
Dir_Entry : constant String := Buffer (1 .. Last);
|
Pathname : constant String :=
|
Pathname : constant String :=
|
Make_Pathname (Directory, Dir_Entry);
|
Make_Pathname (Directory, Dir_Entry);
|
|
|
begin
|
begin
|
if Regexp.Match (Dir_Entry, File_Regexp) then
|
if Regexp.Match (Dir_Entry, File_Regexp) then
|
Index := Index + 1;
|
Index := Index + 1;
|
|
|
begin
|
begin
|
Action (Pathname, Index, Quit);
|
Action (Pathname, Index, Quit);
|
exception
|
exception
|
when others =>
|
when others =>
|
Close (Dir);
|
Close (Dir);
|
raise;
|
raise;
|
end;
|
end;
|
|
|
exit when Quit;
|
exit when Quit;
|
end if;
|
end if;
|
|
|
-- Recursively call for sub-directories, except for . and ..
|
-- Recursively call for sub-directories, except for . and ..
|
|
|
if not (Dir_Entry = "." or else Dir_Entry = "..")
|
if not (Dir_Entry = "." or else Dir_Entry = "..")
|
and then OS_Lib.Is_Directory (Pathname)
|
and then OS_Lib.Is_Directory (Pathname)
|
then
|
then
|
Read_Directory (Pathname);
|
Read_Directory (Pathname);
|
exit when Quit;
|
exit when Quit;
|
end if;
|
end if;
|
end;
|
end;
|
end loop;
|
end loop;
|
|
|
Close (Dir);
|
Close (Dir);
|
end Read_Directory;
|
end Read_Directory;
|
|
|
begin
|
begin
|
Quit := False;
|
Quit := False;
|
Read_Directory (Root_Directory);
|
Read_Directory (Root_Directory);
|
end Find;
|
end Find;
|
|
|
-----------------------
|
-----------------------
|
-- Wildcard_Iterator --
|
-- Wildcard_Iterator --
|
-----------------------
|
-----------------------
|
|
|
procedure Wildcard_Iterator (Path : Path_Name) is
|
procedure Wildcard_Iterator (Path : Path_Name) is
|
|
|
Index : Natural := 0;
|
Index : Natural := 0;
|
|
|
procedure Read
|
procedure Read
|
(Directory : String;
|
(Directory : String;
|
File_Pattern : String;
|
File_Pattern : String;
|
Suffix_Pattern : String);
|
Suffix_Pattern : String);
|
-- Read entries in Directory and call user's callback if the entry
|
-- Read entries in Directory and call user's callback if the entry
|
-- match File_Pattern and Suffix_Pattern is empty otherwise it will go
|
-- match File_Pattern and Suffix_Pattern is empty otherwise it will go
|
-- down one more directory level by calling Next_Level routine above.
|
-- down one more directory level by calling Next_Level routine above.
|
|
|
procedure Next_Level
|
procedure Next_Level
|
(Current_Path : String;
|
(Current_Path : String;
|
Suffix_Path : String);
|
Suffix_Path : String);
|
-- Extract next File_Pattern from Suffix_Path and call Read routine
|
-- Extract next File_Pattern from Suffix_Path and call Read routine
|
-- above.
|
-- above.
|
|
|
----------------
|
----------------
|
-- Next_Level --
|
-- Next_Level --
|
----------------
|
----------------
|
|
|
procedure Next_Level
|
procedure Next_Level
|
(Current_Path : String;
|
(Current_Path : String;
|
Suffix_Path : String)
|
Suffix_Path : String)
|
is
|
is
|
DS : Natural;
|
DS : Natural;
|
SP : String renames Suffix_Path;
|
SP : String renames Suffix_Path;
|
|
|
begin
|
begin
|
if SP'Length > 2
|
if SP'Length > 2
|
and then SP (SP'First) = '.'
|
and then SP (SP'First) = '.'
|
and then Strings.Maps.Is_In (SP (SP'First + 1), Dir_Seps)
|
and then Strings.Maps.Is_In (SP (SP'First + 1), Dir_Seps)
|
then
|
then
|
-- Starting with "./"
|
-- Starting with "./"
|
|
|
DS := Strings.Fixed.Index
|
DS := Strings.Fixed.Index
|
(SP (SP'First + 2 .. SP'Last),
|
(SP (SP'First + 2 .. SP'Last),
|
Dir_Seps);
|
Dir_Seps);
|
|
|
if DS = 0 then
|
if DS = 0 then
|
|
|
-- We have "./"
|
-- We have "./"
|
|
|
Read (Current_Path & ".", "*", "");
|
Read (Current_Path & ".", "*", "");
|
|
|
else
|
else
|
-- We have "./dir"
|
-- We have "./dir"
|
|
|
Read (Current_Path & ".",
|
Read (Current_Path & ".",
|
SP (SP'First + 2 .. DS - 1),
|
SP (SP'First + 2 .. DS - 1),
|
SP (DS .. SP'Last));
|
SP (DS .. SP'Last));
|
end if;
|
end if;
|
|
|
elsif SP'Length > 3
|
elsif SP'Length > 3
|
and then SP (SP'First .. SP'First + 1) = ".."
|
and then SP (SP'First .. SP'First + 1) = ".."
|
and then Strings.Maps.Is_In (SP (SP'First + 2), Dir_Seps)
|
and then Strings.Maps.Is_In (SP (SP'First + 2), Dir_Seps)
|
then
|
then
|
-- Starting with "../"
|
-- Starting with "../"
|
|
|
DS := Strings.Fixed.Index
|
DS := Strings.Fixed.Index
|
(SP (SP'First + 3 .. SP'Last), Dir_Seps);
|
(SP (SP'First + 3 .. SP'Last), Dir_Seps);
|
|
|
if DS = 0 then
|
if DS = 0 then
|
|
|
-- We have "../"
|
-- We have "../"
|
|
|
Read (Current_Path & "..", "*", "");
|
Read (Current_Path & "..", "*", "");
|
|
|
else
|
else
|
-- We have "../dir"
|
-- We have "../dir"
|
|
|
Read (Current_Path & "..",
|
Read (Current_Path & "..",
|
SP (SP'First + 3 .. DS - 1),
|
SP (SP'First + 3 .. DS - 1),
|
SP (DS .. SP'Last));
|
SP (DS .. SP'Last));
|
end if;
|
end if;
|
|
|
elsif Current_Path = ""
|
elsif Current_Path = ""
|
and then SP'Length > 1
|
and then SP'Length > 1
|
and then Characters.Handling.Is_Letter (SP (SP'First))
|
and then Characters.Handling.Is_Letter (SP (SP'First))
|
and then SP (SP'First + 1) = ':'
|
and then SP (SP'First + 1) = ':'
|
then
|
then
|
-- Starting with "<drive>:"
|
-- Starting with "<drive>:"
|
|
|
if SP'Length > 2
|
if SP'Length > 2
|
and then Strings.Maps.Is_In (SP (SP'First + 2), Dir_Seps)
|
and then Strings.Maps.Is_In (SP (SP'First + 2), Dir_Seps)
|
then
|
then
|
-- Starting with "<drive>:\"
|
-- Starting with "<drive>:\"
|
|
|
DS := Strings.Fixed.Index
|
DS := Strings.Fixed.Index
|
(SP (SP'First + 3 .. SP'Last), Dir_Seps);
|
(SP (SP'First + 3 .. SP'Last), Dir_Seps);
|
|
|
if DS = 0 then
|
if DS = 0 then
|
|
|
-- We have "<drive>:\dir"
|
-- We have "<drive>:\dir"
|
|
|
Read (SP (SP'First .. SP'First + 2),
|
Read (SP (SP'First .. SP'First + 2),
|
SP (SP'First + 3 .. SP'Last),
|
SP (SP'First + 3 .. SP'Last),
|
"");
|
"");
|
|
|
else
|
else
|
-- We have "<drive>:\dir\kkk"
|
-- We have "<drive>:\dir\kkk"
|
|
|
Read (SP (SP'First .. SP'First + 2),
|
Read (SP (SP'First .. SP'First + 2),
|
SP (SP'First + 3 .. DS - 1),
|
SP (SP'First + 3 .. DS - 1),
|
SP (DS .. SP'Last));
|
SP (DS .. SP'Last));
|
end if;
|
end if;
|
|
|
else
|
else
|
-- Starting with "<drive>:" and the drive letter not followed
|
-- Starting with "<drive>:" and the drive letter not followed
|
-- by a directory separator. The proper semantic on Windows is
|
-- by a directory separator. The proper semantic on Windows is
|
-- to read the content of the current selected directory on
|
-- to read the content of the current selected directory on
|
-- this drive. For example, if drive C current selected
|
-- this drive. For example, if drive C current selected
|
-- directory is c:\temp the suffix pattern "c:m*" is
|
-- directory is c:\temp the suffix pattern "c:m*" is
|
-- equivalent to c:\temp\m*.
|
-- equivalent to c:\temp\m*.
|
|
|
DS := Strings.Fixed.Index
|
DS := Strings.Fixed.Index
|
(SP (SP'First + 2 .. SP'Last), Dir_Seps);
|
(SP (SP'First + 2 .. SP'Last), Dir_Seps);
|
|
|
if DS = 0 then
|
if DS = 0 then
|
|
|
-- We have "<drive>:dir"
|
-- We have "<drive>:dir"
|
|
|
Read (SP, "", "");
|
Read (SP, "", "");
|
|
|
else
|
else
|
-- We have "<drive>:dir/kkk"
|
-- We have "<drive>:dir/kkk"
|
|
|
Read (SP (SP'First .. DS - 1), "", SP (DS .. SP'Last));
|
Read (SP (SP'First .. DS - 1), "", SP (DS .. SP'Last));
|
end if;
|
end if;
|
end if;
|
end if;
|
|
|
elsif Strings.Maps.Is_In (SP (SP'First), Dir_Seps) then
|
elsif Strings.Maps.Is_In (SP (SP'First), Dir_Seps) then
|
|
|
-- Starting with a /
|
-- Starting with a /
|
|
|
DS := Strings.Fixed.Index
|
DS := Strings.Fixed.Index
|
(SP (SP'First + 1 .. SP'Last), Dir_Seps);
|
(SP (SP'First + 1 .. SP'Last), Dir_Seps);
|
|
|
if DS = 0 then
|
if DS = 0 then
|
|
|
-- We have "/dir"
|
-- We have "/dir"
|
|
|
Read (Current_Path, SP (SP'First + 1 .. SP'Last), "");
|
Read (Current_Path, SP (SP'First + 1 .. SP'Last), "");
|
else
|
else
|
-- We have "/dir/kkk"
|
-- We have "/dir/kkk"
|
|
|
Read (Current_Path,
|
Read (Current_Path,
|
SP (SP'First + 1 .. DS - 1),
|
SP (SP'First + 1 .. DS - 1),
|
SP (DS .. SP'Last));
|
SP (DS .. SP'Last));
|
end if;
|
end if;
|
|
|
else
|
else
|
-- Starting with a name
|
-- Starting with a name
|
|
|
DS := Strings.Fixed.Index (SP, Dir_Seps);
|
DS := Strings.Fixed.Index (SP, Dir_Seps);
|
|
|
if DS = 0 then
|
if DS = 0 then
|
|
|
-- We have "dir"
|
-- We have "dir"
|
|
|
Read (Current_Path & '.', SP, "");
|
Read (Current_Path & '.', SP, "");
|
else
|
else
|
-- We have "dir/kkk"
|
-- We have "dir/kkk"
|
|
|
Read (Current_Path & '.',
|
Read (Current_Path & '.',
|
SP (SP'First .. DS - 1),
|
SP (SP'First .. DS - 1),
|
SP (DS .. SP'Last));
|
SP (DS .. SP'Last));
|
end if;
|
end if;
|
|
|
end if;
|
end if;
|
end Next_Level;
|
end Next_Level;
|
|
|
----------
|
----------
|
-- Read --
|
-- Read --
|
----------
|
----------
|
|
|
Quit : Boolean := False;
|
Quit : Boolean := False;
|
-- Global state to be able to exit all recursive calls
|
-- Global state to be able to exit all recursive calls
|
|
|
procedure Read
|
procedure Read
|
(Directory : String;
|
(Directory : String;
|
File_Pattern : String;
|
File_Pattern : String;
|
Suffix_Pattern : String)
|
Suffix_Pattern : String)
|
is
|
is
|
File_Regexp : constant Regexp.Regexp :=
|
File_Regexp : constant Regexp.Regexp :=
|
Regexp.Compile (File_Pattern, Glob => True);
|
Regexp.Compile (File_Pattern, Glob => True);
|
|
|
Dir : Dir_Type;
|
Dir : Dir_Type;
|
pragma Warnings (Off, Dir);
|
pragma Warnings (Off, Dir);
|
|
|
Buffer : String (1 .. 2_048);
|
Buffer : String (1 .. 2_048);
|
Last : Natural;
|
Last : Natural;
|
|
|
begin
|
begin
|
if OS_Lib.Is_Directory (Directory & Dir_Separator) then
|
if OS_Lib.Is_Directory (Directory & Dir_Separator) then
|
Open (Dir, Directory & Dir_Separator);
|
Open (Dir, Directory & Dir_Separator);
|
|
|
Dir_Iterator : loop
|
Dir_Iterator : loop
|
Read (Dir, Buffer, Last);
|
Read (Dir, Buffer, Last);
|
exit Dir_Iterator when Last = 0;
|
exit Dir_Iterator when Last = 0;
|
|
|
declare
|
declare
|
Dir_Entry : constant String := Buffer (1 .. Last);
|
Dir_Entry : constant String := Buffer (1 .. Last);
|
Pathname : constant String :=
|
Pathname : constant String :=
|
Directory & Dir_Separator & Dir_Entry;
|
Directory & Dir_Separator & Dir_Entry;
|
begin
|
begin
|
-- Handle "." and ".." only if explicit use in the
|
-- Handle "." and ".." only if explicit use in the
|
-- File_Pattern.
|
-- File_Pattern.
|
|
|
if not
|
if not
|
((Dir_Entry = "." and then File_Pattern /= ".")
|
((Dir_Entry = "." and then File_Pattern /= ".")
|
or else
|
or else
|
(Dir_Entry = ".." and then File_Pattern /= ".."))
|
(Dir_Entry = ".." and then File_Pattern /= ".."))
|
then
|
then
|
if Regexp.Match (Dir_Entry, File_Regexp) then
|
if Regexp.Match (Dir_Entry, File_Regexp) then
|
if Suffix_Pattern = "" then
|
if Suffix_Pattern = "" then
|
|
|
-- No more matching needed, call user's callback
|
-- No more matching needed, call user's callback
|
|
|
Index := Index + 1;
|
Index := Index + 1;
|
|
|
begin
|
begin
|
Action (Pathname, Index, Quit);
|
Action (Pathname, Index, Quit);
|
exception
|
exception
|
when others =>
|
when others =>
|
Close (Dir);
|
Close (Dir);
|
raise;
|
raise;
|
end;
|
end;
|
|
|
else
|
else
|
-- Down one level
|
-- Down one level
|
|
|
Next_Level
|
Next_Level
|
(Directory & Dir_Separator & Dir_Entry,
|
(Directory & Dir_Separator & Dir_Entry,
|
Suffix_Pattern);
|
Suffix_Pattern);
|
end if;
|
end if;
|
end if;
|
end if;
|
end if;
|
end if;
|
end;
|
end;
|
|
|
-- Exit if Quit set by call to Action, either at this level
|
-- Exit if Quit set by call to Action, either at this level
|
-- or at some lower recursive call to Next_Level.
|
-- or at some lower recursive call to Next_Level.
|
|
|
exit Dir_Iterator when Quit;
|
exit Dir_Iterator when Quit;
|
end loop Dir_Iterator;
|
end loop Dir_Iterator;
|
|
|
Close (Dir);
|
Close (Dir);
|
end if;
|
end if;
|
end Read;
|
end Read;
|
|
|
-- Start of processing for Wildcard_Iterator
|
-- Start of processing for Wildcard_Iterator
|
|
|
begin
|
begin
|
if Path = "" then
|
if Path = "" then
|
return;
|
return;
|
end if;
|
end if;
|
|
|
Next_Level ("", Path);
|
Next_Level ("", Path);
|
end Wildcard_Iterator;
|
end Wildcard_Iterator;
|
|
|
end GNAT.Directory_Operations.Iteration;
|
end GNAT.Directory_Operations.Iteration;
|
|
|