with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
   type Colour is (Red, Green, Blue);
   type Byte is mod 256;
   package Int_IO is new Integer_IO (Integer);
   package Byte_IO is new Modular_IO (Byte);
   package Real_IO is new Float_IO (Long_Float);
   package Colour_IO is new Enumeration_IO (Colour);
   Name : constant String := "/tmp/verada-text-io-layout.txt";
   F : File_Type;
   N : Integer;
   B : Byte;
   X : Long_Float;
   C : Colour;
   Ch : Character;
   Available : Boolean;
   Last : Positive;
   Line_Text : String (1 .. 40);
   Line_Last : Natural;
   Field_Text : String (1 .. 8);
begin
   --  layout on standard output
   Put ("ab");
   Put_Line (" col" & Positive_Count'Image (Col));
   Set_Col (6);
   Put_Line ("at 6");
   Int_IO.Put (42, Width => 6);
   Int_IO.Put (255, Width => 8, Base => 16);
   Int_IO.Put (-5, Width => 0, Base => 2);
   New_Line;
   Byte_IO.Put (200, Width => 5);
   Real_IO.Put (3.25, Fore => 3, Aft => 2, Exp => 0);
   Real_IO.Put (-1234.5, Fore => 2, Aft => 3, Exp => 2);
   Real_IO.Put (0.1);
   New_Line;
   Colour_IO.Put (Green, Width => 7);
   Colour_IO.Put (Blue, Set => Lower_Case);
   New_Line;

   --  strings
   Int_IO.Get (" 17 rest", N, Last);
   Put_Line ("got" & Integer'Image (N) & " up to" & Positive'Image (Last));
   Real_IO.Get ("2.5E1x", X, Last);
   Put_Line ("got" & Long_Float'Image (X) & " up to" & Positive'Image (Last));
   Colour_IO.Get ("  red,", C, Last);
   Put_Line ("got " & Colour'Image (C) & " up to" & Positive'Image (Last));
   Int_IO.Put (Field_Text, 16#7F#, Base => 16);
   Put_Line ("[" & Field_Text & "]");
   begin
      Int_IO.Get ("abc", N, Last);
   exception
      when Data_Error =>
         Put_Line ("abc: Data_Error");
   end;

   --  a file with lines and pages
   Create (F, Out_File, Name);
   Put_Line (F, "one 1");
   Int_IO.Put (F, 22);
   Put (F, ' ');
   Colour_IO.Put (F, Blue);
   New_Line (F);
   Put_Line ("on line" & Positive_Count'Image (Line (F)));
   New_Page (F);
   Put (F, "page two");
   Put_Line ("on page" & Positive_Count'Image (Page (F)) & ", line" & Positive_Count'Image (Line (F)) & ", column" & Positive_Count'Image (Col (F)));
   Close (F);

   Open (F, In_File, Name);
   --  (a file is read while it has not ended, which the reads' preconditions ask)
   if End_Of_File (F) then
      return;
   end if;
   Get_Line (F, Line_Text, Line_Last);
   Put_Line ("line 1: " & Line_Text (1 .. Line_Last));
   Int_IO.Get (F, N);
   Colour_IO.Get (F, C);
   Put_Line ("then" & Integer'Image (N) & " " & Colour'Image (C) & ", end of line: " & Boolean'Image (End_Of_Line (F))
             & ", end of page: " & Boolean'Image (End_Of_Page (F)));
   if End_Of_File (F) then
      return;
   end if;
   Skip_Line (F);
   Get (F, Ch);
   Put_Line ("page" & Positive_Count'Image (Page (F)) & " starts with " & Ch);
   if End_Of_File (F) then
      return;
   end if;
   Get_Line (F, Line_Text, Line_Last);
   Put_Line ("rest: " & Line_Text (1 .. Line_Last) & ", end of file: " & Boolean'Image (End_Of_File (F)));
   begin
      Get (F, Ch);
   exception
      when End_Error =>
         Put_Line ("End_Error at the end");
   end;
   Reset (F);
   Get_Immediate (F, Ch);
   Put (Ch);
   Get_Immediate (F, Ch, Available);
   --  (the function Get_Line needs a line left to read)
   if not End_Of_File (F) then
      declare
         Rest : constant String := Get_Line (F);
      begin
         Put_Line (" then" & Integer'Image (Character'Pos (Ch)) & " " & Boolean'Image (Available) & " " & Rest);
      end;
   end if;
   Delete (F);
   Put_Line ("deleted: " & Boolean'Image (not Is_Open (F)));
   B := 0;
   Byte_IO.Get ("16#FF#", B, Last);
   Put_Line ("byte" & Byte'Image (B));
exception
   when Name_Error | Use_Error =>
      Put_Line ("could not use the file");
   when Data_Error | End_Error | Layout_Error =>
      Put_Line ("unexpected");
end Main;
