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: read.adb,v 1.8 2004/05/31 10:53:40 vagul Exp $
kusano fc6ab3
kusano fc6ab3
--  Test/demo program for the generic read interface.
kusano fc6ab3
kusano fc6ab3
with Ada.Numerics.Discrete_Random;
kusano fc6ab3
with Ada.Streams;
kusano fc6ab3
with Ada.Text_IO;
kusano fc6ab3
kusano fc6ab3
with ZLib;
kusano fc6ab3
kusano fc6ab3
procedure Read is
kusano fc6ab3
kusano fc6ab3
   use Ada.Streams;
kusano fc6ab3
kusano fc6ab3
   ------------------------------------
kusano fc6ab3
   --  Test configuration parameters --
kusano fc6ab3
   ------------------------------------
kusano fc6ab3
kusano fc6ab3
   File_Size   : Stream_Element_Offset := 100_000;
kusano fc6ab3
kusano fc6ab3
   Continuous  : constant Boolean          := False;
kusano fc6ab3
   --  If this constant is True, the test would be repeated again and again,
kusano fc6ab3
   --  with increment File_Size for every iteration.
kusano fc6ab3
kusano fc6ab3
   Header      : constant ZLib.Header_Type := ZLib.Default;
kusano fc6ab3
   --  Do not use Header other than Default in ZLib versions 1.1.4 and older.
kusano fc6ab3
kusano fc6ab3
   Init_Random : constant := 8;
kusano fc6ab3
   --  We are using the same random sequence, in case of we catch bug,
kusano fc6ab3
   --  so we would be able to reproduce it.
kusano fc6ab3
kusano fc6ab3
   -- End --
kusano fc6ab3
kusano fc6ab3
   Pack_Size : Stream_Element_Offset;
kusano fc6ab3
   Offset    : Stream_Element_Offset;
kusano fc6ab3
kusano fc6ab3
   Filter     : ZLib.Filter_Type;
kusano fc6ab3
kusano fc6ab3
   subtype Visible_Symbols
kusano fc6ab3
      is Stream_Element range 16#20# .. 16#7E#;
kusano fc6ab3
kusano fc6ab3
   package Random_Elements is new
kusano fc6ab3
      Ada.Numerics.Discrete_Random (Visible_Symbols);
kusano fc6ab3
kusano fc6ab3
   Gen : Random_Elements.Generator;
kusano fc6ab3
   Period  : constant Stream_Element_Offset := 200;
kusano fc6ab3
   --  Period constant variable for random generator not to be very random.
kusano fc6ab3
   --  Bigger period, harder random.
kusano fc6ab3
kusano fc6ab3
   Read_Buffer : Stream_Element_Array (1 .. 2048);
kusano fc6ab3
   Read_First  : Stream_Element_Offset;
kusano fc6ab3
   Read_Last   : Stream_Element_Offset;
kusano fc6ab3
kusano fc6ab3
   procedure Reset;
kusano fc6ab3
kusano fc6ab3
   procedure Read
kusano fc6ab3
     (Item : out Stream_Element_Array;
kusano fc6ab3
      Last : out Stream_Element_Offset);
kusano fc6ab3
   --  this procedure is for generic instantiation of
kusano fc6ab3
   --  ZLib.Read
kusano fc6ab3
   --  reading data from the File_In.
kusano fc6ab3
kusano fc6ab3
   procedure Read is new ZLib.Read
kusano fc6ab3
                           (Read,
kusano fc6ab3
                            Read_Buffer,
kusano fc6ab3
                            Rest_First => Read_First,
kusano fc6ab3
                            Rest_Last  => Read_Last);
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
      Last := Stream_Element_Offset'Min
kusano fc6ab3
               (Item'Last,
kusano fc6ab3
                Item'First + File_Size - Offset);
kusano fc6ab3
kusano fc6ab3
      for J in Item'First .. Last loop
kusano fc6ab3
         if J < Item'First + Period then
kusano fc6ab3
            Item (J) := Random_Elements.Random (Gen);
kusano fc6ab3
         else
kusano fc6ab3
            Item (J) := Item (J - Period);
kusano fc6ab3
         end if;
kusano fc6ab3
kusano fc6ab3
         Offset   := Offset + 1;
kusano fc6ab3
      end loop;
kusano fc6ab3
   end Read;
kusano fc6ab3
kusano fc6ab3
   -----------
kusano fc6ab3
   -- Reset --
kusano fc6ab3
   -----------
kusano fc6ab3
kusano fc6ab3
   procedure Reset is
kusano fc6ab3
   begin
kusano fc6ab3
      Random_Elements.Reset (Gen, Init_Random);
kusano fc6ab3
      Pack_Size := 0;
kusano fc6ab3
      Offset := 1;
kusano fc6ab3
      Read_First := Read_Buffer'Last + 1;
kusano fc6ab3
      Read_Last  := Read_Buffer'Last;
kusano fc6ab3
   end Reset;
kusano fc6ab3
kusano fc6ab3
begin
kusano fc6ab3
   Ada.Text_IO.Put_Line ("ZLib " & ZLib.Version);
kusano fc6ab3
kusano fc6ab3
   loop
kusano fc6ab3
      for Level in ZLib.Compression_Level'Range loop
kusano fc6ab3
kusano fc6ab3
         Ada.Text_IO.Put ("Level ="
kusano fc6ab3
            & ZLib.Compression_Level'Image (Level));
kusano fc6ab3
kusano fc6ab3
         --  Deflate using generic instantiation.
kusano fc6ab3
kusano fc6ab3
         ZLib.Deflate_Init
kusano fc6ab3
               (Filter,
kusano fc6ab3
                Level,
kusano fc6ab3
                Header => Header);
kusano fc6ab3
kusano fc6ab3
         Reset;
kusano fc6ab3
kusano fc6ab3
         Ada.Text_IO.Put
kusano fc6ab3
           (Stream_Element_Offset'Image (File_Size) & " ->");
kusano fc6ab3
kusano fc6ab3
         loop
kusano fc6ab3
            declare
kusano fc6ab3
               Buffer : Stream_Element_Array (1 .. 1024);
kusano fc6ab3
               Last   : Stream_Element_Offset;
kusano fc6ab3
            begin
kusano fc6ab3
               Read (Filter, Buffer, Last);
kusano fc6ab3
kusano fc6ab3
               Pack_Size := Pack_Size + Last - Buffer'First + 1;
kusano fc6ab3
kusano fc6ab3
               exit when Last < Buffer'Last;
kusano fc6ab3
            end;
kusano fc6ab3
         end loop;
kusano fc6ab3
kusano fc6ab3
         Ada.Text_IO.Put_Line (Stream_Element_Offset'Image (Pack_Size));
kusano fc6ab3
kusano fc6ab3
         ZLib.Close (Filter);
kusano fc6ab3
      end loop;
kusano fc6ab3
kusano fc6ab3
      exit when not Continuous;
kusano fc6ab3
kusano fc6ab3
      File_Size := File_Size + 1;
kusano fc6ab3
   end loop;
kusano fc6ab3
end Read;