OSDN Git Service

* Makefile.in (reload1.o-warn): Remove.
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-textio.adb
index b61ebd3..86a4986 100644 (file)
@@ -1,12 +1,12 @@
 ------------------------------------------------------------------------------
 --                                                                          --
---                         GNAT RUNTIME COMPONENTS                          --
+--                         GNAT RUN-TIME COMPONENTS                         --
 --                                                                          --
 --                          A D A . T E X T _ I O                           --
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2003 Free Software Foundation, Inc.          --
+--          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
 --                                                                          --
 -- 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- --
@@ -16,8 +16,8 @@
 -- 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 --
--- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
--- MA 02111-1307, USA.                                                      --
+-- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
+-- Boston, MA 02110-1301, 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, --
 
 with Ada.Streams;          use Ada.Streams;
 with Interfaces.C_Streams; use Interfaces.C_Streams;
-with System;
+
 with System.File_IO;
-with Unchecked_Conversion;
-with Unchecked_Deallocation;
+with System.CRTL;
+
+with Ada.Unchecked_Conversion;
+with Ada.Unchecked_Deallocation;
 
 pragma Elaborate_All (System.File_IO);
 --  Needed because of calls to Chain_File in package body elaboration
@@ -47,17 +49,18 @@ package body Ada.Text_IO is
 
    subtype AP is FCB.AFCB_Ptr;
 
-   function To_FCB is new Unchecked_Conversion (File_Mode, FCB.File_Mode);
-   function To_TIO is new Unchecked_Conversion (FCB.File_Mode, File_Mode);
+   function To_FCB is new Ada.Unchecked_Conversion (File_Mode, FCB.File_Mode);
+   function To_TIO is new Ada.Unchecked_Conversion (FCB.File_Mode, File_Mode);
    use type FCB.File_Mode;
 
+   use type System.CRTL.size_t;
+
    -------------------
    -- AFCB_Allocate --
    -------------------
 
    function AFCB_Allocate (Control_Block : Text_AFCB) return FCB.AFCB_Ptr is
       pragma Unreferenced (Control_Block);
-
    begin
       return new Text_AFCB;
    end AFCB_Allocate;
@@ -66,7 +69,7 @@ package body Ada.Text_IO is
    -- AFCB_Close --
    ----------------
 
-   procedure AFCB_Close (File : access Text_AFCB) is
+   procedure AFCB_Close (File : not null access Text_AFCB) is
    begin
       --  If the file being closed is one of the current files, then close
       --  the corresponding current file. It is not clear that this action
@@ -88,11 +91,11 @@ package body Ada.Text_IO is
    -- AFCB_Free --
    ---------------
 
-   procedure AFCB_Free (File : access Text_AFCB) is
+   procedure AFCB_Free (File : not null access Text_AFCB) is
       type FCB_Ptr is access all Text_AFCB;
       FT : FCB_Ptr := FCB_Ptr (File);
 
-      procedure Free is new Unchecked_Deallocation (Text_AFCB, FCB_Ptr);
+      procedure Free is new Ada.Unchecked_Deallocation (Text_AFCB, FCB_Ptr);
 
    begin
       Free (FT);
@@ -115,7 +118,7 @@ package body Ada.Text_IO is
    --  to exceed the value of Count'Last, i.e. no check is required for
    --  overflow raising layout error.
 
-   function Col (File : in File_Type) return Positive_Count is
+   function Col (File : File_Type) return Positive_Count is
    begin
       FIO.Check_File_Open (AP (File));
       return File.Col;
@@ -132,9 +135,9 @@ package body Ada.Text_IO is
 
    procedure Create
      (File : in out File_Type;
-      Mode : in File_Mode := Out_File;
-      Name : in String := "";
-      Form : in String := "")
+      Mode : File_Mode := Out_File;
+      Name : String := "";
+      Form : String := "")
    is
       Dummy_File_Control_Block : Text_AFCB;
       pragma Warnings (Off, Dummy_File_Control_Block);
