with Ada.Text_IO; use Ada.Text_IO;
--  Get_Line, Skip_Line and Skip_Page propagate End_Error at the end of a file, as Ada's do;
--  their exceptional cases say so: only when the file was at its end on entry (or, for
--  Skip_Line, when it ends before Spacing lines)
procedure Main is
   Name : constant String := "/tmp/verada-text-io-end-error-test.txt";
   F : File_Type;
   Line : String (1 .. 80) := (others => ' ');
   Last : Natural;
   Lines : Natural := 0;
begin
   Create (F, Out_File, Name);
   Put_Line (F, "one");
   Put_Line (F, "two");
   Put_Line (F, "three");
   Close (F);

   --  reading lines until End_Error, without testing End_Of_File first
   Open (F, In_File, Name);
   begin
      for I in 1 .. 10 loop
         Get_Line (F, Line, Last);
         Lines := I;
         Put_Line ("read: " & Line (1 .. Last));
      end loop;
   exception
      when End_Error =>
         Put_Line ("End_Error after" & Natural'Image (Lines) & " lines");
   end;
   --  an empty Item reads nothing, even at the end
   Get_Line (F, Line (1 .. 0), Last);
   Put_Line ("empty item:" & Natural'Image (Last));
   Close (F);

   --  Skip_Line past the end
   Open (F, In_File, Name);
   Skip_Line (F, 2);
   begin
      Skip_Line (F, 2);
      Put_Line ("skipped to the end");
      Skip_Line (F);
   exception
      when End_Error =>
         Put_Line ("Skip_Line: End_Error");
   end;
   begin
      Skip_Page (F);
   exception
      when End_Error =>
         Put_Line ("Skip_Page: End_Error");
   end;
   Close (F);

   --  where the file is known not to have ended, no End_Error can be propagated
   Open (F, In_File, Name);
   if not End_Of_File (F) then
      Get_Line (F, Line, Last);
      Put_Line ("first: " & Line (1 .. Last));
   end if;
   Close (F);
exception
   when Name_Error | Use_Error | End_Error =>
      Put_Line ("could not use the file");
end Main;
