Source file : bzip2_enc.adb
1 -- Standalone, command-line, BZip2 encoder (for .bzip2 files).
2
3 with BZip2.Encoding;
4
5 with Ada.Command_Line,
6 Ada.Text_IO,
7 Ada.Streams.Stream_IO;
8
9 procedure BZip2_Enc is
10
11 use BZip2, BZip2.Encoding;
12
13 level : Positive := 4;
14
15 procedure Encode_BZip2_Stream (s_in, s_out : Ada.Streams.Stream_IO.Stream_Access) is
16 EOS : Boolean := False;
17 mem_b : Byte := Character'Pos ('X'); -- delayed by 1 byte to catch the EOS (End-Of-Stream)
18
19 -- NB: The Byte I/O below is not buffered, so it is very slow.
20 -- You need to implement a circular buffer of type Stream_Element_Array for a fast I/O.
21 -- For instance, see the BlockRead in the Zip package for how to do it.
22
23 function Read_Byte return Byte is -- One dummy call to Read_byte is needed before compression
24 prev_b : Byte;
25 begin
26 prev_b := mem_b;
27 Byte'Read (s_in, mem_b);
28 return prev_b;
29 exception
30 when Ada.Streams.Stream_IO.End_Error =>
31 EOS := True;
32 return prev_b;
33 end Read_Byte;
34
35 function More_Bytes return Boolean is
36 begin
37 return not EOS;
38 end More_Bytes;
39
40 procedure Put_Byte (b : Byte) is
41 begin
42 Byte'Write (s_out, b);
43 end Put_Byte;
44
45 procedure BZip2_Encode is new BZip2.Encoding.Encode (Read_Byte, More_Bytes, Put_Byte);
46
47 dummy : Byte := Read_Byte; -- Consume the initial 'X'
48
49 begin
50 -- Whole processing is done here:
51 BZip2_Encode
52 (case level is
53 when 1 => block_100k,
54 when 2 => block_400k,
55 when others => block_900k);
56 end Encode_BZip2_Stream;
57
58 use Ada.Streams.Stream_IO;
59
60 f_in, f_out : File_Type;
61
62 use Ada.Text_IO;
63
64 procedure Print_Data_Bytes_Count (title : String; v : Data_Bytes_Count) is
65 package CIO is new Integer_IO (Data_Bytes_Count);
66 begin
67 Put (title);
68 Put (" : ");
69 CIO.Put (v, 0);
70 Put (" bytes");
71 New_Line;
72 end Print_Data_Bytes_Count;
73
74 use Ada.Command_Line;
75
76 begin
77 New_Line;
78 Put_Line ("BZip2_Enc: a standalone BZip2 encoder.");
79 if Argument_Count = 0 then
80 Put_Line ("Use: bzip2_enc infile outfile [options]");
81 New_Line;
82 Put_Line ("NB: - The "".bz2"" extension is automatically added to outfile.");
83 Put_Line (" - The I/O is not buffered => may be slow. Use the ZipAda tool for fast I/O.");
84 New_Line;
85 Put_Line ("Options: -n, n in 1 .. 3 : strength");
86 New_Line;
87 Put ("Press Return");
88 Skip_Line;
89 return;
90 elsif Argument_Count < 2 then
91 Put_Line ("You must specify at least two parameters");
92 return;
93 end if;
94 for i in 2 .. Argument_Count loop
95 declare
96 arg : constant String := Argument (i);
97 begin
98 if arg (arg'First) = '-' then
99 if i = 2 then
100 Put_Line ("Option needs to be 3rd or later parameter");
101 return;
102 end if;
103 if arg (arg'Last) in '1' .. '4' then
104 level := Character'Pos (arg (arg'Last)) - Character'Pos ('0');
105 end if;
106 end if;
107 end;
108 end loop;
109 Open (f_in, In_File, Argument (1));
110 Create (f_out, Out_File, Argument (2) & ".bz2");
111 Encode_BZip2_Stream (Stream (f_in), Stream (f_out));
112 New_Line;
113 Print_Data_Bytes_Count ("Read ", Data_Bytes_Count (Index (f_in) - 1));
114 Print_Data_Bytes_Count ("Written ", Data_Bytes_Count (Index (f_out) - 1));
115 Close (f_in);
116 Close (f_out);
117
118 end BZip2_Enc;
Web view of Ada source code generated by GNATHTML, project: ALI_Parse version 1.0.
Zip-Ada: Ada library for zip archive files (.zip).
Ada programming.
Some news about Zip-Ada and other Ada projects
on Gautier's blog.