--  Text_IO's file layout at the ends of files, and its errors (RM A.8.2, A.10): the end of a
--  file ends its last page, so Skip_Line over the last line goes on to the next page
--  (CE3403F); an empty file written in Out_File mode has its empty line, page and file
--  terminators read again (CZ1103A's CHECK_FILE); Open never creates a file (CE3102B,
--  CXAA015); a horizontal tabulation before a number read with a Width is a blank
--  (CE3704D); Reset may not turn the current default output into an input file (CE3102H).
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Characters.Latin_1;
procedure Main is
   Name : constant String := "/tmp/verada-text-io-terminators-test.txt";
   Missing : constant String := "/tmp/verada-text-io-terminators-missing.txt";
   F : File_Type;
   C : Character;
   X : Integer;
   Result : String (1 .. 12) := (others => ' ');
begin
   Create (F, Out_File, Name);
   Put (F, "CCC");
   Close (F);
   Open (F, In_File, Name);
   Get (F, C);
   Skip_Line (F);
   Put_Line ("after the last line: page" & Count'Image (Page (F)) & ", line" & Count'Image (Line (F)) & ", at the end " & Boolean'Image (End_Of_File (F)));
   begin
      Skip_Line (F);
      Put_Line ("no End_Error");
   exception
      when End_Error => Put_Line ("Skip_Line at the end: End_Error");
   end;
   Delete (F);

   --  an empty file
   Create (F, Out_File);
   Reset (F, In_File);
   Put_Line ("empty: end of line " & Boolean'Image (End_Of_Line (F)) & ", of page " & Boolean'Image (End_Of_Page (F)) & ", of file " & Boolean'Image (End_Of_File (F)));
   Skip_Page (F);
   Put_Line ("empty: skipped its page, now page" & Count'Image (Page (F)) & ", at the end " & Boolean'Image (End_Of_File (F)));
   Close (F);

   --  Open does not create
   for Mode in File_Mode loop
      begin
         Open (F, Mode, Missing);
         Put_Line (File_Mode'Image (Mode) & ": opened");
         Delete (F);
      exception
         when Name_Error => Put_Line (File_Mode'Image (Mode) & ": Name_Error");
      end;
   end loop;

   --  a tab before a number read with a Width
   Create (F, Out_File);
   Put (F, Ada.Characters.Latin_1.HT & "9");
   Reset (F, In_File);
   Ada.Integer_Text_IO.Get (F, X, 2);
   Put_Line ("read with a width of 2:" & Integer'Image (X));
   Close (F);

   --  the current default output stays an output file
   Create (F, Out_File);
   Set_Output (F);
   begin
      Reset (F, In_File);
      Result (1 .. 12) := "no exception";
   exception
      when Mode_Error => Result (1 .. 10) := "Mode_Error";
   end;
   Set_Output (Standard_Output);
   Put_Line ("Reset of the current output to In_File: " & Result);
   Reset (F, In_File);
   Put_Line ("not current, the mode is " & File_Mode'Image (Mode (F)));
   Close (F);
exception
   when Status_Error | Mode_Error | Name_Error | Use_Error | End_Error | Data_Error =>
      Put_Line ("could not use the files");
end Main;
