getifaddrs: add un-namespaced alias
[minix.git] / common / dist / zlib / contrib / ada / zlib-streams.adb
blob877440339e7c26e38d0a1feb03ada454e8fd5df3
1 ----------------------------------------------------------------
2 -- ZLib for Ada thick binding. --
3 -- --
4 -- Copyright (C) 2002-2003 Dmitriy Anisimkov --
5 -- --
6 -- Open source license information is in the zlib.ads file. --
7 ----------------------------------------------------------------
9 -- Id: zlib-streams.adb,v 1.10 2004/05/31 10:53:40 vagul Exp
11 with Ada.Unchecked_Deallocation;
13 package body ZLib.Streams is
15 -----------
16 -- Close --
17 -----------
19 procedure Close (Stream : in out Stream_Type) is
20 procedure Free is new Ada.Unchecked_Deallocation
21 (Stream_Element_Array, Buffer_Access);
22 begin
23 if Stream.Mode = Out_Stream or Stream.Mode = Duplex then
24 -- We should flush the data written by the writer.
26 Flush (Stream, Finish);
28 Close (Stream.Writer);
29 end if;
31 if Stream.Mode = In_Stream or Stream.Mode = Duplex then
32 Close (Stream.Reader);
33 Free (Stream.Buffer);
34 end if;
35 end Close;
37 ------------
38 -- Create --
39 ------------
41 procedure Create
42 (Stream : out Stream_Type;
43 Mode : in Stream_Mode;
44 Back : in Stream_Access;
45 Back_Compressed : in Boolean;
46 Level : in Compression_Level := Default_Compression;
47 Strategy : in Strategy_Type := Default_Strategy;
48 Header : in Header_Type := Default;
49 Read_Buffer_Size : in Ada.Streams.Stream_Element_Offset
50 := Default_Buffer_Size;
51 Write_Buffer_Size : in Ada.Streams.Stream_Element_Offset
52 := Default_Buffer_Size)
55 subtype Buffer_Subtype is Stream_Element_Array (1 .. Read_Buffer_Size);
57 procedure Init_Filter
58 (Filter : in out Filter_Type;
59 Compress : in Boolean);
61 -----------------
62 -- Init_Filter --
63 -----------------
65 procedure Init_Filter
66 (Filter : in out Filter_Type;
67 Compress : in Boolean) is
68 begin
69 if Compress then
70 Deflate_Init
71 (Filter, Level, Strategy, Header => Header);
72 else
73 Inflate_Init (Filter, Header => Header);
74 end if;
75 end Init_Filter;
77 begin
78 Stream.Back := Back;
79 Stream.Mode := Mode;
81 if Mode = Out_Stream or Mode = Duplex then
82 Init_Filter (Stream.Writer, Back_Compressed);
83 Stream.Buffer_Size := Write_Buffer_Size;
84 else
85 Stream.Buffer_Size := 0;
86 end if;
88 if Mode = In_Stream or Mode = Duplex then
89 Init_Filter (Stream.Reader, not Back_Compressed);
91 Stream.Buffer := new Buffer_Subtype;
92 Stream.Rest_First := Stream.Buffer'Last + 1;
93 Stream.Rest_Last := Stream.Buffer'Last;
94 end if;
95 end Create;
97 -----------
98 -- Flush --
99 -----------
101 procedure Flush
102 (Stream : in out Stream_Type;
103 Mode : in Flush_Mode := Sync_Flush)
105 Buffer : Stream_Element_Array (1 .. Stream.Buffer_Size);
106 Last : Stream_Element_Offset;
107 begin
108 loop
109 Flush (Stream.Writer, Buffer, Last, Mode);
111 Ada.Streams.Write (Stream.Back.all, Buffer (1 .. Last));
113 exit when Last < Buffer'Last;
114 end loop;
115 end Flush;
117 -------------
118 -- Is_Open --
119 -------------
121 function Is_Open (Stream : Stream_Type) return Boolean is
122 begin
123 return Is_Open (Stream.Reader) or else Is_Open (Stream.Writer);
124 end Is_Open;
126 ----------
127 -- Read --
128 ----------
130 procedure Read
131 (Stream : in out Stream_Type;
132 Item : out Stream_Element_Array;
133 Last : out Stream_Element_Offset)
136 procedure Read
137 (Item : out Stream_Element_Array;
138 Last : out Stream_Element_Offset);
140 ----------
141 -- Read --
142 ----------
144 procedure Read
145 (Item : out Stream_Element_Array;
146 Last : out Stream_Element_Offset) is
147 begin
148 Ada.Streams.Read (Stream.Back.all, Item, Last);
149 end Read;
151 procedure Read is new ZLib.Read
152 (Read => Read,
153 Buffer => Stream.Buffer.all,
154 Rest_First => Stream.Rest_First,
155 Rest_Last => Stream.Rest_Last);
157 begin
158 Read (Stream.Reader, Item, Last);
159 end Read;
161 -------------------
162 -- Read_Total_In --
163 -------------------
165 function Read_Total_In (Stream : in Stream_Type) return Count is
166 begin
167 return Total_In (Stream.Reader);
168 end Read_Total_In;
170 --------------------
171 -- Read_Total_Out --
172 --------------------
174 function Read_Total_Out (Stream : in Stream_Type) return Count is
175 begin
176 return Total_Out (Stream.Reader);
177 end Read_Total_Out;
179 -----------
180 -- Write --
181 -----------
183 procedure Write
184 (Stream : in out Stream_Type;
185 Item : in Stream_Element_Array)
188 procedure Write (Item : in Stream_Element_Array);
190 -----------
191 -- Write --
192 -----------
194 procedure Write (Item : in Stream_Element_Array) is
195 begin
196 Ada.Streams.Write (Stream.Back.all, Item);
197 end Write;
199 procedure Write is new ZLib.Write
200 (Write => Write,
201 Buffer_Size => Stream.Buffer_Size);
203 begin
204 Write (Stream.Writer, Item, No_Flush);
205 end Write;
207 --------------------
208 -- Write_Total_In --
209 --------------------
211 function Write_Total_In (Stream : in Stream_Type) return Count is
212 begin
213 return Total_In (Stream.Writer);
214 end Write_Total_In;
216 ---------------------
217 -- Write_Total_Out --
218 ---------------------
220 function Write_Total_Out (Stream : in Stream_Type) return Count is
221 begin
222 return Total_Out (Stream.Writer);
223 end Write_Total_Out;
225 end ZLib.Streams;