kusano fc6ab3
----------------------------------------------------------------
kusano fc6ab3
--  ZLib for Ada thick binding.                               --
kusano fc6ab3
--                                                            --
kusano fc6ab3
--  Copyright (C) 2002-2003 Dmitriy Anisimkov                 --
kusano fc6ab3
--                                                            --
kusano fc6ab3
--  Open source license information is in the zlib.ads file.  --
kusano fc6ab3
----------------------------------------------------------------
kusano fc6ab3
kusano fc6ab3
--  $Id: zlib-streams.adb,v 1.10 2004/05/31 10:53:40 vagul Exp $
kusano fc6ab3
kusano fc6ab3
with Ada.Unchecked_Deallocation;
kusano fc6ab3
kusano fc6ab3
package body ZLib.Streams is
kusano fc6ab3
kusano fc6ab3
   -----------
kusano fc6ab3
   -- Close --
kusano fc6ab3
   -----------
kusano fc6ab3
kusano fc6ab3
   procedure Close (Stream : in out Stream_Type) is
kusano fc6ab3
      procedure Free is new Ada.Unchecked_Deallocation
kusano fc6ab3
         (Stream_Element_Array, Buffer_Access);
kusano fc6ab3
   begin
kusano fc6ab3
      if Stream.Mode = Out_Stream or Stream.Mode = Duplex then
kusano fc6ab3
         --  We should flush the data written by the writer.
kusano fc6ab3
kusano fc6ab3
         Flush (Stream, Finish);
kusano fc6ab3
kusano fc6ab3
         Close (Stream.Writer);
kusano fc6ab3
      end if;
kusano fc6ab3
kusano fc6ab3
      if Stream.Mode = In_Stream or Stream.Mode = Duplex then
kusano fc6ab3
         Close (Stream.Reader);
kusano fc6ab3
         Free (Stream.Buffer);
kusano fc6ab3
      end if;
kusano fc6ab3
   end Close;
kusano fc6ab3
kusano fc6ab3
   ------------
kusano fc6ab3
   -- Create --
kusano fc6ab3
   ------------
kusano fc6ab3
kusano fc6ab3
   procedure Create
kusano fc6ab3
     (Stream            :    out Stream_Type;
kusano fc6ab3
      Mode              : in     Stream_Mode;
kusano fc6ab3
      Back              : in     Stream_Access;
kusano fc6ab3
      Back_Compressed   : in     Boolean;
kusano fc6ab3
      Level             : in     Compression_Level := Default_Compression;
kusano fc6ab3
      Strategy          : in     Strategy_Type     := Default_Strategy;
kusano fc6ab3
      Header            : in     Header_Type       := Default;
kusano fc6ab3
      Read_Buffer_Size  : in     Ada.Streams.Stream_Element_Offset
kusano fc6ab3
                                    := Default_Buffer_Size;
kusano fc6ab3
      Write_Buffer_Size : in     Ada.Streams.Stream_Element_Offset
kusano fc6ab3
                                    := Default_Buffer_Size)
kusano fc6ab3
   is
kusano fc6ab3
kusano fc6ab3
      subtype Buffer_Subtype is Stream_Element_Array (1 .. Read_Buffer_Size);
kusano fc6ab3
kusano fc6ab3
      procedure Init_Filter
kusano fc6ab3
         (Filter   : in out Filter_Type;
kusano fc6ab3
          Compress : in     Boolean);
kusano fc6ab3
kusano fc6ab3
      -----------------
kusano fc6ab3
      -- Init_Filter --
kusano fc6ab3
      -----------------
kusano fc6ab3
kusano fc6ab3
      procedure Init_Filter
kusano fc6ab3
         (Filter   : in out Filter_Type;
kusano fc6ab3
          Compress : in     Boolean) is
kusano fc6ab3
      begin
kusano fc6ab3
         if Compress then
kusano fc6ab3
            Deflate_Init
