with Io;
with Directory_Tools;
package body Low_Level_File_Operations is

    procedure Create (This_File : in String) is
        --
        Test_File : Directory_Tools.Object.Handle :=
           Directory_Tools.Naming.Resolution (This_File);
        --
        New_File : Io.File_Type;
        --
    begin
        if (Directory_Tools.Object.Is_Bad (Test_File)) then
            Io.Create (New_File, Io.Out_File, This_File);
            Io.Close (New_File);
        end if;
    end Create;

    function Open_To_Read  
                (This_File : in String) return Io.File_Type is
        --
        The_File : Io.File_Type;
        --
    begin  
        Io.Open (The_File, Io.In_File, This_File);
        return (The_File);
        --
    exception
        when others =>
            -- Couldn't open the file for some reason.
            raise Io_Failure;
            --
    end Open_To_Read;

    procedure Open_To_Read  
                 (This_File : in String; The_File : out Io.File_Type) is
    begin  
        The_File := Open_To_Read (This_File);
        --
    exception
        when others =>
            -- Couldn't open the file for some reason.
            raise Io_Failure;
            --
    end Open_To_Read;

    function Open_To_Write  
                (This_File : in String) return Io.File_Type is
        --
        The_File : Io.File_Type;
        --
    begin
        -- Try to open the file.
        Io.Open (The_File, Io.Out_File, This_File);
        return (The_File);
        --
    exception
        when others =>  
            begin
                -- Opening the file didn't work, so try to create it.
                Io.Create (The_File, Io.Out_File, This_File);
                return (The_File);
                --
            exception
                when others =>
                    -- Nothing worked.
                    raise Io_Failure;
            end;
            --
    end Open_To_Write;

    procedure Open_To_Write  
                 (This_File : in String; The_File : out Io.File_Type) is
    begin
        The_File := Open_To_Write (This_File);
        --
    exception
        when others =>
            raise Io_Failure;
            --
    end Open_To_Write;

    function Open_To_Append  
                (This_File : in String) return Io.File_Type is
        --
        The_File : Io.File_Type;
        --
    begin
        -- Try to open the file for appending.
        Io.Append (The_File, This_File);
        return (The_File);
        --
    exception
        when others =>
            -- Append failed for some reason.
            raise Io_Failure;
            --
    end Open_To_Append;

    procedure Open_To_Append  
                 (This_File : in String; The_File : out Io.File_Type) is
    begin
        The_File := Open_To_Append (This_File);
        --
    exception
        when others =>
            raise Io_Failure;
            --
    end Open_To_Append;

    procedure Close  
                 (This_File : in out Io.File_Type) is  
    begin  
        Io.Close (This_File);
        --
    exception
        when others =>
            -- Couldn't close the file for some reason: assume is closed.
            null;
            --
    end Close;

    procedure Destroy (This_File : in out Io.File_Type) is
    begin
        Io.Delete (This_File);
        --
    exception
        when others =>
            -- Couldn't destroy the file for some reason: assume is destroyed.
            null;
            --
    end Destroy;

    procedure Destroy (This_File : in String) is
        --
        The_File : Io.File_Type;
        --
    begin  
        The_File := Open_To_Write (This_File);
        Io.Delete (The_File);
        --
    exception
        when others =>
            -- Couldn't destroy the file for some reason: assume is destroyed.
            null;
            --
    end Destroy;

end Low_Level_File_Operations;