-- $Date: 2004/02/14 09:59:14 $
-- $Revision: 1.16 $
-- $Author: jcrocholl $

with Ada.Unchecked_Deallocation;

with Integer_Strings; use Integer_Strings;

package body Readers is

   -----------------
   -- Initialization
   -----------------

   -- Initialize a newly created reader instance.
   procedure Initialize_Reader
     (This : access Reader'Class) is -- Initialize this reader.
   begin
      Get(This); -- Read the first look-ahead character.
   end Initialize_Reader;

   -- Initialize a newly created reader instance with a file to read.
   procedure Initialize_Reader
     (This     : access Reader'Class-- Initialize this reader.
      Filename : in String) is             -- Open this file for reading.
   begin
      Open(This.File, In_File, Filename);
      To_String_Access(Filename, This.Filename);
      Initialize_Reader(This); -- Standard initialization.
   end Initialize_Reader;

   -- Close the input file.
   procedure Finalize_Reader
     (This : access Reader'Class) is -- Finalize this reader.
   begin
      if not Is_Null(This.Filename) then
         Close(This.File);
      end if;
      Free(This.Filename);
   end Finalize_Reader;

   -------------------
   -- Creating readers
   -------------------

   -- Create a reader for current input (stdin).
   function Current_Input
     return Reader_Access -- The newly created reader instance.
   is
      Result : Reader_Access := new Reader;
   begin
      Initialize_Reader(Result);
      return Result;
   end Current_Input;

   -- Create a reader for file input.
   function Open
     (Filename : in String-- Open this file.
     return Reader_Access   -- The newly created reader instance.
   is
      Result : Reader_Access := new Reader;
   begin
      Initialize_Reader(Result, Filename);
      return Result;
   end Open;