kusano fc6ab3
              (Filter, Level, Strategy, Header => Header);
kusano fc6ab3
         else
kusano fc6ab3
            Inflate_Init (Filter, Header => Header);
kusano fc6ab3
         end if;
kusano fc6ab3
      end Init_Filter;
kusano fc6ab3
kusano fc6ab3
   begin
kusano fc6ab3
      Stream.Back := Back;
kusano fc6ab3
      Stream.Mode := Mode;
kusano fc6ab3
kusano fc6ab3
      if Mode = Out_Stream or Mode = Duplex then
kusano fc6ab3
         Init_Filter (Stream.Writer, Back_Compressed);
kusano fc6ab3
         Stream.Buffer_Size := Write_Buffer_Size;
kusano fc6ab3
      else
kusano fc6ab3
         Stream.Buffer_Size := 0;
kusano fc6ab3
      end if;
kusano fc6ab3
kusano fc6ab3
      if Mode = In_Stream or Mode = Duplex then
kusano fc6ab3
         Init_Filter (Stream.Reader, not Back_Compressed);
kusano fc6ab3
kusano fc6ab3
         Stream.Buffer     := new Buffer_Subtype;
kusano fc6ab3
         Stream.Rest_First := Stream.Buffer'Last + 1;
kusano fc6ab3
         Stream.Rest_Last  := Stream.Buffer'Last;
kusano fc6ab3
      end if;
kusano fc6ab3
   end Create;
kusano fc6ab3
kusano fc6ab3
   -----------
kusano fc6ab3
   -- Flush --
kusano fc6ab3
   -----------
kusano fc6ab3
kusano fc6ab3
   procedure Flush
kusano fc6ab3
     (Stream : in out Stream_Type;
kusano fc6ab3
      Mode   : in     Flush_Mode := Sync_Flush)
kusano fc6ab3
   is
kusano fc6ab3
      Buffer : Stream_Element_Array (1 .. Stream.Buffer_Size);
kusano fc6ab3
      Last   : Stream_Element_Offset;
kusano fc6ab3
   begin
kusano fc6ab3
      loop
kusano fc6ab3
         Flush (Stream.Writer, Buffer, Last, Mode);
kusano fc6ab3
kusano fc6ab3
         Ada.Streams.Write (Stream.Back.all, Buffer (1 .. Last));
kusano fc6ab3
kusano fc6ab3
         exit when Last < Buffer'Last;
kusano fc6ab3
      end loop;
kusano fc6ab3
   end Flush;
kusano fc6ab3
kusano fc6ab3
   -------------
kusano fc6ab3
   -- Is_Open --
kusano fc6ab3
   -------------
kusano fc6ab3
kusano fc6ab3
   function Is_Open (Stream : Stream_Type) return Boolean is
kusano fc6ab3
   begin
kusano fc6ab3
      return Is_Open (Stream.Reader) or else Is_Open (Stream.Writer);
kusano fc6ab3
   end Is_Open;
kusano fc6ab3
kusano fc6ab3
   ----------
kusano fc6ab3
   -- Read --
kusano fc6ab3
   ----------
kusano fc6ab3
kusano fc6ab3
   procedure Read
kusano fc6ab3
     (Stream : in out Stream_Type;
kusano fc6ab3
      Item   :    out Stream_Element_Array;
kusano fc6ab3
      Last   :    out Stream_Element_Offset)
kusano fc6ab3
   is
kusano fc6ab3
kusano fc6ab3
      procedure Read
kusano fc6ab3
        (Item : out Stream_Element_Array;
kusano fc6ab3
         Last : out Stream_Element_Offset);
kusano fc6ab3
kusano fc6ab3
      ----------
kusano fc6ab3
      -- Read --
kusano fc6ab3
      ----------
kusano fc6ab3
kusano fc6ab3
      procedure Read
kusano fc6ab3
        (Item : out Stream_Element_Array;
kusano fc6ab3
         Last : out Stream_Element_Offset) is
