-- $Date: 2004/03/04 05:15:29 $
-- $Revision: 1.7 $
-- $Author: jcrocholl $

with Ada.Text_IO; use Ada.Text_IO;

with Strings; use Strings;

-- Light-weight text output file writers.
package Writers is

   -- Light-weight text output file writer.
   type Writer is tagged limited record
      -- File access.
      Filename : String_Access-- Output file name.
      File     : File_Type;     -- Output file handle.
      -- Behaviour.
      Whitespace   : Character := ' '; -- For Write_Whitespace.
      String_Quote : Character := '"'; -- For Write_String.
   end record;

   -- Type for instance variables.
   -- type Writer_Access is access Writer_Record;

   -- Type for class-wide instance variables.
   type Writer_Access is access Writer;

   -------------------------------------------
   -- Internal initialization and finalization
   -------------------------------------------

   -- These don't need to be called directly because the functions
   -- Current_Output, Create and Close will do it for you.

   -- Initialize a newly created writer instance.
   procedure Initialize
     (This : access Writer'Class); -- Writer instance.

   -- Initialize a newly created writer instance with a file to write.
   procedure Initialize
     (This     : access Writer'Class-- Writer instance.
      Filename : in String);               -- Open this file for writing.

   -- Close the output file.
   procedure Finalize
     (This : access Writer'Class); -- Object instance.

   -------------------
   -- Creating writers
   -------------------

   -- Create a writer for current output (stdout).
   function Current_Output
     return Writer_Access-- The newly created writer instance.

   -- Create a writer for file output.
   function Create
     (Filename : in String-- Create this file.
     return Writer_Access;  -- The newly created writer instance.

   -- Close a writer's output file.
   procedure Close
     (This : in out Writer_Access); -- Close this writer.

   -----------------
   -- Error handling
   -----------------

   -- Writing this tagged object is not implemented. Print an error
   -- message and raise Program_Error.
   procedure Unknown_Tag_Error
     (Context : in String;  -- Where the error occurred.
      Tag     : in String); -- The tag that could not be handled.

   ---------------------------
   -- Wrappers for file output
   ---------------------------

   -- Output a character to the file.
   procedure Put
     (This : access Writer'Class-- Writer instance.
      Text : in Character);            -- Put this character.

   -- Output a string to the file.
   procedure Put
     (This : access Writer'Class-- Writer instance.
      Text : in String);               -- Put this string.

   -- Output a string to the file, followed by a newline.
   procedure Put_Line
     (This : access Writer'Class-- Writer instance.
      Text : in String);               -- Put this string plus a newline.

   -- Output a newline to the file.
   procedure New_Line
     (This : access Writer'Class); -- Writer instance.

   ----------
   -- Writing
   ----------

   -- Write whitespace.
   procedure Write_Whitespace
     (This : access Writer'Class); -- Writer instance.

   -- Write a word.
   procedure Write_Word
     (This : access Writer'Class-- Writer instance.
      Text : in String);               -- Write this word.

   -- Write a string plus quotes.
   procedure Write_String
     (This : access Writer'Class-- Writer instance.
      Text : in String);               -- Write this string.

end Writers;