rejuvenation_22.0.1_a6990d10/src/rejuvenation-file_utils.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
with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

package body Rejuvenation.File_Utils is

   procedure Write_String_To_File (Contents : String; File_Name : String) is
      F : Ada.Streams.Stream_IO.File_Type;
   begin
      Create_Path (Containing_Directory (File_Name));
      Create (F, Out_File, File_Name);
      String'Write (Stream (F), Contents);
      Close (F);
   end Write_String_To_File;

   function Get_String_From_File (File_Name : String) return String is
      F            : Ada.Streams.Stream_IO.File_Type;
      Return_Value : Unbounded_String;
   begin
      Open (F, In_File, File_Name);
      while not End_Of_File (F) loop
         declare
            C : Character;
         begin
            Character'Read (Stream (F), C);
            Append (Return_Value, C);
         end;
      end loop;
      Close (F);
      return To_String (Return_Value);
   end Get_String_From_File;

   procedure Walk_Files
     (Directory_Name : String; File_Pattern : String;
      Process_File   : not null access procedure (Item : Directory_Entry_Type);
      Recursive      : Boolean := True)
   is

      procedure Process_Directory (Item : Directory_Entry_Type);
      procedure Process_Directory (Item : Directory_Entry_Type) is
      begin
         if Simple_Name (Item) not in "." | ".." then
            Walk_Files (Full_Name (Item), File_Pattern, Process_File);
         end if;
      exception
         when Ada.Directories.Name_Error =>
            null;
      end Process_Directory;

   begin
      Search (Directory_Name, File_Pattern, (others => True), Process_File);
      Search
        (Directory_Name, "", (Directory => Recursive, others => False),
         Process_Directory'Access);
   end Walk_Files;

end Rejuvenation.File_Utils;