1
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
2
with Ada.Unchecked_Deallocation;
4
with GNAT.Strings; use GNAT.Strings;
6
package body Scripts.Utils is
8
------------------------------------
9
-- Argument_List_To_Quoted_String --
10
------------------------------------
12
function Argument_List_To_Quoted_String
13
(Args : GNAT.Strings.String_List;
14
Quote : Character := '"';
15
Quote_Backslash : Boolean := True) return String
19
-- Compute the maximum length of the output.
21
for J in Args'Range loop
22
-- For each argument we append at most 3 characters, two quotes
23
-- plus an ending space.
25
if Args (J) /= null then
26
Len := Len + Args (J)'Length + 3;
28
for T in Args (J)'Range loop
29
if Args (J)(T) = Quote or else Args (J)(T) = '\' then
37
Result : String (1 .. Len + 1);
38
Ind : Natural := Result'First;
40
procedure Append (Str : String);
41
-- Append the contents of Str to Result, protecting quote characters
47
procedure Append (Str : String) is
49
for J in Str'Range loop
51
or else (Quote_Backslash and then Str (J) = '\')
54
Result (Ind + 1) := Str (J);
57
Result (Ind) := Str (J);
64
for J in Args'Range loop
65
if Args (J) /= null then
66
if Index (Args (J).all, " ") > 0 then
67
Result (Ind) := Quote;
69
Append (Args (J).all);
70
Result (Ind) := Quote;
71
Result (Ind + 1) := ' ';
75
Append (Args (J).all);
82
return Result (1 .. Ind - 1);
84
end Argument_List_To_Quoted_String;
86
------------------------------------------------
87
-- Argument_String_To_List_With_Triple_Quotes --
88
------------------------------------------------
90
function Argument_String_To_List_With_Triple_Quotes
91
(Arg_String : String) return String_List_Access
93
Max_Args : Integer := 128;
94
New_Argv : String_List_Access := new String_List (1 .. Max_Args);
95
New_Argc : Natural := 0;
98
procedure Unchecked_Free is new Ada.Unchecked_Deallocation
99
(String_List, String_List_Access);
100
Backslashed : Boolean;
102
Triple_Quoted : Boolean;
103
Has_Triple : Boolean;
105
Start_With_Triple : Boolean;
106
End_With_Triple : Boolean;
109
Idx := Arg_String'First;
112
exit when Idx > Arg_String'Last;
114
Backslashed := False;
116
Triple_Quoted := False;
118
Start_With_Triple := False;
119
End_With_Triple := False;
121
while Idx <= Arg_String'Last
122
and then (Backslashed
124
or else Triple_Quoted
125
or else Arg_String (Idx) /= ' ')
127
End_With_Triple := False;
130
Backslashed := False;
133
case Arg_String (Idx) is
141
Has_Triple := Idx + 2 <= Arg_String'Last
142
and then Arg_String (Idx) = '"'
143
and then Arg_String (Idx + 1) = '"'
144
and then Arg_String (Idx + 2) = '"';
146
Triple_Quoted := not Triple_Quoted;
147
if Idx = Start_Idx then
148
Start_With_Triple := Triple_Quoted;
150
End_With_Triple := True;
165
New_Argc := New_Argc + 1;
167
-- Resize the table if needed
168
if New_Argc > Max_Args then
170
New_New_Argv : String_List (1 .. Max_Args * 2);
172
New_New_Argv (1 .. Max_Args) := New_Argv.all;
173
Unchecked_Free (New_Argv);
174
New_Argv := new String_List'(New_New_Argv);
177
Max_Args := Max_Args * 2;
180
if Start_With_Triple and End_With_Triple then
181
New_Argv (New_Argc) :=
182
new String'(Arg_String (Start_Idx + 3 .. Idx - 4));
184
New_Argv (New_Argc) :=
185
new String'(Arg_String (Start_Idx .. Idx - 1));
188
-- Skip extraneous spaces
190
while Idx <= Arg_String'Last and then Arg_String (Idx) = ' ' loop
196
Result : constant String_List := New_Argv (1 .. New_Argc);
198
Unchecked_Free (New_Argv);
199
return new String_List'(Result);
201
end Argument_String_To_List_With_Triple_Quotes;
207
function Unprotect (Str : String) return String is
208
Result : String (Str'Range);
209
Index : Natural := Result'First;
210
S : Natural := Str'First;
212
while S <= Str'Last loop
213
if Str (S) = '\' then
215
Result (Index) := Str (S + 1);
220
Result (Index) := Str (S);
227
return Result (Result'First .. Index - 1);
234
function Read_File (File : String) return String_Access is
236
FD : File_Descriptor := Invalid_FD;
237
Buffer : GNAT.Strings.String_Access;
239
Dummy_Length : Integer;
240
pragma Unreferenced (Dummy_Length);
243
FD := Open_Read (File, Fmode => Binary);
245
if FD = Invalid_FD then
249
Length := Integer (File_Length (FD));
250
Buffer := new String (1 .. Length);
251
Dummy_Length := Read (FD, Buffer.all'Address, Length);