@@ -209,8 +212,8 @@ package body Ada.Text_IO is
    -- End_Of_File --
    -----------------
 
-   function End_Of_File (File : in File_Type) return Boolean is
-      ch  : int;
+   function End_Of_File (File : File_Type) return Boolean is
+      ch : int;
 
    begin
       FIO.Check_Read_Status (AP (File));
@@ -267,7 +270,7 @@ package body Ada.Text_IO is
    -- End_Of_Line --
    -----------------
 
-   function End_Of_Line (File : in File_Type) return Boolean is
+   function End_Of_Line (File : File_Type) return Boolean is
       ch : int;
 
    begin
@@ -298,7 +301,7 @@ package body Ada.Text_IO is
    -- End_Of_Page --
    -----------------
 
-   function End_Of_Page (File : in File_Type) return Boolean is
+   function End_Of_Page (File : File_Type) return Boolean is
       ch  : int;
 
    begin
@@ -354,7 +357,7 @@ package body Ada.Text_IO is
    -- Flush --
    -----------
 
-   procedure Flush (File : in File_Type) is
+   procedure Flush (File : File_Type) is
    begin
       FIO.Flush (AP (File));
    end Flush;
@@ -368,7 +371,7 @@ package body Ada.Text_IO is
    -- Form --
    ----------
 
-   function Form (File : in File_Type) return String is
+   function Form (File : File_Type) return String is
    begin
       return FIO.Form (AP (File));
    end Form;
@@ -378,7 +381,7 @@ package body Ada.Text_IO is
    ---------
 
    procedure Get
