Source file : rw_file.adb
1 -- NB: implementation for test purposes, very slow !!
2
3 with Ada.Sequential_IO;
4
5 package body RW_File is
6
7 package Character_Sequential_IO is new Ada.Sequential_IO (Character);
8
9 procedure Read_File (Filename : String; Content : out Unbounded_String) is
10 package Io renames Character_Sequential_IO;
11 use Io;
12 F : Io.File_Type;
13 Ch : Character;
14 begin
15 Content := Null_Unbounded_String;
16 Open (F, In_File, Filename);
17 while not End_Of_File (F) loop
18 Read (F, Ch);
19 Append (Content, Ch);
20 end loop;
21 Close (F);
22 exception when others =>
23 if Is_Open (F) then
24 Close (F);
25 end if;
26 raise;
27 end Read_File;
28
29 procedure Write_File (Filename : String;
30 Content : Unbounded_String) is
31 package Io renames Character_Sequential_IO;
32 use Io;
33 F : Io.File_Type;
34 begin
35 Create (F, Out_File, Filename);
36 for I in 1 .. Length (Content) loop
37 Io.Write (F, Element (Content, I));
38 end loop;
39 Close (F);
40 exception when others =>
41 if Is_Open (F) then
42 Close (F);
43 end if;
44 raise;
45 end Write_File;
46
47 procedure Process_Lines (S : Unbounded_String;
48 Process_Line : Line_processing) is
49
50 function Is_Text (Ch : Character) return Boolean is
51 begin
52 return Ch >= ' ' or else Ch = ASCII.HT;
53 end Is_Text;
54
55 Last : constant Integer := Length (S);
56 From : Integer := 1;
57 Upto1, Upto2 : Integer;
58 begin
59 while From < Last loop
60 Upto1 := From;
61 while Upto1 < Last and then
62 Is_Text (Ada.Strings.Unbounded.Element (S, Upto1 + 1)) loop
63 Upto1 := Upto1 + 1;
64 end loop;
65 Upto2 := Upto1;
66 while Upto2 < Last and then
67 not Is_Text (Ada.Strings.Unbounded.Element (S, Upto2 + 1))
68 loop
69 Upto2 := Upto2 + 1;
70 end loop;
71 Process_Line (Slice (S, From, Upto1));
72 From := Upto2 + 1;
73 end loop;
74 end Process_Lines;
75
76 end RW_File;
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.