with Text_Io;
use Text_Io;
package body File_Manager is

    --| Overview
    --| This package provides some host independent file functions.  These
    --| functions work on text files.  The maximum line lengths of the
    --| files is specified in the parameter Maximum_Line_Size which can be
    --| changed.

    Maximum_Line_Size : constant := 255;

    procedure Copy (In_File_Name : in String; Out_File_Name : in String) is
        Input_Buffer : String (1 .. Maximum_Line_Size);
        Input_File : File_Type;
        Output_File : File_Type;
        Line_Length : Natural;
    begin
        Open (Input_File, In_File, In_File_Name);
        Create (Output_File, Out_File, Out_File_Name);

        while not End_Of_File (Input_File) loop
            Get_Line (Input_File, Input_Buffer, Line_Length);
            Put_Line (Output_File, Input_Buffer (1 .. Line_Length));
        end loop;

        Close (Input_File);
        Close (Output_File);
    exception
        when Status_Error =>
            Put_Line
               ("status_error - trying to open a file that is already open");
        when Name_Error =>
            Put_Line ("name_error - trying to open a file that does not exist");
        when Use_Error =>
            Put_Line ("use_error - incorrect form of file name");
    end Copy;

    procedure Rename (In_File_Name : in String; Out_File_Name : in String) is
        Input_File : File_Type;
    begin
        Copy (In_File_Name, Out_File_Name);
        Open (Input_File, In_File, In_File_Name);
        Delete (Input_File);
    exception
        when Status_Error =>
            Put_Line ("status_error - trying to open/close file");
        when Name_Error =>
            Put_Line ("name_error - trying to open a file that does not exist");
        when Use_Error =>
            Put_Line ("use_error - delete access not allowed");
    end Rename;

    procedure Append (Append_File_Name : in String; To_File_Name : in String) is
        Append_File : File_Type;
        To_File : File_Type;
        Input_Buffer : String (1 .. Maximum_Line_Size);
        Line_Length : Natural;
    begin
        Rename (To_File_Name, "temp0097.rlr");
        Open (Append_File, In_File, "temp0097.rlr");
        Create (To_File, Out_File, To_File_Name);

        while not End_Of_File (Append_File) loop
            Get_Line (Append_File, Input_Buffer, Line_Length);
            Put_Line (To_File, Input_Buffer (1 .. Line_Length));
        end loop;

        Delete (Append_File);
        Open (Append_File, In_File, Append_File_Name);

        while not End_Of_File (Append_File) loop
            Get_Line (Append_File, Input_Buffer, Line_Length);
            Put_Line (To_File, Input_Buffer (1 .. Line_Length));
        end loop;

        Close (Append_File);
        Close (To_File);
    exception
        when Status_Error =>
            Put_Line ("status_error - trying to open/close file");
        when Name_Error =>
            Put_Line ("name_error - trying to open a file that does not exist");
        when Use_Error =>
            Put_Line ("use_error - delete access not allowed");
    end Append;

end File_Manager;
