with Io;
with Directory_Tools;
with Low_Level_File_Operations;
package body Low_Level_File_Utilities is

    procedure Append (This_File : in String;
                      To_This_File : in String;
                      Add_Page_Break : in Boolean := False) is
        --
        Source : Io.File_Type;
        Target : Io.File_Type;
        --
        The_Target : Directory_Tools.Object.Handle :=
           Directory_Tools.Naming.Resolution (To_This_File);
        --
        Target_Is_Nonexistent : Boolean := False;
        --
    begin  
        if (Directory_Tools.Object.Is_Bad (The_Target)) then
            Target_Is_Nonexistent := True;
        end if;
        Low_Level_File_Operations.Open_To_Append (To_This_File, Target);
        Low_Level_File_Operations.Open_To_Read (This_File, Source);
        -- If the target already has some contents, then add a page
        -- break, otherwise don't, since this is the first entry in
        -- the target file.
        if ((Add_Page_Break) and then (not Target_Is_Nonexistent)) then
            Io.New_Page (Target);
        end if;
        while (not Io.End_Of_File (Source)) loop
            Io.Put_Line (Target, Io.Get_Line (Source));
        end loop;
        Low_Level_File_Operations.Close (Target);
        Low_Level_File_Operations.Close (Source);
        --
    exception
        when others =>
            Low_Level_File_Operations.Close (Target);
            Low_Level_File_Operations.Close (Source);
            raise Io_Failure;
            --
    end Append;

    function Flattened_Text
                (This_File : in Io.File_Type; Current_Text : in String)
                return String is
        --
        The_File : Io.File_Type := This_File;
        --
    begin
        if (Io.End_Of_File (The_File)) then
            return (Current_Text);
        else
            return (Flattened_Text (The_File, Current_Text & Ascii.Lf &
                                                 Io.Get_Line (The_File)));
        end if;
    end Flattened_Text;

    function Flattened_Text (This_File : in String) return String is
        --
        The_File : Io.File_Type;
        --
    begin  
        Low_Level_File_Operations.Open_To_Read (This_File, The_File);
        declare
            The_Text : constant String := Flattened_Text (The_File, "");
        begin
            Low_Level_File_Operations.Close (The_File);
            return (The_Text);
        end;
        --
    exception
        when others =>
            Low_Level_File_Operations.Close (The_File);
            raise Io_Failure;
            --
    end Flattened_Text;

end Low_Level_File_Utilities;