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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234 | -- SPDX-License-Identifier: MIT
--
-- Copyright (c) 2008 - 2018 Gautier de Montmollin (maintainer)
-- SWITZERLAND
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
with Ada.IO_Exceptions;
package body DCF.Streams is
function Open (File_Name : String) return Open_File is
begin
return Result : Open_File do
Stream_IO.Open (Result.File, Stream_IO.File_Mode (In_File), File_Name);
end return;
end Open;
function Create (File_Name : String) return Open_File is
begin
return Result : Open_File do
Stream_IO.Create (Result.File, Stream_IO.File_Mode (Out_File), File_Name);
end return;
end Create;
overriding
procedure Finalize (Object : in out Open_File) is
begin
if not Object.Finalized then
if Stream_IO.Is_Open (Object.File) then
Stream_IO.Close (Object.File);
end if;
Object.Finalized := True;
end if;
end Finalize;
-----------------------------------------------------------------------------
procedure Set_Name (S : in out Root_Zipstream_Type; Name : String; UTF_8 : Boolean := False) is
begin
S.Name := To_Unbounded_String (Name);
S.UTF_8 := UTF_8;
end Set_Name;
function Get_Name (S : in Root_Zipstream_Type) return String is
begin
return To_String (S.Name);
end Get_Name;
function UTF_8_Encoding (S : in Root_Zipstream_Type) return Boolean is (S.UTF_8);
procedure Set_Time (S : in out Root_Zipstream_Type; Modification_Time : Time) is
begin
S.Modification_Time := Modification_Time;
end Set_Time;
function Get_Time (S : in Root_Zipstream_Type) return Time is
begin
return S.Modification_Time;
end Get_Time;
function Convert (Date : in Dos_Time) return Time is
begin
return Time (Date); -- Currently a trivial conversion
end Convert;
function Convert (Date : in Time) return Dos_Time is
begin
return Dos_Time (Date); -- Currently a trivial conversion
end Convert;
----------------------------------------------
-- File_Zipstream: stream based on a file --
----------------------------------------------
function Open (File_Name : String) return File_Zipstream is
begin
return (Root_Stream_Type with
File => Open (File_Name),
Name => To_Unbounded_String (File_Name),
others => <>);
end Open;
function Create (File_Name : String) return File_Zipstream is
begin
return (Root_Stream_Type with
File => Create (File_Name),
Name => To_Unbounded_String (File_Name),
others => <>);
end Create;
overriding
procedure Read
(Stream : in out File_Zipstream;
Item : out Stream_Element_Array;
Last : out Stream_Element_Offset)
is
begin
Ada.Streams.Stream_IO.Read (Stream.File.File, Item, Last);
end Read;
overriding
procedure Write (Stream : in out File_Zipstream; Item : Stream_Element_Array) is
begin
Ada.Streams.Stream_IO.Write (Stream.File.File, Item);
end Write;
overriding
procedure Set_Index (S : in out File_Zipstream; To : Zs_Index_Type) is
begin
Ada.Streams.Stream_IO.Set_Index (S.File.File, Ada.Streams.Stream_IO.Positive_Count (To));
end Set_Index;
overriding
function Size (S : in File_Zipstream) return Zs_Size_Type is
begin
return Zs_Size_Type (Ada.Streams.Stream_IO.Size (S.File.File));
end Size;
overriding
function Index (S : in File_Zipstream) return Zs_Index_Type is
begin
return Zs_Index_Type (Ada.Streams.Stream_IO.Index (S.File.File));
end Index;
overriding
function End_Of_Stream (S : in File_Zipstream) return Boolean is
begin
return Ada.Streams.Stream_IO.End_Of_File (S.File.File);
end End_Of_Stream;
--------------------------------------------------------------
-- Array_Zipstream: stream based on a Stream_Element_Array --
--------------------------------------------------------------
overriding
procedure Read
(Stream : in out Array_Zipstream;
Item : out Stream_Element_Array;
Last : out Stream_Element_Offset) is
begin
if Stream.EOF then
-- No elements transferred: Last := Item'First - 1 (See RM 13.13.1 (8))
-- T'Read will raise End_Error (See RM 13.13.2 (37))
if Item'First > Stream_Element_Offset'First then
Last := Item'First - 1;
return;
else
-- RM 13.13.1 (11) requires Read to raise Constraint_Error
-- if Item'First = Stream_Element_Offset'First
raise Constraint_Error;
end if;
end if;
if Stream.Elements'Last - Stream.Index >= Item'Length then
-- Normal case: even after reading, the index will be in the range
Last := Item'Last;
Item := Stream.Elements (Stream.Index .. Stream.Index + Item'Length - 1);
Stream.Index := Stream.Index + Item'Length;
pragma Assert (Stream.Index <= Stream.Elements'Last);
-- At least one element is left to be read, EOF not possible
else
-- Special case: we exhaust the buffer
Last := Item'First + (Stream.Elements'Last - Stream.Index);
Item (Item'First .. Last) := Stream.Elements (Stream.Index .. Stream.Elements'Last);
Stream.EOF := True;
-- If Last < Item'Last, the T'Read attribute raises End_Error
-- because of the incomplete reading
end if;
end Read;
overriding
procedure Write
(Stream : in out Array_Zipstream;
Item : Stream_Element_Array) is
begin
if Stream.EOF then
raise Ada.IO_Exceptions.End_Error;
end if;
if Stream.Elements'Last - Stream.Index >= Item'Length then
Stream.Elements (Stream.Index .. Stream.Index + Item'Length - 1) := Item;
Stream.Index := Stream.Index + Item'Length;
pragma Assert (Stream.Index <= Stream.Elements'Last);
-- At least one element is left to be written, EOF not possible
elsif Stream.Elements'Last - Stream.Index + 1 = Item'Length then
Stream.Elements (Stream.Index .. Stream.Elements'Last) := Item;
Stream.EOF := True;
else
-- RM 13.13.1 (9) requires the item to be appended to the stream,
-- but this might fail because the Stream_Element_Array is bounded.
--
-- Don't write if writing would be incomplete
raise Ada.IO_Exceptions.End_Error;
end if;
end Write;
overriding
procedure Set_Index (Stream : in out Array_Zipstream; To : Zs_Index_Type) is
Index : constant Stream_Element_Offset := Stream_Element_Offset (To);
begin
if (if Stream.Elements'Length > 0 then
Index not in Stream.Elements'Range
else
Index /= Stream.Elements'First)
then
raise Constraint_Error;
end if;
Stream.Index := Index;
Stream.EOF := False;
end Set_Index;
end DCF.Streams;
|