1-- Usage: several_sizes <big_file>
2-- Produces <size>.tmp files that are partial copies of <big_file>, with different sizes.
3-- These files can be compressed for testing.
4
5with Ada.Text_IO, Ada.Integer_Text_IO;
6with Ada.Streams.Stream_IO;             use Ada.Streams.Stream_IO;
7with Ada.Command_Line;                  use Ada.Command_Line;
8with Interfaces;                        use Interfaces;
9
10procedure Several_sizes is
11
12  f_in, f_out: Ada.Streams.Stream_IO.File_Type;
13
14  type Buffer is array(Natural range <>) of Unsigned_8;
15
16  procedure Read( b: out Buffer; last_read: out Natural ) is
17    use Ada.Streams;
18    Last: Stream_Element_Offset;
19    SE_Buffer : Stream_Element_Array (1..b'Length);
20    for SE_Buffer'Address use b'Address;
21    pragma Import (Ada, SE_Buffer);
22  begin
23    Read(Stream(f_in).all, SE_Buffer, Last);
24    last_read:= b'First + Natural(Last) - 1;
25  end Read;
26
27  procedure Write( b: in Buffer ) is
28    use Ada.Streams;
29    SE_Buffer : Stream_Element_Array (1..b'Length);
30    for SE_Buffer'Address use b'Address;
31    pragma Import (Ada, SE_Buffer);
32  begin
33    Write(Stream(f_out).all, SE_Buffer);
34  end Write;
35
36  ----------
37  -- Test --
38  ----------
39
40  function name return String is
41  begin
42    return Argument(1);
43  end name;
44
45  procedure Test(limit: Natural) is
46    b: Buffer(1..1024);
47    l, rest: Natural;
48    s: String(1..15);
49  begin
50    Ada.Integer_Text_IO.Put(s,limit+1_000_000_000);
51    rest:= limit;
52    Open(f_in, In_File, name);
53    Create(f_out, Out_File, s(7..15) & ".tmp");
54    while not End_Of_File(f_in) loop
55      Read(b,l);
56      if rest < l then
57        Write(b(1..rest));
58        exit;
59      end if;
60      Write(b(1..l));
61      rest:= rest - l;
62    end loop;
63    Close(f_out);
64    Close(f_in);
65  end Test;
66
67  use Ada.Text_IO;
68
69  s: Positive;
70
71begin
72  if Argument_Count=0 then
73    Put_Line(" Usage: several_sizes <big_file>");
74    Put_Line(" Produces <size>.tmp files that are partial copies of <big_file>, with different sizes.");
75    return;
76  end if;
77  --  Tiny files
78  for i in 0..126 loop
79    Test(i);
80  end loop;
81  --  Around powers of two (typical LZ sliding window sizes)
82  s:= 128;
83  loop
84    for i in -60..60 loop
85      Test(s+i);
86    end loop;
87    s:= s * 2;
88    exit when s > 300_000;
89  end loop;
90end Several_sizes;
91