1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
with GNAT.Directory_Operations;
use GNAT.Directory_Operations;
with GNAT.Directory_Operations.Iteration;
with Gnat.Regexp;
use Gnat;
with Ada.Text_Io;
use Ada;
with Ada.Strings.Fixed;
use Ada.Strings;
with Gnat.Os_Lib;
function Search_Regexp (Path : in String;
Pattern : in String) return String is
Full_Line : OS_Lib.String_Access := new String ' ("");
procedure Action (Filename : in String;
Index : in Positive;
Verax : in out Boolean) is
Buffer : Os_Lib.String_Access := new String ' ("");
Is_Out : Boolean := True;
begin
--Text_Io.Put_Line(filename);
if Regexp.Match(Dir_name(Filename), Regexp.Compile(Base_Name(Pattern), True, False)) then
if Fixed.Index(Filename, " ") /= 0 then
for I in Filename'Range loop
if Filename(I) = Character'Val(32) then
Buffer := new String ' (Buffer.all & "\ ");
elsif Filename(I) = '/' then
if Fixed.Index(Filename, " ", I) > I and Fixed.Index(Filename, "/", I+1) > Fixed.Index(Filename, " ", I) then
if Is_Out then
Buffer := new String ' (Buffer.all & Filename(I) & "\""");
Is_Out := False;
else
Buffer := new String ' (Buffer.all & Filename(I));
end if;
elsif Fixed.Index(Filename, " ", I) > I and Fixed.Index(Filename, "/", I+1) = 0 then
if Is_Out then
Buffer := new String ' (Buffer.all & Filename(I) & "\""");
Is_Out := False;
else
Buffer := new String ' (Buffer.all & Filename(I));
end if;
else
Buffer := new String ' (Buffer.all & Filename(I));
end if;
else
Buffer := new String ' (Buffer.all & Filename(I));
end if;
end loop;
if not Is_Out then
Buffer := new String ' (Buffer.all & "\""");
end if;
Full_Line := new String ' (Full_Line.all & ' ' & "" & Buffer.all & "");
else
Full_Line := new String ' (Full_Line.all & ' ' & Filename);
end if;
end if;
Verax := False;
end Action;
procedure File_Search is
new Iteration.Wildcard_iterator(Action);
begin
Text_Io.Put_Line("pattern for : " & Expand_Path(Pattern));
if Expand_Path(Pattern) /= "" then
File_Search(Dir_Name(Expand_Path(Path)));
end if;
if Full_Line'Length > 0 then
return Full_Line.all;
else
return Pattern;
end if;
end Search_Regexp;
function Expand_filename (Line : in String) return String is
Current_Dir : constant String := ".";
Absolute_Path : constant Character := '/';
procedure Recursive_Expand (Line : in String; Result : in out Os_Lib.String_Access) is
Start, Stop : Natural := 0;
begin
if Line'Length > 1 then
if Start = 0 then
Start := Line'First;
end if;
if Line(Start) /= Absolute_Path then
Result := new String ' (Result.all & ' ' & Search_Regexp(Dir_Name(Current_Dir), Line(Start..Line'Last)));
else
Result := new String ' (Result.all & ' ' & Search_Regexp(Dir_Name(Line(Start..Line'last)), Line(Start..Line'Last)));
end if;
Start := Fixed.Index(Line, " ", Line'First+1);
if Start = 0 then
start := Line'Last;
end if;
if Start < Line'Last then
Stop := Fixed.Index(Line, " ", Start+1);
if Stop /= 0 then
Recursive_Expand(Line(start..stop), Result);
else
Recursive_Expand(Line(Start..Line'Last), Result);
end if;
end if;
else
if Start = 0 then
Start := Line'First;
end if;
if Line(Start) /= Absolute_Path then
Result := new String ' (Result.all & ' ' & Search_Regexp(Dir_Name(Current_Dir), Line(Start..Line'Last)));
else
Result := new String ' (Result.all & ' ' & Search_Regexp(Dir_Name(Line(Start..Line'last)), Line(Start..Line'Last)));
end if;
end if;
end Recursive_Expand;
Full_Set : Os_Lib.String_Access := new String ' ("");
begin
Recursive_Expand(Line, Full_Set);
return Full_Set.all;
end Expand_Filename; |
Partager