File : a-textio.ads


     ------------------------------------------------------------------------------
     --                                                                          --
     --                         GNAT RUN-TIME COMPONENTS                         --
     --                                                                          --
   5 --                          A D A . T E X T _ I O                           --
     --                                                                          --
     --                                 S p e c                                  --
     --                                                                          --
     --                            $Revision: 1.51 $
  10 --                                                                          --
     --          Copyright (C) 1992-2000 Free Software Foundation, Inc.          --
     --                                                                          --
     -- This specification is derived from the Ada Reference Manual for use with --
     -- GNAT. The copyright notice above, and the license provisions that follow --
  15 -- apply solely to the  contents of the part following the private keyword. --
     --                                                                          --
     -- GNAT is free software;  you can  redistribute it  and/or modify it under --
     -- terms of the  GNU General Public License as published  by the Free Soft- --
     -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  20 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
     -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
     -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
     -- for  more details.  You should have  received  a copy of the GNU General --
     -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  25 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
     -- MA 02111-1307, USA.                                                      --
     --                                                                          --
     -- As a special exception,  if other files  instantiate  generics from this --
     -- unit, or you link  this unit with other files  to produce an executable, --
  30 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
     -- covered  by the  GNU  General  Public  License.  This exception does not --
     -- however invalidate  any other reasons why  the executable file  might be --
     -- covered by the  GNU Public License.                                      --
     --                                                                          --
  35 -- GNAT was originally developed  by the GNAT team at  New York University. --
     -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
     --                                                                          --
     ------------------------------------------------------------------------------
     
  40 --  Note: the generic subpackages of Text_IO (Integer_IO, Float_IO, Fixed_IO,
     --  Modular_IO, Decimal_IO and Enumeration_IO) appear as private children in
     --  GNAT. These children are with'ed automatically if they are referenced, so
     --  this rearrangement is invisible to user programs, but has the advantage
     --  that only the needed parts of Text_IO are processed and loaded.
  45 
     with Ada.IO_Exceptions;
     with Ada.Streams;
     with System;
     with System.File_Control_Block;
  50 
     package Ada.Text_IO is
     pragma Elaborate_Body (Text_IO);
     
        type File_Type is limited private;
  55    type File_Mode is (In_File, Out_File, Append_File);
     
        --  The following representation clause allows the use of unchecked
        --  conversion for rapid translation between the File_Mode type
        --  used in this package and System.File_IO.
  60 
        for File_Mode use
          (In_File     => 0,  -- System.FIle_IO.File_Mode'Pos (In_File)
           Out_File    => 2,  -- System.File_IO.File_Mode'Pos (Out_File)
           Append_File => 3); -- System.File_IO.File_Mode'Pos (Append_File)
  65 
        type Count is range 0 .. Natural'Last;
        --  The value of Count'Last must be large enough so that the assumption
        --  enough so that the assumption that the Line, Column and Page
        --  counts can never exceed this value is a valid assumption.
  70 
        subtype Positive_Count is Count range 1 .. Count'Last;
     
        Unbounded : constant Count := 0;
        --  Line and page length
  75 
        subtype Field is Integer range 0 .. 255;
        --  Note: if for any reason, there is a need to increase this value,
        --  then it will be necessary to change the corresponding value in
        --  System.Img_Real in file s-imgrea.adb.
  80 
        subtype Number_Base is Integer range 2 .. 16;
     
        type Type_Set is (Lower_Case, Upper_Case);
     
  85    ---------------------
        -- File Management --
        ---------------------
     
        procedure Create
  90      (File : in out File_Type;
           Mode : in File_Mode := Out_File;
           Name : in String := "";
           Form : in String := "");
     
  95    procedure Open
          (File : in out File_Type;
           Mode : in File_Mode;
           Name : in String;
           Form : in String := "");
 100 
        procedure Close  (File : in out File_Type);
        procedure Delete (File : in out File_Type);
        procedure Reset  (File : in out File_Type; Mode : in File_Mode);
        procedure Reset  (File : in out File_Type);
 105 
        function Mode (File : in File_Type) return File_Mode;
        function Name (File : in File_Type) return String;
        function Form (File : in File_Type) return String;
     
 110    function Is_Open (File : in File_Type) return Boolean;
     
        ------------------------------------------------------
        -- Control of default input, output and error files --
        ------------------------------------------------------
 115 
        procedure Set_Input  (File : in File_Type);
        procedure Set_Output (File : in File_Type);
        procedure Set_Error  (File : in File_Type);
     
 120    function Standard_Input  return File_Type;
        function Standard_Output return File_Type;
        function Standard_Error  return File_Type;
     
        function Current_Input  return File_Type;
 125    function Current_Output return File_Type;
        function Current_Error  return File_Type;
     
        type File_Access is access constant File_Type;
     
 130    function Standard_Input  return File_Access;
        function Standard_Output return File_Access;
        function Standard_Error  return File_Access;
     
        function Current_Input  return File_Access;
 135    function Current_Output return File_Access;
        function Current_Error  return File_Access;
     
        --------------------
        -- Buffer control --
 140    --------------------
     
        --  Note: The parameter file is IN OUT in the RM, but this is clearly
        --  an oversight, and was intended to be IN, see AI95-00057.
     
 145    procedure Flush (File : in File_Type);
        procedure Flush;
     
        --------------------------------------------
        -- Specification of line and page lengths --
 150    --------------------------------------------
     
        procedure Set_Line_Length (File : in File_Type; To : in Count);
        procedure Set_Line_Length (To : in Count);
     
 155    procedure Set_Page_Length (File : in File_Type; To : in Count);
        procedure Set_Page_Length (To : in Count);
     
        function Line_Length (File : in File_Type) return Count;
        function Line_Length return Count;
 160 
        function Page_Length (File : in File_Type) return Count;
        function Page_Length return Count;
     
        ------------------------------------
 165    -- Column, Line, and Page Control --
        ------------------------------------
     
        procedure New_Line (File : in File_Type; Spacing : in Positive_Count := 1);
        procedure New_Line (Spacing : in Positive_Count := 1);
 170 
        procedure Skip_Line (File : in File_Type; Spacing : in Positive_Count := 1);
        procedure Skip_Line (Spacing : in Positive_Count := 1);
     
        function End_Of_Line (File : in File_Type) return Boolean;
 175    function End_Of_Line return Boolean;
     
        procedure New_Page (File : in File_Type);
        procedure New_Page;
     
 180    procedure Skip_Page (File : in File_Type);
        procedure Skip_Page;
     
        function End_Of_Page (File : in File_Type) return Boolean;
        function End_Of_Page return Boolean;
 185 
        function End_Of_File (File : in File_Type) return Boolean;
        function End_Of_File return Boolean;
     
        procedure Set_Col (File : in File_Type;  To : in Positive_Count);
 190    procedure Set_Col (To : in Positive_Count);
     
        procedure Set_Line (File : in File_Type; To : in Positive_Count);
        procedure Set_Line (To : in Positive_Count);
     
 195    function Col (File : in File_Type) return Positive_Count;
        function Col return Positive_Count;
     
        function Line (File : in File_Type) return Positive_Count;
        function Line return Positive_Count;
 200 
        function Page (File : in File_Type) return Positive_Count;
        function Page return Positive_Count;
     
        ----------------------------
 205    -- Character Input-Output --
        ----------------------------
     
        procedure Get (File : in File_Type; Item : out Character);
        procedure Get (Item : out Character);
 210    procedure Put (File : in File_Type; Item : in Character);
        procedure Put (Item : in Character);
     
        procedure Look_Ahead
          (File        : in File_Type;
 215       Item        : out Character;
           End_Of_Line : out Boolean);
     
        procedure Look_Ahead
          (Item        : out Character;
 220       End_Of_Line : out Boolean);
     
        procedure Get_Immediate
          (File : in File_Type;
           Item : out Character);
 225 
        procedure Get_Immediate
          (Item : out Character);
     
        procedure Get_Immediate
 230      (File      : in File_Type;
           Item      : out Character;
           Available : out Boolean);
     
        procedure Get_Immediate
 235      (Item      : out Character;
           Available : out Boolean);
     
        -------------------------
        -- String Input-Output --
 240    -------------------------
     
        procedure Get (File : in File_Type; Item : out String);
        procedure Get (Item : out String);
        procedure Put (File : in File_Type; Item : in String);
 245    procedure Put (Item : in String);
     
        procedure Get_Line
          (File : in File_Type;
           Item : out String;
 250       Last : out Natural);
     
        procedure Get_Line
          (Item : out String;
           Last : out Natural);
 255 
        procedure Put_Line
          (File : in File_Type;
           Item : in String);
     
 260    procedure Put_Line
          (Item : in String);
     
        ---------------------------------------
        -- Generic packages for Input-Output --
 265    ---------------------------------------
     
        --  The generic packages:
     
        --    Ada.Text_IO.Integer_IO
 270    --    Ada.Text_IO.Modular_IO
        --    Ada.Text_IO.Float_IO
        --    Ada.Text_IO.Fixed_IO
        --    Ada.Text_IO.Decimal_IO
        --    Ada.Text_IO.Enumeration_IO
 275 
        --  are implemented as separate child packages in GNAT, so the
        --  spec and body of these packages are to be found in separate
        --  child units. This implementation detail is hidden from the
        --  Ada programmer by special circuitry in the compiler that
 280    --  treats these child packages as though they were nested in
        --  Text_IO. The advantage of this special processing is that
        --  the subsidiary routines needed if these generics are used
        --  are not loaded when they are not used.
     
 285    ----------------
        -- Exceptions --
        ----------------
     
        Status_Error : exception renames IO_Exceptions.Status_Error;
 290    Mode_Error   : exception renames IO_Exceptions.Mode_Error;
        Name_Error   : exception renames IO_Exceptions.Name_Error;
        Use_Error    : exception renames IO_Exceptions.Use_Error;
        Device_Error : exception renames IO_Exceptions.Device_Error;
        End_Error    : exception renames IO_Exceptions.End_Error;
 295    Data_Error   : exception renames IO_Exceptions.Data_Error;
        Layout_Error : exception renames IO_Exceptions.Layout_Error;
     
     private
        -----------------------------------
 300    -- Handling of Format Characters --
        -----------------------------------
     
        --  Line marks are represented by the single character ASCII.LF (16#0A#).
        --  In DOS and similar systems, underlying file translation takes care
 305    --  of translating this to and from the standard CR/LF sequences used in
        --  these operating systems to mark the end of a line. On output there is
        --  always a line mark at the end of the last line, but on input, this
        --  line mark can be omitted, and is implied by the end of file.
     
 310    --  Page marks are represented by the single character ASCII.FF (16#0C#),
        --  The page mark at the end of the file may be omitted, and is normally
        --  omitted on output unless an explicit New_Page call is made before
        --  closing the file. No page mark is added when a file is appended to,
        --  so, in accordance with the permission in (RM A.10.2(4)), there may
 315    --  or may not be a page mark separating preexising text in the file
        --  from the new text to be written.
     
        --  A file mark is marked by the physical end of file. In DOS translation
        --  mode on input, an EOF character (SUB = 16#1A#) gets translated to the
 320    --  physical end of file, so in effect this character is recognized as
        --  marking the end of file in DOS and similar systems.
     
        LM : constant := Character'Pos (ASCII.LF);
        --  Used as line mark
 325 
        PM : constant := Character'Pos (ASCII.FF);
        --  Used as page mark, except at end of file where it is implied
     
        --------------------------------
 330    -- Text_IO File Control Block --
        --------------------------------
     
        package FCB renames System.File_Control_Block;
     
 335    type Text_AFCB;
        type File_Type is access all Text_AFCB;
     
        type Text_AFCB is new FCB.AFCB with record
           Page        : Count := 1;
 340       Line        : Count := 1;
           Col         : Count := 1;
           Line_Length : Count := 0;
           Page_Length : Count := 0;
     
 345       Self : aliased File_Type;
           --  Set to point to the containing Text_AFCB block. This is used to
           --  implement the Current_{Error,Input,Ouput} functions which return
           --  a File_Access, the file access value returned is a pointer to
           --  the Self field of the corresponding file.
 350 
           Before_LM : Boolean := False;
           --  This flag is used to deal with the anomolies introduced by the
           --  peculiar definition of End_Of_File and End_Of_Page in Ada. These
           --  functions require looking ahead more than one character. Since
 355       --  there is no convenient way of backing up more than one character,
           --  what we do is to leave ourselves positioned past the LM, but set
           --  this flag, so that we know that from an Ada point of view we are
           --  in front of the LM, not after it. A bit of a kludge, but it works!
     
 360       Before_LM_PM : Boolean := False;
           --  This flag similarly handles the case of being physically positioned
           --  after a LM-PM sequence when logically we are before the LM-PM. This
           --  flag can only be set if Before_LM is also set.
     
 365    end record;
     
        function AFCB_Allocate (Control_Block : Text_AFCB) return FCB.AFCB_Ptr;
     
        procedure AFCB_Close (File : access Text_AFCB);
 370    procedure AFCB_Free  (File : access Text_AFCB);
     
        procedure Read
          (File : in out Text_AFCB;
           Item : out Ada.Streams.Stream_Element_Array;
 375       Last : out Ada.Streams.Stream_Element_Offset);
        --  Read operation used when Text_IO file is treated directly as Stream
     
        procedure Write
          (File : in out Text_AFCB;
 380       Item : in Ada.Streams.Stream_Element_Array);
        --  Write operation used when Text_IO file is treated directly as Stream
     
        ------------------------
        -- The Standard Files --
 385    ------------------------
     
        Null_Str : aliased constant String := "";
        --  Used as name and form of standard files
     
 390    Standard_Err_AFCB : aliased Text_AFCB;
        Standard_In_AFCB  : aliased Text_AFCB;
        Standard_Out_AFCB : aliased Text_AFCB;
     
        Standard_Err : aliased File_Type := Standard_Err_AFCB'Access;
 395    Standard_In  : aliased File_Type := Standard_In_AFCB'Access;
        Standard_Out : aliased File_Type := Standard_Out_AFCB'Access;
        --  Standard files
     
        Current_In   : aliased File_Type := Standard_In;
 400    Current_Out  : aliased File_Type := Standard_Out;
        Current_Err  : aliased File_Type := Standard_Err;
        --  Current files
     
        -----------------------
 405    -- Local Subprograms --
        -----------------------
     
        --  These subprograms are in the private part of the spec so that they can
        --  be shared by the routines in the body of Ada.Text_IO.Wide_Text_IO.
 410 
        --  Note: we use Integer in these declarations instead of the more accurate
        --  Interfaces.C_Streams.int, because we do not want to drag in the spec of
        --  this interfaces package with the spec of Ada.Text_IO, and we know that
        --  in fact these types are identical
 415 
        function Getc (File : File_Type) return Integer;
        --  Gets next character from file, which has already been checked for
        --  being in read status, and returns the character read if no error
        --  occurs. The result is EOF if the end of file was read.
 420 
        function Nextc (File : File_Type) return Integer;
        --  Returns next character from file without skipping past it (i.e. it
        --  is a combination of Getc followed by an Ungetc).
     
 425    procedure Putc (ch : Integer; File : File_Type);
        --  Outputs the given character to the file, which has already been
        --  checked for being in output status. Device_Error is raised if the
        --  character cannot be written.
     
 430    procedure Terminate_Line (File : File_Type);
        --  If the file is in Write_File or Append_File mode, and the current
        --  line is not terminated, then a line terminator is written using
        --  New_Line. Note that there is no Terminate_Page routine, because
        --  the page mark at the end of the file is implied if necessary.
 435 
        procedure Ungetc (ch : Integer; File : File_Type);
        --  Pushes back character into stream, using ungetc. The caller has
        --  checked that the file is in read status. Device_Error is raised
        --  if the character cannot be pushed back. An attempt to push back
 440    --  and end of file character (EOF) is ignored.
     
     end Ada.Text_IO;