zipada_56.0.2_b3043499/test/rw_file.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
--  NB: implementation for test purposes, very slow !!

with Ada.Sequential_IO;

package body RW_File is

   package Character_Sequential_IO is new Ada.Sequential_IO (Character);

   procedure Read_File (Filename : String; Content : out Unbounded_String) is
      package Io renames Character_Sequential_IO;
      use Io;
      F : Io.File_Type;
      Ch : Character;
   begin
      Content := Null_Unbounded_String;
      Open (F, In_File, Filename);
      while not End_Of_File (F) loop
         Read (F, Ch);
         Append (Content, Ch);
      end loop;
      Close (F);
   exception when others =>
         if Is_Open (F) then
            Close (F);
         end if;
         raise;
   end Read_File;

   procedure Write_File (Filename : String;
                         Content : Unbounded_String) is
      package Io renames Character_Sequential_IO;
      use Io;
      F : Io.File_Type;
   begin
      Create (F, Out_File, Filename);
      for I in 1 .. Length (Content) loop
         Io.Write (F, Element (Content, I));
      end loop;
      Close (F);
   exception when others =>
         if Is_Open (F) then
            Close (F);
         end if;
         raise;
   end Write_File;

   procedure Process_Lines (S : Unbounded_String;
                            Process_Line : Line_processing) is

      function Is_Text (Ch : Character) return Boolean is
      begin
         return Ch >= ' ' or else Ch = ASCII.HT;
      end Is_Text;

      Last : constant Integer := Length (S);
      From : Integer := 1;
      Upto1, Upto2 : Integer;
   begin
      while From < Last loop
         Upto1 := From;
         while Upto1 < Last and then
               Is_Text (Ada.Strings.Unbounded.Element (S, Upto1 + 1)) loop
            Upto1 := Upto1 + 1;
         end loop;
         Upto2 := Upto1;
         while Upto2 < Last and then
               not Is_Text (Ada.Strings.Unbounded.Element (S, Upto2 + 1))
         loop
            Upto2 := Upto2 + 1;
         end loop;
         Process_Line (Slice (S, From, Upto1));
         From := Upto2 + 1;
      end loop;
   end Process_Lines;

end RW_File;