-     (File : in File_Type;
+     (File : File_Type;
       Item : out Character)
    is
       ch : int;
@@ -427,7 +430,7 @@ package body Ada.Text_IO is
    end Get;
 
    procedure Get
-     (File : in File_Type;
+     (File : File_Type;
       Item : out String)
    is
       ch : int;
@@ -486,7 +489,7 @@ package body Ada.Text_IO is
    --  More work required here ???
 
    procedure Get_Immediate
-     (File : in File_Type;
+     (File : File_Type;
       Item : out Character)
    is
       ch          : int;
@@ -527,7 +530,7 @@ package body Ada.Text_IO is
    end Get_Immediate;
 
    procedure Get_Immediate
-     (File      : in File_Type;
+     (File      : File_Type;
       Item      : out Character;
       Available : out Boolean)
    is
@@ -591,7 +594,7 @@ package body Ada.Text_IO is
    --------------
 
    procedure Get_Line
-     (File : in File_Type;
+     (File : File_Type;
       Item : out String;
       Last : out Natural)
    is
@@ -709,6 +712,58 @@ package body Ada.Text_IO is
       Get_Line (Current_In, Item, Last);
    end Get_Line;
 
+   function Get_Line (File : File_Type) return String is
+      Buffer : String (1 .. 500);
+      Last   : Natural;
+
+      function Get_Rest (S : String) return String;
+      --  This is a recursive function that reads the rest of the line and
+      --  returns it. S is the part read so far.
+
+      --------------
+      -- Get_Rest --
+      --------------
+
+      function Get_Rest (S : String) return String is
+
+         --  Each time we allocate a buffer the same size as what we have
+         --  read so far. This limits us to a logarithmic number of calls
+         --  to Get_Rest and also ensures only a linear use of stack space.
+
+         Buffer : String (1 .. S'Length);
+         Last   : Natural;
+
+      begin
+         Get_Line (File, Buffer, Last);
+
+         declare
+            R : constant String := S & Buffer (1 .. Last);
+         begin
+            if Last < Buffer'Last then
+               return R;
+            else
+               return Get_Rest (R);
+            end if;
+         end;
+      end Get_Rest;
+
+   --  Start of processing for Get_Line
+
+   begin
+      Get_Line (File, Buffer, Last);
+
+      if Last < Buffer'Last then
+         return Buffer (1 .. Last);
+      else
+         return Get_Rest (Buffer (1 .. Last));
+      end if;
+   end Get_Line;
+
+   function Get_Line return String is
+   begin
+      return Get_Line (Current_In);
+   end Get_Line;
+
    ----------
    -- Getc --
    ----------
@@ -730,7 +785,7 @@ package body Ada.Text_IO is
    -- Is_Open --
    -------------
 
-   function Is_Open (File : in File_Type) return Boolean is
+   function Is_Open (File : File_Type) return Boolean is
    begin
       return FIO.Is_Open (AP (File));
    end Is_Open;
@@ -743,7 +798,7 @@ package body Ada.Text_IO is
    --  to exceed the value of Count'Last, i.e. no check is required for
    --  overflow raising layout error.
 
-   function Line (File : in File_Type) return Positive_Count is
+   function Line (File : File_Type) return Positive_Count is
    begin
       FIO.Check_File_Open (AP (File));
       return File.Line;
@@ -758,7 +813,7 @@ package body Ada.Text_IO is
    -- Line_Length --
    -----------------
 
-   function Line_Length (File : in File_Type) return Count is
+   function Line_Length (File : File_Type) return Count is
    begin
       FIO.Check_Write_Status (AP (File));
       return File.Line_Length;
@@ -774,7 +829,7 @@ package body Ada.Text_IO is
    ----------------
 
    procedure Look_Ahead
-     (File        : in File_Type;
+     (File        : File_Type;
       Item        : out Character;
       End_Of_Line : out Boolean)
    is
@@ -815,7 +870,7 @@ package body Ada.Text_IO is
    -- Mode --
    ----------
 
-   function Mode (File : in File_Type) return File_Mode is
+   function Mode (File : File_Type) return File_Mode is
    begin
       return To_TIO (FIO.Mode (AP (File)));
    end Mode;
@@ -824,7 +879,7 @@ package body Ada.Text_IO is
    -- Name --
    ----------
 
-   function Name (File : in File_Type) return String is
+   function Name (File : File_Type) return String is
    begin
       return FIO.Name (AP (File));
    end Name;
@@ -834,15 +889,15 @@ package body Ada.Text_IO is
    --------------
 
    procedure New_Line
-     (File    : in File_Type;
-      Spacing : in Positive_Count := 1)
+     (File    : File_Type;
+      Spacing : Positive_Count := 1)
    is
    begin
       --  Raise Constraint_Error if out of range value. The reason for this
       --  explicit test is that we don't want junk values around, even if
       --  checks are off in the caller.
 
-      if Spacing not in Positive_Count then
+      if not Spacing'Valid then
          raise Constraint_Error;
       end if;
 
@@ -864,7 +919,7 @@ package body Ada.Text_IO is
       File.Col := 1;
    end New_Line;
 
-   procedure New_Line (Spacing : in Positive_Count := 1) is
+   procedure New_Line (Spacing : Positive_Count := 1) is
    begin
       New_Line (Current_Out, Spacing);
    end New_Line;
@@ -873,7 +928,7 @@ package body Ada.Text_IO is
    -- New_Page --
    --------------
 
-   procedure New_Page (File : in File_Type) is
+   procedure New_Page (File : File_Type) is
    begin
       FIO.Check_Write_Status (AP (File));
 
@@ -922,9 +977,9 @@ package body Ada.Text_IO is
 
    procedure Open
      (File : in out File_Type;
-      Mode : in File_Mode;
-      Name : in String;
-      Form : in String := "")
+      Mode : File_Mode;
+      Name : String;
+      Form : String := "")
    is
       Dummy_File_Control_Block : Text_AFCB;
       pragma Warnings (Off, Dummy_File_Control_Block);
@@ -952,7 +1007,7 @@ package body Ada.Text_IO is
    --  to exceed the value of Count'Last, i.e. no check is required for
    --  overflow raising layout error.
 
-   function Page (File : in File_Type) return Positive_Count is
+   function Page (File : File_Type) return Positive_Count is
    begin
       FIO.Check_File_Open (AP (File));
       return File.Page;
@@ -967,7 +1022,7 @@ package body Ada.Text_IO is
    -- Page_Length --
    -----------------
 
-   function Page_Length (File : in File_Type) return Count is
+   function Page_Length (File : File_Type) return Count is
    begin
       FIO.Check_Write_Status (AP (File));
       return File.Page_Length;
@@ -983,8 +1038,8 @@ package body Ada.Text_IO is
    ---------
 
    procedure Put
-     (File : in File_Type;
-      Item : in Character)
+     (File : File_Type;
+      Item : Character)
    is
    begin
       FIO.Check_Write_Status (AP (File));
@@ -1000,7 +1055,7 @@ package body Ada.Text_IO is
       File.Col := File.Col + 1;
    end Put;
 
-   procedure Put (Item : in Character) is
+   procedure Put (Item : Character) is
    begin
       FIO.Check_Write_Status (AP (Current_Out));
 
@@ -1022,8 +1077,8 @@ package body Ada.Text_IO is
    ---------
 
    procedure Put
-     (File : in File_Type;
-      Item : in String)
+     (File : File_Type;
+      Item : String)
    is
    begin
       FIO.Check_Write_Status (AP (File));
@@ -1049,7 +1104,7 @@ package body Ada.Text_IO is
       end if;
    end Put;
 
-   procedure Put (Item : in String) is
+   procedure Put (Item : String) is
    begin
       Put (Current_Out, Item);
    end Put;
@@ -1059,8 +1114,8 @@ package body Ada.Text_IO is
    --------------
 
    procedure Put_Line
-     (File : in File_Type;
-      Item : in String)
+     (File : File_Type;
+      Item : String)
    is
       Ilen   : Natural := Item'Length;
       Istart : Natural := Item'First;
@@ -1124,7 +1179,7 @@ package body Ada.Text_IO is
       end;
    end Put_Line;
 
-   procedure Put_Line (Item : in String) is
+   procedure Put_Line (Item : String) is
    begin
       Put_Line (Current_Out, Item);
    end Put_Line;
@@ -1228,7 +1283,7 @@ package body Ada.Text_IO is
 
    procedure Reset
      (File : in out File_Type;
-      Mode : in File_Mode)
+      Mode : File_Mode)
    is
    begin
       --  Don't allow change of mode for current file (RM A.10.2(5))
@@ -1270,8 +1325,8 @@ package body Ada.Text_IO is
    -------------
 
    procedure Set_Col
-     (File : in File_Type;
-      To   : in Positive_Count)
+     (File : File_Type;
+      To   : Positive_Count)
    is
       ch : int;
 
@@ -1280,49 +1335,88 @@ package body Ada.Text_IO is
       --  explicit test is that we don't want junk values around, even if
       --  checks are off in the caller.
 
-      if To not in Positive_Count then
+      if not To'Valid then
          raise Constraint_Error;
       end if;
 
       FIO.Check_File_Open (AP (File));
 
-      if To = File.Col then
-         return;
-      end if;
+      --  Output case
 
       if Mode (File) >= Out_File then
+
+         --  Error if we attempt to set Col to a value greater than the
+         --  maximum permissible line length.
+
          if File.Line_Length /= 0 and then To > File.Line_Length then
             raise Layout_Error;
          end if;
 
+         --  If we are behind current position, then go to start of new line
+
          if To < File.Col then
             New_Line (File);
          end if;
 
+         --  Loop to output blanks till we are at the required column
+
          while File.Col < To loop
             Put (File, ' ');
          end loop;
 
+      --  Input case
+
       else
+         --  If we are logically before a LM, but physically after it, the
+         --  file position still reflects the position before the LM, so eat
+         --  it now and adjust the file position appropriately.
+
+         if File.Before_LM then
+            File.Before_LM := False;
+            File.Before_LM_PM := False;
+            File.Line := File.Line + 1;
+            File.Col := 1;
+         end if;
+
+         --  Loop reading characters till we get one at the required Col value
+
          loop
+            --  Read next character. The reason we have to read ahead is to
+            --  skip formatting characters, the effect of Set_Col is to set
+            --  us to a real character with the right Col value, and format
+            --  characters don't count.
+
             ch := Getc (File);
 
+            --  Error if we hit an end of file
+
             if ch = EOF then
                raise End_Error;
 
+            --  If line mark, eat it and adjust file position
+
             elsif ch = LM then
                File.Line := File.Line + 1;
                File.Col := 1;
 
+            --  If recognized page mark, eat it, and adjust file position
+
             elsif ch = PM and then File.Is_Regular_File then
                File.Page := File.Page + 1;
                File.Line := 1;
                File.Col := 1;
 
+            --  Otherwise this is the character we are looking for, so put it
+            --  back in the input stream (we have not adjusted the file
+            --  position yet, so everything is set right after this ungetc).
+
             elsif To = File.Col then
                Ungetc (ch, File);
                return;
 
+            --  Keep skipping characters if we are not there yet, updating the
+            --  file position past the skipped character.
+
             else
                File.Col := File.Col + 1;
             end if;
@@ -1330,7 +1424,7 @@ package body Ada.Text_IO is
       end if;
    end Set_Col;
 
-   procedure Set_Col (To : in Positive_Count) is
+   procedure Set_Col (To : Positive_Count) is
    begin
       Set_Col (Current_Out, To);
    end Set_Col;
@@ -1339,7 +1433,7 @@ package body Ada.Text_IO is
    -- Set_Error --
    ---------------
 
-   procedure Set_Error (File : in File_Type) is
+   procedure Set_Error (File : File_Type) is
    begin
       FIO.Check_Write_Status (AP (File));
       Current_Err := File;
@@ -1349,7 +1443,7 @@ package body Ada.Text_IO is
    -- Set_Input --
    ---------------
 
-   procedure Set_Input (File : in File_Type) is
+   procedure Set_Input (File : File_Type) is
    begin
       FIO.Check_Read_Status (AP (File));
       Current_In := File;
@@ -1360,15 +1454,15 @@ package body Ada.Text_IO is
    --------------
 
    procedure Set_Line
-     (File : in File_Type;
-      To   : in Positive_Count)
+     (File : File_Type;
+      To   : Positive_Count)
    is
    begin
       --  Raise Constraint_Error if out of range value. The reason for this
       --  explicit test is that we don't want junk values around, even if
       --  checks are off in the caller.
 
-      if To not in Positive_Count then
+      if not To'Valid then
          raise Constraint_Error;
       end if;
 
@@ -1398,7 +1492,7 @@ package body Ada.Text_IO is
       end if;
    end Set_Line;
 
-   procedure Set_Line (To : in Positive_Count) is
+   procedure Set_Line (To : Positive_Count) is
    begin
       Set_Line (Current_Out, To);
    end Set_Line;
@@ -1407,13 +1501,13 @@ package body Ada.Text_IO is
    -- Set_Line_Length --
    ---------------------
 
-   procedure Set_Line_Length (File : in File_Type; To : in Count) is
+   procedure Set_Line_Length (File : File_Type; To : Count) is
    begin
       --  Raise Constraint_Error if out of range value. The reason for this
       --  explicit test is that we don't want junk values around, even if
       --  checks are off in the caller.
 
-      if To not in Count then
+      if not To'Valid then
          raise Constraint_Error;
       end if;
 
@@ -1421,7 +1515,7 @@ package body Ada.Text_IO is
       File.Line_Length := To;
    end Set_Line_Length;
 
-   procedure Set_Line_Length (To : in Count) is
+   procedure Set_Line_Length (To : Count) is
    begin
       Set_Line_Length (Current_Out, To);
    end Set_Line_Length;
@@ -1430,7 +1524,7 @@ package body Ada.Text_IO is
    -- Set_Output --
    ----------------
 
-   procedure Set_Output (File : in File_Type) is
+   procedure Set_Output (File : File_Type) is
    begin
       FIO.Check_Write_Status (AP (File));
       Current_Out := File;
@@ -1440,13 +1534,13 @@ package body Ada.Text_IO is
    -- Set_Page_Length --
    ---------------------
 
-   procedure Set_Page_Length (File : in File_Type; To : in Count) is
+   procedure Set_Page_Length (File : File_Type; To : Count) is
    begin
       --  Raise Constraint_Error if out of range value. The reason for this
       --  explicit test is that we don't want junk values around, even if
       --  checks are off in the caller.
 
-      if To not in Count then
+      if not To'Valid then
          raise Constraint_Error;
       end if;
 
@@ -1454,7 +1548,7 @@ package body Ada.Text_IO is
       File.Page_Length := To;
    end Set_Page_Length;
 
-   procedure Set_Page_Length (To : in Count) is
+   procedure Set_Page_Length (To : Count) is
    begin
       Set_Page_Length (Current_Out, To);
    end Set_Page_Length;
@@ -1464,8 +1558,8 @@ package body Ada.Text_IO is
    ---------------
 
    procedure Skip_Line
-     (File    : in File_Type;
-      Spacing : in Positive_Count := 1)
+     (File    : File_Type;
+      Spacing : Positive_Count := 1)
    is
       ch : int;
 
@@ -1474,7 +1568,7 @@ package body Ada.Text_IO is
       --  explicit test is that we don't want junk values around, even if
       --  checks are off in the caller.
 
-      if Spacing not in Positive_Count then
+      if not Spacing'Valid then
          raise Constraint_Error;
       end if;
 
@@ -1483,7 +1577,12 @@ package body Ada.Text_IO is
       for L in 1 .. Spacing loop
          if File.Before_LM then
             File.Before_LM := False;
-            File.Before_LM_PM := False;
+
+            --  Note that if File.Before_LM_PM is currently set, we also have
+            --  to reset it (because it makes sense for Before_LM_PM to be set
+            --  only when Before_LM is also set). This is done later on in this
+            --  subprogram, as soon as Before_LM_PM has been taken into account
+            --  for the purpose of page and line counts.
 
          else
             ch := Getc (File);
@@ -1545,7 +1644,7 @@ package body Ada.Text_IO is
       end loop;
    end Skip_Line;
 
-   procedure Skip_Line (Spacing : in Positive_Count := 1) is
+   procedure Skip_Line (Spacing : Positive_Count := 1) is
    begin
       Skip_Line (Current_In, Spacing);
    end Skip_Line;
@@ -1554,7 +1653,7 @@ package body Ada.Text_IO is
    -- Skip_Page --
    ---------------
 
-   procedure Skip_Page (File : in File_Type) is
+   procedure Skip_Page (File : File_Type) is
       ch : int;
 
    begin
@@ -1675,8 +1774,12 @@ package body Ada.Text_IO is
          --  because it is too much of a nuisance to have these odd line
          --  feeds when nothing has been written to the file.
 
+         --  We also avoid this for files opened in append mode, in
+         --  accordance with (RM A.8.2(10))
+
          elsif (File /= Standard_Err and then File /= Standard_Out)
            and then (File.Line = 1 and then File.Page = 1)
+           and then Mode (File) = Out_File
          then
             New_Line (File);
          end if;
@@ -1705,15 +1808,15 @@ package body Ada.Text_IO is
 
    procedure Write
      (File : in out Text_AFCB;
-      Item : in Stream_Element_Array)
+      Item : Stream_Element_Array)
    is
-
       function Has_Translated_Characters return Boolean;
       --  return True if Item array contains a character which will be
       --  translated under the text file mode. There is only one such
       --  character under DOS based systems which is character 10.
 
       text_translation_required : Boolean;
+      for text_translation_required'Size use Character'Size;
       pragma Import (C, text_translation_required,
                      "__gnat_text_translation_required");