kusano fc6ab3
      begin
kusano fc6ab3
         Ada.Streams.Read (Stream.Back.all, Item, Last);
kusano fc6ab3
      end Read;
kusano fc6ab3
kusano fc6ab3
      procedure Read is new ZLib.Read
kusano fc6ab3
         (Read       => Read,
kusano fc6ab3
          Buffer     => Stream.Buffer.all,
kusano fc6ab3
          Rest_First => Stream.Rest_First,
kusano fc6ab3
          Rest_Last  => Stream.Rest_Last);
kusano fc6ab3
kusano fc6ab3
   begin
kusano fc6ab3
      Read (Stream.Reader, Item, Last);
kusano fc6ab3
   end Read;
kusano fc6ab3
kusano fc6ab3
   -------------------
kusano fc6ab3
   -- Read_Total_In --
kusano fc6ab3
   -------------------
kusano fc6ab3
kusano fc6ab3
   function Read_Total_In (Stream : in Stream_Type) return Count is
kusano fc6ab3
   begin
kusano fc6ab3
      return Total_In (Stream.Reader);
kusano fc6ab3
   end Read_Total_In;
kusano fc6ab3
kusano fc6ab3
   --------------------
kusano fc6ab3
   -- Read_Total_Out --
kusano fc6ab3
   --------------------
kusano fc6ab3
kusano fc6ab3
   function Read_Total_Out (Stream : in Stream_Type) return Count is
kusano fc6ab3
   begin
kusano fc6ab3
      return Total_Out (Stream.Reader);
kusano fc6ab3
   end Read_Total_Out;
kusano fc6ab3
kusano fc6ab3
   -----------
kusano fc6ab3
   -- Write --
kusano fc6ab3
   -----------
kusano fc6ab3
kusano fc6ab3
   procedure Write
kusano fc6ab3
     (Stream : in out Stream_Type;
kusano fc6ab3
      Item   : in     Stream_Element_Array)
kusano fc6ab3
   is
kusano fc6ab3
kusano fc6ab3
      procedure Write (Item : in Stream_Element_Array);
kusano fc6ab3
kusano fc6ab3
      -----------
kusano fc6ab3
      -- Write --
kusano fc6ab3
      -----------
kusano fc6ab3
kusano fc6ab3
      procedure Write (Item : in Stream_Element_Array) is
kusano fc6ab3
      begin
kusano fc6ab3
         Ada.Streams.Write (Stream.Back.all, Item);
kusano fc6ab3
      end Write;
kusano fc6ab3
kusano fc6ab3
      procedure Write is new ZLib.Write
kusano fc6ab3
         (Write       => Write,
kusano fc6ab3
          Buffer_Size => Stream.Buffer_Size);
kusano fc6ab3
kusano fc6ab3
   begin
kusano fc6ab3
      Write (Stream.Writer, Item, No_Flush);
kusano fc6ab3
   end Write;
kusano fc6ab3
kusano fc6ab3
   --------------------
kusano fc6ab3
   -- Write_Total_In --
kusano fc6ab3
   --------------------
kusano fc6ab3
kusano fc6ab3
   function Write_Total_In (Stream : in Stream_Type) return Count is
kusano fc6ab3
   begin
kusano fc6ab3
      return Total_In (Stream.Writer);
kusano fc6ab3
   end Write_Total_In;
kusano fc6ab3
kusano fc6ab3
   ---------------------
kusano fc6ab3
   -- Write_Total_Out --
kusano fc6ab3
   ---------------------
kusano fc6ab3
kusano fc6ab3
   function Write_Total_Out (Stream : in Stream_Type) return Count is
kusano fc6ab3
   begin
kusano fc6ab3
      return Total_Out (Stream.Writer);
kusano fc6ab3
   end Write_Total_Out;
kusano fc6ab3
kusano fc6ab3
end ZLib.Streams;